要允许外部访问MySQL,需配置防火墙开放3306端口并修改MySQL的bind-address为0.0.0.0或指定IP,同时确保用户权限设置正确,并在云服务器上配置安全组规则。

要允许外部访问 MySQL,配置防火墙是关键。本质上,我们需要开放 MySQL 默认的 3306 端口(或者你自定义的端口)。
解决方案:
确定防火墙类型: 常见的有
firewalld
ufw
iptables
使用 firewalld
firewalld
sudo firewall-cmd --state
firewalld
sudo systemctl start firewalld
sudo firewall-cmd --permanent --add-port=3306/tcp
sudo firewall-cmd --reload
sudo firewall-cmd --list-all
ports
3306/tcp
使用 ufw
ufw
sudo ufw enable
sudo ufw allow 3306
ufw
sudo ufw status
使用 iptables
sudo iptables -A INPUT -p tcp --dport 3306 -j ACCEPT
iptables
sudo iptables-save > /etc/iptables/rules.v4
iptables-services
sudo service iptables save
MySQL 配置 (重要): 仅仅开放防火墙端口是不够的,还需要确保 MySQL 允许来自外部的连接。
my.cnf
my.ini
bind-address
bind-address
127.0.0.1
localhost
0.0.0.0
sudo systemctl restart mysql
用户权限: 确保 MySQL 用户有从外部 IP 连接的权限。
mysql -u root -p
user
192.168.1.100
CREATE USER 'user'@'192.168.1.100' IDENTIFIED BY 'password';
user
CREATE USER 'user'@'%' IDENTIFIED BY 'password';
user
GRANT ALL PRIVILEGES ON *.* TO 'user'@'192.168.1.100';
GRANT ALL PRIVILEGES ON *.* TO 'user'@'%';
FLUSH PRIVILEGES;
最简单的办法是使用
telnet
nc
telnet <mysql服务器IP> 3306
nc -vz <mysql服务器IP> 3306
bind-address
0.0.0.0
将
bind-address
0.0.0.0
如果你的 MySQL 服务器运行在云服务器上(例如,AWS EC2、阿里云 ECS、腾讯云 CVM),除了配置服务器自身的防火墙(如
firewalld
ufw
为了提高 MySQL 连接的安全性,建议使用 SSL 加密。这可以防止数据在传输过程中被窃听或篡改。配置 SSL 加密涉及到生成 SSL 证书和密钥,并在 MySQL 服务器和客户端上进行配置。具体步骤可以参考 MySQL 官方文档或相关的教程。启用 SSL 加密后,客户端连接 MySQL 服务器时需要指定 SSL 相关的参数。
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号