2017-09-06 44 views
0

我正在創建帶有一些示例數據的mysql映像。我在圖像創建過程中嘗試啓動mysql過程時遇到了錯誤,因此我可以將數據放入圖像中。下面是我的文件結構無法通過Docker中的套接字錯誤連接到本地MySQL服務器

[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]

#!/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" 

我收到以下錯誤:

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) 
+0

我覺得你的mysql窗格上,我的意思是說,MySQL是在3306端口監聽你的地方,所以請關閉然後嘗試運行docker file.It將運行比...謝謝 –

+0

@AamirMeman我證實,3306沒有被任何本地實例的Mysql使用 –

+0

https://www.youtube.com/watch?v=oXjJRrbKjp0 ,這個視頻可能會幫助你比... –

回答

0

我找到了解決方案,它變成了ou我做得比我需要的多。下面是我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 
相關問題