不小心配置 skip-networking,業務連不上數據庫了。
作者:張昊,DBA,主要負責 MySQL 故障處理、DMP 產品支持,擅長 MySQL,喜歡打球~唱歌~
愛可生開源社區出品,原創內容未經授權不得隨意使用,轉載請聯繫小編並註明來源。
本文約 600 字,預計閲讀需要 2 分鐘。
背景
某客户的測試同事本地部署 MySQL 8.0 數據庫,配置文件增加部分變量重啓數據庫之後發現數據庫遠程連接失敗。
排查
1 查看數據庫進程狀態
[root@hao-3 ~]# ps -ef|grep mysqld |grep 8888
actiont+ 25825 1 0 02:10 ? 00:00:01 /opt/mysql/base/8.0.28/bin/mysqld --defaults-file=/opt/mysql/etc/8888/my.cnf --daemonize --pid-file=/opt/mysql/data/8888/mysqld.pid --user=actiontech-mysql --socket=/opt/mysql/data/8888/mysqld.sock --port=8888
2 查看服務 TCP 連接情況
使用 netstat 命令查看數據庫服務的 TCP 連接情況,輸出結果為空。
[root@hao-3 ~]# ps -ef|grep mysqld|grep 8888
actiont+ 25825 1 0 02:10 ? 00:00:05 /opt/mysql/base/8.0.28/bin/mysqld --defaults-file=/opt/mysql/etc/8888/my.cnf --daemonize --pid-file=/opt/mysql/data/8888/mysqld.pid --user=actiontech-mysql --socket=/opt/mysql/data/8888/mysqld.sock --port=8888
[root@hao-3 ~]# netstat -lantup |grep 25825 //查看進程
[root@hao-3 ~]#
[root@hao-3 ~]# netstat -lantup |grep 8888 //查看端口
[root@hao-3 ~]#
3 查看數據庫端口
查看數據庫端口,結果為 0!
[root@hao-3 ~]# /opt/mysql/base/8.0.28/bin/mysql -uroot -p1 -S /opt/mysql/data/8888/mysqld.sock
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 8.0.28 MySQL Community Server - GPL
Copyright (c) 2000, 2022, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show variables like 'port';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| port | 0 |
+---------------+-------+
1 row in set (0.00 sec)
4 查看配置文件
發現客户配置了 skip_networking 變量。該配置會導致數據庫無法通過 TCP/IP 進行連接。
mysql> show variables like '%networking%';
+-----------------+-------+
| Variable_name | Value |
+-----------------+-------+
| skip_networking | ON |
+-----------------+-------+
1 row in set (0.00 sec)
[root@hao-3 ~]# grep skip /opt/mysql/etc/8888/my.cnf
skip_external_locking = 1
skip_name_resolve = 1
skip_replica_start = 1
skip_networking=on
5 官方文檔解釋
skip_networking 控制 MySQL 服務是否允許 TCP/IP 連接,默認是關閉。如果開啓這個變量,MySQL 服務只允許本地連接,不允許任何 TCP/IP 連接。
需要注意的是當配置了 --skip-grant-tables 變量之後,skip_networking 變量默認也會開啓,此時禁用任何遠程連接。

結論
skip_networking 變量需要根據業務情況來進行配置,對於只允許本地訪問的 MySQL 數據庫系統來説,強烈建議配置該變量;對於大部分需要遠程訪問的數據庫是不需要進行配置的,保持默認關閉就可以。這個變量不能動態修改,需要修改配置文件,然後重啓數據庫服務。