Installing MySQL in Docker fails with the error message "Cannot connect to local MySQL server via socket"

I try to install mysql inside the docker container, tried various images from github, it seems they all manage to successfully install mysql, but when I try to start mysql, it gives an error:

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

System Features:

  • Ubuntu 12.04 on AWS
  • Docker 0.10.0

The packages I've tried so far:

+8
mysql docker
source share
6 answers

Remember that you will need to connect to the working docker container. Therefore, you probably want to use tcp instead of a unix socket. Check the output of the docker ps and find the running mysql containers. If you find it, use the mysql command as follows: mysql -h 127.0.0.1 -P <mysql_port> (you will find the port in docker ps output). If you cannot find any running mysql container in the output of docker ps , try docker images find the name of the mysql image and try something like this to run it: docker run -d -p 3306:3306 tutum/mysql where "tutum / mysql "is the name of the image found in docker images .

+17
source share

I don’t know how to achieve this, but I can achieve 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........... 
+8
source share

I had the same problem, in fact, I just forgot to start the service after installation.

Start mysql server:

 /etc/init.d/mysql start 
+5
source share

If you do not have MySQL on your host, you should execute it in the container ( https://docs.docker.com/engine/reference/commandline/exec/#/examples gives an explanation of docker run vs docker exec).

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

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

+3
source share

Maybe I'm a little late for the answer, and probably the world knows about it now.

All you need to open your docker container ports is to access it. For example, when starting a container:

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

This will allow your mysql container to be accessible from the host machine. You can connect to it later.

docker exec -it mysql_container mysql -u root -p

0
source share

Check that in your database.yml file. If you already have your Rails application and just wrap 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 is 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 run your application on the console (in the application folder) as docker-compose up . Then WAIT 1 MINUTE (let your mysql service be fully loaded) until new logs appear in the console. Usually the last line should be like

db_1 | 2017-12-24T12: 25: 20.397174Z 0 [Note] The end of the list of the table without partitioning

Then (in a new terminal window) are applied:

 docker-compose run web rake db:create 

and then

 docker-compose run web rake db:migrate 

When finished, stop the downloaded images with

 docker-compose stop 

Do not use docker-compose down , but because if you do, you will delete the contents of your database.

The next time you want to resume your work, follow these steps:

 docker-compose start 

The rest of the stuff does what is explained here: https://docs.docker.com/compose/rails/

0
source share

All Articles