简体   繁体   English

远程连接 Mysql Ubuntu

[英]Remote Connections Mysql Ubuntu

For some reason, I've been unable to connect remotely to my MySQL server.出于某种原因,我一直无法远程连接到我的 MySQL 服务器。 I've tried everything and I'm still getting errors.我已经尝试了一切,但仍然出现错误。

root@server1:/home/administrator# mysql -u monty -p -h www.ganganadores.cl
Enter password:
ERROR 1045 (28000): Access denied for user 'monty'@'server1.ganganadores.cl' (using password: YES)

Now, I've tried running现在,我试过跑步

 GRANT ALL ON *.* to monty@localhost IDENTIFIED BY 'XXXXX'; 
 GRANT ALL ON *.* to monty@'%' IDENTIFIED BY 'XXXXXX';` 

and still nothing!还是什么都没有! What I'm doing wrong?我做错了什么?

EDIT : my.cnf has commented out the bind ip .编辑my.cnf已注释掉绑定 ip。

To expose MySQL to anything other than localhost you will have to have the following line要将 MySQL 公开给 localhost 以外的任何内容,您必须具有以下行

For mysql version 5.6 and below对于 mysql 5.6 及以下版本

uncommented in /etc/mysql/my.cnf and assigned to your computers IP address and not loopback/etc/mysql/my.cnf取消/etc/mysql/my.cnf并分配给您的计算机 IP 地址而不是环回

For mysql version 5.7 and above对于 mysql 5.7 及以上版本

uncommented in /etc/mysql/mysql.conf.d/mysqld.cnf and assigned to your computers IP address and not loopback/etc/mysql/mysql.conf.d/mysqld.cnf取消/etc/mysql/mysql.conf.d/mysqld.cnf并分配给您的计算机 IP 地址而不是环回

#Replace xxx with your IP Address 
bind-address        = xxx.xxx.xxx.xxx

Or add a bind-address = 0.0.0.0 if you don't want to specify the IP或者如果不想指定IP,则添加一个bind-address = 0.0.0.0

Then stop and restart MySQL with the new my.cnf entry.然后停止并使用新的 my.cnf 条目重新启动 MySQL。 Once running go to the terminal and enter the following command.运行后转到终端并输入以下命令。

lsof -i -P | grep :3306

That should come back something like this with your actual IP in the xxx's这应该会与您在 xxx 中的实际 IP 返回类似的内容

mysqld  1046  mysql  10u  IPv4  5203  0t0  TCP  xxx.xxx.xxx.xxx:3306 (LISTEN)

If the above statement returns correctly you will then be able to accept remote users.如果上述语句正确返回,您将能够接受远程用户。 However for a remote user to connect with the correct priveleges you need to have that user created in both the localhost and '%' as in.但是,要使远程用户使用正确的权限连接,您需要在 localhost 和 '%' 中都创建该用户。

CREATE USER 'myuser'@'localhost' IDENTIFIED BY 'mypass';
CREATE USER 'myuser'@'%' IDENTIFIED BY 'mypass';

then,然后,

GRANT ALL ON *.* TO 'myuser'@'localhost';
GRANT ALL ON *.* TO 'myuser'@'%';

and finally,最后,

FLUSH PRIVILEGES; 
EXIT;

If you don't have the same user created as above, when you logon locally you may inherit base localhost privileges and have access issues.如果您没有创建与上述相同的用户,当您在本地登录时,您可能会继承基本的 localhost 权限并遇到访问问题。 If you want to restrict the access myuser has then you would need to read up on the GRANT statement syntax HERE If you get through all this and still have issues post some additional error output and the my.cnf appropriate lines.如果您想限制 myuser 的访问权限,那么您需要在此处阅读 GRANT 语句语法。

NOTE: If lsof does not return or is not found you can install it HERE based on your Linux distribution.注意:如果 lsof 没有返回或未找到,您可以根据您的 Linux 发行版在此处安装它。 You do not need lsof to make things work, but it is extremely handy when things are not working as expected.您不需要 lsof 来使事情正常工作,但是当事情没有按预期工作时它非常方便。

UPDATE: If even after adding/changing the bind-address in my.cnf did not work, then go and change it in the place it was originally declared:更新:如果即使在my.cnf添加/更改bind-address也不起作用,那么请在最初声明的位置更改它:

/etc/mysql/mariadb.conf.d/50-server.cnf

Add few points on top of apesa's excellent post:在 apesa 的优秀帖子之上补充几点:

1) You can use command below to check the ip address mysql server is listening 1)您可以使用下面的命令来检查mysql服务器正在侦听的IP地址

netstat -nlt | grep 3306

sample result:

tcp 0  0  xxx.xxx.xxx.xxx:3306  0.0.0.0:*   LISTEN

2) Use FLUSH PRIVILEGES to force grant tables to be loaded if for some reason the changes not take effective immediately 2) 如果由于某种原因更改没有立即生效,则使用FLUSH PRIVILEGES强制加载授权表

GRANT ALL ON *.* TO 'user'@'localhost' IDENTIFIED BY 'passwd' WITH GRANT OPTION;
GRANT ALL ON *.* TO 'user'@'%' IDENTIFIED BY 'passwd' WITH GRANT OPTION;
FLUSH PRIVILEGES; 
EXIT;

user == the user u use to connect to mysql ex.root用户 == 用于连接到 mysql ex.root 的用户
passwd == the password u use to connect to mysql with passwd == 你用来连接到 mysql 的密码

3) If netfilter firewall is enabled ( sudo ufw enable ) on mysql server machine, do the following to open port 3306 for remote access: 3) 如果在 mysql 服务器机器上启用了 netfilter 防火墙( sudo ufw enable ),请执行以下操作以打开端口 3306 以进行远程访问:

sudo ufw allow 3306

check status using检查状态使用

sudo ufw status

4) Once a remote connection is established, it can be verified in either client or server machine using commands 4) 一旦建立了远程连接,就可以使用命令在客户端或服务器机器上进行验证

netstat -an | grep 3306
netstat -an | grep -i established

MySQL only listens to localhost, if we want to enable the remote access to it, then we need to made some changes in my.cnf file: MySQL 只监听 localhost,如果我们想要远程访问它,那么我们需要在 my.cnf 文件中做一些更改:

sudo nano /etc/mysql/my.cnf

We need to comment out the bind-address and skip-external-locking lines:我们需要注释掉 bind-address 和 skip-external-locking 行:

#bind-address = 127.0.0.1
# skip-external-locking

After making these changes, we need to restart the mysql service:进行这些更改后,我们需要重新启动 mysql 服务:

sudo service mysql restart

You are using ubuntu 12 (quite old one)您正在使用 ubuntu 12(相当旧的)

First, Open the /etc/mysql/mysql.conf.d/mysqld.cnf file ( /etc/mysql/my.cnf in Ubuntu 14.04 and earlier versions首先,打开/etc/mysql/mysql.conf.d/mysqld.cnf文件( Ubuntu 14.04及更早版本中为/etc/mysql/my.cnf

Under the [mysqld] Locate the Line, bind-address = 127.0.0.1 And change it to, bind-address = 0.0.0.0 or comment it在 [mysqld] 下找到那行,bind-address = 127.0.0.1 并改成,bind-address = 0.0.0.0 或者注释掉

Then, Restart the Ubuntu MysQL Server systemctl restart mysql.service然后,重启 Ubuntu MySQL Server systemctl restart mysql.service

Now Ubuntu Server will allow remote access to the MySQL Server, But still you need to configure MySQL users to allow access from any host.现在 Ubuntu Server 将允许远程访问 MySQL 服务器,但您仍然需要配置 MySQL 用户以允许从任何主机访问。

User must be 'username'@'%' with all the required grants用户必须是'username'@'%'并具有所有必需的授权

To make sure that, MySQL server listens on all interfaces, run the netstat command as follows.为确保 MySQL 服务器侦听所有接口,请按如下方式运行 netstat 命令。

netstat -tulnp | grep mysql

Hope this works !希望这有效!

如果在 Windows 上测试,不要忘记打开端口 3306。

In my case I was using MySql Server version: 8.0.22就我而言,我使用的是 MySql Server 版本:8.0.22

I had to add我不得不添加

bind-address        = 0.0.0.0

and change this line to be并将此行更改为

mysqlx-bind-address = 0.0.0.0

in file at /etc/mysql/mysql.conf.d在 /etc/mysql/mysql.conf.d 的文件中

then restart MySQL by running然后通过运行重新启动MySQL

sudo service mysql restart

I was facing the same problem when I was trying to connect Mysql to a Remote Server.当我尝试将 Mysql 连接到远程服务器时,我遇到了同样的问题。 I had found out that I had to change the bind-address to the current private IP address of the DB server.我发现我必须将绑定地址更改为数据库服务器的当前私有 IP 地址。 But when I was trying to add the bind-address =0.0.0.0 line in my.cnf file, it was not understanding the line when I tried to create a DB.但是当我尝试在my.cnf文件中添加bind-address =0.0.0.0行时,当我尝试创建数据库时它不理解该行。

Upon searching, I found out the original place where bind-address was declared.经过搜索,我找到了声明 bind-address 的原始位置。

The actual declaration is in : /etc/mysql/mariadb.conf.d/50-server.cnf实际声明在: /etc/mysql/mariadb.conf.d/50-server.cnf

Therefore I changed the bind-address directly there and then all seems working.因此我直接在那里更改了绑定地址,然后一切似乎都在工作。

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

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