Docker-使用RUN(ERROR 2002)在Dockerfile中使用MySQL命令 [英] Docker - MySQL commands within Dockerfile using RUN (ERROR 2002)

查看:189
本文介绍了Docker-使用RUN(ERROR 2002)在Dockerfile中使用MySQL命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Docker创建一个以mysql作为基本映像的dockerfile:

I am using Docker to create a dockerfile with mysql as the base image:

FROM mysql
#set root pass
ENV MYSQL_ROOT_PASSWORD password
#update linux
RUN apt-get update
#create database
RUN mysql -u root -ppassword -e "CREATE DATABASE dbname"
#install vim
RUN apt-get install vim -y

dockerfile在我尝试创建数据库的步骤上失败,它没有完成构建,并且我收到此错误:

The dockerfile fails on the step where I try to create a database, it doesn't finish building and i receive this error:

错误2002(HY000):可以t通过套接字'/var/run/mysqld/mysqld.sock'连接到本地MySQL服务器

当我删除#create database run命令时,dockerfile将建立,我可以从该图像运行容器。我知道这不是mysql服务器的问题,因为我可以输入容器并成功手动运行mysql命令,并且服务状态正在运行。

When I remove the #create database run command the dockerfile will build and I am able to run a container from that image. I know that it isn't a problem with the mysql server as I can enter the container and run the mysql command manually with success and the service status is running.

使用文件中的环境变量(即MYSQL_ROOT_PASSWORD)也允许我成功创建数据库,但这仅适用于单个数据库,我需要能够使用mysql命令进行查询,例如创建其他数据库/分配用户等。

Using an environment variable i.e MYSQL_ROOT_PASSWORD within the file also allows me to create a database successfully but this will only work with a single database, I need to be able to use the mysql command to make queries, such as creating additional databases / assigning users etc.

这可能是因为我需要指定Docker容器的主机和端口,但这仍然不允许我连接

This may be because I need to specify the host and port of the docker container but this still does not allow me to connect

RUN mysql -u root -ppassword -h 127.0.0.1 -P 3308 -e "CREATE DATABASE dbname"

奇怪的是,这样做还会使容器崩溃,并使其处于每次启动时都会再次崩溃的状态。 / p>

Strangely, doing this also often crashes the container and puts it in a state where it will crash again on start-up every time that I try to restart it again.

推荐答案

出现了非常相同的问题:容器并运行一组 RUN 指令,或 .sh .sql /docker-entrypoint-initdb.d / 中的c $ c>脚本无法建立与数据库服务器的连接。

Had the very same problem: When starting the container and running a set of RUN instructions, or .sh or .sql scripts in /docker-entrypoint-initdb.d/ no connection to the database server could be established.

我通过 mysql-image上的@wpalmer


入口点内部运行的初始化脚本使用变量 $ { mysql [@]}以调用mysql(例如,在加载放置在docker-entrypoint-initdb.d目录中的.sql文件时。通过源包含由入口点处理的任何.sh文件,这意味着变量可供运行的任何.sh文件使用。)

The init scripts run by the entrypoint, internally, use the variable "${mysql[@]}" to call mysql (for example, when loading .sql files placed in the docker-entrypoint-initdb.d directory. Any .sh files which are processed by the entrypoint are included by "sourcing" them, meaning that variable is available for use by any .sh files which are run).

那么这对您意味着什么,而不是简单地提供 mysql 命令,包括用户,传递等。

So what this means for you, instead of providing the plain mysql command with user, pass etc. as in

RUN mysql -u root -ppassword -e "CREATE DATABASE dbname"

使用占位符:

RUN "${mysql[@]}" -e "CREATE DATABASE dbname"

这篇关于Docker-使用RUN(ERROR 2002)在Dockerfile中使用MySQL命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