ChatGPT解决这个技术问题 Extra ChatGPT

Installing MySQL in Docker fails with error message "Can't connect to local MySQL server through socket"

I'm trying to install mysql inside a docker container,Tried various images from github, it seems they all manage to successfully install the mysql but when I try to run the mysql it gives an error:

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock'

System specifications:

Ubuntu 12,04 its on AWS

Docker 0.10.0

Packages I tried so far:

https://github.com/eugeneware/docker-wordpress-nginx

https://github.com/tutumcloud/tutum-docker-mysql

Is the mysql service running? Services do not automatically run within containers. They must be explicitly started
Yeah I know that,but if you check out the first link, there is a start file, in which will run all of the services, once the container is loaded.

o
odk

Remember that you will need to connect to running docker container. So you probably want to use tcp instead of unix socket. Check output of docker ps command and look for running mysql containers. If you find one then use mysql command like this: mysql -h 127.0.0.1 -P <mysql_port> (you will find port in docker ps output). If you can't find any running mysql container in docker ps output then try docker images to find mysql image name and try something like this to run it: docker run -d -p 3306:3306 tutum/mysql where "tutum/mysql" is image name found in docker images.


Running mysql command with --protocol=tcp will make client to connect through "tcp". Full command would look like: mysql -h localhost -P 3306 --protocol=tcp
In Unix based systems, MySQL will assume you want to use a socket if you connect to the host "localhost". Here you have a complete answer how to resolve this problem: serverfault.com/a/337928/467190
Yes! Changing localhost to 127.0.0.1 solved the problem. Thx!
the -h ip that worked for me was the one in ifconfig docker0
I spent 5 hours refusing to do an easy local install (20 minutes sure) of mysql just to use it with docker, and found that --protocol=tcp does the magic. i'm happy and stressed, thx
M
Mega

I had the same problem, in fact, I juste forgot to run the service after installation ..

Start mysql server :

/etc/init.d/mysql start

The question is about connecting to a docker container not about running mysql locally.
Thank you Mega! Even though you did not answer the PRECISE QUESTION, you solved my problem!!
b
benftwc

Don't know how do i achieve this, but, i've be able to reach MYSQL by typing

$ mysql -u root -h

mywebsite:
  image: benftwc/pldev-webserver
  volumes:
    - ./mywebsite.fr/:/var/www/
  working_dir: /var/www/
  ports:
    - "8009:8009"
  command: php -S 0.0.0.0:8009
  links:
    - database
database:
  image: library/mysql
  environment:
    MYSQL_ROOT_PASSWORD: root
  ports:
    - "3310:3306

root@422f4d1f454a:/# mysql -u root -h 127.0.0.1 -p3310
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

root@422f4d1f454a:/# mysql -u root -h database -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g...........

That looks something new, I'm going to try it. Thanks for the answer.
I've try so many things, can't explain how that work. I think it create a new host based on "links" param
with your first try you cannot connect, because you have to put -p instead of -P and -h 127.0.0.1 instead of -h 0.0.0.0. Then, in the second command it works, because you don't give up a port in your command, which then defaults to 3306.
a
aiven

In my case I tried to connect to DB (which was inside docker) like this:

mysql -ppass -u root

but got same error as OP.

Specifying host and port helped:

mysql --host 0.0.0.0 --port 3306 -ppass -u root

D
Dave Ranjan

I might be little late for answer and probably world knows about this now.

All you have to open your ports of docker container to access it. For example while running the container :

docker run --name mysql_container -e MYSQL_ROOT_PASSWORD=root -d -p 3306:3306 mysql/mysql-server:5.7

This will allow your container's mysql to be accessible from the host machine. Later you can connect to it.

docker exec -it mysql_container mysql -u root -p


J
Jeremy Jumeau

If you don't have MySQL installed on your host, you have to execute it in the container (https://docs.docker.com/engine/reference/commandline/exec/#/examples gives explanation about docker run vs docker exec).

Considering your container is running, you might use docker exec yourcontainername mysql -u root -p to access to the client.

Also, if you are using Docker Compose, and you've declared a mysql db service named database, you can use : docker-compose exec database mysql -u root -p


l
l3x

Assuming you're using docker-compose, where your docker-compose.yml file looks like:

version: '3.7'
services:
  mysql_db_container:
    image: mysql:latest
    command: --default-authentication-plugin=mysql_native_password
    environment:
      MYSQL_ROOT_PASSWORD: rootpassword
    ports:
      - 3307:3306
    volumes:
      - mysql_db_data_container:/var/lib/mysql


  web:
    image: ${DOCKER_IMAGE_NAME-eis}:latest
    build:
      context: .
    links:
      - mysql_db_container
    ports:
      - 4000:3000
    command: ["./scripts/wait-for-it.sh", "mysql_db_container:3306", "--", "./scripts/start_web_server.sh"]
    volumes:
      - .:/opt/eis:cached
    env_file:
      - .env

volumes:
  mysql_db_data_container:

Notice the ports definition for mysql_db_container

    ports:
      - 3307:3306

<= That indicates that mysql will be accessible via port 3307 to the localhost workstation and via port 3306 within the docker net

Run the following to see your container names:

$ dc config --services
mysql_db_container
web

In this case, we have two containers.

Errors

