1概述

在一次项目配置主从同步时一直有1023 报错,因为主备数据不一致导致的,数据库中没有使用触发器,slave服务器my.cnf上也配置了read-only选项,为什么还可以在slave中插入/更新数据呢?
经过一番不懈努力,找到了原因:因为使用的是具有super权限的帐号连接的,恰好服务请求配置错了,配置到了从库,导致从库有数据写入,导致主备数据不一致,主从链路异常,改用普通帐号从库就不可写入了,
<1>也就是从库连接账号授权时不能指定有super或all权限(一般情况);
<2>修改参数 super_read_only ,默认值为OFF,修改为ON,可以使super 用户read-only。

《read_only

Command-Line Format    --read-only[={OFF|ON}]
System Variable    read_only
Scope    Global
Dynamic    Yes
Type    Boolean
Default Value    OFF
If the read_only system variable is enabled, the server permits no client updates except from users who have the SUPER 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 the SUPER privilege.

Setting super_read_only to ON implicitly forces read_only to ON.

Setting read_only to OFF implicitly forces super_read_only to OFF.

如果read_only启用了系统变量,则除了具有SUPER特权的用户之外,服务器不允许客户端更新 。默认情况下禁用此变量。

服务器还支持 super_read_only系统变量(默认禁用),具有以下效果:

如果super_read_only启用,服务器将禁止客户端更新,即使是具有SUPER 权限的用户。

设置super_read_only 为ON隐式强制 read_only为 ON。

设置read_only为 OFF隐式强制 super_read_only为 OFF。》
以上摘自官网https://dev.mysql.com/doc/refman/5.7/en/server-system-variables.html

2验证

下面简单测试一下:

2.1 参数值查看

mysql> show global variables like 'read-only%';
Empty set (0.00 sec)mysql>
mysql> show global variables like 'read_only%';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| read_only     | ON    |
+---------------+-------+
1 row in set (0.00 sec)mysql>
mysql> show global variables like 'super_read%';
+-----------------+-------+
| Variable_name   | Value |
+-----------------+-------+
| super_read_only | OFF   |
+-----------------+-------+
1 row in set (0.01 sec)mysql>

2.2创建测试用户并授权all

mysql> CREATE USER 'readtest'@'localhost' IDENTIFIED BY '123456';
Query OK, 0 rows affected (0.00 sec)
mysql> grant all on *.* to 'readtest'@'localhost';
Query OK, 0 rows affected, 1 warning (0.00 sec)mysql> ^DBye

2.3测试用户登录建表

[root@test-centos ~]# mysql -ureadtest -p
Enter password:
Server version: 5.7.28-log MySQL Community Server (GPL)
mysql>
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| readme_ccc         |
| sys                |
| test              |
+--------------------+
6 rows in set (0.00 sec)
mysql> use test;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -ADatabase changed
mysql>
mysql> create table retest (a int);
Query OK, 0 rows affected (0.02 sec)mysql> drop table retest;
Query OK, 0 rows affected (0.01 sec)mysql>
mysql> ^DBye

read-only状态下,super权限用户可以写入。

2.4修改收回测试用户权限

[root@test-centos ~]# mysql -uroot -p
Enter password:
mysql>
mysql> revoke all on *.* from 'readtest'@'localhost';
Query OK, 0 rows affected, 1 warning (0.01 sec)mysql>
mysql> ^DBye

2.5测试用户登录写入测试

[root@test-centos ~]# mysql -ureadtest -p
Enter password:
Server version: 5.7.28-log MySQL Community Server (GPL)
mysql> use test;
ERROR 1044 (42000): Access denied for user 'readtest'@'localhost' to database 'test'
mysql> ^DBye

没有操作权限。

2.6修改测试用户权限为没有super

[root@test-centos ~]# mysql -uroot -p
Enter password:
mysql>
mysql> grant all on *.* to 'readtest'@'localhost';
Query OK, 0 rows affected, 1 warning (0.01 sec)
mysql> revoke super on *.* from 'readtest'@'localhost';
Query OK, 0 rows affected, 1 warning (0.00 sec)mysql> ^DBye

2.7测试用户登录写入测试

[root@test-centos ~]# mysql -ureadtest -p
Enter password:
mysql> use test;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql>
mysql> create table retest (a int);
ERROR 1290 (HY000): The MySQL server is running with the --read-only option so it cannot execute this statement
mysql>

没有super权限用户在read-only库不可写入。

mysql> set global super_read_only=on;
Query OK, 0 rows affected (0.00 sec)mysql>
mysql> create table test (a int);
ERROR 1290 (HY000): The MySQL server is running with the --super-read-only option so it cannot execute this statement
mysql>

super_read_only配置为on,所有用户不可写入。

3 参数配置

3.1打开super_read_only=on, read_only也会隐式强制打开为on;

mysql> show global variables like 'read_only%';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| read_only     | ON    |
+---------------+-------+
1 row in set (0.00 sec)
mysql> set global super_read_only=off;
Query OK, 0 rows affected (0.00 sec)mysql> show global variables like 'read_only%';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| read_only     | ON    |
+---------------+-------+
1 row in set (0.00 sec)mysql> set global read_only=off;
Query OK, 0 rows affected (0.01 sec)mysql>
mysql> show global variables like 'read_only%';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| read_only     | OFF   |
+---------------+-------+
1 row in set (0.01 sec)mysql> set global super_read_only=on;
Query OK, 0 rows affected (0.01 sec)mysql> show global variables like 'read_only%';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| read_only     | ON    |
+---------------+-------+
1 row in set (0.00 sec)mysql>

