溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

mysql只讀模式下數據遷移,保證數據一致性

發布時間:2020-06-29 10:19:09 來源:網絡 閱讀:999 作者:crazy_charles 欄目:數據庫

   

   在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;  
<span >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:



 

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);  
<span >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后主從同步正常,設定表讀鎖后,不能同步,解除讀鎖后,主從同步恢復。



  copy

  1. mysql>   

  2. mysql> show databases;  

  3. +--------------------+  

  4. | Database           |  

  5. +--------------------+  

  6. | information_schema |  

  7. | bitvc              |  

  8. | data03             |  

  9. | ga                 |  

  10. | jiradb             |  

  11. | meibi              |  

  12. | meibi02            |  

  13. | mysql              |  

  14. | performance_schema |  

  15. | sbtest             |  

  16. +--------------------+  

  17. 10 rows in set (0.00 sec)  

  18.   

  19. mysql>   

  20. mysql>   

  21. mysql>   

  22. mysql>  show global variables like "%read_only%";  

  23. +------------------+-------+  

  24. | Variable_name    | Value |  

  25. +------------------+-------+  

  26. | innodb_read_only | OFF   |  

  27. | read_only        | ON    |  

  28. | tx_read_only     | OFF   |  

  29. +------------------+-------+  

  30. 3 rows in set (0.00 sec)  

  31.   

  32. mysql>   

  33. mysql> show slave status\G  

  34. *************************** 1. row ***************************  

  35.                Slave_IO_State: Waiting for master to send event  

  36.                   Master_Host: 172.32.1.200  

  37.                   Master_User: repl  

  38.                   Master_Port: 3307  

  39.                 Connect_Retry: 60  

  40.               Master_Log_File: mysql-bin.000009  

  41.           Read_Master_Log_Pos: 5853  

  42.                Relay_Log_File: huobiDBtest-relay-bin.000002  

  43.                 Relay_Log_Pos: 6016  

  44.         Relay_Master_Log_File: mysql-bin.000009  

  45.              Slave_IO_Running: Yes  

  46.             Slave_SQL_Running: Yes  

  47.               Replicate_Do_DB:   

  48.           Replicate_Ignore_DB:   

  49.            Replicate_Do_Table:   

  50.        Replicate_Ignore_Table:   

  51.       Replicate_Wild_Do_Table:   

    1.   Replicate_Wild_Ignore_Table:   


  52.                    Last_Errno: 0  

  53.                    Last_Error:   

  54.                  Skip_Counter: 0  

  55.           Exec_Master_Log_Pos: 5853  

  56.               Relay_Log_Space: 6195  

  57.               Until_Condition: None  

  58.                Until_Log_File:   

  59.                 Until_Log_Pos: 0  

  60.            Master_SSL_Allowed: No  

  61.            Master_SSL_CA_File:   

  62.            Master_SSL_CA_Path:   

  63.               Master_SSL_Cert:   

  64.             Master_SSL_Cipher:   

  65.                Master_SSL_Key:   

  66.         Seconds_Behind_Master: 0  

  67. Master_SSL_Verify_Server_Cert: No  

  68.                 Last_IO_Errno: 0  

  69.                 Last_IO_Error:   

  70.                Last_SQL_Errno: 0  

  71.                Last_SQL_Error:   

  72.          Replicate_Ignore_Server_Ids:   

  73.            Master_Server_Id: 2003307  

  74.            Master_UUID: 6f68eea7-76e9-11e4-8f99-00221904cd5d  

  75.            Master_Info_File: /data/mysqldata/3308/data/master.info  

  76.            SQL_Delay: 0

  77.            SQL_Remaining_Delay: NULL  

  78.     Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it  

  79.           Master_Retry_Count: 86400  

  80.           Master_Bind:   

  81.           Last_IO_Error_Timestamp:   

  82.           Last_SQL_Error_Timestamp:   

  83.               Master_SSL_Crl:   

  84.            Master_SSL_Crlpath:   

  85.            Retrieved_Gtid_Set:   

  86.             Executed_Gtid_Set:   

  87.                Auto_Position: 0  

  88. 1 row in set (0.00 sec)  

  89.   

  90. mysql>   

  91. mysql> flush tables with read lock;  

  92. Query OK, 0 rows affected (0.00 sec)  

  93.   

  94. mysql> show slave status\G  

  95. *************************** 1. row ***************************  

  96.                Slave_IO_State: Waiting for master to send event  

  97.                   Master_Host: 172.32.1.200  

  98.                   Master_User: repl  

  99.                   Master_Port: 3307  

  100.                 Connect_Retry: 60  

  101.         Master_Log_File: mysql-bin.000009  

  102.           Read_Master_Log_Pos: 6531   

  103.                Relay_Log_File: huobiDBtest-relay-bin.000002  

  104.                 Relay_Log_Pos: 6016  

  105.         Relay_Master_Log_File: mysql-bin.000009  

  106.              Slave_IO_Running: Yes  

  107.             Slave_SQL_Running: Yes  

  108.               Replicate_Do_DB:   

  109.           Replicate_Ignore_DB:   

  110.            Replicate_Do_Table:   

  111.        Replicate_Ignore_Table:   

  112.       Replicate_Wild_Do_Table:   

  113.            Replicate_Wild_Ignore_Table:   

  114.             Last_Errno: 0  

  115.           Last_Error:   


    1.    Skip_Counter: 0  



  116.          Exec_Master_Log_Pos: 5853  

  117.                  Relay_Log_Space: 6873  

  118.               Until_Condition: None  

  119.                Until_Log_File:   

  120.                 Until_Log_Pos: 0  

  121.            Master_SSL_Allowed: No  

  122.            Master_SSL_CA_File:   

  123.            Master_SSL_CA_Path:   

  124.               Master_SSL_Cert:   

  125.             Master_SSL_Cipher:   

  126.                Master_SSL_Key:   

  127.         Seconds_Behind_Master: 120  

  128. Master_SSL_Verify_Server_Cert: No  

  129.                 Last_IO_Errno: 0  

  130.                 Last_IO_Error:   

  131.                Last_SQL_Errno: 0  

  132.                Last_SQL_Error:   

  133.   Replicate_Ignore_Server_Ids:   

  134.              Master_Server_Id: 2003307  

    1.              Master_UUID: 6f68eea7-76e9-11e4-8f99-00221904cd5d  


  135.                   Master_Info_File: /data/mysqldata/3308/data/master.info  

  136.                   SQL_Delay: 0  

  137.           SQL_Remaining_Delay: NULL  

  138.       Slave_SQL_Running_State: Waiting for global read lock  

  139.               Master_Retry_Count: 86400  

  140.               Master_Bind:   

  141.       Last_IO_Error_Timestamp:   

  142.      Last_SQL_Error_Timestamp:   

  143.                Master_SSL_Crl:   

  144.               Master_SSL_Crlpath:   

  145.            Retrieved_Gtid_Set:   

  146.             Executed_Gtid_Set:   

  147.                 Auto_Position: 0  

  148. 1 row in set (0.00 sec)  

  149.   

  150. mysql>   

  151. mysql>   

  152. mysql> select * from data03.t01;  

  153. +-----+------+------+  

  154. | id1 | a1   | b1   |  

  155. +-----+------+------+  

  156. |   1 |    1 |    1 |  

  157. |   2 |    2 |    2 |  

  158. |   4 |    4 |    4 |  

  159. |   5 |    5 |    5 |  

  160. |   6 |    6 |    6 |  

  161. +-----+------+------+  

  162. 5 rows in set (0.00 sec)  

  163.   

  164. mysql>   

  165. mysql> unlock tables;  

  166. Query OK, 0 rows affected (0.00 sec)  

  167.   

  168. mysql> show slave status\G  

  169. *************************** 1. row ***************************  

  170.                Slave_IO_State: Waiting for master to send event  

  171.                   Master_Host: 172.32.1.200  

  172.                   Master_User: repl  

  173.                   Master_Port: 3307  

  174.                 Connect_Retry: 60  

  175.             Master_Log_File: mysql-bin.000009  

  176.           Read_Master_Log_Pos: 6531  

  177.                Relay_Log_File: huobiDBtest-relay-bin.000002  

  178.                 Relay_Log_Pos: 6694  

  179.         Relay_Master_Log_File: mysql-bin.000009  

  180.              Slave_IO_Running: Yes  

  181.             Slave_SQL_Running: Yes  

  182.               Replicate_Do_DB:   

  183.           Replicate_Ignore_DB:   

  184.            Replicate_Do_Table:   

  185.        Replicate_Ignore_Table:   

  186.       Replicate_Wild_Do_Table:   

  187.   Replicate_Wild_Ignore_Table:   

  188.         Last_Errno: 0  

  189.      

        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

 

這個命令是全局讀鎖定,執行了命令之后所有庫所有表都被鎖定只讀。一般都是用在數據庫聯機備份,這個時候數據庫的寫操作將被阻塞,讀操作順利進行。

 

解鎖的語句也是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)。也叫獨占鎖




向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女