Error start mysql docker

℡╲_俬逩灬. 提交于 2020-01-25 04:32:08

问题


I am creating an image in the docker to the mysql install mode, but it is giving error to start mysql.

My Dockerfile

The error occurs when processing and line 25 of Dockerfile:

Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (111)

I found that the error occurs because mysql is not running. The docker print below to rotate the line 22:

bin boot dev etc home lib lib64 media mnt opt proc root run sbin scripts srv sys tmp usr var MySQL is stopped.

Stack trace complete

Any suggestion?


回答1:


The docker daemon will execute RUN command one by one and commit the result, and seems your mysql service status is not committed to the image. To solve this problem, you may try these ways

  • Put all commands into one RUN command
    RUN echo $(service mysql restart) && echo $(service mysql status) && sudo mysql -uroot -pmysql_pass -e "CREATE DATABASE wordpress;" && sudo mysql -uroot -pmysql_pass -e "CREATE USER 'wordpressuser'@'%';" && sudo mysql -uroot -pmysql_pass -e "SET PASSWORD FOR 'wordpressuser'@'%'= PASSWORD('${mysql_pass}');" && sudo mysql -uroot -pmysql_pass -e "GRANT ALL PRIVILEGES ON wordpress.* TO 'wordpressuser'@'%'; FLUSH PRIVILEGES;"

  • initialize your mysql in entrypoint with a bash script.

init_mysql.sh

#!/bin/sh
sudo mysql -uroot -pmysql_pass -e "CREATE DATABASE wordpress;"
sudo mysql -uroot -pmysql_pass -e "CREATE USER 'wordpressuser'@'%';"
sudo mysql -uroot -pmysql_pass -e "SET PASSWORD FOR 'wordpressuser'@'%'= PASSWORD('${mysql_pass}');"
sudo mysql -uroot -pmysql_pass -e "GRANT ALL PRIVILEGES ON wordpress.* TO 'wordpressuser'@'%'; FLUSH PRIVILEGES;"

and add following lines to dockerfile

ADD init_mysql.sh /
RUN \
  cd / \
  chmod 777 init_mysql.sh

CMD ./init_mysql.sh



回答2:


You need to figure out why MySQL isn't starting. Log into the environment and manually try to start MySQL and see what the error is.

Have you considered basing your image on one of the official MySQL Docker images? https://registry.hub.docker.com/_/mysql/



来源:https://stackoverflow.com/questions/31256874/error-start-mysql-docker

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!