简体   繁体   English

无法使用 localhost 从 vps 连接到 MySQL 数据库

[英]Unable to connect to MySQL database from vps using localhost

I have two environments: my local machine Mac and linux on VPS.我有两个环境:我的本地机器 Mac 和 VPS 上的 linux。 The problem is that I can not connect using localhost to my db from VPS.问题是我无法使用 localhost 从 VPS 连接到我的数据库。 On my local machine everything works fine.在我的本地机器上一切正常。

Local:当地的:

mysql -u root mydb -h 127.0.0.1 --password=password --port=2345 //OK
mysql -u root mydb -h 0.0.0.0 --password=password --port=2345   //OK
mysql -u root mydb -h localhost --password=password --port=2345 //OK

Even using public ip of VPS I'm able to connect to remote db from local machine.即使使用 VPS 的公共 ip 我也可以从本地机器连接到远程数据库。

VPS:虚拟主机:

mysql -u root mydb -h 127.0.0.1 --password=password --port=2345 //OK
mysql -u root mydb -h 0.0.0.0 --password=password --port=2345   //OK
mysql -u root mydb -h localhost --password=password --port=2345 //KO <-

Error:错误:

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

I used the following docker file to create my image.我使用以下 docker 文件来创建我的图像。

DockerFile: DockerFile:

FROM mysql:8.0
COPY *.sql /docker-entrypoint-initdb.d/

docker-compose.yaml: docker-compose.yaml:

version: '3.7'
services:
  ls:
    image: ***/db-container:v6 # <-my image
    container_name: db-container
    restart: always
    environment:
      MYSQL_DATABASE: 'mydb'
      MYSQL_USER: 'user'
      MYSQL_PASSWORD: 'password'
      MYSQL_ROOT_PASSWORD: 'password'
    ports:
      # <Port exposed> : < MySQL Port running inside container>
      - '2345:3306'
    expose:
      # Opens port 3306 on the container
      - '3306'
    volumes:
      - db-container:/var/lib/mysql
volumes:
  db-container:

Query inside container:查询容器内:

mysql> SELECT host, user FROM mysql.user;
+-----------+------------------+
| host      | user             |
+-----------+------------------+
| %         | root             |
| %         | user             |
| localhost | mysql.infoschema |
| localhost | mysql.session    |
| localhost | mysql.sys        |
| localhost | root             |
+-----------+------------------+


mysql> SELECT * FROM performance_schema.host_cache; 
Empty set (0.02 sec)

"localhost" says to use a socket. “localhost”表示使用套接字。 But VPS probably allows only TCP/IP connections, not "socket".但是 VPS 可能只允许 TCP/IP 连接,而不是“套接字”。

I guess on local machine, you are not using docker.我猜在本地机器上,您没有使用 docker。 However, on vps, you are.但是,在 vps 上,你是。

On you local, mysql is installed on the OS itself, hence, it has exposed its socket file at /var/run/mysqld/mysqld.sock.在您本地,mysql 安装在操作系统本身上,因此,它在 /var/run/mysqld/mysqld.sock 中公开了其套接字文件。 However, on VPS, the socket is inside docker.但是,在 VPS 上,套接字位于 docker 内部。

The highlight here is the difference between localhost and 127.0.0.1 in reference to mysql.这里的重点是localhost127.0.0.1在参考 mysql 方面的区别。 On unix based systems(including mac), localhost connects via socket, while 127.0.0.1 connects via TCP/IP.在基于 unix 的系统(包括 mac)上, localhost通过套接字连接,而127.0.0.1通过 TCP/IP 连接。

Try adding following under docker-compose volumes section.尝试在 docker-compose 卷部分下添加以下内容。 See if it works then.看看它是否有效。

volumes:
    - /var/run/mysqld/mysqld.sock:/var/run/mysqld/mysqld.sock

There are many different root causes for a connection denied.连接被拒绝有许多不同的根本原因。

Since it is possible to execute queries inside the container, the best course of action is to inspect the performance_schema.host_cache table:由于可以在容器内执行查询,因此最好的做法是检查performance_schema.host_cache表:

SELECT * FROM performance_schema.host_cache;

It should point more precisely at the problem.它应该更准确地指出问题所在。

Doc: https://dev.mysql.com/doc/mysql-perfschema-excerpt/8.0/en/host-cache-table.html文档: https://dev.mysql.com/doc/mysql-perfschema-excerpt/8.0/en/host-cache-table.ZFC35FDC70D5FC69D269883A822C7A53E

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

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