3.2设置read_only为OFF,super-read-only 会隐式强制转为OFF

mysql> show global variables like '%read_only%';
+-----------------------+-------+
| Variable_name         | Value |
+-----------------------+-------+
| innodb_read_only      | OFF   |
| read_only             | ON    |
| super_read_only       | ON    |
| transaction_read_only | OFF   |
| tx_read_only          | OFF   |
+-----------------------+-------+
5 rows in set (0.04 sec)
mysql> set global read_only=off;
Query OK, 0 rows affected (0.01 sec)mysql> show global variables like '%read_only%';
+-----------------------+-------+
| Variable_name         | Value |
+-----------------------+-------+
| innodb_read_only      | OFF   |
| read_only             | OFF   |
| super_read_only       | OFF   |
| transaction_read_only | OFF   |
| tx_read_only          | OFF   |
+-----------------------+-------+
5 rows in set (0.01 sec)mysql>

MySQL 的read_only super_read_only相关推荐

  1. MySQL 的read_only 只读属性说明

    在MySQL数据库中,在进行数据迁移和从库只读状态设置时,都会涉及到只读状态和Master-Slave主从关系设置, 以下针对real_only只读属性做些笔记记录: 1) 对于MySQL单实例数据库 ...

  2. MySQL read_only 与 super_read_only 之间的关系

    目录 当super_read_only = 0, read_only=0,设置super_read_only=1 当super_read_only=0, read_only=1,设置 super_re ...

  3. mgr在mysql中指是什么字段_MySQL MGR

    一.安装插件(先进入数据库安装插件) INSTALL PLUGIN group_replication SONAME 'group_replication.so'; 二.调整MySQL各节点的配置文件 ...

  4. mysql代理中间件_MySQL-ProxySQL中间件(二)

    彩蛋监控MGR,需要在MySQL实例中配置一些监控脚本(MySQL 5.7 和 MySQL 8.0略有不同) 该脚本需要配置到sys库下,因笔记web显示问题,无法显示折行,但是不影响复制,可以自行复 ...

  5. MySQL用中间件ProxySQL实现读写分离和主节点故障应用无感应

    昨天做的用proxysql实现的读写分离,但是在实际的应用中这样的结构还很不完整,如果主节点出现故障那么整个拓扑的数据库也无法通过proxysql来调用了,所以还需要增加主节点故障后proxysql能 ...

  6. docker二进制安装mysql_Docker搭建MySQL读写分离主从模式 分布式数据库中间件Mycat分库分表应用...

    一.MySQL读写分离主从模式 1. 下载镜像 docker pull mysql 当前最新版本:mysql Ver 8.0.19 for Linux on x86_64 (MySQL Communi ...

  7. mysql 5.7 super_MySQL 5.7 下的对super用户只读

    在MySQL的主从复制场景下,遇上slave被意外写入数据是一件比较严重的问题,毕竟在一般情况下我们都希望slave仅用只读数据库,如果被意外写入数据可能会造成数据的不一致,从而导致主从的报错.因此在 ...

  8. 万字详述 MySQL ProxySQL

    墨墨导读:Proxysql读写分离的中间件,支持高可用 主从\ MGR \ PXC等环境,并提供连接池.缓存.日志记录等功能. ProxySQL是用C++语言开发的,一个轻量级开源软件,性能和功能满足 ...

  9. mysql mgr 读写分离_MySQL Group Replication mgr 单主 proxysql 读写分离配置过程

    1.前期准备,mgr安装见上一篇文章 2.创建用户和导入脚本 GRANT ALL ON *.* TO 'rootuser'@'%' IDENTIFIED BY '123456'; /mgr/mysql ...

最新文章

  1. 解决scrapy安装失败
  2. 查看python版本和安装路径
  3. Angular 8 + Spring Boot 2.2:立即构建一个CRUD应用程序!
  4. 汇编语言中常用指令对标志位寄存器的影响
  5. codeforces 758 A
  6. 【学习率预热】Warm up
  7. 分布式架构中常见理论以及如何才能设计出高可用的分布式架构?
  8. MVC设计之MVC设计模式(介绍)
  9. java 反射 asm,Java反射工具包reflectasm
  10. FreeMarker(七)Html转义
  11. 无法导入 指定文件不是注册脚本 您在注册表编辑器中只能导入二进位注册文件.reg
  12. Red Hat 9.0下载及安装
  13. 2022城通网盘仿蓝奏云修复版源码
  14. 【MySQL数据库设计与应用(四)】视图
  15. MFC字符串资源IDR_MAINFRAME和IDR_XXXXTYPE
  16. compass项目配置文件config.rb
  17. ArcGIS教程:要素类基础知识(一)
  18. mac m1上esc键失灵不能退出vi解决方法
  19. 测鬼记(上)——回岗(十)
  20. 5G及后5G时代:万物互联到万物智能的黄金时代

热门文章

  1. 血泪史!外包如何找到靠谱的兼职程序员?
  2. python django面试题(第八章)
  3. 最详细的MySQL事务特性及原理讲解!(一)
  4. Unity之webGL问题汇总
  5. 读书笔记:Mysql实战45讲 (1-10讲)
  6. 为什么美国程序员不用加班,而中国程序员就只能996?
  7. Jetson nano/NX 部署Yolo v5过程记录
  8. 2018函授英语计算机统考,大学英语b网络统考
  9. 路由器输入宽带账号密码上网
  10. Ubuntu22.04更换国内镜像源(阿里、网易163、清华、中科大)