简体   繁体   English

无法通过Docker映像中的套接字错误连接到本地MySQL服务器

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

相关问题 带有 docker 的 Mysql:无法通过套接字连接到本地 MySQL 服务器 - Mysql with docker: Can't connect to local MySQL server through socket R Docker:无法通过socket连接到本地MySQL服务器 - R Docker: Can't connect to local MySQL server through socket Docker无法通过socket连接到本地MySQL服务器 - Docker can't connect to local MySQL server through socket mySQL Docker“ ERROR 2002(HY000):无法通过套接字连接到本地MySQL服务器” - mySQL Docker “ERROR 2002 (HY000): Can't connect to local MySQL server through socket” 在 Docker 中安装 MySQL 失败并显示错误消息“无法通过套接字连接到本地 MySQL 服务器” - Installing MySQL in Docker fails with error message "Can't connect to local MySQL server through socket" 错误:无法通过套接字连接到本地MySQL服务器 - Error: Can't connect to local MySQL server through socket Docker alpine image:ERROR 2002(HY000):无法通过套接字&#39;/run/mysqld/mysqld.sock&#39;连接到本地MySQL服务器(2“没有这样的文件或目录”) - Docker alpine image : ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/run/mysqld/mysqld.sock' (2 “No such file or directory”) `connect&#39;:无法通过套接字&#39;/tmp/mysql.sock&#39;(2)连接到本地MySQL服务器(Mysql2 :: Error) - `connect': Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2) (Mysql2::Error) Docker无法通过套接字&#39;/var/run/mysqld/mysqld.sock&#39;连接到本地MySQL服务器(2)Ubuntu - Docker Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2) Ubuntu Docker容器bash无法通过套接字连接到本地MySQL服务器 - Docker container bash can't connect to local MySQL server through socket
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM