在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錯誤:
(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; <span style="color:#ff0000;">ERROR 1290 (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+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:
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); <span style="color:#ff0000;">ERROR 1223 (HY000): Can't execute the query because you have a conflicting read lock</span> 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 <span style="color:#ff0000;"> Master_Log_File: mysql-bin.000009 Read_Master_Log_Pos: 6531</span> 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 <span style="color:#ff0000;"> Exec_Master_Log_Pos: 5853</span> 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: 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: 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 <span style="color:#ff0000;"> Master_Log_File: mysql-bin.000009 Read_Master_Log_Pos: 6531</span> 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: 0 Last_Error: Skip_Counter: 0 <span style="color:#ff0000;"> Exec_Master_Log_Pos: 6531</span> 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)
文章轉載:https://blog.csdn.net/yumushui/article/details/41645469