转自

mysql只读模式的设置方法与实验 - yumushui的专栏 - CSDN博客
http://blog.csdn.net/yumushui/article/details/41645469

MySQL数据库中,在进行数据迁移和从库只读状态设置时,都会涉及到只读状态和Master-slave的设置和关系。

经过实际测试,对于mysql单实例数据库和master库,如果需要设置为只读状态,需要进行如下操作和设置:
      将MySQL设置为只读状态的命令:

# mysql -uroot -p
mysql> show global variables like "%read_only%";
mysql> flush tables with read lock;
mysql> set global read_only=;
mysql> show global variables like "%read_only%";

将MySQL从只读设置为读写状态的命令:

mysql> unlock tables;
mysql> set global read_only=;

对于需要保证master-slave主从同步的salve库,如果要设置为只读状态,需要执行的命令为:

mysql> set global read_only=;

将salve库从只读状态变为读写状态,需要执行的命令是:

mysql> set global read_only=;

对于数据库读写状态,主要靠 “read_only”全局参数来设定;默认情况下,数据库是用于读写操作的,所以read_only参数也是0或faluse状态,这时候不论是本地用户还是远程访问数据库的用户,都可以进行读写操作;如需设置为只读状态,将该read_only参数设置为1或TRUE状态,但设置 read_only=1 状态有两个需要注意的地方:

  1.read_only=1只读模式,不会影响slave同步复制的功能,所以在MySQL slave库中设定了read_only=1后,通过 show slave status\G 命令查看salve状态,可以看到salve仍然会读取master上的日志,并且在slave库中应用日志,保证主从数据库同步一致;

2.read_only=1只读模式,可以限定普通用户进行数据修改的操作,但不会限定具有super权限的用户的数据修改操作;在MySQL中设置read_only=1后,普通的应用用户进行insert、update、delete等会产生数据变化的DML操作时,都会报出数据库处于只读模式不能发生数据变化的错误,但具有super权限的用户,例如在本地或远程通过root用户登录到数据库,还是可以进行数据变化的DML操作;

为了确保所有用户,包括具有super权限的用户也不能进行读写操作,就需要执行给所有的表加读锁的命令 “flush tables with read lock;”,这样使用具有super权限的用户登录数据库,想要发生数据变化的操作时,也会提示表被锁定不能修改的报错。

这样通过 设置“read_only=1”和“flush tables with read lock;”两条命令,就可以确保数据库处于只读模式,不会发生任何数据改变,在MySQL进行数据库迁移时,限定master主库不能有任何数据变化,就可以通过这种方式来设定。

但同时由于加表锁的命令对数据库表限定非常严格,如果再slave从库上执行这个命令后,slave库可以从master读取binlog日志,但不能够应用日志,slave库不能发生数据改变,当然也不能够实现主从同步了,这时如果使用 “unlock tables;”解除全局的表读锁,slave就会应用从master读取到的binlog日志,继续保证主从库数据库一致同步。所以从库slave不要使用 “flush tables with read lock;” 否则无法写入数据同步。

为了保证主从同步可以一直进行,在slave库上要保证具有super权限的root等用户只能在本地登录,不会发生数据变化,其他远程连接的应用用户只按需分配为select,insert,update,delete等权限,保证没有super权限,则只需要将salve设定“read_only=1”模式,即可保证主从同步,又可以实现从库只读。

相对的,设定“read_only=1”只读模式开启的解锁命令为设定“read_only=0”;设定全局锁“flush tables with read lock;”,对应的解锁模式命令为:“unlock tables;”.

当然设定了read_only=1后,所有的select查询操作都是可以正常进行的。

实验一:设置了read_only=1后,远程业务用户进行数据库修改会提示ERROR 1290错误

