业务连不上数据库了?小心你的 skip-networking 配置

不小心配置 skip-networking,业务连不上数据库了。

作者:张昊,DBA,主要负责 MySQL 故障处理、DMP 产品支持,擅长 MySQL,喜欢打球唱歌

爱可生开源社区出品,原创内容未经授权不得随意使用,转载请联系小编并注明来源。

本文约 600 字,预计阅读需要 2 分钟。

背景

某客户的测试同事本地部署 MySQL 8.0 数据库,配置文件增加部分变量重启数据库之后发现数据库远程连接失败。

排查

1 查看数据库进程状态

shell 复制代码
[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 连接情况,输出结果为空。

shell 复制代码
[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!

shell 复制代码
[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 进行连接。

shell 复制代码
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 数据库系统来说,强烈建议配置该变量;对于大部分需要远程访问的数据库是不需要进行配置的,保持默认关闭就可以。这个变量不能动态修改,需要修改配置文件,然后重启数据库服务。

相关推荐
wearegogog12323 分钟前
MySQL中实施排序(sorting)及分组(grouping)操作
数据库·mysql
2301_8035545233 分钟前
MySQL 主从读写分离架构
数据库·mysql·架构
正在走向自律39 分钟前
Ubuntu系统下Python连接国产KingbaseES数据库实现增删改查
开发语言·数据库·python·ubuntu·kingbasees·ksycopg2
没有bug.的程序员1 小时前
Redis 内存管理机制:深度解析与性能优化实践
java·数据库·redis·性能优化·内存管理机制
小蒜学长1 小时前
基于SpringBoot+Vue的健身房管理系统的设计与实现(代码+数据库+LW)
java·数据库·vue.js·spring boot·后端
失散131 小时前
分布式专题——2 深入理解Redis线程模型
java·数据库·redis·分布式·架构
DemonAvenger1 小时前
数据库迁移实战:最小化停机时间的方法与经验分享
数据库·sql·性能优化
xhbh6662 小时前
Python操作MySQL的两种姿势:原生SQL与ORM框架SQLAlchemy详解
数据库·oracle
CookieCrusher6 小时前
数据泄露危机逼近:五款电脑加密软件为企业筑起安全防线
运维·数据库·windows·安全·文件加密·数据防泄漏·dlp
这周也會开心7 小时前
SQL-窗口函数
数据库·sql