Mysql:read-only 配置
重点:
super_read_only、read_only:不会从master传递(复制)到slave
super_read_only=on隐含实现read_only=on
read_only=off隐含实现super_read_only=off
-
Property Value Command-Line Format --super-read-only[={OFF|ON}]
System Variable super_read_only
Scope Global Dynamic Yes Type Boolean Default Value OFF
If the
read_only
system variable is enabled, the server permits client updates only from users who have theSUPER
privilege. If thesuper_read_only
system variable is also enabled, the server prohibits client updates even from users who haveSUPER
. See the description of theread_only
system variable for a description of read-only mode and information about howread_only
andsuper_read_only
interact.Client updates prevented when
super_read_only
is enabled include operations that do not necessarily appear to be updates, such asCREATE FUNCTION
(to install a UDF) andINSTALL PLUGIN
. These operations are prohibited because they involve changes to tables in themysql
system database.Changes to
super_read_only
on a master server are not replicated to slave servers. The value can be set on a slave server independent of the setting on the master.
-
Property Value Command-Line Format --read-only[={OFF|ON}]
System Variable read_only
Scope Global Dynamic Yes Type Boolean Default Value OFF
When the
read_only
system variable is enabled, the server permits no client updates except from users who have theSUPER
privilege. This variable is disabled by default.The server also supports a
super_read_only
system variable (disabled by default), which has these effects:-
If
super_read_only
is enabled, the server prohibits client updates, even from users who have theSUPER
privilege. -
Setting
super_read_only
toON
implicitly forcesread_only
toON
. -
Setting
read_only
toOFF
implicitly forcessuper_read_only
toOFF
.
Even with
read_only
enabled, the server permits these operations:-
Updates performed by slave threads, if the server is a replication slave. In replication setups, it can be useful to enable
read_only
on slave servers to ensure that slaves accept updates only from the master server and not from clients. -
Use of
ANALYZE TABLE
orOPTIMIZE TABLE
statements. The purpose of read-only mode is to prevent changes to table structure or contents. Analysis and optimization do not qualify as such changes. This means, for example, that consistency checks on read-only replication slaves can be performed with mysqlcheck --all-databases --analyze. -
Operations on
TEMPORARY
tables. -
Inserts into the log tables (
mysql.general_log
andmysql.slow_log
); see Section 5.4.1, “Selecting General Query Log and Slow Query Log Output Destinations”. -
As of MySQL 5.7.16, updates to Performance Schema tables, such as
UPDATE
orTRUNCATE TABLE
operations.
Changes to
read_only
on a master server are not replicated to slave servers. The value can be set on a slave server independent of the setting on the master.The following conditions apply to attempts to enable
read_only
(including implicit attempts resulting from enablingsuper_read_only
):-
The attempt fails and an error occurs if you have any explicit locks (acquired with
LOCK TABLES
) or have a pending transaction. -
The attempt blocks while other clients have any ongoing statement, active
LOCK TABLES WRITE
, or ongoing commit, until the locks are released and the statements and transactions end. While the attempt to enableread_only
is pending, requests by other clients for table locks or to begin transactions also block untilread_only
has been set. -
The attempt blocks if there are active transactions that hold metadata locks, until those transactions end.
-
read_only
can be enabled while you hold a global read lock (acquired withFLUSH TABLES WITH READ LOCK
) because that does not involve
-
-
Property Value Command-Line Format --transaction-read-only[={OFF|ON}]
System Variable (>= 5.7.20) transaction_read_only
Scope (>= 5.7.20) Global, Session Dynamic (>= 5.7.20) Yes Type Boolean Default Value OFF
The transaction access mode. The value can be
OFF
(read/write; the default) orON
(read only).The transaction access mode has three scopes: global, session, and next transaction. This three-scope implementation leads to some nonstandard access-mode assignment semantics, as described later.
To set the global transaction access mode at startup, use the
--transaction-read-only
server option.At runtime, the access mode can be set directly using the
SET
statement to assign a value to thetransaction_read_only
system variable, or indirectly using theSET TRANSACTION
statement. For example, use thisSET
statement to set the global value:SET GLOBAL transaction_read_only = ON;
Setting the global
transaction_read_only
value sets the access mode for all subsequent sessions. Existing sessions are unaffected.To set the session or next-level
transaction_read_only
value, use theSET
statement. For most session system variables, these statements are equivalent ways to set the value:var_name value var_name value var_name value var_name value As mentioned previously, the transaction access mode has a next-transaction scope, in addition to the global and session scopes. To enable the next-transaction scope to be set,
SET
syntax for assigning session system variable values has nonstandard semantics fortransaction_read_only
,-
To set the session access mode, use any of these syntaxes:
value value value For each of those syntaxes, these semantics apply:
-
Sets the access mode for all subsequent transactions performed within the session.
-
Permitted within transactions, but does not affect the current ongoing transaction.
-
If executed between transactions, overrides any preceding statement that sets the next-transaction access mode.
-
Corresponds to
SET SESSION TRANSACTION {READ WRITE | READ ONLY}
(with theSESSION
keyword).
-
-
To set the next-transaction access mode, use this syntax:
SET @@transaction_read_only =
value
;For that syntax, these semantics apply:
-
Sets the access mode only for the next single transaction performed within the session.
-
Subsequent transactions revert to the session access mode.
-
Not permitted within transactions.
-
Corresponds to
SET TRANSACTION {READ WRITE | READ ONLY}
(without theSESSION
keyword).
-
For more information about
SET TRANSACTION
and its relationship to thetransaction_read_only
system variable, see Section 13.3.6, “SET TRANSACTION Statement”.Notetransaction_read_only
was added in MySQL 5.7.20 as an alias fortx_read_only
, which is now deprecated and is removed in MySQL 8.0. Applications should be adjusted to usetransaction_read_only
in preference totx_read_only
. -
-
Property Value Deprecated 5.7.20 System Variable tx_read_only
Scope Global, Session Dynamic Yes Type Boolean Default Value OFF
The default transaction access mode. The value can be
OFF
(read/write, the default) orON
(read only).Notetransaction_read_only
was added in MySQL 5.7.20 as an alias fortx_read_only
, which is now deprecated and is removed in MySQL 8.0. Applications should be adjusted to usetransaction_read_only
in preference totx_read_only
. See the description oftransaction_read_only
for details.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)