If you connect to mysql_db_container from your localhost workstation and try to access the mysql console there, you'll get that error:

docker-compose run mysql_db_container bash
root@8880ffe47962:/# mysql -u root -p
Enter password: 
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
root@8880ffe47962:/# exit

Also, if you try to connect from your local workstation, you'll also get that error:

$ mysql -u root -p -P 3307
Enter password: 
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

Solutions

Connecting from local workstation

Just add the --protocol=tcp parameter (otherwise mysql assumes you want to connect via the mysql socket):

$ mysql --protocol=tcp -u root -p -P 3307
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>

Connecting from web container

Reference the docker hostname -h mysql_db_container. Note that when you're running within the context of Docker that the TCP protocol is assumed.

$ dc run web bash
Starting eligibility-service_mysql_db_container_1_d625308b5a77 ... done
root@e7852ff02683:/opt/eis# mysql -h mysql_db_container -u root -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 18
Server version: 8.0.21 MySQL Community Server - GPL

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MySQL [(none)]> 

Connecting from mysql container

Assuming your mysql container name is eis_mysql_db_container_1_d625308b5a77 (that you can see when running docker ps), the following should work:

$ docker exec -it eis_mysql_db_container_1_d625308b5a77 bash
root@3738cf6eb3e9:/# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 19
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> 

p
prograils

Check out what's in your database.yml file. If you already have your plain Rails app and simply wrapping it with Docker, you should change (inside database.yml):

socket: /var/run/mysqld/mysqld.sock #just comment it out

to

host: db 

where db is the name of my db-service from docker-compose.yml. And here's my docker-compose.yml:

version: '3'
services:
  web:
    build: .
    command: bundle exec rails s -p 3000 -b '0.0.0.0'
    volumes:
      - .:/myapp
    ports:
      - "3000:3000"
    links:
      - db
  db:
    image: mysql:5.7
    environment:
      MYSQL_ROOT_PASSWORD: root

You start your app in console (in app folder) as docker-compose up. Then WAIT 1 MINUTE (let your mysql service to completely load) until some new logs stop appearing in console. Usually the last line should be like

db_1 | 2017-12-24T12:25:20.397174Z 0 [Note] End of list of non-natively partitioned tables

Then (in a new terminal window) apply:

docker-compose run web rake db:create

and then

docker-compose run web rake db:migrate

After you finish your work stop the loaded images with

docker-compose stop

Don't use docker-compose down here instead because if you do, you will erase your database content.

Next time when you want to resume your work apply:

docker-compose start

The rest of the things do exactly as explained here: https://docs.docker.com/compose/rails/


b
benftwc

Months after this question, I've levelup my Docker skills. I should use Docker container name instead.

That use dokerized-nginx as bridge to expose ip+port of the container.

Within WEB configuration, I now use mysql://USERNAME:PASSWORD@docker_container_name/DB_NAME to access to Mysql socket through docker (also works with docker-compose, use compose-name instead of container one)


H
Hashan Kanchana

Specifying the host as 0.0.0.0 did work for me.

I created docker container using below command

docker run --detach --name=mysql --env="MYSQL_ROOT_PASSWORD=root" --publish 3306:3306 mysql

Then installed mysql client

sudo apt-get install mysql-client

Connected to mysql via terminal using below command

mysql --host 0.0.0.0 --port 3306 -proot -u root

B
Bjartmar Kristjansson

My problem was that I was trying to connect from a version of mysql client that seems to be incompatible with the mysql server I installed (mysql:latest which installed version 8.0.22 at the time of this writing).

my mysql client version:

$ mysql --version
mysql  Ver 14.14 Distrib 5.7.26, for Linux (x86_64) using  EditLine wrapper

The docker command that I used to install mysql:latest:

$ docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=somerootpassword -e MYSQL_USER=someuser -e MYSQL_PASSWORD=someuserpassword -d -p 3306:3306 mysql:latest

The errors I got when connecting from my local mysql client to the mysql server:

$ mysql -u someuser -p -h 127.0.0.1
ERROR 2026 (HY000): SSL connection error: unknown error number

(sometimes I would get another error: "ERROR 2013 (HY000): Lost connection to MySQL server at 'reading initial communication packet', system error: 2". But I think this happens when I try to connect to the server too early after I started it)

My solution was to install mysql:5.7 instead:

$ docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=somerootpassword -e MYSQL_USER=someuser -e MYSQL_PASSWORD=someuserpassword -d -p 3306:3306 mysql:5.7

and then I can connect to the server (after waiting perhaps 1 minute until the server is ready to accept connections):

$ mysql -u someuser -p -h 127.0.0.1

Z
ZiGiUs

For me it was simply a matter of restarting the docker daemon..


U
USMAN FAZIL

I ran into the same issue today, try running ur container with this command.

docker run --name mariadbtest -p 3306:3306 -e MYSQL_ROOT_PASSWORD=mypass -d mariadb/server:10.3

R
Rafal

use ip address '127.0.0.1' instead hostname 'localhost'


M
MD SHAYON

Basically, it takes some time to start after pulling MySQL. Make sure you tried after a few minutes (maybe 2-3 minutes).


M
Marvo

To connect to mysql from your machine use 127.0.0.1

To connect to mysql-container from inside another container you need to figure out the internal ip.

docker inspect mysqlcontainername | grep Gateway