(test01@172.32.1.200) [data03]> show tables;
+------------------+
| Tables_in_data03 |
+------------------+
| t01 |
| t02 |
| user |
+------------------+
rows in set (0.00 sec) (test01@172.32.1.200) [data03]>
(test01@172.32.1.200) [data03]>
(test01@172.32.1.200) [data03]> show global variables like "%read_only%";
+------------------+-------+
| Variable_name | Value |
+------------------+-------+
| innodb_read_only | OFF |
| read_only | ON |
| tx_read_only | OFF |
+------------------+-------+
rows in set (0.00 sec) (test01@172.32.1.200) [data03]>
(test01@172.32.1.200) [data03]>
(test01@172.32.1.200) [data03]> delete from t01 where id1=;
<span style="color:#ff0000;">ERROR (HY000): The MySQL server is running with the --read-only option so it cannot execute this statement</span>
(test01@172.32.1.200) [data03]> update t01 set id1=id1+ where id1=;
ERROR (HY000): The MySQL server is running with the --read-only option so it cannot execute this statement
(test01@172.32.1.200) [data03]> insert into t01(id1,a1,b1) values(,,);
ERROR (HY000): The MySQL server is running with the --read-only option so it cannot execute this statement
(test01@172.32.1.200) [data03]>
(test01@172.32.1.200) [data03]> select * from t01;
+-----+------+------+
| id1 | a1 | b1 |
+-----+------+------+
| | | |
| | | |
| | | |
| | | |
| | | |
+-----+------+------+
rows in set (0.00 sec) (test01@172.32.1.200) [data03]>

实验二:设定了全局读写后,具有super权限的用户进行数据修改后,也会提示错误ERROR 1223:

mysql> use data03;
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> show tables;
+------------------+
| Tables_in_data03 |
+------------------+
| t01 |
| t02 |
| user |
+------------------+
rows in set (0.00 sec) mysql> select * from t01;
+-----+------+------+
| id1 | a1 | b1 |
+-----+------+------+
| | | |
| | | |
| | | |
| | | |
| | | |
+-----+------+------+
rows in set (0.00 sec) mysql>
mysql> show global variables like "%read_only%";
+------------------+-------+
| Variable_name | Value |
+------------------+-------+
| innodb_read_only | OFF |
| read_only | ON |
| tx_read_only | OFF |
+------------------+-------+
rows in set (0.00 sec) mysql>
mysql>
mysql> insert into t01(id1,a1,b1) values(,,);
Query OK, row affected (0.00 sec) mysql> update t01 set a1=a1+ where id1=;
Query OK, row affected (0.00 sec)
Rows matched: Changed: Warnings: mysql> delete from t01 where id1=;
Query OK, row affected (0.00 sec) mysql> select * from t01;
+-----+------+------+
| id1 | a1 | b1 |
+-----+------+------+
| | | |
| | | |
| | | |
| | | |
| | | |
+-----+------+------+
rows in set (0.00 sec) mysql>
mysql> flush tables with read lock;
Query OK, rows affected (0.00 sec) mysql>
mysql> insert into t01(id1,a1,b1) values(,,);
<span style="color:#ff0000;">ERROR (HY000): Can't execute the query because you have a conflicting read lock</span>
mysql>
mysql> update t01 set a1=a1+ where id1=;
ERROR (HY000): Can't execute the query because you have a conflicting read lock
mysql>
mysql> delete from t01 where id1=;
ERROR (HY000): Can't execute the query because you have a conflicting read lock
mysql>
mysql>

实验三:MySQL从库设定read_only=1后主从同步正常,设定表读锁后,不能同步,解除读锁后,主从同步恢复。

