当前位置:   article > 正文

mysql只读模式的设置方法与实验_查看mysql当前是否只读

查看mysql当前是否只读


      在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错误:

  1. (test01@172.32.1.200) [data03]> show tables;
  2. +------------------+
  3. | Tables_in_data03 |
  4. +------------------+
  5. | t01 |
  6. | t02 |
  7. | user |
  8. +------------------+
  9. 3 rows in set (0.00 sec)
  10. (test01@172.32.1.200) [data03]>
  11. (test01@172.32.1.200) [data03]>
  12. (test01@172.32.1.200) [data03]> show global variables like "%read_only%";
  13. +------------------+-------+
  14. | Variable_name | Value |
  15. +------------------+-------+
  16. | innodb_read_only | OFF |
  17. | read_only | ON |
  18. | tx_read_only | OFF |
  19. +------------------+-------+
  20. 3 rows in set (0.00 sec)
  21. (test01@172.32.1.200) [data03]>
  22. (test01@172.32.1.200) [data03]>
  23. (test01@172.32.1.200) [data03]> delete from t01 where id1=3;
  24. <span style="color:#ff0000;">ERROR 1290 (HY000): The MySQL server is running with the --read-only option so it cannot execute this statement</span>
  25. (test01@172.32.1.200) [data03]> update t01 set id1=id1+30 where id1=3;
  26. ERROR 1290 (HY000): The MySQL server is running with the --read-only option so it cannot execute this statement
  27. (test01@172.32.1.200) [data03]> insert into t01(id1,a1,b1) values(9,9,9);
  28. ERROR 1290 (HY000): The MySQL server is running with the --read-only option so it cannot execute this statement
  29. (test01@172.32.1.200) [data03]>
  30. (test01@172.32.1.200) [data03]> select * from t01;
  31. +-----+------+------+
  32. | id1 | a1 | b1 |
  33. +-----+------+------+
  34. | 1 | 1 | 1 |
  35. | 2 | 2 | 2 |
  36. | 4 | 4 | 4 |
  37. | 5 | 5 | 5 |
  38. | 6 | 6 | 6 |
  39. +-----+------+------+
  40. 5 rows in set (0.00 sec)
  41. (test01@172.32.1.200) [data03]>

实验二:设定了全局读写后,具有super权限的用户进行数据修改后,也会提示错误ERROR 1223:

  1. mysql> use data03;
  2. Reading table information for completion of table and column names
  3. You can turn off this feature to get a quicker startup with -A
  4. Database changed
  5. mysql> show tables;
  6. +------------------+
  7. | Tables_in_data03 |
  8. +------------------+
  9. | t01 |
  10. | t02 |
  11. | user |
  12. +------------------+
  13. 3 rows in set (0.00 sec)
  14. mysql> select * from t01;
  15. +-----+------+------+
  16. | id1 | a1 | b1 |
  17. +-----+------+------+
  18. | 1 | 1 | 1 |
  19. | 2 | 2 | 2 |
  20. | 4 | 4 | 4 |
  21. | 5 | 5 | 5 |
  22. | 6 | 6 | 6 |
  23. +-----+------+------+
  24. 5 rows in set (0.00 sec)
  25. mysql>
  26. mysql> show global variables like "%read_only%";
  27. +------------------+-------+
  28. | Variable_name | Value |
  29. +------------------+-------+
  30. | innodb_read_only | OFF |
  31. | read_only | ON |
  32. | tx_read_only | OFF |
  33. +------------------+-------+
  34. 3 rows in set (0.00 sec)
  35. mysql>
  36. mysql>
  37. mysql> insert into t01(id1,a1,b1) values(8,8,8);
  38. Query OK, 1 row affected (0.00 sec)
  39. mysql> update t01 set a1=a1+10 where id1=2;
  40. Query OK, 1 row affected (0.00 sec)
  41. Rows matched: 1 Changed: 1 Warnings: 0
  42. mysql> delete from t01 where id1=4;
  43. Query OK, 1 row affected (0.00 sec)
  44. mysql> select * from t01;
  45. +-----+------+------+
  46. | id1 | a1 | b1 |
  47. +-----+------+------+
  48. | 1 | 1 | 1 |
  49. | 2 | 12 | 2 |
  50. | 5 | 5 | 5 |
  51. | 6 | 6 | 6 |
  52. | 8 | 8 | 8 |
  53. +-----+------+------+
  54. 5 rows in set (0.00 sec)
  55. mysql>
  56. mysql> flush tables with read lock;
  57. Query OK, 0 rows affected (0.00 sec)
  58. mysql>
  59. mysql> insert into t01(id1,a1,b1) values(9,9,9);
  60. <span style="color:#ff0000;">ERROR 1223 (HY000): Can't execute the query because you have a conflicting read lock</span>
  61. mysql>
  62. mysql> update t01 set a1=a1+10 where id1=5;
  63. ERROR 1223 (HY000): Can't execute the query because you have a conflicting read lock
  64. mysql>
  65. mysql> delete from t01 where id1=5;
  66. ERROR 1223 (HY000): Can't execute the query because you have a conflicting read lock
  67. mysql>
  68. mysql>


