創(chuàng)新互聯(lián)公司服務(wù)項目包括當(dāng)陽網(wǎng)站建設(shè)、當(dāng)陽網(wǎng)站制作、當(dāng)陽網(wǎng)頁制作以及當(dāng)陽網(wǎng)絡(luò)營銷策劃等。多年來,我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢、行業(yè)經(jīng)驗、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機(jī)構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,當(dāng)陽網(wǎng)站推廣取得了明顯的社會效益與經(jīng)濟(jì)效益。目前,我們服務(wù)的客戶以成都為中心已經(jīng)輻射到當(dāng)陽省份的部分城市,未來相信會繼續(xù)擴(kuò)大服務(wù)區(qū)域并繼續(xù)獲得客戶的支持與信任!
在MySQL數(shù)據(jù)庫中,在進(jìn)行數(shù)據(jù)遷移和從庫只讀狀態(tài)設(shè)置時,都會涉及到只讀狀態(tài)和Master-slave的設(shè)置和關(guān)系。
經(jīng)過實際測試,對于MySQL單實例數(shù)據(jù)庫和master庫,如果需要設(shè)置為只讀狀態(tài),需要進(jìn)行如下操作和設(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è)定;
默認(rèn)情況下,數(shù)據(jù)庫是用于讀寫操作的,所以read_only參數(shù)也是0或faluse狀態(tài),這時候不論是本地用戶還是遠(yuǎn)程訪問數(shù)據(jù)庫的用戶,都可以進(jìn)行讀寫操作;
如需設(shè)置為只讀狀態(tài),將該read_only參數(shù)設(shè)置為1或TRUE狀態(tài),但設(shè)置 read_only=1 狀態(tài)有兩個需要注意的地方:
1.read_only=1只讀模式,不會影響slave同步復(fù)制的功能,所以在MySQL slave庫中設(shè)定了read_only=1后,通過 show slave status\G 命令查看salve狀態(tài),可以看到salve仍然會讀取master上的日志,并且在slave庫中應(yīng)用日志,保證主從數(shù)據(jù)庫同步一致;
2.read_only=1只讀模式,可以限定普通用戶進(jìn)行數(shù)據(jù)修改的操作,但不會限定具有super權(quán)限的用戶的數(shù)據(jù)修改操作;在MySQL中設(shè)置read_only=1后,普通的應(yīng)用用戶進(jìn)行insert、update、delete等會產(chǎn)生數(shù)據(jù)變化的DML操作時,都會報出數(shù)據(jù)庫處于只讀模式不能發(fā)生數(shù)據(jù)變化的錯誤,但具有super權(quán)限的用戶,例如在本地或遠(yuǎn)程通過root用戶登錄到數(shù)據(jù)庫,還是可以進(jìn)行數(shù)據(jù)變化的DML操作;
為了確保所有用戶,包括具有super權(quán)限的用戶也不能進(jì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進(jìn)行數(shù)據(jù)庫遷移時,限定master主庫不能有任何數(shù)據(jù)變化,就可以通過這種方式來設(shè)定。
但同時由于加表鎖的命令對數(shù)據(jù)庫表限定非常嚴(yán)格,如果再slave從庫上執(zhí)行這個命令后,slave庫可以從master讀取binlog日志,但不能夠應(yīng)用日志,slave庫不能發(fā)生數(shù)據(jù)改變,當(dāng)然也不能夠?qū)崿F(xiàn)主從同步了,這時如果使用 “unlock tables;”解除全局的表讀鎖,slave就會應(yīng)用從master讀取到的binlog日志,繼續(xù)保證主從庫數(shù)據(jù)庫一致同步。
為了保證主從同步可以一直進(jìn)行,在slave庫上要保證具有super權(quán)限的root等用戶只能在本地登錄,不會發(fā)生數(shù)據(jù)變化,其他遠(yuǎn)程連接的應(yīng)用用戶只按需分配為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;”,對應(yīng)的解鎖模式命令為:“unlock tables;”.
當(dāng)然設(shè)定了read_only=1后,所有的select查詢操作都是可以正常進(jìn)行的。
實驗一:設(shè)置了read_only=1后,遠(yuǎn)程業(yè)務(wù)用戶進(jìn)行數(shù)據(jù)庫修改會提示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]>
實驗二:設(shè)定了全局讀寫后,具有super權(quán)限的用戶進(jìn)行數(shù)據(jù)修改后,也會提示錯誤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從庫設(shè)定read_only=1后主從同步正常,設(shè)定表讀鎖后,不能同步,解除讀鎖后,主從同步恢復(fù)。
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
Last_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
這個命令是全局讀鎖定,執(zhí)行了命令之后所有庫所有表都被鎖定只讀。一般都是用在數(shù)據(jù)庫聯(lián)機(jī)備份,這個時候數(shù)據(jù)庫的寫操作將被阻塞,讀操作順利進(jìn)行。
解鎖的語句也是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)。也叫獨(dú)占鎖