[英]Can't connect to local MySQL server through socket error in Docker Image
I am working on creating the mysql image with some sample data in it. 我正在创建带有一些示例数据的mysql映像。 I am getting the error when I am trying to start the mysql process during the image creation so that I can put data inside the image.
在映像创建过程中尝试启动mysql进程时出现错误,以便可以将数据放入映像中。 Below are my file structure
下面是我的文件结构
[DockerFile] [DockerFile]
FROM mysql:5.6.29
# MYSQL volumes
VOLUME ["/etc/mysql", "/var/lib/mysql"]
COPY data_dump.sql /tmp/
COPY init_db.sh /tmp/
RUN chmod -R 777 /tmp/init_db.sh
RUN chmod -R 777 /tmp/data_dump.sql
RUN /tmp/init_db.sh
EXPOSE 3306
[init_db.sh] [init_db.sh]
#!/bin/bash
echo "starting the bash command"
/usr/bin/mysqld_safe &
echo "started the mysqld_safe process"
sleep 15
mysql -u root -h localhost -e "CREATE DATABASE test"
echo "Done creating the test schema"
mysql -u root -h localhost test < /tmp/data_dump.sql
echo "Done creating the test data"
I am getting the following errors: 我收到以下错误:
started the mysqld_safe process
170906 13:56:49 mysqld_safe Logging to '/var/lib/mysql/bcad5a346f31.err'.
170906 13:56:49 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
170906 13:56:49 mysqld_safe mysqld from pid file /var/run/mysqld/mysqld.pid ended
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
Done creating the test schema
Done creating the test data
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
I found a solution, it turns out I was doing more than I needed to. 我找到了解决方案,事实证明我在做比所需要做的更多的事情。 Below is my DockerFile looks like now
下面是我的DockerFile看起来像现在
[DockerFile] [DockerFile]
FROM mysql:5.6.29
# 1. Set up the environment variables
ENV MYSQL_ALLOW_EMPTY_PASSWORD yes
ENV MYSQL_DATABASE testDB
ENV MYSQL_USER root
# 2. copy the data dump file in the /docker-entrypoint-initdb.d/ location so that it gets executed by default
COPY data-dump.sql /docker-entrypoint-initdb.d/data-dump.sql
# 3. change the data location in the my.conf file in the docker, it is needed to create the data from the /docker-entrypoint-initdb.d/data-dump.sql dump file
# for mysql 5.7 if the below line doesn't work try: RUN sed -i 's|/var/lib/mysql|/var/lib/mysql-data|g' /etc/mysql/mysql.conf.d/mysqld.cnf
RUN sed -i 's|/var/lib/mysql|/var/lib/mysql-data|g' /etc/mysql/my.cnf
# 4. start the mysql process so that it creates the required data
RUN /entrypoint.sh mysqld & sleep 30 && killall mysqld
# 5. remove the dump file from the image to save image space
RUN rm /docker-entrypoint-initdb.d/data-dump.sql
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.