如何从docker容器中运行任意MySQL命令?

时间:2017-06-22 20:29:38

标签: mysql docker

我想要一个为它的效果运行命令的docker容器,然后可以停止直到我再次运行它。

我只想运行它,删除过期的行:

mysql --user=$MYSQL_USER --host=$MYSQL_HOST --database=$MYSQL_DB --password=$MYSQL_PASS -se "DELETE FROM sometable WHERE expiration < NOW();"

我试过这个:

FROM ?????

ENV MYSQL_USER=$MYSQL_USER
ENV MYSQL_HOST=$MYSQL_HOST
ENV MYSQL_PASS=$MYSQL_PASS
ENV MYSQL_DB=$MYSQL_DB

ENTRYPOINT mysql --user=$MYSQL_USER --host=$MYSQL_HOST --database=$MYSQL_DB --password=$MYSQL_PASS -se "DELETE FROM sometable WHERE expiration < NOW();"

并运行:

docker run --name some-process \
-e MYSQL_USER=$MYSQL_USER \
-e MYSQL_PASS=$MYSQL_PASS \
-e MYSQL_HOST=$MYSQL_HOST \
-e MYSQL_DB=$MYSQL_DB \
-d some-image:latest

我尝试了很多这种情况,但是,我真的迷失了。

我的Dockerfiledocker run命令应该是什么样的建议?

编辑:

我已经尝试了很多这样的事情并且遇到了很多错误,我认为“修复”我的尝试是徒劳的,而只是请求“如何让容器运行mysql -se "DELETE ..."

但是,如果它有帮助,这里有一些我得到的错误(我最常见的第一个)

ERROR 2002 (HY000): Can't connect to local MySQL server through socket 

------

ERROR 2003 (HY000): Can't connect to MySQL server on '127.0.0.1' (111)

------

Error response from daemon: No such container:

------

docker: Error response from daemon: repository my-mysql-server not found: does not exist or no pull access.

------

ERROR: mysqld failed while attempting to check config

------

error: database is uninitialized and password option is not specified

1 个答案:

答案 0 :(得分:1)

您无需构建图像即可执行此操作。您可以在官方图片中使用MySQL客户端:

docker run -it -e MYSQL_PWD=yourpass mysql:5.6 mysql -h $MYSQL_HOST --port 3306 -u youruser -e "DELETE FROM sometable WHERE expiration < NOW();"