MySQL Docker Mysql容器root密码【mysqld监听端口0】
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39664141/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Docker Mysql container root password [mysqld listens port 0]
提问by Kern
Today I'm trying to make my Docker environment working !
今天我正在努力让我的 Docker 环境正常工作!
In this matter I've encountered quite a problem : my MySQL container, extending the MySQL official Docker image, seems to fail to create the root account, despite the setting of the MYSQL_ROOT_PASSWORD
environment variable in my docker-compose`.yml
.
在这件事上我遇到了一个相当大的问题:我的 MySQL 容器,扩展了 MySQL 官方 Docker 镜像,尽管MYSQL_ROOT_PASSWORD
在我的docker-compose`.yml
.
I copy here my Docker files :
我在这里复制我的 Docker 文件:
docker-compose.yml
docker-compose.yml
Most of the environment variables are used in scripts and applications independantly from the MySQL server. Only MYSQL_ROOT_PASSWORD
deserves interest (perhaps this statement is the cause of my failure to make this work..! ).
大多数环境变量都独立于 MySQL 服务器在脚本和应用程序中使用。只MYSQL_ROOT_PASSWORD
值得关注(也许这句话是我未能完成这项工作的原因..!)。
mysql:
container_name: my_mysql
build: mysql
environment:
- MYSQL_DATABASES=my-database
- MYSQL_ROOT_PASSWORD=root
- MYSQL_HOST=127.0.0.1
- MYSQL_PORT=33306
- MYSQL_USER=user
- MYSQL_PASSWORD=password
- MYSQL_MY_DATABASE=my-database
ports:
- "33306:3306"
volumes:
- "./volumes/mysql:/var/lib/mysql"
mysql/Dockerfile
mysql/Dockerfile
The dos2unix command is meant to convert Windows line endings to Unix.
dos2unix 命令旨在将 Windows 行尾转换为 Unix。
The custom entrypoint is named differently to avoid overidding the default mysql entrypoint script.
自定义入口点的名称不同,以避免覆盖默认的 mysql 入口点脚本。
FROM mysql:5.7
MAINTAINER Wonderful Dev <[email protected]>
RUN apt-get update && apt-get install -y dos2unix
COPY conf.d/custom.cnf /etc/mysql/conf.d/
COPY docker-entrypoint-initdb.d/databases.sh /docker-entrypoint-initdb.d/databases.sh
COPY my-entrypoint.sh /my-entrypoint.sh
RUN dos2unix /docker-entrypoint-initdb.d/databases.sh && dos2unix /my-entrypoint.sh && dos2unix /etc/mysql/conf.d/custom.cnf && apt-get --purge remove -y dos2unix && rm -rf /var/lib/apt/lists/*
RUN chmod a+x /docker-entrypoint-initdb.d/databases.sh && chown root:root /docker-entrypoint-initdb.d/databases.sh
RUN chmod a+x /my-entrypoint.sh && chown root:root /my-entrypoint.sh
ENTRYPOINT ["/entrypoint.sh", "/my-entrypoint.sh"]
CMD ["mysqld"]
mysql/my-entrypoint.sh
mysql/my-entrypoint.sh
This chmod command is meant to avoid this kind of error
这个 chmod 命令是为了避免这种错误
#!bin/bash
chmod 664 /etc/mysql/conf.d/custom.cnf
exec "$@"
mysql/config/custom.cnf
mysql/config/custom.cnf
[mysqld]
default-storage-engine=INNODB
init-connect='SET NAMES utf8'
character-set-server=utf8
collation-server=utf8_unicode_ci
bind-address = 0.0.0.0
skip-external-locking
key_buffer = 16M
max_allowed_packet = 16M
thread_stack = 192K
thread_cache_size = 8
query_cache_limit = 1M
query_cache_size = 16M
expire_logs_days = 10
max_binlog_size = 100M
[mysqldump]
quick
quote-names
max_allowed_packet = 16M
[isamchk]
key_buffer = 16M
All this stuff is working, except that I haven't any root access, blocking me from creating databases or mysql users for my apps !
所有这些东西都在工作,除了我没有任何 root 访问权限,阻止我为我的应用程序创建数据库或 mysql 用户!
Thank you for any advice :D !
感谢您的任何建议:D!
EDIT: After days of investigation, and an issue in docker-library/mysql
repository, we found out that one of the problems was a deprecated configuratio key in custom.cnf.
编辑:经过几天的调查和docker-library/mysql
存储库中的一个问题,我们发现其中一个问题是custom.cnf 中不推荐使用的配置键。
key_buffer => key_buffer_size
Now the container runs a few seconds after building, then crash.
现在容器在构建后运行几秒钟,然后崩溃。
The main clue in the logs is that line :
日志中的主要线索是那一行:
Version: '5.7.15' socket: '/var/run/mysqld/mysqld.sock' port: 0 MySQL Community Server (GPL)
I already tried a proposed solution to configure manually the port in custom.cnfbut it does not work. The container tries to connect to MySQL and crash due to the port.
我已经尝试了一个建议的解决方案来手动配置custom.cnf 中的端口,但它不起作用。容器尝试连接到 MySQL 并由于端口而崩溃。
The problem was not pretty visible because when launching the container a second time, the initialization was skipped, the good port was configured, and the server worked fine.
问题不是很明显,因为第二次启动容器时,跳过了初始化,配置了良好的端口,并且服务器工作正常。
The thing is that due to the crash, the end of the initialization was not executed, including my scripts and the databases creation along with users.
问题是由于崩溃,初始化的结尾没有执行,包括我的脚本和数据库创建以及用户。
So I'd like to find out why this damn mysqld is listening on port 0 the first time I launch the container after building.
所以我想知道为什么这个该死的 mysqld 在我构建后第一次启动容器时监听端口 0。
If you have a clue, I'd be glad to here about it !
如果你有线索,我很乐意来这里!
回答by Kern
Finally I figured it out, thanks to the guys on this issue : https://github.com/docker-library/mysql/issues/82
最后我想通了,感谢关于这个问题的人:https: //github.com/docker-library/mysql/issues/82
The culprit was the MYSQL_HOST
environment variable, which was causing the mysqld --initialize-insecure
command fail.
罪魁祸首是MYSQL_HOST
环境变量,它导致mysqld --initialize-insecure
命令失败。
The solution is to replace 127.0.0.1
with localhost
, which produces the following docker-compose.yml:
解决方案是替换127.0.0.1
为localhost
,它会生成以下docker-compose.yml:
mysql:
container_name: my_mysql
build: mysql
environment:
- MYSQL_DATABASES=my-database
- MYSQL_ROOT_PASSWORD=root
- MYSQL_HOST=localhost
- MYSQL_PORT=33306
- MYSQL_USER=user
- MYSQL_PASSWORD=password
- MYSQL_MY_DATABASE=my-database
ports:
- "33306:3306"
volumes:
- "./volumes/mysql:/var/lib/mysql"
I hope it will help some people :D !
我希望它会帮助一些人:D!
回答by BMitch
Try reversing your entrypoints:
尝试反转您的入口点:
ENTRYPOINT ["/my-entrypoint.sh", "/entrypoint.sh"]
The builtin entrypoint is looking for "mysqld" as the first arg, not "my-entrypoint.sh". So you want to call yours and then spin up the default entrypoint with the passed options.
内置入口点正在寻找“mysqld”作为第一个参数,而不是“my-entrypoint.sh”。所以你想调用你的,然后使用传递的选项启动默认入口点。