十年網(wǎng)站開發(fā)經(jīng)驗 + 多家企業(yè)客戶 + 靠譜的建站團隊
量身定制 + 運營維護+專業(yè)推廣+無憂售后,網(wǎng)站問題一站解決
在MySQL數(shù)據(jù)庫中,在進行數(shù)據(jù)遷移和從庫只讀狀態(tài)設(shè)置時,都會涉及到只讀狀態(tài)和Master-slave的設(shè)置和關(guān)系。
經(jīng)過實際測試,對于MySQL單實例數(shù)據(jù)庫和master庫,如果需要設(shè)置為只讀狀態(tài),需要進行如下操作和設(shè)置:
一般單實例情況下將MySQL設(shè)置為只讀狀態(tài)的命令:
# 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從只讀設(shè)置為讀寫狀態(tài)的命令:
mysql> unlock tables; mysql> set global read_only=0; 對于需要保證master-slave主從同步的salve庫,如果要設(shè)置為只讀狀態(tài),需要執(zhí)行的命令為:
mysql> set global read_only=1; 打開只讀開關(guān)
將salve庫從只讀狀態(tài)變?yōu)樽x寫狀態(tài),需要執(zhí)行的命令是:
mysql> set global read_only=0; 打開讀寫開關(guān)
對于數(shù)據(jù)庫讀寫狀態(tài),主要靠 “read_only”全局參數(shù)來設(shè)定;
默認情況下,數(shù)據(jù)庫是用于讀寫操作的,所以read_only參數(shù)也是0或faluse狀態(tài),這時候不論是本地用戶還是遠程訪問數(shù)據(jù)庫的用戶,都可以進行讀寫操作;
如需設(shè)置為只讀狀態(tài),將該read_only參數(shù)設(shè)置為1或TRUE狀態(tài),但設(shè)置 read_only=1 狀態(tài)有兩個需要注意的地方:
1.read_only=1只讀模式,不會影響slave同步復制的功能,所以在MySQL slave庫中設(shè)定了read_only=1后,通過 show slave status\G 命令查看salve狀態(tài),可以看到salve仍然會讀取master上的日志,并且在slave庫中應用日志,保證主從數(shù)據(jù)庫同步一致;
2.read_only=1只讀模式,可以限定普通用戶進行數(shù)據(jù)修改的操作,但不會限定具有super權(quán)限的用戶的數(shù)據(jù)修改操作;在MySQL中設(shè)置read_only=1后,普通的應用用戶進行insert、update、delete等會產(chǎn)生數(shù)據(jù)變化的DML操作時,都會報出數(shù)據(jù)庫處于只讀模式不能發(fā)生數(shù)據(jù)變化的錯誤,但具有super權(quán)限的用戶,例如在本地或遠程通過root用戶登錄到數(shù)據(jù)庫,還是可以進行數(shù)據(jù)變化的DML操作;
為了確保所有用戶,包括具有super權(quán)限的用戶也不能進行讀寫操作,就需要執(zhí)行給所有的表加讀鎖的命令 “flush tables with read lock;”,這樣使用具有super權(quán)限的用戶登錄數(shù)據(jù)庫,想要發(fā)生數(shù)據(jù)變化的操作時,也會提示表被鎖定不能修改的報錯。
這樣通過 設(shè)置“read_only=1”和“flush tables with read lock;”兩條命令,就可以確保數(shù)據(jù)庫處于只讀模式,不會發(fā)生任何數(shù)據(jù)改變,在MySQL進行數(shù)據(jù)庫遷移時,限定master主庫不能有任何數(shù)據(jù)變化,就可以通過這種方式來設(shè)定。
但同時由于加表鎖的命令對數(shù)據(jù)庫表限定非常嚴格,如果再slave從庫上執(zhí)行這個命令后,slave庫可以從master讀取binlog日志,但不能夠應用日志,slave庫不能發(fā)生數(shù)據(jù)改變,當然也不能夠?qū)崿F(xiàn)主從同步了,這時如果使用 “unlock tables;”解除全局的表讀鎖,slave就會應用從master讀取到的binlog日志,繼續(xù)保證主從庫數(shù)據(jù)庫一致同步。
為了保證主從同步可以一直進行,在slave庫上要保證具有super權(quán)限的root等用戶只能在本地登錄,不會發(fā)生數(shù)據(jù)變化,其他遠程連接的應用用戶只按需分配為select,insert,update,delete等權(quán)限,保證沒有super權(quán)限,則只需要將salve設(shè)定“read_only=1”模式,即可保證主從同步,又可以實現(xiàn)從庫只讀。
相對的,設(shè)定“read_only=1”只讀模式開啟的解鎖命令為設(shè)定“read_only=0”;設(shè)定全局鎖“flush tables with read lock;”,對應的解鎖模式命令為:“unlock tables;”.
當然設(shè)定了read_only=1后,所有的select查詢操作都是可以正常進行的。
實驗一:設(shè)置了read_only=1后,遠程業(yè)務用戶進行數(shù)據(jù)庫修改會提示ERROR 1290錯誤:
實驗二:設(shè)定了全局讀寫后,具有super權(quán)限的用戶進行數(shù)據(jù)修改后,也會提示錯誤ERROR 1223:
實驗三:MySQL從庫設(shè)定read_only=1后主從同步正常,設(shè)定表讀鎖后,不能同步,解除讀鎖后,主從同步恢復。
copy
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: 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
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: 0
##########################################
.FLUSH TABLES WITH READ LOCK
這個命令是全局讀鎖定,執(zhí)行了命令之后所有庫所有表都被鎖定只讀。一般都是用在數(shù)據(jù)庫聯(lián)機備份,這個時候數(shù)據(jù)庫的寫操作將被阻塞,讀操作順利進行。
解鎖的語句也是unlock tables。
2.LOCK TABLES tbl_name [AS alias] {READ [LOCAL] | [LOW_PRIORITY] WRITE}
這個命令是表級別的鎖定,可以定制鎖定某一個表。例如: lock tables test read; 不影響其他表的寫操作。
解鎖語句也是unlock tables。
重點:
這兩個語句在執(zhí)行的時候都需要注意個特點,就是 隱式提交的語句。在退出MySQL終端的時候都會隱式的執(zhí)行unlock tables。也就是如果要讓表鎖定生效就必須一直保持對話。
P.S. MYSQL的read lock和wirte lock
read-lock: 允許其他并發(fā)的讀請求,但阻塞寫請求,即可以同時讀,但不允許任何寫。也叫共享鎖
write-lock: 不允許其他并發(fā)的讀和寫請求,是排他的(exclusive)。也叫獨占鎖
另外有需要云服務器可以了解下創(chuàng)新互聯(lián)cdcxhl.cn,海內(nèi)外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。