GreatSQL社區原創內容未經授權不得隨意使用,轉載請聯繫小編並註明來源。 GreatSQL是MySQL的國產分支版本,使用上與MySQL一致。 作者:Yejinrong/葉金榮 文章來源:GreatSQL社區原創 繼續吹MySQL 8.0~ 在以前,當需要對MySQL資料庫進行維護操作時,通常需 ...
- GreatSQL社區原創內容未經授權不得隨意使用,轉載請聯繫小編並註明來源。
- GreatSQL是MySQL的國產分支版本,使用上與MySQL一致。
- 作者:Yejinrong/葉金榮
- 文章來源:GreatSQL社區原創
繼續吹MySQL 8.0~
在以前,當需要對MySQL資料庫進行維護操作時,通常需要先進行主從切換,然後修改設置並重啟實例,關閉網路監聽,只允許從本地socket方式登入,再進行相應的維護操作;有時候甚至還要修改相應的防火牆,或者乾脆關閉前端業務服務,總體比較麻煩。
從MySQL 5.7開始,支持設置為離線模式(offline_mode),再有維護操作需求就不用這麼麻煩了。只需線上動態修改,可立即生效,非常的簡單粗暴:
mysql> set global offline_mode = on; -- 打開離線模式,拒絕外部請求
mysql> set global offline_mode = off; -- 關閉離線模式,允許外部連接請求
當設置為離線模式後,普通用戶將無法繼續發起連接請求,甚至當前正在執行的SQL也會立即被終止並被斷開連接。
1. 無法創建新連接
$ mysql -h127.0.0.1 -uyejr -pxx -P4306 sbtest
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 3032 (HY000): The server is currently in offline mode
2. 即便是普通用戶通過本地socket連接,當啟用離線模式後,也會被斷開
$ mysql -S/data/MySQL/mysql.sock
...
mysql> show processlist;
+-----+------+-----------+------+---------+------+-------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+-----+------+-----------+------+---------+------+-------+------------------+
| 304 | yejr | localhost | NULL | Query | 0 | init | show processlist |
+-----+------+-----------+------+---------+------+-------+------------------+
1 row in set (0.00 sec)
[sbtest]>select *,sleep(10) from t1 limit 3; -- 正在運行的SQL會立即被終止,並斷開連接
ERROR 2013 (HY000): Lost connection to MySQL server during query
No connection. Trying to reconnect...
ERROR 3032 (HY000): The server is currently in offline mode
ERROR:
Can't connect to the server
3. 正在運行中的sysbench壓測,也會被立即斷開
[ 1s ] thds: 16 tps: 442.02 qps: 9078.28 (r/w/o: 6382.37/1795.94/899.96) lat (ms,99%,99%,99.9%): 150.29/150.29/150.29 err/s: 0.00 reconn/s: 0.00
[ 2s ] thds: 16 tps: 471.23 qps: 9387.56 (r/w/o: 6576.19/1868.91/942.46) lat (ms,99%,99%,99.9%): 61.08/61.08/65.65 err/s: 0.00 reconn/s: 0.00
[ 3s ] thds: 16 tps: 386.03 qps: 7712.68 (r/w/o: 5399.48/1541.14/772.07) lat (ms,99%,99%,99.9%): 82.96/82.96/84.47 err/s: 0.00 reconn/s: 0.00
[ 4s ] thds: 16 tps: 547.00 qps: 10894.97 (r/w/o: 7609.98/2190.99/1094.00) lat (ms,99%,99%,99.9%): 65.65/65.65/68.05 err/s: 0.00 reconn/s: 0.00
FATAL: mysql_drv_query() returned error 2013 (Lost connection to MySQL server during query) for query 'COMMIT'
(last message repeated 1 times)
FATAL: mysql_drv_query() returned error 2013 (Lost connection to MySQL server during query) for query 'SELECT c FROM sbtest1 WHERE id=4822870'
FATAL: mysql_drv_query() returned error 2013 (Lost connection to MySQL server during query) for query 'COMMIT'
FATAL: mysql_drv_query() returned error 2013 (Lost connection to MySQL server during query) for query 'UPDATE sbtest1 SET k=k+1 WHERE id=2265001'
FATAL: mysql_drv_query() returned error 2013 (Lost connection to MySQL server during query) for query 'SELECT c FROM sbtest8 WHERE id BETWEEN 3389984 AND 3390083'
另外,從MySQL 8.0開始,對於離線模式又做了些改進和完善,比如新引入 CONNECTION_ADMIN
許可權等,細化離線模式的許可權管理模式。
簡單幾點小結關於離線模式:
- 必須要有
CONNECTION_ADMIN
以及CONNECTION_ADMIN
許可權 或者SUPER
許可權(SUPER
許可權在未來會被廢棄,而細分成更多細粒度許可權),才能線上設置離線模式。 - 複製線程不會受到離線模式影響,還能正常工作。
- 當設置為離線模式時,沒有授予
CONNECTION_ADMIN
或SUPER
許可權的普通用戶,正在執行的SQL會被立即終止,連接也會被立即斷開。 - 當設置為離線模式時,擁有
CONNECTION_ADMIN
或SUPER
許可權的用戶,不會被斷開連接。 - 當設置離線模式的用戶不具備
SYSTEM_USER
許可權(只擁有CONNECTION_ADMIN
以及CONNECTION_ADMIN
許可權)的話,擁有SYSTEM_USER
許可權的活躍用戶連接不會被斷開(因為想要斷開SYSTEM_USER
許可權級別用戶連接同樣需要至少有SYSTEM_USER
許可權),詳見下麵的案例。
有 u1 和 u2 兩個用戶,授權模式不同
mysql> show grants for u1;
+----------------------------------------+
| Grants for u1@% |
+----------------------------------------+
| GRANT USAGE ON *.* TO `u1`@`%` |
| GRANT SELECT ON `sbtest`.* TO `u1`@`%` |
+----------------------------------------+
mysql> show grants for u2;
+----------------------------------------+
| Grants for u2@% |
+----------------------------------------+
| GRANT USAGE ON *.* TO `u2`@`%` |
| GRANT SYSTEM_USER ON *.* TO `u2`@`%` |
| GRANT SELECT ON `sbtest`.* TO `u2`@`%` |
+----------------------------------------+
用戶 yejr 的授權模式如下
+--------------------------------------------------------------------+
| Grants for yejr@% |
+--------------------------------------------------------------------+
| GRANT USAGE ON *.* TO `yejr`@`%` |
| GRANT CONNECTION_ADMIN,SYSTEM_VARIABLES_ADMIN ON *.* TO `yejr`@`%` |
| GRANT ALL PRIVILEGES ON `sbtest`.* TO `yejr`@`%` |
+--------------------------------------------------------------------+
當 yejr 用戶設置離線模式後,u2 用戶的連接不會被斷開(但不能再建立新連接),而 u1 用戶的連接會被斷開
# 三個用戶先分別建立連接
$ jobs
[1] Stopped mysql -h127.0.0.1 -uyejr -pxx -P4306 sbtest
[2]- Stopped mysql -h127.0.0.1 -uu2 -pxx -P4306 sbtest
[3]+ Stopped mysql -h127.0.0.1 -uu1 -pxx -P4306 sbtest
# 設置離線模式
$ fg 1
mysql -h127.0.0.1 -uyejr -pxx -P4306 sbtest
[yejr@db160] [sbtest]>set global offline_mode=on;
Query OK, 0 rows affected (0.00 sec)
# u1用戶被斷開連接
$ fg 3
mysql -h127.0.0.1 -uu1 -pxx -P4306 sbtest
[u1@db160] [sbtest]>select 1;
ERROR 2013 (HY000): Lost connection to MySQL server during query
No connection. Trying to reconnect...
ERROR 3032 (HY000): The server is currently in offline mode
ERROR:
Can't connect to the server
# u2用戶不會被斷開連接
$ fg 2
mysql -h127.0.0.1 -uu2 -pxx -P4306 sbtest
[u2@db160] [sbtest]>select 1;
+---+
| 1 |
+---+
| 1 |
+---+
# 但u1/u2用戶均不能再建立新鏈接
$ mysql -h127.0.0.1 -uu2 -pxx -P4306 sbtest
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 3032 (HY000): The server is currently in offline mode
$ mysql -h127.0.0.1 -uu1 -pxx -P4306 sbtest
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 3032 (HY000): The server is currently in offline mode
是不是有點好玩呀~
結合前面的兩篇文章 MySQL 8.0不再擔心被垃圾SQL搞爆記憶體 以及 InnoDB buffer pool size進度更透明 可以看到MySQL 8.0在各個細節方面做的是越來越好了。
延伸閱讀
- #sysvar_offline_mode, https://dev.mysql.com/doc/refman/8.0/en/server-system-variables.html
- Changes in MySQL 8.0.31, https://dev.mysql.com/doc/relnotes/mysql/8.0/en/news-8-0-31.html
Enjoy GreatSQL