问题一:本机(localhost)连接一切正常,但是无法从其他电脑上登入 MySQL 数据库!


下面是 /etc/mysql/my.cnf 中的一段摘录:


# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
bind-address = 10.0.0.1 # 127.0.0.1

其中说明 MySQL默认设定为只在 localhost 进行侦听,所以如果登录的 MySQL Client 与 MySQL Server 不为同一台电脑的话,MySQL 是不会响应的。故需要把这里的 bind-address 修改为此台电脑的 external IP 即可。

问题二:ERROR 1045 (28000): Access denied for user 'root'@'10.0.0.1' (using password: YES) !

这个也是颇有意思,首先看看原本系统表中的资料:


mysql> USE mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed

mysql> SELECT Host FROM user WHERE user='root';
+-----------+
| Host      |
+-----------+
| sharkwang |
| localhost |
+-----------+
2 rows in set (0.00 sec)

恩,这里除了有 localhost 外,居然还有一个 sharkwang ,而我连接的时候是用IP而不是hostname !

所以,有2个方案:

1. 在 MySQL Client 所在的电脑上面,修改 /etc/hosts 文件,加入 hostname 和 IP 的映射关系。

   
登录方式为: shell> mysql -h <mysql_server_hostname> -u root -p

2. 在 MySQL 数据库的 user 表中增加一条记录,内容参照:

    SELECT * FROM user WHERE User='root' AND Host='localhost'

    然后把 Host 对应的值修改为 MySQL Server 的 external IP。

    登录方式为: shell> mysql -h <mysql_server_ip> -u root -p


Logo

更多推荐