简体   繁体   English

无法将远程用户连接到MySQL(托管在Ubuntu VPS上)

[英]Can't connect with remote user to MySQL (hosted on Ubuntu VPS)

I am trying to setup a remote user for my MySQL db so I can access it from any IP. 我正在尝试为我的MySQL数据库设置一个远程用户,以便可以从任何IP访问它。 The MySQL server is hosted on a Ubuntu VPS server. MySQL服务器托管在Ubuntu VPS服务器上。

I already created a remote user as you can see here: 您已经在这里看到了,我已经创建了一个远程用户:

login as: root
root@xx.xx.xx's password:
Welcome to Ubuntu 16.04.3 LTS (GNU/Linux 4.4.0-108-generic x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

  Get cloud support with Ubuntu Advantage Cloud Guest:
    http://www.ubuntu.com/business/services/cloud

82 packages can be updated.
42 updates are security updates.


Last login: Fri Mar  2 19:47:15 2018 from xxxxxxx
root@vpsxxxxxx:~# mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 5.7.21-0ubuntu0.16.04.1 (Ubuntu)

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

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

mysql> SHOW variables WHERE Variable_name = 'hostname' OR Variable_name =     'port';
+---------------+-----------+
| Variable_name | Value     |
+---------------+-----------+
| hostname      | vps520749 |
| port          | 3306      |
+---------------+-----------+
2 rows in set (0.01 sec)

mysql> SELECT Host,User from mysql.user;
+-----------+------------------+
| Host      | User             |
+-----------+------------------+
| %         | user             |
| localhost | debian-sys-maint |
| localhost | mysql.session    |
| localhost | mysql.sys        |
| localhost | root             |
+-----------+------------------+
5 rows in set (0.00 sec)

mysql>

But once I try to login with the user user via Navicat or MySQL Workbench I get this error: 但是一旦我尝试通过Navicat或MySQL Workbench用用户user登录,就会收到此错误:

Navicat中的错误消息

Even if I try to connect to the SQL server over SSH I still get an error: 即使我尝试通过SSH连接到SQL Server,我仍然会收到错误消息:

使用SSH的MySQL Workbench中的错误

Btw here my settings for the connection: 在这里我的连接设置:

Navi获取连接设置

在此处输入图片说明

What am I missing here? 我在这里想念什么?

Perhaps a firewall setting is not allowing access to port 3306? 也许防火墙设置不允许访问端口3306? You can check this from the command prompt to see if it is a network problem or an application issue by connecting to the port with a utility such as netcat (nc). 您可以通过使用实用程序(例如netcat(nc))连接到端口,从命令提示符处检查它是网络问题还是应用程序问题。

For example, from the machine running the Navicat/MySQLWorkbench, connecting to the database server with up address 192.168.0.100: 例如,从运行Navicat / MySQLWorkbench的计算机连接到地址为192.168.0.100的数据库服务器:

user@server1:~> nc -zvn 192.168.0.100 3306
nc: connect to 192.168.0.100 port 3306 (tcp) failed: Connection refused
user@server1:~>
-----
user@server1:~> nc -zvn 192.168.0.100 3306
Connection to 192.168.0.100 3306 port [tcp/*] succeeded!
user@server1:~>

To connect by the server's hostname, leave out the -n option: 要通过服务器的主机名进行连接,请省略-n选项:

You might have a possible problem with remote privileges. 您可能对远程特权有问题。 If that is the case, open MySQL on the command line and enter this in: 如果是这种情况,请在命令行上打开MySQL,然后在以下命令中输入:

GRANT ALL PRIVILEGES
ON my_database_name.*
TO 'user'@'%'
IDENTIFIED BY 'new_remote_password';
FLUSH PRIVILEGES;

'%' is a wildcard, so the TO line says that a username 'user' with any IP address has access. '%'是通配符,因此TO行表示具有任何IP地址的用户名'user'可以访问。 But they'll need the password new_remote_password . 但他们需要密码new_remote_password And as you can see, the ON line limits this access to one database, my_database_name. 如您所见, ON行将这种访问限制为对一个数据库my_database_name的访问。

And there are some obvious security risks by doing this, so make sure you know what you are doing in that regard. 这样做有一些明显的安全隐患,因此请确保您知道这方面的工作。

Here are some other possible fixes: 以下是一些其他可能的修复:

Change a line or two in the config file. 在配置文件中更改一两行。 Typically the config file is at /etc/mysql/my.cnf or /etc/mysql/mysql.conf.d/mysqld.cnf on Ubuntu. 通常,配置文件位于Ubuntu上的/etc/mysql/my.cnf/etc/mysql/mysql.conf.d/mysqld.cnf上。 Get rid of or comment out the lines that say 摆脱或注释掉那些说

bind-address = 127.0.0.1  

and

skip-networking 

You'll want to restart MySQL then, too, with sudo service mysql restart on the command line. 您也将需要使用命令行中的sudo service mysql restart来重新启动MySQL。

If those don't work, it could be a firewall or port problem, as suggested in another answer. 如果这些都不起作用,则可能是防火墙或端口问题,如另一个答案所建议。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM