不小心装备 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 数据库系统来说,强烈建议装备该变量;关于大部分需求长途拜访的数据库是不需求进行装备的,保持默许关闭就可以。这个变量不能动态修改,需求修改装备文件,然后重启数据库服务。