从撰写到群集:在'阅读授权包'丢失连接到MySQL服务器

我有一个docker-compose文件,允许我使用compose命令启动和访问一个web应用程序: docker-compose -f docker-compose-dev.yml up -d on the original docker-compose-dev.yml file :

 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 

现在,我想用swarm模式运行它。 我知道swarm模式是可用的,因为我运行17.05.0-ce版本17.05.0-ce都为引擎和客户端。 主机是Ubuntu 16.04

我可以通过docker swarm init命令启动docker swarm init

然后我尝试启动Web应用程序:

 $ 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服务器。

但没有这样的容器名称,因此不再有这样的主机名称。

在非群集模式之前,当容器名称被用作主机名时,现在没有这样的容器名称。

我试着在docker stack deploy --compose-file docker-compose-swarm-dev.yml learnintouch使用以下docker stack deploy --compose-file docker-compose-swarm-dev.yml learnintouch命令:

 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命令以non swarm模式启动应用程序,则应用程序响应正常。 只有当我以docker stack deploy --compose-file docker-compose-swarm-dev.yml learnintouch swarm模式启动它时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

更新:看来问题不是一个主机名,而是一个群/ mysql的问题。 在mysql容器中,我可以login到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服务,但不能login它:

 $ 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 

其实我也可以telnet:

 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 

mysql容器中没有/etc/hosts.allow/etc/hosts.deny文件。

周围没有防火墙。

在引擎和客户端上的Docker版本是17.05.0-ce,docker-compose版本是1.16.1。

更新:删除群体并重新启动后,我可以在浏览器中运行应用程序,看到它运行得很好。 所以,在此之后,我做了另一个删除,然后是一个开始,同样的错误发生。 所以这个错误,有时候,但很less,不会发生。

更新:看来,从客户端容器内运行telnet mysql 3306命令,消除了问题,并允许MySQL客户端连接继续。 这是一致的:我删除群并重新启动它,我在客户端容器中打开一个bash,我尝试login到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

如果你想在不同的networking中使用不同的主机名,那么你可以使用别名https://docs.docker.com/compose/compose-file/#networks

我怀疑在这个mysql:5.6.30一个错误,所以我删除并用mariadb:10.1.24代替它,问题就没有了。