iT邦幫忙

第 12 屆 iT 邦幫忙鐵人賽

DAY 6
1
自我挑戰組

30天菜鳥學 Linux 系列 第 6

6天-WSL2安裝MySQL

第六天 讀完 鳥哥的 Linux 私房菜 -- 第八章、檔案與檔案系統的壓縮,打包與備份

紀錄 創建文件方式

$:test.bar
$:cat > test.txt
$:echo '' > test.txt

今天嘗試使用 WSL2 安裝 docker MySQL 運行出現錯誤 「ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock'」

我根據這篇文章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>


補充小技巧 : 如何離開 mysql、docker 命令行
答案是 : exit

mysql> select 1
    -> ;
+---+
| 1 |
+---+
| 1 |
+---+
1 row in set (0.00 sec)
mysql> exit
Bye
root@74a7e237c26a:/# exit
exit
sa@DESKTOP:/mnt/c/Users/Test$

上一篇
5天-搞清楚為何 WSL2 需不需要 Windows Docker Desktop
下一篇
7天-搞懂多人多工
系列文
30天菜鳥學 Linux 59

尚未有邦友留言

立即登入留言