虽然安全性可能会有一些问题,但用诸如 navicat 等工具来管理数据库,比 phpmyadmin 或者 adminer 要方便的多,所以需要将 mysql 配置为允许远程访问的形式。

防火墙开启 3306 端口

1
vim /etc/iptables.conf

添加规则

1
-A INPUT -p tcp --dport 3306 -j ACCEPT

重启服务

1
iptables-restore < /etc/iptables.conf

修改 mysql 的默认监听地址

1
vim /etc/mysql/my.cnf

注释掉

1
bind-address = 127.0.0.1

或者改为

1
bind-address = 0.0.0.0

修改root权限

1
mysql -u root -p 'yourpassword'

进入终端后输入

1
2
GRANT ALL ON *.* TO 'root'@'%' IDENTIFIED BY 'yourpassword'; 
exit;

重启服务

1
/etc/init.d/mysql restart

一般情形经过上述四步就可以了,但偏偏我的仍然不行,在第三步中设置权限时总是提示:“Access denied for user ‘root’@’localhost’ (using password: YES)”,原因在于 debian 系统下 mysql 安装时设置的密码和我当前的root密码不一致导致的,安装时的密码被配置在了 /etc/mysql/debian.cnf 中,应该使用这个配置来登录 mysql 终端,然后再改掉 root 密码。

1
mysql -u debian-sys-maint -p 'password in debian.cnf'

进入后:

1
2
3
use mysql; 
update user set password=PASSWORD('your new root password') where user='root';
FLUSH PRIVILEGES;

这时重复第三步应该就没有问题了。远程使用 navicat 连接也正常了。