本文档风哥主要介绍MySQL 8.4的网络配置与防火墙设置,包括网络参数配置、防火墙规则设置、SSL安全连接配置等内容。风哥教程参考MySQL官方文档MySQL Server
Administration等。更多视频教程www.fgedu.net.cn
Part01-基础概念与理论知识
1.1 MySQL网络架构
MySQL的网络架构主要包括以下组件:
- 服务器端:监听客户端连接请求,默认端口3306
- 客户端:发起连接请求,包括mysql命令行工具、应用程序等
- 网络协议:主要使用TCP/IP协议进行通信
- 连接管理:负责建立、维护和关闭客户端连接
1.2 网络配置的重要性
合理的网络配置可以:
- 提高连接安全性,防止未授权访问
- 优化网络性能,减少连接延迟
- 确保服务可用性,避免网络故障影响业务
- 便于管理和监控,及时发现网络问题
学习交流加群风哥微信: itpux-com
1.3 MySQL网络协议
MySQL主要使用以下网络协议:
- TCP/IP:默认的网络通信协议,适用于大多数场景
- Unix套接字:适用于本地连接,性能更好
- Named Pipes:主要用于Windows系统
Part02-生产环境规划与建议
2.1 网络架构规划
在生产环境中,MySQL的网络架构规划应考虑以下因素:
- 网络隔离:将数据库服务器放在独立的子网或VLAN中
- 连接方式:根据业务需求选择TCP/IP或Unix套接字
- 端口设置:可以考虑更改默认端口以提高安全性
- 负载均衡:对于高并发场景,考虑使用负载均衡器
2.2 安全策略规划
网络安全策略规划:
- 防火墙规则:仅允许必要的IP地址访问MySQL端口
- SSL加密:启用SSL加密传输,保护数据安全
- 访问控制:使用IP绑定和用户权限控制访问
- 连接限制:设置合理的最大连接数,防止资源耗尽
2.3 性能优化规划
网络性能优化规划:
- 连接池配置:在应用程序端使用连接池,减少连接开销
- 网络参数调优:根据网络环境调整缓冲区大小等参数
- 连接复用:尽量复用现有连接,避免频繁建立新连接
- 批量操作:使用批量操作减少网络往返次数
Part03-生产环境项目实施方案
3.1 MySQL网络参数配置
MySQL的网络参数配置主要在my.cnf文件中进行,以下是生产环境中常用的网络参数配置。
3.1.1 基本网络参数配置
vi /etc/my.cnf
[mysqld]
# 绑定的IP地址,0.0.0.0表示所有IP
bind-address=0.0.0.0
# 端口号
port=3306
# 最大连接数
max_connections=500
# 连接超时时间(秒)
connect_timeout=10
# 等待数据包的时间(秒)
wait_timeout=7200
# 交互式连接超时时间(秒)
interactive_timeout=7200
# 重启MySQL服务使配置生效
systemctl restart mysqld
# 验证网络参数配置
mysql -u root -p -e “SHOW VARIABLES LIKE ‘%bind_address%’; SHOW VARIABLES LIKE ‘port’; SHOW VARIABLES LIKE
‘max_connections’;”
Enter password: Fgedu123!
+—————+———–+
| Variable_name | Value |
+—————+———–+
| bind_address | 0.0.0.0 |
+—————+———–+
+—————+——-+
| Variable_name | Value |
+—————+——-+
| port | 3306 |
+—————+——-+
+—————–+——-+
| Variable_name | Value |
+—————–+——-+
| max_connections | 500 |
+—————–+——-+
3.1.2 高级网络参数配置
vi /etc/my.cnf
[mysqld]
# 网络缓冲区大小
net_buffer_length=16384
# 最大数据包大小
max_allowed_packet=64M
# 线程缓存大小
thread_cache_size=64
# 表定义缓存
table_definition_cache=1000
# 表打开缓存
table_open_cache=2000
# 排序缓冲区大小
sort_buffer_size=2M
# 连接缓冲区大小
read_buffer_size=1M
# 随机读取缓冲区大小
read_rnd_buffer_size=2M
# 连接队列大小
back_log=128
# 重启MySQL服务使配置生效
systemctl restart mysqld
# 验证高级网络参数配置
mysql -u root -p -e “SHOW VARIABLES LIKE ‘net_buffer_length’; SHOW VARIABLES LIKE ‘max_allowed_packet’; SHOW
VARIABLES LIKE ‘thread_cache_size’;”
Enter password: Fgedu123!
+——————+——-+
| Variable_name | Value |
+——————+——-+
| net_buffer_length | 16384 |
+——————+——-+
+——————–+———-+
| Variable_name | Value |
+——————–+———-+
| max_allowed_packet | 67108864 |
+——————–+———-+
+——————-+——-+
| Variable_name | Value |
+——————-+——-+
| thread_cache_size | 64 |
+——————-+——-+
3.2 防火墙配置方案
防火墙是保护MySQL服务器安全的重要防线,以下是不同环境下的防火墙配置方案。学习交流加群风哥QQ113257174
3.2.1 Linux firewalld配置
systemctl status firewalld
# 允许MySQL端口
firewall-cmd –permanent –add-port=3306/tcp
# 允许特定IP访问MySQL端口
firewall-cmd –permanent –add-rich-rule=’rule family=”ipv4″ source address=”192.168.1.100″ port
protocol=”tcp” port=”3306″ accept’
# 重新加载规则使配置生效
firewall-cmd –reload
# 查看已添加的规则
firewall-cmd –list-ports
firewall-cmd –list-rich-rules
# 输出示例
3306/tcp
rule family=”ipv4″ source address=”192.168.1.100″ port port=”3306″ protocol=”tcp” accept
3.2.2 Linux iptables配置
iptables -A INPUT -p tcp –dport 3306 -j ACCEPT
# 允许特定IP访问MySQL端口
iptables -A INPUT -p tcp -s 192.168.1.100 –dport 3306 -j ACCEPT
# 保存规则
iptables-save > /etc/sysconfig/iptables
# 重启iptables服务
systemctl restart iptables
# 查看iptables规则
iptables -L -n | grep 3306
# 输出示例
ACCEPT tcp — 192.168.1.100 0.0.0.0/0 tcp dpt:3306
ACCEPT tcp — 0.0.0.0/0 0.0.0.0/0 tcp dpt:3306
3.2.3 Windows防火墙配置
New-NetFirewallRule -DisplayName “MySQL Server” -Direction Inbound -Protocol TCP -LocalPort 3306 -Action
Allow
# 添加特定IP允许规则
New-NetFirewallRule -DisplayName “MySQL Server – 192.168.1.100” -Direction Inbound -Protocol TCP -LocalPort
3306 -RemoteAddress 192.168.1.100 -Action Allow
# 查看防火墙规则
Get-NetFirewallRule -DisplayName “MySQL Server*”
# 输出示例
Name : {12345678-1234-1234-1234-123456789012}
DisplayName : MySQL Server
Enabled : True
Direction : Inbound
Action : Allow
Protocol : TCP
LocalPort : 3306
RemoteAddress : Any
3.3 SSL安全连接配置
SSL加密是保护MySQL网络传输安全的重要措施,可以防止数据在传输过程中被窃取或篡改。风哥提示:生产环境中建议启用SSL加密连接。
3.3.1 生成SSL证书
mkdir -p /mysql/ssl
cd /mysql/ssl
# 生成CA证书
openssl genrsa 2048 > ca-key.pem
openssl req -new -x509 -nodes -days 3600 -key ca-key.pem -out ca.pem -subj
“/C=CN/ST=Beijing/L=Beijing/O=FGEDU/CN=MySQL CA”
# 生成服务器证书
openssl req -newkey rsa:2048 -days 3600 -nodes -keyout server-key.pem -out server-req.pem -subj
“/C=CN/ST=Beijing/L=Beijing/O=FGEDU/CN=fgedu.net.cn”
openssl rsa -in server-key.pem -out server-key.pem
openssl x509 -req -in server-req.pem -days 3600 -CA ca.pem -CAkey ca-key.pem -set_serial 01 -out
server-cert.pem
# 生成客户端证书
openssl req -newkey rsa:2048 -days 3600 -nodes -keyout client-key.pem -out client-req.pem -subj
“/C=CN/ST=Beijing/L=Beijing/O=FGEDU/CN=MySQL Client”
openssl rsa -in client-key.pem -out client-key.pem
openssl x509 -req -in client-req.pem -days 3600 -CA ca.pem -CAkey ca-key.pem -set_serial 02 -out
client-cert.pem
# 设置证书权限
chown mysql:mysql *.pem
chmod 400 *.pem
3.3.2 配置MySQL使用SSL
vi /etc/my.cnf
[mysqld]
# SSL配置
ssl_ca=/mysql/ssl/ca.pem
ssl_cert=/mysql/ssl/server-cert.pem
ssl_key=/mysql/ssl/server-key.pem
# 重启MySQL服务使配置生效
systemctl restart mysqld
# 验证SSL配置
mysql -u root -p -e “SHOW VARIABLES LIKE ‘%ssl%’;”
Enter password: Fgedu123!
+—————+—————–+
| Variable_name | Value |
+—————+—————–+
| have_openssl | YES |
| have_ssl | YES |
| ssl_ca | /mysql/ssl/ca.pem |
| ssl_capath | |
| ssl_cert | /mysql/ssl/server-cert.pem |
| ssl_cipher | |
| ssl_crl | |
| ssl_crlpath | |
| ssl_key | /mysql/ssl/server-key.pem |
+—————+—————–+
3.3.3 强制使用SSL连接
mysql -u root -p -e “CREATE USER ‘fgedu_ssl’@’%’ IDENTIFIED BY ‘Fgedu123!’ REQUIRE SSL; GRANT ALL PRIVILEGES
ON fgedudb.* TO ‘fgedu_ssl’@’%’; FLUSH PRIVILEGES;”
Enter password: Fgedu123!
# 修改现有用户需要SSL
mysql -u root -p -e “ALTER USER ‘fgedu_user’@’%’ REQUIRE SSL;”
Enter password: Fgedu123!
# 测试SSL连接
mysql -u fgedu_ssl -p -h 192.168.1.10 -e “STATUS” | grep SSL
Enter password: Fgedu123!
SSL: Cipher in use is TLS_AES_256_GCM_SHA384
Part04-生产案例与实战讲解
4.1 生产环境网络配置案例
以下是一个生产环境中MySQL网络配置的完整案例:
vi /etc/my.cnf
[mysqld]
# 基本网络配置
bind-address=0.0.0.0
port=3306
max_connections=1000
connect_timeout=10
wait_timeout=7200
interactive_timeout=7200
# 高级网络配置
net_buffer_length=16384
max_allowed_packet=64M
thread_cache_size=128
table_definition_cache=2000
table_open_cache=4000
sort_buffer_size=2M
read_buffer_size=1M
read_rnd_buffer_size=2M
back_log=256
# SSL配置
ssl_ca=/mysql/ssl/ca.pem
ssl_cert=/mysql/ssl/server-cert.pem
ssl_key=/mysql/ssl/server-key.pem
# 重启MySQL服务
systemctl restart mysqld
# 验证所有网络参数
mysql -u root -p -e “SHOW VARIABLES LIKE ‘%max_connections%’; SHOW VARIABLES LIKE ‘%wait_timeout%’; SHOW
VARIABLES LIKE ‘%ssl%’;”
Enter password: Fgedu123!
4.2 防火墙规则配置案例
生产环境中限制特定IP段访问MySQL的防火墙配置案例:
firewall-cmd –permanent –remove-port=3306/tcp
# 只允许应用服务器网段访问MySQL
firewall-cmd –permanent –add-rich-rule=’rule family=”ipv4″ source address=”192.168.10.0/24″ port
protocol=”tcp” port=”3306″ accept’
firewall-cmd –permanent –add-rich-rule=’rule family=”ipv4″ source address=”172.16.0.0/16″ port
protocol=”tcp” port=”3306″ accept’
# 允许DBA管理IP访问
firewall-cmd –permanent –add-rich-rule=’rule family=”ipv4″ source address=”10.0.0.5″ port protocol=”tcp”
port=”3306″ accept’
# 重新加载规则
firewall-cmd –reload
# 查看最终的规则
firewall-cmd –list-rich-rules | grep 3306
4.3 网络连接管理与监控
生产环境中MySQL连接管理与监控的案例:
mysql -u root -p -e “SHOW PROCESSLIST;”
Enter password: Fgedu123!
# 查看连接统计信息
mysql -u root -p -e “SHOW GLOBAL STATUS LIKE ‘Connections’; SHOW GLOBAL STATUS LIKE ‘Threads_connected’;
SHOW GLOBAL STATUS LIKE ‘Threads_running’;”
Enter password: Fgedu123!
+—————+——-+
| Variable_name | Value |
+—————+——-+
| Connections | 1563 |
+—————+——-+
+——————-+——-+
| Variable_name | Value |
+——————-+——-+
| Threads_connected | 45 |
+——————-+——-+
+—————–+——-+
| Variable_name | Value |
+—————–+——-+
| Threads_running | 5 |
+—————–+——-+
# 创建连接监控脚本
cat > /mysql/scripts/connection_monitor.sh << 'EOF' #!/bin/bash # connection_monitor.sh #
from:www.itpux.com.qq113257174.wx:itpux-com # web: http://www.fgedu.net.cn DATE=$(date +"%Y-%m-%d %H:%M:%S")
CONN_COUNT=$(mysql -u root -pFgedu123! -e "SHOW GLOBAL STATUS LIKE 'Threads_connected';" | grep
Threads_connected | awk '{print $2}' ) RUN_COUNT=$(mysql -u root -pFgedu123!
-e "SHOW GLOBAL STATUS LIKE 'Threads_running';" | grep Threads_running | awk '{print $2}' )
echo "$DATE - Connected: $CONN_COUNT, Running: $RUN_COUNT">> /mysql/logs/connection_monitor.log
# 如果连接数超过阈值,发送告警
if [ $CONN_COUNT -gt 800 ]; then
echo “MySQL连接数过高: $CONN_COUNT” | mail -s “MySQL连接告警” admin@fgedu.net.cn
fi
EOF
# 赋予执行权限并添加到定时任务
chmod +x /mysql/scripts/connection_monitor.sh
crontab -l > /tmp/crontab.tmp
echo “* * * * * /mysql/scripts/connection_monitor.sh” >> /tmp/crontab.tmp
crontab /tmp/crontab.tmp
rm /tmp/crontab.tmp
Part05-风哥经验总结与分享
5.1 网络配置最佳实践
[mysqld]
# 基础网络配置
bind-address=0.0.0.0 # 根据实际需要绑定特定IP
port=3306 # 可考虑更改默认端口提高安全性
max_connections=1000 # 根据服务器内存和CPU资源调整
connect_timeout=10
wait_timeout=7200
interactive_timeout=7200
# 性能优化参数
net_buffer_length=16384
max_allowed_packet=64M
thread_cache_size=128 # 建议设置为max_connections的1/8到1/4
table_open_cache=4000
sort_buffer_size=2M
read_buffer_size=1M
read_rnd_buffer_size=2M
back_log=256
# 安全配置
ssl_ca=/mysql/ssl/ca.pem
ssl_cert=/mysql/ssl/server-cert.pem
ssl_key=/mysql/ssl/server-key.pem
5.2 常见问题与解决方案
# 排查步骤:
1. 检查MySQL服务是否运行:systemctl status mysqld
2. 检查端口是否监听:netstat -tuln | grep 3306
3. 检查防火墙规则:firewall-cmd –list-ports | grep 3306
4. 测试本地连接:mysql -u root -p
5. 测试远程连接:mysql -u root -p -h 192.168.1.10
# 问题2:连接超时
# 可能原因:网络延迟、服务器负载高、connect_timeout设置过小
# 解决方案:
– 检查网络状况:ping 192.168.1.10
– 检查服务器负载:top、vmstat
– 调整connect_timeout参数:set global connect_timeout=30;
# 问题3:连接数达到上限
# 解决方案:
– 查看当前连接数:show global status like ‘Threads_connected’;
– 增加max_connections值:set global max_connections=1500;
– 优化应用程序连接池配置
# 问题4:SSL连接失败
# 排查步骤:
1. 检查SSL配置:show variables like ‘%ssl%’;
2. 检查证书权限:ls -l /mysql/ssl/*.pem
3. 测试SSL连接:mysql -u ssl_user -p -h 192.168.1.10 –ssl-mode=REQUIRED
5.3 网络性能监控与调优
生产环境中,网络性能监控是确保MySQL稳定运行的重要环节:
mysql -u root -p -e “SHOW GLOBAL STATUS LIKE ‘Threads%’; SHOW GLOBAL STATUS LIKE ‘Connections’; SHOW GLOBAL
STATUS LIKE ‘Aborted_connects’;”
Enter password: Fgedu123!
# 监控网络流量
vnstat -i eth0 # 需要安装vnstat工具
iptraf-ng eth0 # 实时网络流量监控
# 网络性能调优建议:
1. 使用连接池减少连接建立开销
2. 优化max_connections和thread_cache_size参数
3. 合理设置wait_timeout和interactive_timeout
4. 启用SSL时选择高效的加密算法
5. 考虑使用Unix套接字进行本地连接
5.4 安全加固建议
- 网络层面:使用防火墙限制访问IP,更改默认端口,启用SSL加密
- 认证层面:使用强密码,限制用户访问IP,定期更新密码
- 授权层面:遵循最小权限原则,定期检查用户权限
- 审计层面:启用审计日志,定期检查异常连接和操作
更多学习教程公众号风哥教程itpux_com
from MySQL:www.itpux.com
本文由风哥教程整理发布,仅用于学习测试使用,转载注明出处:http://www.fgedu.net.cn/10327.html
