iT邦幫忙

1

WSL2 安裝 docker MySQL 運行出現錯誤 「ERROR 2002 (HY000): Can't connect to local MySQL server...'」

  • 分享至 

  • xImage

我根據這篇文章Docker 安装 MySQL | 菜鸟教程在 WSL2 安裝 MySQL

結果出現 ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2) 錯誤

image

以下是完整的過程 :

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 : vi /var/run/mysqld/mysqld.sock 是空的

image

嘗試解決1 : google 搜尋錯誤訊息, 找到 https://stackoverflow.com/a/15039113/9131476 , 下指令sudo apt-get install mysql-server 安裝
結果 : 不行

嘗試解決2 : 使用 docker exec -it mysql bash 進去容器內執行命令

結果 : 可以驗證容器沒問題,但還是沒有解決外部不能連結問題

sa@DESKTOP:/mnt/c/Users/Test$ docker exec -it mysql bash
Error: No such container: mysql
sa@DESKTOP:/mnt/c/Users/Test$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                               NAMES
74a7e237c26a        mysql               "docker-entrypoint.s…"   21 minutes ago      Up 21 minutes       0.0.0.0:3306->3306/tcp, 33060/tcp   mysql-test
sa@DESKTOP:/mnt/c/Users/Test$ docker exec -it 74a7e237c26a bash
root@74a7e237c26a:/# mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
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)

嘗試3 : 按照 cshalove 大神的方式

因為wsl2 裡面 mysql client 要連到 mysqld.sock ,但mysqld.sock 只存在docker container中
所以你wsl2環境要連到 mysql 要 使用IP連線,如下 -h 127.0.0.1

結果 : 成功解決問題

$ 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)

mysql>

cshalove iT邦新手 4 級 ‧ 2020-09-09 12:12:33 檢舉
因為wsl2 裡面 mysql client 要連到 mysqld.sock ,但mysqld.sock 只存在docker container中
所以你wsl2環境要連到 mysql 要 使用IP連線,如下 -h 127.0.0.1
感謝,請問能底下留言嗎?
大神您的回答解決我的問題
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

2 個回答

4
cshalove
iT邦新手 4 級 ‧ 2020-09-09 13:48:19
最佳解答

因為wsl2 裡面 mysql client 要連到 mysqld.sock ,
但mysqld.sock 只存在docker container中

所以你wsl2環境要連到 mysql 要 使用IP連線,如下 -h 127.0.0.1

1

給你一個觀念。

docker你要將其視為一種虛擬機。
也就是另外一台機器。

所以你想要外部連結就得要用外部連結的方式。

127.0.0.1是一種很神奇的ip。盡量不要這樣連結。
它並不一定會連結到你的虛擬機上。除非你有設定port導向來處理。

感謝大神
可是這邊有疑問 WSL2 最大標榜的功能不就是可以 localhost 訪問嗎?

你要了解一下轉發的應用原理。
可以用127.0.0.1沒錯,但盡量不要這樣連結。
正常也因該會分配一個內網ip才對。盡量用那個內部ip。
才不會出現一些奇奇怪怪的問題。

感謝!

我要發表回答

立即登入回答