前言
mysql 是一个广泛使用的开源关系型数据库管理系统。在实际开发中,我们经常需要从本地或外部服务器连接到 mysql 数据库。本文将详细介绍如何配置 mysql 以允许远程连接。
准备工作
服务器为 linux
已安装 mysql 的服务器(本博客基于 mysql 8.0)
拥有管理员权限的用户
确保目标客户端可以通过网络访问 mysql 服务器的 3306 端口
步骤一:修改 mysql 配置文件
默认情况下,mysql 只允许本地连接(localhost)。要开启远程连接,需修改其配置文件。
1. 找到配置文件
linux 系统一般位于 /etc/mysql/mysql.conf.d/mysqld.cnf
或者使用命令查找:
mysql --help | grep "my.cnf"
2. 修改 bind-address
找到如下行:
bind-address = 127.0.0.1
将其改为:
[mysqld] bind-address = 0.0.0.0
这表示监听所有 ip 地址上的连接请求。
注意:如果你使用的是云服务器(如阿里云、腾讯云),还需开放对应的安全组规则。
步骤二:创建或授权远程访问用户
1.登录 mysql:
mysql -u root -p
2. 创建新用户并授权远程访问
create user 'remote_user'@'%' identified by 'your_password'; grant all privileges on *.* to 'remote_user'@'%' with grant option; flush privileges;
‘remote_user’@‘%’ 表示任何 ip 都可以使用 remote_user 登录。
若只允许特定 ip,可替换为 ‘remote_user’@‘192.168.1.100’。
3. (可选)修改已有用户的权限
grant all privileges on *.* to 'existing_user'@'%' identified by 'password'; flush privileges;
步骤三:重启 mysql 服务
使配置生效,重启 mysql 服务:
sudo systemctl restart mysql
安全建议
避免使用 root 用户进行远程连接
使用强密码策略
在生产环境中限制访问 ip 范围
启用 ssl 加密连接(高级用法)
测试远程连接
使用客户端工具(如 navicat、dbeaver、mysql workbench 或命令行)尝试连接:
mysql -h your_server_ip -u remote_user -p
如果提示连接成功,则说明配置已生效!
云服务器注意事项
如果使用的是云服务器,上面操作确保无误之后还是不能远程连接,请务必检查:
安全组是否放行 3306 端口
是否关闭了防火墙(如 ufw、iptables)
是否绑定了公网 ip(某些 vpc 环境需要注意)
方法补充
mysql 设置允许远程连接完整指南
1.基础配置步骤(以mysql 8.0为例)
修改mysql绑定地址
默认情况下mysql仅监听127.0.0.1,需改为0.0.0.0或服务器公网ip。
打开配置文件:
sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf # ubuntu/debian # 或 sudo nano /etc/my.cnf # centos/rhel
找到 bind-address 并修改:
[mysqld] bind-address = 0.0.0.0 # 允许所有ip访问(生产环境慎用) # 或指定特定ip # bind-address = 192.168.1.100
重启mysql服务:
sudo systemctl restart mysql # systemd系统 # 或 sudo service mysql restart # init.d系统
创建远程访问用户
切勿直接使用root账户远程连接!
登录mysql控制台:
mysql -u root -p
创建专用远程用户:
-- mysql 8.0+ 需指定加密插件 create user 'remote_user'@'%' identified with mysql_native_password by 'strongpassw0rd!'; -- 授权所有数据库(按需缩小权限) grant all privileges on *.* to 'remote_user'@'%' with grant option; flush privileges;
关键参数说明:
- 'remote_user'@'%':允许从任何ip连接(建议改为具体ip段如'192.168.1.%')
- mysql_native_password:兼容旧客户端的加密方式(8.0默认使用caching_sha2_password)
配置防火墙
开放mysql默认端口3306:
# ubuntu ufw sudo ufw allow 3306/tcp # centos firewalld sudo firewall-cmd --permanent --add-port=3306/tcp sudo firewall-cmd --reload
2.高级安全加固方案
限制访问ip
通过mysql用户权限限制来源ip:
-- 仅允许192.168.1.0/24网段访问 create user 'secure_user'@'192.168.1.%' identified by 'password123!';
使用ssh隧道
更安全的连接方式(无需开放3306端口):
ssh -l 3306:localhost:3306 user@mysql-server.com
客户端连接本地127.0.0.1:3306即可穿透到远程mysql。
启用ssl加密
检查mysql ssl支持:
show variables like '%ssl%';
强制用户使用ssl连接:
alter user 'remote_user'@'%' require ssl;
客户端连接时添加参数:
mysql -u remote_user -p -h mysql-host --ssl-mode=required
审计与监控
启用查询日志:
[mysqld] general_log = 1 general_log_file = /var/log/mysql/query.log
使用审计插件(企业版)或第三方工具如percona audit plugin。
3.连接测试与故障排查
测试远程连接
mysql -u remote_user -p -h mysql-server-ip --port=3306
常见错误解决方案
错误提示 | 原因 | 解决方案 |
---|---|---|
error 1130 (hy000) | 用户权限不足 | 检查grant语句和用户主机限制 |
error 2003 (hy000) | 端口不通/防火墙拦截 | 使用telnet mysql-host 3306测试 |
error 1045 (28000) | 密码错误 | 重置密码:alter user ... |
public key retrieval is not allowed | 加密插件冲突 | 添加连接参数:--ssl-mode=disabled |
诊断工具
检查端口监听状态:
sudo netstat -tuln | grep 3306
查看实时连接:
show processlist;
到此这篇关于mysql允许远程连接的配置指南的文章就介绍到这了,更多相关mysql允许远程连接内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论