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

Why WSL2 connect mysql work for 127.0.0.1 but localhost not work?

为什么 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)

我的完整命令记录:

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)

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

Ubuntu 20.04 认为 localhost 是 127.0.0.1.

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

编辑:这不是一个完整的解决方案,因为 Windows 网络仍然会自动错误配置自身。当它停止工作时,我执行开始 -> 网络重置并像 1985 年一样重新启动我的计算机。