实验三:MySQL从库设定read_only=1后主从同步正常,设定表读锁后,不能同步,解除读锁后,主从同步恢复。


  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. mysql>
  19. mysql>
  20. mysql>
  21. mysql> show global variables like "%read_only%";
  22. +------------------+-------+
  23. | Variable_name | Value |
  24. +------------------+-------+
  25. | innodb_read_only | OFF |
  26. | read_only | ON |
  27. | tx_read_only | OFF |
  28. +------------------+-------+
  29. 3 rows in set (0.00 sec)
  30. mysql>
  31. mysql> show slave status\G
  32. *************************** 1. row ***************************
  33. Slave_IO_State: Waiting for master to send event
  34. Master_Host: 172.32.1.200
  35. Master_User: repl
  36. Master_Port: 3307
  37. Connect_Retry: 60
  38. Master_Log_File: mysql-bin.000009
  39. Read_Master_Log_Pos: 5853
  40. Relay_Log_File: huobiDBtest-relay-bin.000002
  41. Relay_Log_Pos: 6016
  42. Relay_Master_Log_File: mysql-bin.000009
  43. Slave_IO_Running: Yes
  44. Slave_SQL_Running: Yes
  45. Replicate_Do_DB:
  46. Replicate_Ignore_DB:
  47. Replicate_Do_Table:
  48. Replicate_Ignore_Table:
  49. Replicate_Wild_Do_Table:
  50. Replicate_Wild_Ignore_Table:
  51. Last_Errno: 0
  52. Last_Error:
  53. Skip_Counter: 0
  54. Exec_Master_Log_Pos: 5853
  55. Relay_Log_Space: 6195
  56. Until_Condition: None
  57. Until_Log_File:
  58. Until_Log_Pos: 0
  59. Master_SSL_Allowed: No
  60. Master_SSL_CA_File:
  61. Master_SSL_CA_Path:
  62. Master_SSL_Cert:
  63. Master_SSL_Cipher:
  64. Master_SSL_Key:
  65. Seconds_Behind_Master: 0
  66. Master_SSL_Verify_Server_Cert: No
  67. Last_IO_Errno: 0
  68. Last_IO_Error:
  69. Last_SQL_Errno: 0
  70. Last_SQL_Error:
  71. Replicate_Ignore_Server_Ids:
  72. Master_Server_Id: 2003307
  73. Master_UUID: 6f68eea7-76e9-11e4-8f99-00221904cd5d
  74. Master_Info_File: /data/mysqldata/3308/data/master.info
  75. SQL_Delay: 0
  76. SQL_Remaining_Delay: NULL
  77. Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
  78. Master_Retry_Count: 86400
  79. Master_Bind:
  80. Last_IO_Error_Timestamp:
  81. Last_SQL_Error_Timestamp:
  82. Master_SSL_Crl:
  83. Master_SSL_Crlpath:
  84. Retrieved_Gtid_Set:
  85. Executed_Gtid_Set:
  86. Auto_Position: 0
  87. 1 row in set (0.00 sec)
  88. mysql>
  89. mysql> flush tables with read lock;
  90. Query OK, 0 rows affected (0.00 sec)
  91. mysql> show slave status\G
  92. *************************** 1. row ***************************
  93. Slave_IO_State: Waiting for master to send event
  94. Master_Host: 172.32.1.200
  95. Master_User: repl
  96. Master_Port: 3307
  97. Connect_Retry: 60
  98. <span style="color:#ff0000;"> Master_Log_File: mysql-bin.000009
  99. Read_Master_Log_Pos: 6531</span>
  100. Relay_Log_File: huobiDBtest-relay-bin.000002
  101. Relay_Log_Pos: 6016
  102. Relay_Master_Log_File: mysql-bin.000009
  103. Slave_IO_Running: Yes
  104. Slave_SQL_Running: Yes
  105. Replicate_Do_DB:
  106. Replicate_Ignore_DB:
  107. Replicate_Do_Table:
  108. Replicate_Ignore_Table:
  109. Replicate_Wild_Do_Table:
  110. Replicate_Wild_Ignore_Table:
  111. Last_Errno: 0
  112. Last_Error:
  113. Skip_Counter: 0
  114. <span style="color:#ff0000;"> Exec_Master_Log_Pos: 5853</span>
  115. Relay_Log_Space: 6873
  116. Until_Condition: None
  117. Until_Log_File:
  118. Until_Log_Pos: 0
  119. Master_SSL_Allowed: No
  120. Master_SSL_CA_File:
  121. Master_SSL_CA_Path:
  122. Master_SSL_Cert:
  123. Master_SSL_Cipher:
  124. Master_SSL_Key:
  125. Seconds_Behind_Master: 120
  126. Master_SSL_Verify_Server_Cert: No
  127. Last_IO_Errno: 0
  128. Last_IO_Error:
  129. Last_SQL_Errno: 0
  130. Last_SQL_Error:
  131. Replicate_Ignore_Server_Ids:
  132. Master_Server_Id: 2003307
  133. Master_UUID: 6f68eea7-76e9-11e4-8f99-00221904cd5d
  134. Master_Info_File: /data/mysqldata/3308/data/master.info
  135. SQL_Delay: 0
  136. SQL_Remaining_Delay: NULL
  137. Slave_SQL_Running_State: Waiting for global read lock
  138. Master_Retry_Count: 86400
  139. Master_Bind:
  140. Last_IO_Error_Timestamp:
  141. Last_SQL_Error_Timestamp:
  142. Master_SSL_Crl:
  143. Master_SSL_Crlpath:
  144. Retrieved_Gtid_Set:
  145. Executed_Gtid_Set:
  146. Auto_Position: 0
  147. 1 row in set (0.00 sec)
  148. mysql>
  149. mysql>
  150. mysql> select * from data03.t01;
  151. +-----+------+------+
  152. | id1 | a1 | b1 |
  153. +-----+------+------+
  154. | 1 | 1 | 1 |
  155. | 2 | 2 | 2 |
  156. | 4 | 4 | 4 |
  157. | 5 | 5 | 5 |
  158. | 6 | 6 | 6 |
  159. +-----+------+------+
  160. 5 rows in set (0.00 sec)
  161. mysql>
  162. mysql> unlock tables;
  163. Query OK, 0 rows affected (0.00 sec)
  164. mysql> show slave status\G
  165. *************************** 1. row ***************************
  166. Slave_IO_State: Waiting for master to send event
  167. Master_Host: 172.32.1.200
  168. Master_User: repl
  169. Master_Port: 3307
  170. Connect_Retry: 60
  171. <span style="color:#ff0000;"> Master_Log_File: mysql-bin.000009
  172. Read_Master_Log_Pos: 6531</span>
  173. Relay_Log_File: huobiDBtest-relay-bin.000002
  174. Relay_Log_Pos: 6694
  175. Relay_Master_Log_File: mysql-bin.000009
  176. Slave_IO_Running: Yes
  177. Slave_SQL_Running: Yes
  178. Replicate_Do_DB:
  179. Replicate_Ignore_DB:
  180. Replicate_Do_Table:
  181. Replicate_Ignore_Table:
  182. Replicate_Wild_Do_Table:
  183. Replicate_Wild_Ignore_Table:
  184. Last_Errno: 0
  185. Last_Error:
  186. Skip_Counter: 0
  187. <span style="color:#ff0000;"> Exec_Master_Log_Pos: 6531</span>
  188. Relay_Log_Space: 6873
  189. Until_Condition: None
  190. Until_Log_File:
  191. Until_Log_Pos: 0
  192. Master_SSL_Allowed: No
  193. Master_SSL_CA_File:
  194. Master_SSL_CA_Path:
  195. Master_SSL_Cert:
  196. Master_SSL_Cipher:
  197. Master_SSL_Key:
  198. Seconds_Behind_Master: 0
  199. Master_SSL_Verify_Server_Cert: No
  200. Last_IO_Errno: 0
  201. Last_IO_Error:
  202. Last_SQL_Errno: 0
  203. Last_SQL_Error:
  204. Replicate_Ignore_Server_Ids:
  205. Master_Server_Id: 2003307
  206. Master_UUID: 6f68eea7-76e9-11e4-8f99-00221904cd5d
  207. Master_Info_File: /data/mysqldata/3308/data/master.info
  208. SQL_Delay: 0
  209. SQL_Remaining_Delay: NULL
  210. Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
  211. Master_Retry_Count: 86400
  212. Master_Bind:
  213. Last_IO_Error_Timestamp:
  214. Last_SQL_Error_Timestamp:
  215. Master_SSL_Crl:
  216. Master_SSL_Crlpath:
  217. Retrieved_Gtid_Set:
  218. Executed_Gtid_Set:
  219. Auto_Position: 0
  220. 1 row in set (0.00 sec)
  221. mysql> select * from data03.t01;
  222. +-----+------+------+
  223. | id1 | a1 | b1 |
  224. +-----+------+------+
  225. | 1 | 1 | 1 |
  226. | 2 | 12 | 2 |
  227. | 5 | 5 | 5 |
  228. | 6 | 6 | 6 |
  229. | 8 | 8 | 8 |
  230. +-----+------+------+
  231. 5 rows in set (0.00 sec)




声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/514731
推荐阅读
相关标签
  

闽ICP备14008679号