Skip to content

远程访问 Linux 上 MySQL 出错解决方案

1. 修改 /etc/init.d/my.cnf 文件

修订:文件也可能在 /etc/my.cnf,/etc/mysql/my.cnf

修订:新 Linux 自带的 mariadb/etc/mysql/mariadb.conf.d/50-server.cnf 中修改。

将文件中的bind-address = 127.0.0.1改为bind-address = 0.0.0.0,让所有 IP 都能访问

2. 修改 user 表

使用

bash
sudo mysql
sudo mysql

输入密码

use mysql<ENTER>
select user,host from user;<ENTER>将看到以下结果:
-------------------------------------------------------------------------
mysql> select user,host from user;
+------------------+-----------+
| user             | host      |
+------------------+-----------+
| root             | 127.0.0.1 |
| root             | debian    |
| debian-sys-maint | localhost |
| root             | localhost |
+------------------+-----------+
4 rows in set (0.00 sec)
-------------------------------------------------------------------------
use mysql<ENTER>
select user,host from user;<ENTER>将看到以下结果:
-------------------------------------------------------------------------
mysql> select user,host from user;
+------------------+-----------+
| user             | host      |
+------------------+-----------+
| root             | 127.0.0.1 |
| root             | debian    |
| debian-sys-maint | localhost |
| root             | localhost |
+------------------+-----------+
4 rows in set (0.00 sec)
-------------------------------------------------------------------------

修改最后一行中的 localhost%

sql
update user set host="%" where user="root" and host="localhost";
update user set password=password('123') where user='root' and host='%';
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123' WITH GRANT OPTION;
update user set host="%" where user="root" and host="localhost";
update user set password=password('123') where user='root' and host='%';
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123' WITH GRANT OPTION;

3. 重启 mysql 服务

bash
sudo systemctl restart mysqld
sudo systemctl restart mysqld

最后编辑时间:

Version 4.0 (framework-1.0.0-rc.20)