为什么WSL2 connect mysql适用于127.0.0.1,但localhost不起作用?

时间:2020-09-09 05:40:00

标签: docker wsl-2

为什么WSL2连接mysql对于127.0.0.1起作用,但是localhost不起作用?

如果我安装mysql映像并创建容器并使用$ mysql -h localhost -u root -p运行它,系统将重新运行ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock'

但是当我使用$ mysql -h 127.0.0.1 -u root -p时就可以使用。

$ mysql -h 127.0.0.1 -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 8.0.21 MySQL Community Server - GPL

Copyright (c) 2000, 2020, 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> select 1
    -> ;
+---+
| 1 |
+---+
| 1 |
+---+
1 row in set (0.00 sec)

我的完整cmd记录:

sa@DESKTOP:/mnt/c/Users/TesT$ docker pull mysql
Using default tag: latest
latest: Pulling from library/mysql
bf5952930446: Pull complete
8254623a9871: Pull complete
938e3e06dac4: Pull complete
ea28ebf28884: Pull complete
f3cef38785c2: Pull complete
894f9792565a: Pull complete
1d8a57523420: Pull complete
6c676912929f: Pull complete
3cdd8ff735c9: Pull complete
4c70cbe51682: Pull complete
e21cf0cb4dc3: Pull complete
28c36cd3abcc: Pull complete
Digest: sha256:6ded54eb1e5d048d8310321ba7b92587e9eadc83b519165b70bbe47e4046e76a
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest

sa@DESKTOP:/mnt/c/Users/TesT$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
mysql               latest              3646af3dc14a        4 days ago          544MB

sa@DESKTOP:/mnt/c/Users/TesT$ docker run -itd --name mysql-test -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 mysql
74a7e237c26a96f069092e92a80d4e0c399150998723ea11cc37ddeea4d65ae3

sa@DESKTOP:/mnt/c/Users/TesT$ sudo apt install mysql-client-core-8.0
[sudo] password for sa:
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
  bridge-utils dns-root-data dnsmasq-base libidn11 ubuntu-fan

Use 'sudo apt autoremove' to remove them.
The following NEW packages will be installed:
  mysql-client-core-8.0
0 upgraded, 1 newly installed, 0 to remove and 45 not upgraded.
Need to get 4207 kB of archives.
After this operation, 64.7 MB of additional disk space will be used.
Get:1 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 mysql-client-core-8.0 amd64 8.0.21-0ubuntu0.20.04.4 [4207 kB]
Fetched 4207 kB in 10s (410 kB/s)
Selecting previously unselected package mysql-client-core-8.0.
(Reading database ... 32206 files and directories currently installed.)
Preparing to unpack .../mysql-client-core-8.0_8.0.21-0ubuntu0.20.04.4_amd64.deb ...
Unpacking mysql-client-core-8.0 (8.0.21-0ubuntu0.20.04.4) ...
Setting up mysql-client-core-8.0 (8.0.21-0ubuntu0.20.04.4) ...
Processing triggers for man-db (2.9.1-1) ...

sa@DESKTOP:/mnt/c/Users/TesT$ mysql -h localhost -u root -p
Enter password:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

1 个答案:

答案 0 :(得分:1)

Windows认为本地主机是::1 IPv6回送。

Ubuntu 20.04认为本地主机是127.0.0.1

它们是不同的地址,这就是为什么该名称不起作用的原因。您可以为“所有网络适配器”将服务器绑定到0.0.0.0,其中将同时包括ipv4和ipv6。

相关问题