mysql - Mysql docker container - can't get much of anything to work
问题描述
I'm trying to use the mysql docker image to do some database development. I can't seem to connect to it, nor use its client to connect to an existing database.
Server
Here's me trying to create a database and connect to it:
% cat Dockerfile
FROM mysql:5.7
COPY drupal.sql.gz /docker-entrypoint-initdb.d
COPY civicrm.sql.gz /docker-entrypoint-initdb.d
RUN perl -pi -e 's/^#(bind-address\s*=\s*127\.0\.0\.1)/$1/' /etc/mysql/mysql.conf.d/mysqld.cnf
% docker build -t my-mysql .
Sending build context to Docker daemon 7.166MB
Step 1/4 : FROM mysql:5.7
---> e1e1680ac726
Step 2/4 : COPY drupal.sql.gz /docker-entrypoint-initdb.d
---> Using cache
---> 93d57dcd45e9
Step 3/4 : COPY civicrm.sql.gz /docker-entrypoint-initdb.d
---> Using cache
---> cb0342af9523
Step 4/4 : RUN perl -pi -e 's/^#(bind-address\s*=\s*127\.0\.0\.1)/$1/' /etc/mysql/mysql.conf.d/mysqld.cnf
---> Using cache
---> bf68cb50c994
Successfully built bf68cb50c994
Successfully tagged my-mysql:latest
% docker run -p 4306:3306 -e MYSQL_ROOT_PASSWORD=1234 -d my-mysql
ff19571bebf58ac1d58e44d40677c693953cd981fd70d18fd93d3d2fe8738349
% mysql --port 4306 -h 127.0.0.1 -u root -p1234
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 2013 (HY000): Lost connection to MySQL server at 'reading initial communication packet', system error: 0
It is actually finding something at that port, because if I connect to some random port with no server on it, there's a different error:
% mysql --port 4307 -h 127.0.0.1 -u root -p1234
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 2003 (HY000): Can't connect to MySQL server on '127.0.0.1' (61)
The logs do seem to show the server starting up successfully:
% docker logs ff19571bebf5 2>&1 |tail -n3
2019-10-22T01:04:51.953141Z 0 [Note] mysqld: ready for connections.
Version: '5.7.27' socket: '/var/run/mysqld/mysqld.sock' port: 3306 MySQL Community Server (GPL)
2019-10-22T01:04:52.933930Z 0 [Note] InnoDB: Buffer pool(s) load completed at 191022 1:04:52
Client
Similarly, when I try to use the container as a client to connect to a local mysql server, the client can't connect. Here's connecting using a local client:
% mysql -u ken -p -h 127.0.0.1 --port 3306
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 253
Server version: 5.7.27 Homebrew
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
and here's trying the same thing using the Docker client:
% docker run -it --network=host --rm mysql mysql -h 127.0.0.1 --port 3306 -u ken
ERROR 2003 (HY000): Can't connect to MySQL server on '127.0.0.1' (111)
What am I doing wrong here? Is this a problem of not understanding Docker networking, or is something wrong with these images?
Eventually
Eventually I do want to get this working using docker-compose, so that my web container can talk to my database container, but I thought I'd simplify this down to something easier to debug.
解决方案
如果你在 Docker 容器内设置一个服务器进程绑定到 127.0.0.1,即该容器的localhost 接口,它将无法从容器外部访问。图像的mysql
默认值在这里是正确的;删除RUN
更改 MySQLbind-address
设置的行。
推荐阅读
- docker - 如何为不同的服务连接重用 Azure DevOps 管道模板?
- firebase - Flutter Firebase - 可以公开访问谷歌登录吗?
- mysql - 如何使用mysql配置passport.js而不用sequelize?
- java - 字符流中的第一个非重复字符 InterviewBit
- pyqt - 如何将PyQT4转换为PyQT5关于opencv多摄像头
- c - 在多个 .c 文件中使用 TWI 实例变量
- swift - 检查从 Firestore 检索到的文件是否为空
- android - Android Studio 4.2 在增量构建时抛出空指针异常
- typescript - 只允许某组字符串作为接口中的键
- azure-cli - az keyvault 未列出机密