MySQL數據庫設置爲只讀及測試【轉】

轉自mysql

mysql只讀模式的設置方法與實驗 - yumushui的專欄 - CSDN博客
http://blog.csdn.net/yumushui/article/details/41645469sql

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庫,若是要設置爲只讀狀態,須要執行的命令爲:ui

mysql> set global read_only=1;

     將salve庫從只讀狀態變爲讀寫狀態,須要執行的命令是:this

mysql> set global read_only=0;

     對於數據庫讀寫狀態,主要靠 「read_only」全局參數來設定;默認狀況下,數據庫是用於讀寫操做的,因此read_only參數也是0或faluse狀態,這時候不管是本地用戶仍是遠程訪問數據庫的用戶,均可以進行讀寫操做;如需設置爲只讀狀態,將該read_only參數設置爲1或TRUE狀態,但設置 read_only=1 狀態有兩個須要注意的地方:
      spa

  1.read_only=1只讀模式,不會影響slave同步複製的功能,以在MySQL slave庫中設定了read_only=1後,經過 show slave status\G 命令查看salve狀態,能夠看到salve仍然會讀取master上的日誌,而且在slave庫中應用日誌,保證主從數據庫同步一致;.net


      2.read_only=1只讀模式,能夠限定普通用戶進行數據修改的操做,但不會限定具備super權限的用戶的數據修改操做;在MySQL中設置read_only=1後,普通的應用用戶進行insert、update、delete等會產生數據變化的DML操做時,都會報出數據庫處於只讀模式不能發生數據變化的錯誤,但具備super權限的用戶,例如在本地或遠程經過root用戶登陸到數據庫,仍是能夠進行數據變化的DML操做;日誌


      爲了確保全部用戶,包括具備super權限的用戶也不能進行讀寫操做,就須要執行給全部的表加讀鎖的命令 「flush tables with read lock;」,這樣使用具備super權限的用戶登陸數據庫,想要發生數據變化的操做時,也會提示表被鎖定不能修改的報錯。code


        這樣經過 設置「read_only=1」和「flush tables with read lock;」兩條命令,就能夠確保數據庫處於只讀模式,不會發生任何數據改變,在MySQL進行數據庫遷移時,限定master主庫不能有任何數據變化,就能夠經過這種方式來設定。


       但同時因爲加表鎖的命令對數據庫表限定很是嚴格,若是再slave從庫上執行這個命令後,slave庫能夠從master讀取binlog日誌,但不可以應用日誌,slave庫不能發生數據改變,固然也不可以實現主從同步了,這時若是使用 「unlock tables;」解除全局的表讀鎖,slave就會應用從master讀取到的binlog日誌,繼續保證主從庫數據庫一致同步。因此從庫slave不要使用 「flush tables with read lock;」 不然沒法寫入數據同步。

       爲了保證主從同步能夠一直進行,在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)
相關文章
相關標籤/搜索