MySQL 远程访问配置指南:让其他电脑也能连接你的数据库

      发布在:个人笔记      评论:0 条评论
<h2 style="font-family: &quot;Segoe UI&quot;, Tahoma, Geneva, Verdana, sans-serif; font-size: 22px; color: rgb(41, 128, 185); margin-top: 40px;">? 前言</h2><p style="font-family: &quot;Segoe UI&quot;, Tahoma, Geneva, Verdana, sans-serif;">在开发、测试或部署项目时,我们经常需要从<strong>其他电脑访问 MySQL 数据库</strong>,比如:</p><ul style="font-family: &quot;Segoe UI&quot;, Tahoma, Geneva, Verdana, sans-serif;"><li>前端同事连接你的后端数据库调试接口</li><li>测试人员在另一台机器运行自动化脚本</li><li>服务器部署后,本地开发机远程管理数据</li><li>使用 Navicat、DBeaver 等图形工具连接远程 MySQL</li></ul><p style="font-family: &quot;Segoe UI&quot;, Tahoma, Geneva, Verdana, sans-serif;">但默认安装的 MySQL&nbsp;<strong>只允许本机访问(localhost)</strong>,直接连接会报错:</p><div style="background: rgb(248, 249, 250); border-left: 4px solid rgb(231, 76, 60); padding: 12px; font-family: Consolas, &quot;Courier New&quot;, monospace; margin: 15px 0px;">ERROR 1130 (HY000): Host 'xxx' is not allowed to connect to this MySQL server</div><p style="font-family: &quot;Segoe UI&quot;, Tahoma, Geneva, Verdana, sans-serif;">别慌!本文将手把手教你&nbsp;<strong>4 步配置 MySQL 支持远程访问</strong>,并附带安全加固建议,让你既方便又安全!</p><hr style="font-family: &quot;Segoe UI&quot;, Tahoma, Geneva, Verdana, sans-serif; margin-top: 30px; margin-bottom: 30px;"><h2 style="font-family: &quot;Segoe UI&quot;, Tahoma, Geneva, Verdana, sans-serif; font-size: 22px; color: rgb(41, 128, 185); margin-top: 40px;">✅ 第一步:修改 MySQL 配置文件,允许外部连接</h2><p style="font-family: &quot;Segoe UI&quot;, Tahoma, Geneva, Verdana, sans-serif;">默认 MySQL 只监听&nbsp;<code>127.0.0.1</code>(本机),我们需要让它监听所有 IP。</p><h3 style="font-size: 60px; color: rgb(238, 238, 238); text-align: center; padding-top: 30px; font-family: &quot;Segoe UI&quot;, Tahoma, Geneva, Verdana, sans-serif;">? 配置文件路径:</h3><table style="font-family: &quot;Segoe UI&quot;, Tahoma, Geneva, Verdana, sans-serif; width: 800px; margin: 15px 0px;"><thead><tr style="background-color: rgb(242, 242, 242);"><th style="border: 1px solid rgb(221, 221, 221); padding: 8px;">系统</th><th style="border: 1px solid rgb(221, 221, 221); padding: 8px;">配置文件路径</th></tr></thead><tbody><tr><td style="border: 1px solid rgb(221, 221, 221); padding: 8px;">Windows</td><td style="border: 1px solid rgb(221, 221, 221); padding: 8px;">D:\phpstudy_pro\Extensions\MySQL*\my.ini</td></tr><tr><td style="border: 1px solid rgb(221, 221, 221); padding: 8px;">Linux</td><td style="border: 1px solid rgb(221, 221, 221); padding: 8px;">/etc/mysql/mysql.conf.d/mysqld.cnf 或 /etc/my.cnf</td></tr><tr><td style="border: 1px solid rgb(221, 221, 221); padding: 8px;">macOS</td><td style="border: 1px solid rgb(221, 221, 221); padding: 8px;">/usr/local/mysql/etc/my.cnf</td></tr></tbody></table><p style="font-family: &quot;Segoe UI&quot;, Tahoma, Geneva, Verdana, sans-serif;">? 使用 phpStudy、WAMP、XAMPP 等集成环境?配置文件通常在安装目录下的 MySQL/conf/ 或直接搜索 my.ini。</p><h3 style="font-size: 60px; color: rgb(238, 238, 238); text-align: center; padding-top: 30px; font-family: &quot;Segoe UI&quot;, Tahoma, Geneva, Verdana, sans-serif;">? 修改内容:</h3><p style="font-family: &quot;Segoe UI&quot;, Tahoma, Geneva, Verdana, sans-serif;">找到&nbsp;<code>bind-address</code>&nbsp;配置项:</p><div style="background: rgb(45, 45, 45); color: rgb(248, 248, 242); padding: 12px; font-family: Consolas, &quot;Courier New&quot;, monospace; overflow-x: auto; margin: 15px 0px;">bind-address = 127.0.0.1</div><p style="font-family: &quot;Segoe UI&quot;, Tahoma, Geneva, Verdana, sans-serif;">? 修改为:</p><div style="background: rgb(45, 45, 45); color: rgb(248, 248, 242); padding: 12px; font-family: Consolas, &quot;Courier New&quot;, monospace; overflow-x: auto; margin: 15px 0px;">bind-address = 0.0.0.0</div><p style="font-family: &quot;Segoe UI&quot;, Tahoma, Geneva, Verdana, sans-serif;">✅&nbsp;<code>0.0.0.0</code>&nbsp;表示监听所有网卡/IP。你也可以指定服务器内网 IP,如&nbsp;<code>192.168.1.100</code>。</p><h3 style="font-size: 60px; color: rgb(238, 238, 238); text-align: center; padding-top: 30px; font-family: &quot;Segoe UI&quot;, Tahoma, Geneva, Verdana, sans-serif;">? 重启 MySQL 服务</h3><div style="background: rgb(248, 249, 250); padding: 12px; font-family: Consolas, &quot;Courier New&quot;, monospace; margin: 15px 0px;"># Linux<br>sudo systemctl restart mysql<br><br># Windows(管理员 CMD)<br>net stop mysql<br>net start mysql</div><p style="font-family: &quot;Segoe UI&quot;, Tahoma, Geneva, Verdana, sans-serif;">⚠️ phpStudy 用户请在界面中“停止 MySQL → 启动 MySQL”。</p><hr style="font-family: &quot;Segoe UI&quot;, Tahoma, Geneva, Verdana, sans-serif; margin-top: 30px; margin-bottom: 30px;"><h2 style="font-family: &quot;Segoe UI&quot;, Tahoma, Geneva, Verdana, sans-serif; font-size: 22px; color: rgb(41, 128, 185); margin-top: 40px;">✅ 第二步:创建或授权远程用户</h2><p style="font-family: &quot;Segoe UI&quot;, Tahoma, Geneva, Verdana, sans-serif;">MySQL 权限系统是&nbsp;<code>用户名@主机名</code>&nbsp;双重验证。默认&nbsp;<code>root@localhost</code>&nbsp;不允许远程登录。</p><h3 style="font-size: 60px; color: rgb(238, 238, 238); text-align: center; padding-top: 30px; font-family: &quot;Segoe UI&quot;, Tahoma, Geneva, Verdana, sans-serif;">✅ 推荐方式:创建专用远程用户(安全!)</h3><div style="background: rgb(248, 249, 250); padding: 12px; font-family: Consolas, &quot;Courier New&quot;, monospace; overflow-x: auto; margin: 15px 0px;">-- 登录 MySQL(在服务器本机执行)<br>mysql -u root -p<br><br>-- 创建用户(允许从任意 IP 连接)<br>CREATE USER 'dev_user'@'%' IDENTIFIED BY 'YourStrongPassword123!';<br><br>-- 授权数据库操作权限(按需调整)<br>GRANT SELECT, INSERT, UPDATE, DELETE ON your_database.* TO 'dev_user'@'%';<br><br>-- 刷新权限<br>FLUSH PRIVILEGES;</div><h3 style="font-size: 60px; color: rgb(238, 238, 238); text-align: center; padding-top: 30px; font-family: &quot;Segoe UI&quot;, Tahoma, Geneva, Verdana, sans-serif;">⚠️ 不推荐:开放 root 远程访问(仅测试环境)</h3><div style="background: rgb(254, 254, 254); border-left: 4px solid rgb(230, 126, 34); padding: 12px; font-family: Consolas, &quot;Courier New&quot;, monospace; margin: 15px 0px;">-- 允许 root 从任意主机连接(危险!)<br>GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'your_root_password' WITH GRANT OPTION;<br>FLUSH PRIVILEGES;</div><p style="font-family: &quot;Segoe UI&quot;, Tahoma, Geneva, Verdana, sans-serif;">?️ 生产环境强烈建议使用独立用户,避免 root 泄露导致删库跑路!</p><hr style="font-family: &quot;Segoe UI&quot;, Tahoma, Geneva, Verdana, sans-serif; margin-top: 30px; margin-bottom: 30px;"><h2 style="font-family: &quot;Segoe UI&quot;, Tahoma, Geneva, Verdana, sans-serif; font-size: 22px; color: rgb(41, 128, 185); margin-top: 40px;">✅ 第三步:开放防火墙端口(3306)</h2><p style="font-family: &quot;Segoe UI&quot;, Tahoma, Geneva, Verdana, sans-serif;">MySQL 默认端口是&nbsp;<code>3306</code>,需放行入站连接。</p><h3 style="font-size: 60px; color: rgb(238, 238, 238); text-align: center; padding-top: 30px; font-family: &quot;Segoe UI&quot;, Tahoma, Geneva, Verdana, sans-serif;">? Windows 防火墙设置:</h3><ol style="font-family: &quot;Segoe UI&quot;, Tahoma, Geneva, Verdana, sans-serif;"><li>打开“控制面板” → “Windows Defender 防火墙” → “高级设置”</li><li>点击“入站规则” → “新建规则”</li><li>选择“端口” → TCP → 特定端口:<code>3306</code></li><li>允许连接 → 下一步 → 命名如 “MySQL Remote” → 完成</li></ol><h3 style="font-size: 60px; color: rgb(238, 238, 238); text-align: center; padding-top: 30px; font-family: &quot;Segoe UI&quot;, Tahoma, Geneva, Verdana, sans-serif;">? Linux(Ubuntu/CentOS):</h3><div style="background: rgb(248, 249, 250); padding: 12px; font-family: Consolas, &quot;Courier New&quot;, monospace; margin: 15px 0px;"># Ubuntu<br>sudo ufw allow 3306/tcp<br><br># CentOS<br>sudo firewall-cmd --permanent --add-port=3306/tcp<br>sudo firewall-cmd --reload</div><hr style="font-family: &quot;Segoe UI&quot;, Tahoma, Geneva, Verdana, sans-serif; margin-top: 30px; margin-bottom: 30px;"><h2 style="font-family: &quot;Segoe UI&quot;, Tahoma, Geneva, Verdana, sans-serif; font-size: 22px; color: rgb(41, 128, 185); margin-top: 40px;">✅ 第四步:云服务器用户额外配置(阿里云/腾讯云/AWS)</h2><p style="font-family: &quot;Segoe UI&quot;, Tahoma, Geneva, Verdana, sans-serif;">如果你的 MySQL 跑在云服务器上,还需:</p><ol style="font-family: &quot;Segoe UI&quot;, Tahoma, Geneva, Verdana, sans-serif;"><li>登录云控制台 → 安全组</li><li>添加入站规则:协议 TCP,端口 3306,来源 IP 可设为:<ul><li><code>0.0.0.0/0</code>(任意 IP,测试用)</li><li><code>192.168.1.0/24</code>(公司内网段)</li><li><code>你的公网IP/32</code>(仅你个人电脑)</li></ul></li></ol><hr style="font-family: &quot;Segoe UI&quot;, Tahoma, Geneva, Verdana, sans-serif; margin-top: 30px; margin-bottom: 30px;"><h2 style="font-family: &quot;Segoe UI&quot;, Tahoma, Geneva, Verdana, sans-serif; font-size: 22px; color: rgb(41, 128, 185); margin-top: 40px;">? 第五步:测试远程连接</h2><p style="font-family: &quot;Segoe UI&quot;, Tahoma, Geneva, Verdana, sans-serif;">在其他电脑上执行:</p><div style="background: rgb(248, 249, 250); padding: 12px; font-family: Consolas, &quot;Courier New&quot;, monospace; margin: 15px 0px;">mysql -h [服务器IP] -u [用户名] -p</div><p style="font-family: &quot;Segoe UI&quot;, Tahoma, Geneva, Verdana, sans-serif;">示例:</p><div style="background: rgb(248, 249, 250); padding: 12px; font-family: Consolas, &quot;Courier New&quot;, monospace; margin: 15px 0px;">mysql -h 192.168.117.150 -u dev_user -p</div><p style="font-family: &quot;Segoe UI&quot;, Tahoma, Geneva, Verdana, sans-serif;">或使用图形工具:</p><ul style="font-family: &quot;Segoe UI&quot;, Tahoma, Geneva, Verdana, sans-serif;"><li>Navicat:新建连接 → 填写 IP、端口、用户名、密码</li><li>DBeaver / MySQL Workbench 同理</li></ul><p style="font-family: &quot;Segoe UI&quot;, Tahoma, Geneva, Verdana, sans-serif;">✅ 成功登录 = 配置完成!</p><hr style="font-family: &quot;Segoe UI&quot;, Tahoma, Geneva, Verdana, sans-serif; margin-top: 30px; margin-bottom: 30px;"><h2 style="font-family: &quot;Segoe UI&quot;, Tahoma, Geneva, Verdana, sans-serif; font-size: 22px; color: rgb(192, 57, 43); margin-top: 40px;">?️ 安全加固建议(必看!)</h2><p style="font-family: &quot;Segoe UI&quot;, Tahoma, Geneva, Verdana, sans-serif;">远程访问虽方便,但也带来安全风险。请务必:</p><table style="font-family: &quot;Segoe UI&quot;, Tahoma, Geneva, Verdana, sans-serif; width: 800px; margin: 15px 0px;"><thead><tr style="background-color: rgb(242, 242, 242);"><th style="border: 1px solid rgb(221, 221, 221); padding: 8px;">建议项</th><th style="border: 1px solid rgb(221, 221, 221); padding: 8px;">说明</th></tr></thead><tbody><tr><td style="border: 1px solid rgb(221, 221, 221); padding: 8px;">❌ 不开放 root 远程</td><td style="border: 1px solid rgb(221, 221, 221); padding: 8px;">创建专用用户,权限最小化</td></tr><tr><td style="border: 1px solid rgb(221, 221, 221); padding: 8px;">✅ 限制访问 IP</td><td style="border: 1px solid rgb(221, 221, 221); padding: 8px;">使用 'user'@'192.168.1.%' 或固定 IP</td></tr><tr><td style="border: 1px solid rgb(221, 221, 221); padding: 8px;">✅ 使用强密码</td><td style="border: 1px solid rgb(221, 221, 221); padding: 8px;">包含大小写、数字、符号,长度 ≥12</td></tr><tr><td style="border: 1px solid rgb(221, 221, 221); padding: 8px;">✅ 启用 SSL 加密</td><td style="border: 1px solid rgb(221, 221, 221); padding: 8px;">防止中间人窃听(进阶)</td></tr><tr><td style="border: 1px solid rgb(221, 221, 221); padding: 8px;">✅ 定期审计用户权限</td><td style="border: 1px solid rgb(221, 221, 221); padding: 8px;"><code>SELECT host,user FROM mysql.user;</code></td></tr><tr><td style="border: 1px solid rgb(221, 221, 221); padding: 8px;">✅ 使用 SSH 隧道</td><td style="border: 1px solid rgb(221, 221, 221); padding: 8px;">更安全的远程访问方式(推荐生产环境)</td></tr></tbody></table><hr style="font-family: &quot;Segoe UI&quot;, Tahoma, Geneva, Verdana, sans-serif; margin-top: 30px; margin-bottom: 30px;"><h2 style="font-family: &quot;Segoe UI&quot;, Tahoma, Geneva, Verdana, sans-serif; font-size: 22px; color: rgb(142, 68, 173); margin-top: 40px;">❗ 常见错误排查</h2><h3 style="font-size: 60px; color: rgb(238, 238, 238); text-align: center; padding-top: 30px; font-family: &quot;Segoe UI&quot;, Tahoma, Geneva, Verdana, sans-serif;">1.&nbsp;<code>ERROR 1130: Host 'xxx' is not allowed...</code></h3><p style="font-family: &quot;Segoe UI&quot;, Tahoma, Geneva, Verdana, sans-serif;">? 原因:用户未授权该主机连接<br>✅ 解决:执行&nbsp;<code>GRANT ... TO 'user'@'%'</code>&nbsp;并&nbsp;<code>FLUSH PRIVILEGES;</code></p><h3 style="font-size: 60px; color: rgb(238, 238, 238); text-align: center; padding-top: 30px; font-family: &quot;Segoe UI&quot;, Tahoma, Geneva, Verdana, sans-serif;">2.&nbsp;<code>ERROR 2003: Can't connect to MySQL server</code></h3><p style="font-family: &quot;Segoe UI&quot;, Tahoma, Geneva, Verdana, sans-serif;">? 原因:网络不通 / 防火墙拦截 / MySQL 未监听<br>✅ 解决:</p><ul style="font-family: &quot;Segoe UI&quot;, Tahoma, Geneva, Verdana, sans-serif;"><li><code>ping 服务器IP</code>&nbsp;→ 通吗?</li><li><code>telnet 服务器IP 3306</code>&nbsp;→ 能连上吗?</li><li>检查&nbsp;<code>bind-address</code>&nbsp;和防火墙</li></ul><p style="font-family: &quot;Segoe UI&quot;, Tahoma, Geneva, Verdana, sans-serif;"></p><h3 style="font-size: 60px; color: rgb(238, 238, 238); text-align: center; padding-top: 30px; font-family: &quot;Segoe UI&quot;, Tahoma, Geneva, Verdana, sans-serif;">3. 连接超时 / 无响应</h3><p style="font-family: &quot;Segoe UI&quot;, Tahoma, Geneva, Verdana, sans-serif;">? 可能是云服务器安全组未放行,或 MySQL 服务未启动。</p><hr style="font-family: &quot;Segoe UI&quot;, Tahoma, Geneva, Verdana, sans-serif; margin-top: 30px; margin-bottom: 30px;"><h2 style="font-family: &quot;Segoe UI&quot;, Tahoma, Geneva, Verdana, sans-serif; font-size: 22px; color: rgb(22, 160, 133); margin-top: 40px;">? 总结</h2><p style="font-family: &quot;Segoe UI&quot;, Tahoma, Geneva, Verdana, sans-serif;">配置 MySQL 远程访问只需四步:</p><ol style="font-family: &quot;Segoe UI&quot;, Tahoma, Geneva, Verdana, sans-serif;"><li>改&nbsp;<code>bind-address = 0.0.0.0</code></li><li>创建或授权远程用户(推荐非 root)</li><li>开放防火墙 3306 端口</li><li>云服务器配置安全组</li></ol><p style="font-family: &quot;Segoe UI&quot;, Tahoma, Geneva, Verdana, sans-serif;">?&nbsp;<strong>小贴士:</strong>开发环境可适当宽松,生产环境务必遵循最小权限原则!</p>
相关文章
热门推荐