mysql只读库的数据同步_mysql只读模式下数据迁移,保证数据一致性
在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=1;
mysql> show global variables like "%read_only%";
将MySQL从只读设置为读写状态的命令:mysql> unlock tables;
mysql> set global read_only=0;
对于需要保证master-slave主从同步的salve库,如果要设置为只读状态,需要执行的命令为:
mysql> set global read_only=1; 打开只读开关
将salve库从只读状态变为读写状态,需要执行的命令是:
mysql> set global read_only=0; 打开读写开关
对于数据库读写状态,主要靠 “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库上要保证具有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错误:
copy
(test01@172.32.1.200) [data03]> show tables;
+------------------+
| Tables_in_data03 |
+------------------+
| t01 |
| t02 |
| user |
+------------------+
3 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 |
+------------------+-------+
3 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=3;
ERROR 1290 (HY000): The MySQL server is running with the --read-only option so it cannot execute this statement
(test01@172.32.1.200) [data03]> update t01 set id1=id1+30 where id1=3;
ERROR 1290 (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(9,9,9);
ERROR 1290 (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 |
+-----+------+------+
| 1 | 1 | 1 |
| 2 | 2 | 2 |
| 4 | 4 | 4 |
| 5 | 5 | 5 |
| 6 | 6 | 6 |
+-----+------+------+
5 rows in set (0.00 sec)
(test01@172.32.1.200) [data03]>
实验二:设定了全局读写后,具有super权限的用户进行数据修改后,也会提示错误ERROR 1223:
view plain copy
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 |
+------------------+
3 rows in set (0.00 sec)
mysql> select * from t01;
+-----+------+------+
| id1 | a1 | b1 |
+-----+------+------+
| 1 | 1 | 1 |
| 2 | 2 | 2 |
| 4 | 4 | 4 |
| 5 | 5 | 5 |
| 6 | 6 | 6 |
+-----+------+------+
5 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 |
+------------------+-------+
3 rows in set (0.00 sec)
mysql>
mysql>
mysql> insert into t01(id1,a1,b1) values(8,8,8);
Query OK, 1 row affected (0.00 sec)
mysql> update t01 set a1=a1+10 where id1=2;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> delete from t01 where id1=4;
Query OK, 1 row affected (0.00 sec)
mysql> select * from t01;
+-----+------+------+
| id1 | a1 | b1 |
+-----+------+------+
| 1 | 1 | 1 |
| 2 | 12 | 2 |
| 5 | 5 | 5 |
| 6 | 6 | 6 |
| 8 | 8 | 8 |
+-----+------+------+
5 rows in set (0.00 sec)
mysql>
mysql> flush tables with read lock;
Query OK, 0 rows affected (0.00 sec)
mysql>
mysql> insert into t01(id1,a1,b1) values(9,9,9);
ERROR 1223 (HY000): Can't execute the query because you have a conflicting read lock
mysql>
mysql> update t01 set a1=a1+10 where id1=5;
ERROR 1223 (HY000): Can't execute the query because you have a conflicting read lock
mysql>
mysql> delete from t01 where id1=5;
ERROR 1223 (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 |
+--------------------+
10 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 |
+------------------+-------+
3 rows in set (0.00 sec)
mysql>
mysql>show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 172.32.1.200
Master_User: repl
Master_Port: 3307
Connect_Retry: 60
Master_Log_File: mysql-bin.000009
Read_Master_Log_Pos: 5853
Relay_Log_File: huobiDBtest-relay-bin.000002
Relay_Log_Pos: 6016
Relay_Master_Log_File: mysql-bin.000009
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: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 5853
Relay_Log_Space: 6195
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 2003307
Master_UUID: 6f68eea7-76e9-11e4-8f99-00221904cd5d
Master_Info_File: /data/mysqldata/3308/data/master.info
SQL_Delay: 0
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: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
1 row in set (0.00 sec)
mysql>
mysql>flush tables with read lock;
Query OK, 0 rows affected (0.00 sec)
mysql>show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 172.32.1.200
Master_User: repl
Master_Port: 3307
Connect_Retry: 60
Master_Log_File: mysql-bin.000009
Read_Master_Log_Pos: 6531
Relay_Log_File: huobiDBtest-relay-bin.000002
Relay_Log_Pos: 6016
Relay_Master_Log_File: mysql-bin.000009
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: 0
Last_Error:Skip_Counter: 0
Exec_Master_Log_Pos: 5853
Relay_Log_Space: 6873
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 120
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 2003307Master_UUID: 6f68eea7-76e9-11e4-8f99-00221904cd5d
Master_Info_File: /data/mysqldata/3308/data/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Waiting for global read lock
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
1 row in set (0.00 sec)
mysql>
mysql>
mysql>select * from data03.t01;
+-----+------+------+
| id1 | a1 | b1 |
+-----+------+------+
| 1 | 1 | 1 |
| 2 | 2 | 2 |
| 4 | 4 | 4 |
| 5 | 5 | 5 |
| 6 | 6 | 6 |
+-----+------+------+
5 rows in set (0.00 sec)
mysql>
mysql>unlock tables;
Query OK, 0 rows affected (0.00 sec)
mysql>show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 172.32.1.200
Master_User: repl
Master_Port: 3307
Connect_Retry: 60
Master_Log_File: mysql-bin.000009
Read_Master_Log_Pos: 6531
Relay_Log_File: huobiDBtest-relay-bin.000002
Relay_Log_Pos: 6694
Relay_Master_Log_File: mysql-bin.000009
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: 0Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 6531
Relay_Log_Space: 6873
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 2003307
Master_UUID: 6f68eea7-76e9-11e4-8f99-00221904cd5d
Master_Info_File: /data/mysqldata/3308/data/master.info
SQL_Delay: 0
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: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
1 row in set (0.00 sec)
mysql> select * from data03.t01;
+-----+------+------+
| id1 | a1 | b1 |
+-----+------+------+
| 1 | 1 | 1 |
| 2 | 12 | 2 |
| 5 | 5 | 5 |
| 6 | 6 | 6 |
| 8 | 8 | 8 |
+-----+------+------+
5 rows in set (0.00 sec)
##########################################
.FLUSH TABLES WITH READ LOCK
这个命令是全局读锁定,执行了命令之后所有库所有表都被锁定只读。一般都是用在数据库联机备份,这个时候数据库的写操作将被阻塞,读操作顺利进行。
解锁的语句也是unlock tables。
2.LOCK TABLES tbl_name [AS alias] {READ [LOCAL] | [LOW_PRIORITY] WRITE}
这个命令是表级别的锁定,可以定制锁定某一个表。例如: lock tables test read; 不影响其他表的写操作。
解锁语句也是unlock tables。
重点:
这两个语句在执行的时候都需要注意个特点,就是 隐式提交的语句。在退出MySQL终端的时候都会隐式的执行unlock tables。也就是如果要让表锁定生效就必须一直保持对话。
P.S. MYSQL的read lock和wirte lock
read-lock: 允许其他并发的读请求,但阻塞写请求,即可以同时读,但不允许任何写。也叫共享锁
write-lock: 不允许其他并发的读和写请求,是排他的(exclusive)。也叫独占锁
mysql只读库的数据同步_mysql只读模式下数据迁移,保证数据一致性相关推荐
- 如何利用DTS数据同步功能,快速创建数据同步作业
数据传输服务DTS(Data Transmission Service)提供的数据同步功能简单易用,您只需在控制台上进行简单操作,即可完成整个数据同步作业的配置. 注意事项 本文仅简单介绍数据同步作业 ...
- ag-grid 中外部编辑数据同步修改table单元格数据,数据实时更新
ag-grid 中外部编辑数据同步修改table单元格数据 这里只探讨本人已经使用的一个方法 getTags(params).then(res => {console.log(res, '=== ...
- 无线WLAN隧道转发模式下数据的封装以及转发过程
无线WLAN隧道转发模式下数据的封装以及转发过程 实验用的拓扑: AP1.AP2的业务vlan为101.102,管理vlan为100,AR路由器作为DHCP服务器为AP和终端分配IP地址.DNS等信息 ...
- Write operations are not allowed in read-only mode 只读模式下(FlushMode.NEVER/MANUAL)写操作不允
来源:http://blog.sina.com.cn/s/blog_656ffe730100ugtw.html org.springframework.dao.InvalidDataAccessApi ...
- mysql otter 数据同步_MySQL数据同步之otter
一.otter介绍 基于日志数据,用于MySQL或者ORACLE之间准实时同步数据. 用途: mysql/oracle互相同步 中间表/行记录同步 二.原理及架构图 otter整体模块 manager ...
- mysql如何关闭只读模式_mysql只读模式的设置方法与实验【转】
在MySQL数据库中,在进行数据迁移和从库只读状态设置时,都会涉及到只读状态和Master-slave的设置和关系. 经过实际测试,对于MySQL单实例数据库和master库,如果需要设置为只读状态, ...
- oracle往mysql数据同步存储过程_Oracle数据库之间数据同步
源库(ENV库)中定义如下包: 包定义如下: CREATE OR REPLACE PACKAGE PKG_DATA_REPORT IS - Author : JOHNFNASH - Created : ...
- mysql 先删后增 更新_MySQL 高级操作——新增数据、更新数据、删除数据、查询数据...
新增数据 多数据插入 只要写一次insert指令,但是可以插入多条记录 语法:insert into 表名 [(字段列表)] values (值列表1),(值列表2),(值列表3); 主键冲突 主键冲 ...
- mysql主主复制半同步_mysql主从复制中的半同步复制
实验mysql借助google提供的插件,完成半同步复制模型: 物理机依然是win7系统,虚拟机为centos7: 主节点:192.168.255.2 从节点:192.168.255.3 先配置为主从 ...
最新文章
- C++_STL——deque and vector
- 皮一皮:被看穿的既视感...
- python组件的react实现_React-Router动态路由设计最佳实践
- 鸟哥的Linux私房菜(基础篇)-第三章、主机规划与磁盘分区(三.1. Linux与硬件的搭配)
- 原始nginx.conf备份
- Spring 提供几种配置方式来设置元数据?
- 什么是java构造函数_什么是java构造函数
- 记一次用WPScan辅助渗透WordPress站点
- 【转】漫谈ANN(2):BP神经网络
- 一些常见的光学标定板模式
- mysql 批量替换域名_msyql 中批量替换url网址中的域名方案。
- python用空格隔开每一个字符_python实现将一串字符每两个一组,中间用空格隔开...
- win10安装ipython_win10下安装Anaconda的教程(python环境+jupyter_notebook)
- Magnific Popup – 免费的响应式 jQuery Lightbox 插件
- 汇川PLC AM600、AC800系列Modbus TCP通讯案例(PLC和C++源码)
- 全球最快下载工具 XDM
- 神州数码:我国市民卡发展之路探讨
- 开发可以自动运行程序的U盘
- 艾媒:ofo用户份额领先摩拜超六成 每10辆共享单车7辆小黄车
- 服务器显示中国移动,中国移动服务器地址是什么