MySQL 的 super_read_only 和 read_only 参数

MySQL 的 super_read_only 和 read_only 参数

一、参数基本概念

1. read_only 参数

  • 作用:控制普通用户是否只能读取数据
  • 影响范围:所有非SUPER权限的用户
  • 默认值:OFF(可读写)

2. super_read_only 参数

  • 作用:控制包括SUPER权限用户在内的所有用户是否只能读取数据
  • 影响范围:所有用户(包括具有SUPER权限的用户)
  • 默认值:OFF(可读写)

二、参数关系与优先级

特性 read_only super_read_only
影响普通用户 ✔️ ✔️
影响SUPER权限用户 ✖️ ✔️
优先级
依赖关系 - 依赖read_only

重要规则

  1. super_read_only=ON 时,会自动将 read_only 设置为 ON
  2. read_only=OFF 时,super_read_only 必须为 OFF(不能单独设置 super_read_only=ON 而 read_only=OFF)

三、参数设置方法

1. 动态设置(无需重启)

sql 复制代码
-- 设置只读模式(普通用户)
SET GLOBAL read_only = ON;

-- 设置超级只读模式
SET GLOBAL super_read_only = ON;

-- 取消只读模式(必须先取消super_read_only)
SET GLOBAL super_read_only = OFF;
SET GLOBAL read_only = OFF;

2. 配置文件设置(需重启)

ini 复制代码
[mysqld]
read_only=1
super_read_only=1

四、参数使用场景

1. read_only 适用场景

  • 主从复制中,从库设置为只读
  • 报表库、数据分析库等只需读取的环境
  • 系统维护期间防止数据修改

2. super_read_only 适用场景

  • 高安全性要求的只读环境
  • 防止管理员误操作修改数据
  • 关键备份服务器保护
  • 云数据库的灾备实例

五、参数行为验证

1. 普通用户测试

sql 复制代码
-- 当read_only=ON时
mysql> INSERT INTO test.t1 VALUES(1); 
ERROR 1290 (HY000): The MySQL server is running with the --read-only option so it cannot execute this statement

-- 当read_only=OFF时(正常执行)

2. SUPER用户测试

sql 复制代码
-- 当read_only=ON但super_read_only=OFF时
mysql> INSERT INTO test.t1 VALUES(1); 
Query OK, 1 row affected (0.00 sec)  -- SUPER用户仍可写入

-- 当super_read_only=ON时
mysql> INSERT INTO test.t1 VALUES(1);
ERROR 1290 (HY000): The MySQL server is running with the --super-read-only option so it cannot execute this statement

六、注意事项

  1. 复制环境 :从库设置 read_only=ON 不会阻止复制线程写入
  2. 临时表:只读模式仍允许创建临时表
  3. 系统表:某些系统表的更新不受只读模式限制
  4. 权限变更:只读模式不影响GRANT/REVOKE操作
  5. 启动顺序super_read_only 在启动时最后应用

七、相关系统变量

sql 复制代码
SHOW VARIABLES LIKE '%read_only%';
/*
+------------------+-------+
| Variable_name    | Value |
+------------------+-------+
| read_only        | ON    |
| super_read_only  | ON    |
+------------------+-------+
*/

八、最佳实践建议

  1. 主从架构 :建议从库设置 read_only=ON
  2. 关键备份 :建议设置 super_read_only=ON
  3. 变更流程 :先设置 read_only=ON,再设置 super_read_only=ON
  4. 取消流程 :先取消 super_read_only,再取消 read_only
  5. 监控报警:监控只读状态异常变化

详细内容请查看官方文档:

dart 复制代码
https://dev.mysql.com/doc/refman/8.4/en/server-system-variables.html#sysvar_read_only
https://dev.mysql.com/doc/refman/8.4/en/server-system-variables.html#sysvar_super_read_only
相关推荐
极限实验室17 分钟前
IK 字段级别词典的升级之路
数据库
chen_note41 分钟前
LAMP及其环境的部署搭建
linux·运维·mysql·php·apache·lamp·phpmyadmin
瓶子xf1 小时前
基于mysql云数据库对比PowerBI vs QuickBI vs FineBI更换数据源的可行性
mysql·powerbi·quickbi
曾几何时`1 小时前
MySQL(配置)——MariaDB使用
数据库·mysql
努力学习java的哈吉米大王1 小时前
MySQL——MVCC
数据库·mysql
数据要素X1 小时前
【数据架构10】数字政府架构篇
大数据·运维·数据库·人工智能·架构
lixzest2 小时前
Redis实现数据传输简介
数据库·redis·缓存
搬砖的小熊猫2 小时前
MySQL常见面试题
数据库·mysql
weixin_419658312 小时前
MySQL的JDBC编程
数据库·mysql
JavaLearnerZGQ2 小时前
Docker部署Nacos
数据库·docker·容器