mysql>
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| bitvc |
| data03 |
| ga |
| jiradb |
| meibi |
| meibi02 |
| mysql |
| performance_schema |
| sbtest |
+--------------------+
rows in set (0.00 sec) mysql>
mysql>
mysql>
mysql> show global variables like "%read_only%";
+------------------+-------+
| Variable_name | Value |
+------------------+-------+
| innodb_read_only | OFF |
| read_only | ON |
| tx_read_only | OFF |
+------------------+-------+
rows in set (0.00 sec) mysql>
mysql> show slave status\G
*************************** . row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 172.32.1.200
Master_User: repl
Master_Port:
Connect_Retry:
Master_Log_File: mysql-bin.
Read_Master_Log_Pos:
Relay_Log_File: huobiDBtest-relay-bin.
Relay_Log_Pos:
Relay_Master_Log_File: mysql-bin.
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno:
Last_Error:
Skip_Counter:
Exec_Master_Log_Pos:
Relay_Log_Space:
Until_Condition: None
Until_Log_File:
Until_Log_Pos:
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master:
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno:
Last_IO_Error:
Last_SQL_Errno:
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id:
Master_UUID: 6f68eea7-76e9-11e4-8f99-00221904cd5d
Master_Info_File: /data/mysqldata//data/master.info
SQL_Delay:
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
Master_Retry_Count:
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position:
row in set (0.00 sec) mysql>
mysql> flush tables with read lock;
Query OK, rows affected (0.00 sec) mysql> show slave status\G
*************************** . row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 172.32.1.200
Master_User: repl
Master_Port:
Connect_Retry:
<span style="color:#ff0000;"> Master_Log_File: mysql-bin.
Read_Master_Log_Pos: </span>
Relay_Log_File: huobiDBtest-relay-bin.
Relay_Log_Pos:
Relay_Master_Log_File: mysql-bin.
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno:
Last_Error:
Skip_Counter:
<span style="color:#ff0000;"> Exec_Master_Log_Pos: </span>
Relay_Log_Space:
Until_Condition: None
Until_Log_File:
Until_Log_Pos:
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master:
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno:
Last_IO_Error:
Last_SQL_Errno:
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id:
Master_UUID: 6f68eea7-76e9-11e4-8f99-00221904cd5d
Master_Info_File: /data/mysqldata//data/master.info
SQL_Delay:
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Waiting for global read lock
Master_Retry_Count:
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position:
row in set (0.00 sec) mysql>
mysql>
mysql> select * from data03.t01;
+-----+------+------+
| id1 | a1 | b1 |
+-----+------+------+
| | | |
| | | |
| | | |
| | | |
| | | |
+-----+------+------+
rows in set (0.00 sec) mysql>
mysql> unlock tables;
Query OK, rows affected (0.00 sec) mysql> show slave status\G
*************************** . row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 172.32.1.200
Master_User: repl
Master_Port:
Connect_Retry:
<span style="color:#ff0000;"> Master_Log_File: mysql-bin.
Read_Master_Log_Pos: </span>
Relay_Log_File: huobiDBtest-relay-bin.
Relay_Log_Pos:
Relay_Master_Log_File: mysql-bin.
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno:
Last_Error:
Skip_Counter:
<span style="color:#ff0000;"> Exec_Master_Log_Pos: </span>
Relay_Log_Space:
Until_Condition: None
Until_Log_File:
Until_Log_Pos:
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master:
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno:
Last_IO_Error:
Last_SQL_Errno:
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id:
Master_UUID: 6f68eea7-76e9-11e4-8f99-00221904cd5d
Master_Info_File: /data/mysqldata//data/master.info
SQL_Delay:
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
Master_Retry_Count:
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position:
row in set (0.00 sec) mysql> select * from data03.t01;
+-----+------+------+
| id1 | a1 | b1 |
+-----+------+------+
| | | |
| | | |
| | | |
| | | |
| | | |
+-----+------+------+
rows in set (0.00 sec)

