通过在服务器启动时启用 --innodb-read-only 配置选项,您可以查询 MySQL 数据目录位于只读介质上的 InnoDB 表。You can query InnoDB tables where the MySQL
data directory is on read-only media by enabling the
--innodb-read-only configuration
option at server startup.
如何启用How to Enable
为了将实例准备为只读操作,请确保在将其存储到只读介质之前,所有必要的信息都已刷新到数据文件中。以禁用变更缓冲的方式运行服务器(innodb_change_buffering=0)并进行慢速关闭。To prepare an instance for read-only operation, make sure all the
necessary information is flushed
to the data files before storing it on the read-only medium. Run
the server with change buffering disabled
(innodb_change_buffering=0) and
do a slow shutdown.
要为整个 MySQL 实例启用只读模式,在服务器启动时指定以下配置选项:To enable read-only mode for an entire MySQL instance, specify the following configuration options at server startup:
如果实例位于只读介质上,例如 DVD 或 CD,或者
/var目录不是所有用户可写:--pid-file=和path_on_writeable_media--event-scheduler=disabledIf the instance is on read-only media such as a DVD or CD, or the/vardirectory is not writeable by all:--pid-file=andpath_on_writeable_media--event-scheduler=disabled--innodb-temp-data-file-path。此选项指定InnoDB临时表空间数据文件的路径、文件名和文件大小。默认设置为ibtmp1:12M:autoextend,它会在数据目录中创建ibtmp1临时表空间数据文件。要准备实例以进行只读操作,请将innodb_temp_data_file_path设置为数据目录之外的位置。路径必须相对于数据目录。例如:--innodb-temp-data-file-path. This option specifies the path, file name, and file size forInnoDBtemporary tablespace data files. The default setting isibtmp1:12M:autoextend, which creates theibtmp1temporary tablespace data file in the data directory. To prepare an instance for read-only operation, setinnodb_temp_data_file_pathto a location outside of the data directory. The path must be relative to the data directory. For example:--innodb-temp-data-file-path=../../../tmp/ibtmp1:12M:autoextend
启用 innodb_read_only 会阻止所有存储引擎的表创建和删除操作。这些操作会修改 mysql 系统数据库中的数据字典表,但这些表使用的是 InnoDB 存储引擎,在启用 innodb_read_only 时无法修改。对任何修改数据字典表的操作也适用相同的限制,例如 ANALYZE
TABLE 和 ALTER TABLE
tbl_name
ENGINE=engine_name。Enabling innodb_read_only
prevents table creation and drop operations for all storage
engines. These operations modify data dictionary tables in the
mysql system database, but those tables use the
InnoDB storage engine and cannot be modified
when innodb_read_only is enabled.
The same restriction applies to any operation that modifies data
dictionary tables, such as ANALYZE
TABLE and
ALTER TABLE
.tbl_name
ENGINE=engine_name
In addition, other tables in the mysql system
database use the InnoDB storage engine in MySQL
8.4. Making those tables read only results in
restrictions on operations that modify them. For example,
CREATE USER,
GRANT,
REVOKE,以及INSTALL PLUGIN operations are not
permitted in read-only mode.In addition, other tables in the mysql system
database use the InnoDB storage engine in MySQL
8.4. Making those tables read only results in
restrictions on operations that modify them. For example,
CREATE USER,
GRANT,
REVOKE, and
INSTALL PLUGIN operations are not
permitted in read-only mode.
使用场景Usage Scenarios
这种操作模式适用于以下情况:This mode of operation is appropriate in situations such as:
将 MySQL 应用程序或一组 MySQL 数据分发到只读存储介质上,例如 DVD 或 CD。Distributing a MySQL application, or a set of MySQL data, on a read-only storage medium such as a DVD or CD.
多个 MySQL 实例同时查询同一个数据目录,通常用于数据仓库配置。您可能会使用此技术来避免在负载较重的 MySQL 实例上可能发生的瓶颈,或者您可能会为各个实例使用不同的配置选项,以便针对特定类型的查询优化每个实例。Multiple MySQL instances querying the same data directory simultaneously, typically in a data warehousing configuration. You might use this technique to avoid bottlenecks that can occur with a heavily loaded MySQL instance, or you might use different configuration options for the various instances to tune each one for particular kinds of queries.
查询因安全或数据完整性原因而被置于只读状态的数据,例如归档的备份数据。Querying data that has been put into a read-only state for security or data integrity reasons, such as archived backup data.
此功能主要旨在提高分发和部署的灵活性,而不是基于只读特性提供原始性能。有关优化只读查询性能的方法,请参见 第 10.5.3 节,“优化 InnoDB 只读事务”,这些方法无需将整个服务器设置为只读。This feature is mainly intended for flexibility in distribution and deployment, rather than raw performance based on the read-only aspect. See Section 10.5.3, “Optimizing InnoDB Read-Only Transactions” for ways to tune the performance of read-only queries, which do not require making the entire server read-only.
工作原理How It Works
当通过 --innodb-read-only 选项以只读模式运行服务器时,某些 InnoDB 功能和组件会被减少或完全关闭:When the server is run in read-only mode through the
--innodb-read-only option,
certain InnoDB features and components are
reduced or turned off entirely:
没有进行变更缓冲,特别是没有从变更缓冲进行合并。为了确保在准备实例进入只读操作时变更缓冲为空,请禁用变更缓冲(
innodb_change_buffering=0)并首先进行慢速关闭。No change buffering is done, in particular no merges from the change buffer. To make sure the change buffer is empty when you prepare the instance for read-only operation, disable change buffering (innodb_change_buffering=0) and do a slow shutdown first.启动时没有崩溃恢复阶段。实例在进入只读状态之前必须执行过慢速关闭。There is no crash recovery phase at startup. The instance must have performed a slow shutdown before being put into the read-only state.
由于 重做日志 在只读操作中不使用,因此在将实例设置为只读之前,可以将
innodb_log_file_size设置为可能的最小值(1 MB)。Because the redo log is not used in read-only operation, you can setinnodb_log_file_sizeto the smallest size possible (1 MB) before making the instance read-only.大多数后台线程被关闭。I/O 读取线程仍然存在,以及 I/O 写入线程和一个用于写入临时文件的页面刷新协调线程,这在只读模式下是允许的。缓冲池调整线程也仍然处于活动状态,以支持缓冲池的在线调整大小。Most background threads are turned off. I/O read threads remain, as well as I/O write threads and a page flush coordinator thread for writes to temporary files, which are permitted in read-only mode. A buffer pool resize thread also remains active to enable online resizing of the buffer pool.
关于死锁、监控输出等信息不会写入临时文件。因此,
SHOW ENGINE INNODB STATUS不会产生任何输出。Information about deadlocks, monitor output, and so on is not written to temporary files. As a consequence,SHOW ENGINE INNODB STATUSdoes not produce any output.对配置选项设置的更改通常会影响写操作的行为,但在服务器处于只读模式时不起作用。Changes to configuration option settings that would normally change the behavior of write operations, have no effect when the server is in read-only mode.
该多版本并发控制(MVCC) processing to enforce isolation levels is turned off. All queries read the latest version of a record, because update and deletes are not possible.The MVCC processing to enforce isolation levels is turned off. All queries read the latest version of a record, because update and deletes are not possible.
撤销日志不会被使用。请禁用
innodb_undo_tablespaces和innodb_undo_directory配置选项的任何设置。The undo log is not used. Disable any settings for theinnodb_undo_tablespacesandinnodb_undo_directoryconfiguration options.