8.4 中文镜像

MySQL 8.4 参考手册 · 中文镜像

正文为中文,英文原文默认隐藏。点击任意中文段落即可展开 / 收起对应英文原文。

打开官方英文原文 ↗

译文由机器翻译生成,仅供理解参考;有疑问请以官方英文原文为准。

中文点击中文段落可显示 / 隐藏英文原文本页译文覆盖 100%

10.11.5 外部锁定10.11.5 External Locking

外部锁定是使用文件系统锁来管理多个进程对MyISAM表的争用。当不能假设单个进程(例如 MySQL 服务器)是唯一需要访问表的进程时,就会使用外部锁定。以下是一些示例:External locking is the use of file system locking to manage contention for MyISAM database tables by multiple processes. External locking is used in situations where a single process such as the MySQL server cannot be assumed to be the only process that requires access to tables. Here are some examples:

  • 如果你运行多个使用相同数据库目录的服务器(不推荐),每个服务器必须启用外部锁定。If you run multiple servers that use the same database directory (not recommended), each server must have external locking enabled.

  • 如果您使用 myisamchk 对 MyISAM 表进行维护操作,您必须确保服务器未运行,或者服务器已启用外部锁定,以便在访问表时必要时锁定表文件以与 myisamchk 协调。使用 myisampack 打包 MyISAM 表时也同样适用。If you use myisamchk to perform table maintenance operations on MyISAM tables, you must either ensure that the server is not running, or that the server has external locking enabled so that it locks table files as necessary to coordinate with myisamchk for access to the tables. The same is true for use of myisampack to pack MyISAM tables.

    如果服务器启用了外部锁定运行,您可以随时使用myisamchk进行读取操作,例如检查表。在这种情况下,如果服务器尝试更新一个正在被myisamchk使用的表,服务器会等待myisamchk完成后才继续。If the server is run with external locking enabled, you can use myisamchk at any time for read operations such a checking tables. In this case, if the server tries to update a table that myisamchk is using, the server waits for myisamchk to finish before it continues.

    如果你使用 myisamchk 进行写操作,如修复或优化表,或者使用 myisampack 来打包表,你 必须 始终确保 mysqld 服务器没有使用该表。如果你没有停止 mysqld,至少在运行 myisamchk 之前执行 mysqladmin flush-tables。如果服务器和 myisamchk 同时访问表,你的表 可能会损坏。If you use myisamchk for write operations such as repairing or optimizing tables, or if you use myisampack to pack tables, you must always ensure that the mysqld server is not using the table. If you do not stop mysqld, at least do a mysqladmin flush-tables before you run myisamchk. Your tables may become corrupted if the server and myisamchk access the tables simultaneously.

启用外部锁定后,每个需要访问表的进程在访问表之前都会为表文件获取文件系统锁。如果无法获取所有必要的锁,该进程将被阻止访问表,直到可以获得锁(在当前持有锁的进程释放锁之后)。With external locking in effect, each process that requires access to a table acquires a file system lock for the table files before proceeding to access the table. If all necessary locks cannot be acquired, the process is blocked from accessing the table until the locks can be obtained (after the process that currently holds the locks releases them).

外部锁定会影响服务器性能,因为服务器有时必须等待其他进程才能访问表。External locking affects server performance because the server must sometimes wait for other processes before it can access tables.

如果你只运行单个服务器来访问给定的数据目录(通常情况如此),并且在服务器运行时没有其他程序如myisamchk需要修改表,那么外部加锁是不必要的。如果你只是使用其他程序读取表格,则不需要外部加锁,尽管如果服务器在myisamchk读取表格时更改了表,myisamchk可能会报告警告。External locking is unnecessary if you run a single server to access a given data directory (which is the usual case) and if no other programs such as myisamchk need to modify tables while the server is running. If you only read tables with other programs, external locking is not required, although myisamchk might report warnings if the server changes tables while myisamchk is reading them.

在禁用外部锁定的情况下,要使用 myisamchk,你必须在 myisamchk 执行期间停止服务器,或者在运行 myisamchk 之前锁定并刷新表。为了避免这一要求,可以使用 CHECK TABLE 和 REPAIR TABLE 语句来检查和修复 MyISAM 表。With external locking disabled, to use myisamchk, you must either stop the server while myisamchk executes or else lock and flush the tables before running myisamchk. To avoid this requirement, use the CHECK TABLE and REPAIR TABLE statements to check and repair MyISAM tables.

对于 mysqld,外部锁定由系统变量 skip_external_locking 的值控制。当该变量启用时,外部锁定被禁用,反之亦然。默认情况下,外部锁定是禁用的。For mysqld, external locking is controlled by the value of the skip_external_locking system variable. When this variable is enabled, external locking is disabled, and vice versa. External locking is disabled by default.

可以通过使用 --external-locking 或 --skip-external-locking 选项在服务器启动时控制外部锁定的使用。Use of external locking can be controlled at server startup by using the --external-locking or --skip-external-locking option.

如果您确实使用外部锁选项来允许多个 MySQL 进程更新 MyISAM 表,请不要在启动服务器时将 delay_key_write 系统变量设置为 ALL,也不要对任何共享表使用 DELAY_KEY_WRITE=1 表选项。否则,可能会发生索引损坏。If you do use external locking option to enable updates to MyISAM tables from many MySQL processes, do not start the server with the delay_key_write system variable set to ALL or use the DELAY_KEY_WRITE=1 table option for any shared tables. Otherwise, index corruption can occur.

满足此条件的最简单方法是始终将 --external-locking 与 --delay-key-write=OFF 一起使用。(默认情况下不会这样做,因为在许多设置中,使用上述选项的混合是有用的。)The easiest way to satisfy this condition is to always use --external-locking together with --delay-key-write=OFF. (This is not done by default because in many setups it is useful to have a mixture of the preceding options.)