我有一个 docker-compose 文件,允许我启动和访问 Web 应用程序,这使用 compose 命令:在原始 docker-compose -f docker-compose-dev.yml up -d
文件上使用 docker-compose-dev.yml
:
version: "3"
services:
learnintouch-startup:
image: localhost:5000/learnintouch-startup
container_name: learnintouch-startup
restart: always
volumes:
- "~/dev/docker/projects/learnintouch/volumes/engine:/usr/bin/learnintouch/engine"
- "~/dev/docker/projects/learnintouch/volumes/www.learnintouch/account/data:/usr/bin/learnintouch/www/learnintouch.com/account/data"
- "~/dev/docker/projects/learnintouch/volumes/www.thalasoft/account/data:/usr/bin/learnintouch/www/thalasoft.com/account/data"
- "~/dev/docker/projects/learnintouch/volumes/www.folkuniversitet/account/data:/usr/bin/learnintouch/www/folkuniversitet/account/data"
ports:
- "81:80"
links:
- mysql
- redis
- nodejs-learnintouch
nodejs-learnintouch:
image: localhost:5000/nodejs-learnintouch
container_name: nodejs-learnintouch
restart: always
volumes:
- "~/dev/docker/projects/learnintouch/volumes/engine:/usr/bin/learnintouch/engine"
ports:
- "9001:9001"
links:
- redis
mysql:
image: localhost:5000/mysql:5.6.30
container_name: mysql
restart: always
environment:
- MYSQL_ROOT_PASSWORD=root
volumes:
- "~/dev/docker/projects/learnintouch/volumes/mysql/data:/usr/bin/mysql/install/data"
redis:
image: localhost:5000/redis:3.0.7
container_name: redis
restart: always
现在,我想以集群模式运行它。我知道集群模式是可用的,因为我正在为引擎和客户端运行 docker 版本 17.05.0-ce
。主机是 Ubuntu 16.04
。
我可以使用 docker swarm init
命令初始化集群。
然后我尝试启动网络应用程序:
$ docker stack deploy --compose-file docker-compose-swarm-dev.yml learnintouch
Creating network learnintouch_default
Creating service learnintouch_mysql
Creating service learnintouch_redis
Creating service learnintouch_learnintouch-startup
Creating service learnintouch_nodejs-learnintouch
learnintouch-startup
容器有一个MySQL客户端正在尝试访问mysql
容器中的MySQL服务器。
但是不存在这样的容器名称,因此不再存在这样的主机名。
以前,在非 Swarm 模式下,容器名称被用作主机名,但现在,没有这样的容器名称。
我尝试在 docker stack deploy --compose-file docker-compose-swarm-dev.yml learnintouch
文件上使用以下 docker-compose-swarm-dev.yml
命令:
version: "3"
services:
learnintouch-startup:
image: localhost:5000/learnintouch-startup
volumes:
- "~/dev/docker/projects/learnintouch/volumes/engine:/usr/bin/learnintouch/engine"
- "~/dev/docker/projects/learnintouch/volumes/www.learnintouch/account/data:/usr/bin/learnintouch/www/learnintouch.com/account/data"
- "~/dev/docker/projects/learnintouch/volumes/www.thalasoft/account/data:/usr/bin/learnintouch/www/thalasoft.com/account/data"
- "~/dev/docker/projects/learnintouch/volumes/www.folkuniversitet/account/data:/usr/bin/learnintouch/www/folkuniversitet/account/data"
ports:
- "81:80"
nodejs-learnintouch:
image: localhost:5000/nodejs-learnintouch
volumes:
- "~/dev/docker/projects/learnintouch/volumes/engine:/usr/bin/learnintouch/engine"
ports:
- "9001:9001"
mysql:
image: localhost:5000/mysql:5.6.30
environment:
- MYSQL_ROOT_PASSWORD=root
volumes:
- "~/dev/docker/projects/learnintouch/volumes/mysql/data:/usr/bin/mysql/install/data"
hostname: mysql
redis:
image: localhost:5000/redis:3.0.7
如何指定 swarm 服务的主机名?
请注意,如果我使用 docker-compose -f docker-compose-dev.yml up -d
命令在非集群模式下启动应用程序,则应用程序响应良好。只有当我使用 docker stack deploy --compose-file docker-compose-swarm-dev.yml learnintouch
命令以群体模式启动它时,才不再找到 mysql
主机名: The data source for the database db_learnintouch could not be initialized for the user learnintouch on the host mysql:3306
。事实上,在后一种情况下,容器名称是 learnintouch_mysql.1.pu846rr8to5gwxwnxpdm4hdth
而不是 mysql
。
更新:看来问题不是主机名问题,而是 swarm/mysql 问题。
在 mysql
容器中我可以正常登录 MySQL:
$ docker exec -it learnintouch_mysql.1.m51o8deg8cslb8mzayp119m67 bash
root@mysql:/usr/bin/mysql-5.6.30# cd /usr/bin/mysql/install;
root@mysql:/usr/bin/mysql/install# bin/mysql --protocol=tcp -h mysql -P 3306 -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
但是在 learnintouch-startup
客户端容器中,我只能 ping 通 mysql
服务,但无法登录它:
$ docker exec -it learnintouch_learnintouch-startup.1.kf13qktlvxs9rgjzaea2xxd68 bash
root@aa0f0539ba52:/usr/bin/learnintouch/www/folkuniversitet# cd /usr/bin/mysql/install
root@aa0f0539ba52:/usr/bin/mysql/install# bin/mysql --protocol=tcp -h mysql -P 3306 -u root -p
Enter password:
ERROR 2013 (HY000): Lost connection to MySQL server at 'reading authorization packet', system error: 0
root@aa0f0539ba52:/usr/bin/mysql/install# ping mysql
PING mysql (10.0.0.8) 56(84) bytes of data.
64 bytes from 10.0.0.8: icmp_seq=1 ttl=64 time=0.061 ms
64 bytes from 10.0.0.8: icmp_seq=2 ttl=64 time=0.084 ms
64 bytes from 10.0.0.8: icmp_seq=3 ttl=64 time=0.075 ms
^C
--- mysql ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 1998ms
事实上我也可以远程登录:
root@861cc3861e67:/usr/bin/learnintouch/www/folkuniversitet# telnet mysql 3306
Trying 10.0.0.6...
Connected to mysql.
Escape character is '^]'.
N
5.6.30-logs5L\i|Sj��=0t}62:k,8s]mysql_native_password
!#08S01Got packets out of orderConnection closed by foreign host.
我正在使用自定义构建的 MySQL 版本 mysql:5.6.30 和 my.cnf
文件:
[mysqld]
bind-address = 0.0.0.0 # Allow client binding from any IP address instead of just 127.0.0.1
port = 3306
sql_mode = NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION # This is strict mode: NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
socket = /usr/bin/mysql/install/tmp/mysql.sock
user = root
basedir = /usr/bin/mysql/install
datadir = /usr/bin/mysql/install/data
log-bin = /usr/bin/mysql/install/mysql.bin.log
log-error = /usr/bin/mysql/install/mysql.error.log
general-log-file = /usr/bin/mysql/install/mysql.log
slow-query-log-file = /usr/bin/mysql/install/mysql.slow.queries.log
innodb_file_per_table = 1
innodb_flush_log_at_trx_commit = 1
sync_binlog = 1
innodb_flush_method = O_DIRECT
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci
init-connect = 'SET NAMES utf8mb4'
character-set-client-handshake = FALSE
connect_timeout = 60
wait_timeout = 28800 # amount of seconds during inactivity that MySQL will wait before it will close a connection on a non-interactive connection
interactive_timeout = 28800 # same, but for interactive sessions
[client]
socket = /usr/bin/mysql/install/tmp/mysql.sock
default-character-set = utf8mb4
[mysql]
default-character-set = utf8mb4
protocol = tcp # Forces the client to use the TCP protocol, except for PHP which ignores it
/etc/hosts.allow
容器中没有 /etc/hosts.deny
和 mysql
文件。
周围没有防火墙。
引擎和客户端上的 Docker 版本均为 17.05.0-ce,docker-compose 版本为 1.16.1。
更新:删除群并再次启动后,我可以在浏览器中运行该应用程序,并看到它运行得很好。因此,在此之后,我又进行了一次删除,然后启动,并且再次发生了相同的错误。因此,有时(但很少)不会发生错误。
更新:看来,从客户端容器内运行 telnet mysql 3306
命令可以解决该问题并允许 MySQL 客户端连接继续进行。这是一致的:我删除了 swarm 并重新启动它,我在客户端容器中打开了 bash,我尝试登录 MySQL 服务器容器,但它失败了,我重试了很多次,但它仍然失败,我运行 ping mysql
命令,然后再次尝试连接多次,但失败,然后我运行 telnet mysql 3306
命令,然后尝试连接,结果成功。
root@2651380ce02e:/usr/bin/mysql/install# cd /usr/bin/mysql/install; bin/mysql --protocol=tcp -h mysql -P 3306 -u root -p
Enter password:
ERROR 2013 (HY000): Lost connection to MySQL server at 'reading authorization packet', system error: 0
root@2651380ce02e:/usr/bin/mysql/install#
root@2651380ce02e:/usr/bin/mysql/install# ping mysql
PING mysql (10.0.0.4) 56(84) bytes of data.
64 bytes from 10.0.0.4: icmp_seq=1 ttl=64 time=0.088 ms
64 bytes from 10.0.0.4: icmp_seq=2 ttl=64 time=0.109 ms
64 bytes from 10.0.0.4: icmp_seq=3 ttl=64 time=0.091 ms
^C
--- mysql ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 1999ms
rtt min/avg/max/mdev = 0.088/0.096/0.109/0.009 ms
root@2651380ce02e:/usr/bin/mysql/install# telnet mysql 3306
Trying 10.0.0.4...
Connected to mysql.
Escape character is '^]'.
N
5.6.30-logf[tT)mlX��Pi@EwHCT\SkBmysql_native_password
Connection closed by foreign host.
root@2651380ce02e:/usr/bin/mysql/install# cd /usr/bin/mysql/install; bin/mysql --protocol=tcp -h mysql -P 3306 -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
这是一个完整的演示运行作为证据:
$ docker exec -it learnintouch_learnintouch-startup.1.ili3m1kre9q2eaiyzlucr2uot bash
root@ce21d5b78886:/usr/bin/learnintouch/www/folkuniversitet# cd /usr/bin/mysql/install; bin/mysql --protocol=tcp -h mysql -P 3306 -u root -pEnter password:
ERROR 2013 (HY000): Lost connection to MySQL server at 'reading authorization packet', system error: 0
root@ce21d5b78886:/usr/bin/mysql/install#
root@ce21d5b78886:/usr/bin/mysql/install#
root@ce21d5b78886:/usr/bin/mysql/install# ping mysql
PING mysql (10.0.0.4) 56(84) bytes of data.
64 bytes from 10.0.0.4: icmp_seq=1 ttl=64 time=0.040 ms
64 bytes from 10.0.0.4: icmp_seq=2 ttl=64 time=0.110 ms
64 bytes from 10.0.0.4: icmp_seq=3 ttl=64 time=0.093 ms
64 bytes from 10.0.0.4: icmp_seq=4 ttl=64 time=0.091 ms
^C
--- mysql ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 2999ms
rtt min/avg/max/mdev = 0.040/0.083/0.110/0.027 ms
root@ce21d5b78886:/usr/bin/mysql/install# bin/mysql --protocol=tcp -h mysql -P 3306 -u root -p
Enter password:
ERROR 2013 (HY000): Lost connection to MySQL server at 'reading authorization packet', system error: 0
root@ce21d5b78886:/usr/bin/mysql/install# bin/mysql --protocol=tcp -h mysql -P 3306 -u root -p
Enter password:
ERROR 2013 (HY000): Lost connection to MySQL server at 'reading authorization packet', system error: 0
root@ce21d5b78886:/usr/bin/mysql/install# ping mysql
PING mysql (10.0.0.4) 56(84) bytes of data.
64 bytes from 10.0.0.4: icmp_seq=1 ttl=64 time=0.091 ms
64 bytes from 10.0.0.4: icmp_seq=2 ttl=64 time=0.105 ms
64 bytes from 10.0.0.4: icmp_seq=3 ttl=64 time=0.082 ms
^C
--- mysql ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2000ms
rtt min/avg/max/mdev = 0.082/0.092/0.105/0.014 ms
root@ce21d5b78886:/usr/bin/mysql/install# bin/mysql --protocol=tcp -h mysql -P 3306 -u root -p
Enter password:
ERROR 2013 (HY000): Lost connection to MySQL server at 'reading authorization packet', system error: 0
root@ce21d5b78886:/usr/bin/mysql/install# bin/mysql --protocol=tcp -h mysql -P 3306 -u root -p
Enter password:
ERROR 2013 (HY000): Lost connection to MySQL server at 'reading authorization packet', system error: 0
root@ce21d5b78886:/usr/bin/mysql/install# telnet mysql 3306
Trying 10.0.0.4...
Connected to mysql.
Escape character is '^]'.
N
5.6.30-logZo?*R@)M��(~cWg"q5R}?amysql_native_password
^CConnection closed by foreign host.
root@ce21d5b78886:/usr/bin/mysql/install# bin/mysql --protocol=tcp -h mysql -P 3306 -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.6.30-log Source distribution
Copyright (c) 2000, 2016, 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> quit;
Bye
root@ce21d5b78886:/usr/bin/mysql/install#
最佳答案
您可以使用您在 docker-compose 文件中指定的服务名称作为 swarm 中的主机名,例如您的 mysql 服务名称为 mysql
您可以使用该名称作为主机名,而且docker stack https://docs.docker.com/compose/compose-file/#not-supported-for-docker-stack-deploy 不支持链接等某些选项
如果你想在不同的网络中使用不同的主机名,那么你可以使用别名 https://docs.docker.com/compose/compose-file/#networks
关于mysql - 从撰写到集群 : Lost connection to MySQL server at 'reading authorization packet' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47123779/