Cant connect to local mysql server through socket /var/run/mysqld/mysqld.sock docker

0

Docker mysql: ERROR 2002 (HY000): no se puede conectar al servidor MySQL local a través del socket '/var/run/mysqld/mysqld.sock' (2)

Estoy tratando de conectarme al servidor mysql usando mysql cli. La imagen fue creada usando el siguiente comando:

docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=****** -d mysql

El contenedor acoplable se está ejecutando . docker ps imprime:

johnd@john-pc:~$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 598c0f8680dc mysql "docker-entrypoint.s…" 4 minutes ago Up 4 minutes 3306/tcp, 33060/tcp some-mysql

Cuando ingreso el siguiente comando: mysql -h 127.0.0.1 -P 3306 -u root -p devuelve:

ERROR 2003 (HY000): Can't connect to MySQL server on '127.0.0.1' (111)

También probé con el atributo --protocol=tcp . ¿Cómo me conecto desde el cliente al servidor mysql que se ejecuta en la ventana acoplable usando la terminal de la máquina cliente (no desde otra ventana acoplable)?

EDITAR: También intenté conectarme a mysql usando este comando:

docker run -it --rm mysql mysql -h227.0.0.1 -uexample-user -p

me devuelve el mismo error

4 months ago ·

Santiago Trujillo

0

mysql -h 127.0.0.1 -P 3306 -u raíz -p

Se está conectando al servidor sql de su localhost pero no asignó el puerto del contenedor de la ventana acoplable al host. Intente mapear el puerto a su host con esto:

docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=****** -d -p 3306:3306 mysql

Luego vuelve a intentarlo:

mysql -h 127.0.0.1 -P 3306 -u raíz -p

4 months ago · Santiago Trujillo Report

0

el mismo error para mí, solo recuerde iniciar el servicio mysql primero en su contenedor.

exec en su contenedor

/etc/init.d/mysql start

4 months ago · Santiago Trujillo Report

0

El uso de la IP interna de la ventana acoplable como 127.0.0.1 no funcionó para mí

Usé la IP de mi POD y funcionó

mysql -h 10.10.10.41 -P 3306 -u root -p

Todavía no entiendo por qué no pude iniciar sesión y antes y después de este comando pude iniciar sesión

4 months ago · Santiago Trujillo Report

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

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

Cant connect to local mysql server through socket /var/run/mysqld/mysqld.sock docker

以下是完整的過程 :

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 是空的

Cant connect to local mysql server through socket /var/run/mysqld/mysqld.sock docker

嘗試解決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>

Can't connect to local MySQL server through socket Kubernetes?

How to Fix 'Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)'.
Method 1: Check the MySQL Service..
Method 2: Verify the mysqld.sock Location..
Method 3: Check the MySQL Folder Permission..
Method 4: Check for Multiple MySQL Instances..

How do I connect to a MySQL docker container?

Here are the steps you can follow to install the Dockerhub MySQL Container: Step 1: Pull the Docker Image for MySQL. Step 2: Deploy and Start the MySQL Container. Step 3: Connect with the Docker MySQL Container.

Can't connect to local MySQL server?

normally means that there is no MySQL server running on the system or that you are using an incorrect Unix socket file name or TCP/IP port number when trying to connect to the server. You should also check that the TCP/IP port you are using has not been blocked by a firewall or port blocking service.

Can't connect to local server through socket '/ tmp MySQL sock?

It means either the MySQL server is not installed/running, or the file mysql. sock doesn't exist in /var/lib/mysql/ . There are a couple of solutions for this error. Then try to connect again.