MySQL数据库设置为只读及测试【转】的更多相关文章

  1. 在mysql数据库中制作千万级测试表

    在mysql数据库中制作千万级测试表 前言: 最近准备深入的学一下mysql,包括各种引擎的特性.性能优化.分表分库等.为了方便测试性能.分表等工作,就需要先建立一张比较大的数据表.我这里准备先建一张 ...

  2. MySQL数据库设置远程访问权限方法小结

    http://www.jb51.net/article/42441.htm MySQL基础知识第一期,如何远程访问MySQL数据库设置权限方法总结,讨论访问单个数据库,全部数据库,指定用户访问,设置访 ...

  3. 远程访问CENTOS的MYSQL数据库设置

    远程访问CENTOS的MYSQL数据库设置 mysql -u root grant all privileges on *.* to root@'%'identified by 'root'; 后面的 ...

  4. MySQL数据库设置编码格式和时区

    MySQL数据库设置编码格式和时区 MySQL5版本: url=jdbc:mysql://localhost:3306/test?characterEncoding=utf-8 MySQL6版本及以上 ...

  5. MySQL Replication 详解MySQL数据库设置主从同步的方法

    MySQL同步的流程大致如下:  1.主服务器(master)将变更事件(更新.删除.表结构改变等等)写入二进制日志(master log). 2.从服务器(slave)的IO线程从主服务器(binl ...

  6. 使用SQLyog远程访问mysql数据库设置

    mysql数据库远程访问设置方法 1.修改localhost更改 "mysql" 数据库里的 "user" 表里的 "host" 项,从&q ...

  7. django中mysql数据库设置错误解决方法

    刚在django中settings.py进行设置mysql数据库. 当进行执行python manage.py shell命令时会报以下错误: 只需要在settings.py中 DATABASES = ...

  8. Mysql 数据库设置三大范式 数据库五大约束 数据库基础配置

    数据库设置三大范式 1.第一范式(确保每列保持原子性) 第一范式是最基本的范式.如果数据库表中的所有字段值都是不可分解的原子值,就说明该数据库满足第一范式. 第一范式的合理遵循需要根据系统给的实际需求 ...

  9. c/c++连接mysql数据库设置及乱码问题(vs2013连接mysql数据库,使用Mysql API操作数据库)

    我的安装环境: (1)vs2013(32位版) (vs2013只有32位的 没有64位的,但是它可以编译出64位的程序)  : (2)mysql-5.7.15(64位) vs2013中的设置(按步骤来 ...

随机推荐

  1. HDU2993_MAX Average Problem

    题目要求你在n个数的序列中,找出一段连续的长度不小于k的连续的序列,使得这个序列的平均数最大.输出这个平均数. 典型的优先队列.首先我们需要根据输入的序列,制造一个和序列. 然后从k开始往后面走,其实 ...

  2. Struts按着配置文件的加载的顺序,后面文件和前面文件相同的配置,后面的会把前面的文件的值覆盖

    Struts按着配置文件的加载的顺序,后面文件和前面文件相同的配置,后面的会把前面的文件的值覆盖

  3. BZOJ 2152 聪聪可可(树形DP)

    给出一颗n个点带边权的树(n<=20000),求随机选择两个点,使得它们之间的路径边权是3的倍数的概率是多少. 首先总的对数是n*n,那么只需要统计路径边权是3的倍数的点对数量就行了. 考虑将无 ...

  4. 【bzoj4011】[HNOI2015]落忆枫音 容斥原理+拓扑排序+dp

    题目描述 给你一张 $n$ 个点 $m$ 条边的DAG,$1$ 号节点没有入边.再向这个DAG中加入边 $x\to y$ ,求形成的新图中以 $1$ 为根的外向树形图数目模 $10^9+7$ . 输入 ...

  5. 【bzoj1004】[HNOI2008]Cards Burnside引理+背包dp

    题目描述 用三种颜色染一个长度为 $n=Sr+Sb+Sg$ 序列,要求三种颜色分别有 $Sr,Sb,Sg$ 个.给出 $m$ 个置换,保证这 $m$ 个置换和置换 ${1,2,3,...,n\choo ...

  6. Unbuntu+nginx+mysql+php

    1/准备 sudo su --切换到root 2/nginx安装 apt-get update apt-get install nginx 3/mysql 安装 apt-get install mys ...

  7. Tajo--一个分布式数据仓库系统(设计架构)

    上一篇Tajo--一个分布式数据仓库系统(概述)废话了一通,下面介绍一下Tajo的体系结构.以及官方的实验成果吧 一.体系架构 Tajo采用了Master-Worker架构(下图虚线框目前还在计划中) ...

  8. Windows用户相关操作

    获取所有用户 NET_API_STATUS NetUserEnum( LPCWSTR servername, DWORD level, DWORD filter, LPBYTE* bufptr, DW ...

  9. miya--图片上传--搭建分布式文件服务器(FastDFS+Nginx)

    资料获取(FastDFS+Nginx): 链接:https://pan.baidu.com/s/1kUI5WH5 密码:kzfd 安装rz,sz功能: yum install lrzsz 主攻: 利用 ...

  10. hiho 1044 : 状态压缩

    #1044 : 状态压缩·一 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho在兑换到了喜欢的奖品之后,便继续起了他们的美国之行,思来想去,他们决定乘坐火车 ...