【问题标题】:Can't connect to MySQL server remotely无法远程连接到 MySQL 服务器
【发布时间】:2017-06-26 03:11:25
【问题描述】:

我已经阅读了很多关于这个问题的答案,但没有找到解决办法。 我在 Azure 上有一个 mysql 服务器(例如 13.25.147.140)。

我的.cnf:

[mysqld]
# bind-address=127.0.0.1
init_connect= ^`^xSET collation_connection = utf8_unicode_ci ^`^y
character-set-server = utf8
collation-server = utf8_unicode_ci

[client]
default-character-set = utf8

然后,我做了sudo service mysql restart

然后,授予root权限:

GRANT ALL PRIVILEGES ON *.* TO 'root'@'57.26.24.157' IDENTIFIED BY 'password' WITH GRANT OPTION;
mysql> SELECT user, host from user;
+------------------+--------------+
| user             | host         |
+------------------+--------------+
| root             | %            |
| root             | 57.26.24.157 |
| debian-sys-maint | localhost    |
| mysql.sys        | localhost    |
| paymon           | localhost    |
| phpmyadmin       | localhost    |
| root             | localhost    |
+------------------+--------------+

但是当我尝试从我的电脑连接时,我得到了这个:

mysql -u root -p -h 13.25.147.140
Enter password: 
ERROR 1045 (28000): Access denied for user 'root'@'57.26.24.157' (using password: YES)

我该如何解决这个问题?

【问题讨论】:

  • 这并不是一个真正的 Azure 问题,因为您似乎拥有可靠的连接性。您是否尝试过创建普通用户?您可以在linux.azure.david.betz.space/_/mariadb 上参考我的 Linux on Azure 指南以了解 MariaDB 的操作方法。我认为这是一个很好的下一步。

标签: mysql linux azure azure-virtual-machine


【解决方案1】:

默认情况下,mysql不允许root远程登录,这是一种安全预防措施。 如果你想使用root远程登录进行一些测试,我们可以使用这个命令来修改它:

[root@jasonvm etc]# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.1.73 Source distribution
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, user from user where user="root";
+-----------+------+
| host      | user |
+-----------+------+
| %         | root |
| 127.0.0.1 | root |
| localhost | root |
+-----------+------+
3 rows in set (0.00 sec)

mysql> grant all privileges on *.* to 'root'@'%' identified by 'password' with grant option;
Query OK, 0 rows affected (0.00 sec)

另一个使用root登录mysql的VM:

[root@localhost ~]# mysql -u root -p -h 40.71.33.231
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.1.73 Source distribution

Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MySQL [(none)]> 

【讨论】:

  • 我的数据库中有那个 'root@%'。
  • @VladMarkushin 请试试这个命令,1 使用 mysql 2 将 . 上的所有权限授予由 'password' 标识的 'root'@'%' 和授予选项;
  • @VladMarkushin 在我的测试中,选择结果和你一样,但是仍然不能远程使用root登录,我们应该将.上的所有权限授予'root'@ '%'。
  • 好的,我会用另一种方式告诉你。我的数据库中有 'root@%' 因为我之前已经使用过“将所有权限授予 .to 'root'@'%'”。
  • @VladMarkushin 在你授予所有权限之前,你应该选择“mysql”数据库,使用命令“use mysql;”
猜你喜欢
  • 2016-12-25
  • 1970-01-01
  • 2019-05-13
  • 2016-10-21
  • 1970-01-01
  • 2014-03-04
  • 2015-01-22
  • 2015-09-13
相关资源
最近更新 更多