为什么无法使用mysql服务?

时间:2019-08-19 09:38:38

标签: mysql docker flask docker-compose

在建立连接期间,服务名称未解析到mysql的主机。 服务名称是mysqldb,但是当我在连接数据库时将其用作主机时,会产生错误。

已解决:问题在于容器启动。 flask容器没有等待mysql容器准备就绪 。这就是为什么它退出得早。我通过对烧瓶容器使用失败时的重启策略来进行快速修复。

docker-compose.yml文件:

version: '3.7'
services:
  mysqldb:
    image: mysql:latest
    environment:
        MYSQL_ROOT_PASSWORD: root
        MYSQL_DATABASE: myapp
        MYSQL_PASSWORD: root
    ports:
      - "3333:3306"
    volumes:
      - ./db:/docker-entrypoint-initdb.d/:ro
  flask-app:
    build: .
    ports:
      - "5000:5000"
    depends_on:
      - mysqldb

使用mysql.connector

mydb = mysql.connector.connect(host="mysqldb", user="root",password="root", database="myapp", port="3306")

这是我遇到的错误

flask-app_1  | Traceback (most recent call last):
flask-app_1  |   File "/usr/local/lib/python3.7/site-packages/mysql/connector/network.py", line 509, in open_connection
flask-app_1  |     self.sock.connect(sockaddr)
flask-app_1  | ConnectionRefusedError: [Errno 111] Connection refused
flask-app_1  | 
flask-app_1  | During handling of the above exception, another exception occurred:
flask-app_1  | 
flask-app_1  | Traceback (most recent call last):
flask-app_1  |   File "app.py", line 10, in <module>
flask-app_1  |     mydb = mysql.connector.connect(host="mysqldb", user="root",password="root", database="myapp", port="3306")
flask-app_1  |   File "/usr/local/lib/python3.7/site-packages/mysql/connector/__init__.py", line 179, in connect
flask-app_1  |     return MySQLConnection(*args, **kwargs)
flask-app_1  |   File "/usr/local/lib/python3.7/site-packages/mysql/connector/connection.py", line 95, in __init__
flask-app_1  |     self.connect(**kwargs)
flask-app_1  |   File "/usr/local/lib/python3.7/site-packages/mysql/connector/abstracts.py", line 716, in connect
flask-app_1  |     self._open_connection()
flask-app_1  |   File "/usr/local/lib/python3.7/site-packages/mysql/connector/connection.py", line 206, in _open_connection
flask-app_1  |     self._socket.open_connection()
flask-app_1  |   File "/usr/local/lib/python3.7/site-packages/mysql/connector/network.py", line 512, in open_connection
flask-app_1  |     errno=2003, values=(self.get_address(), _strioerror(err)))
flask-app_1  | mysql.connector.errors.InterfaceError: 2003: Can't connect to MySQL server on 'mysqldb:3306' (111 Connection refused)
flaskapp_flask-app_1 exited with code 1


这是docker-compose ps

Name                     Command             State                 Ports              
----------------------------------------------------------------------------------------------
flaskapp_flask-app_1   python3 app.py                Up      0.0.0.0:5000->5000/tcp           
flaskapp_mysqldb_1     docker-entrypoint.sh mysqld   Up      0.0.0.0:3333->3306/tcp, 33060/tcp

docker network ls

NETWORK ID          NAME                DRIVER              SCOPE
509b1e597bc4        bridge              bridge              local
b841295e0dc6        flaskapp_default    bridge              local
2cd96006df91        host                host                local
260025d73006        none                null                local

2 个答案:

答案 0 :(得分:0)

正如您在评论中指出的那样,您需要配置networkslinks

以下是使用docker-compose.yml示例配置网络的示例:

version: '3.7'
services:
  mysqldb:
    image: mysql:latest
    environment:
        MYSQL_ROOT_PASSWORD: root
        MYSQL_DATABASE: myapp
        MYSQL_PASSWORD: root
    ports:
      - "3333:3306"
    volumes:
      - ./db:/docker-entrypoint-initdb.d/:ro
    networks:
      - mynetwork
  flask-app:
    build: .
    ports:
      - "5000:5000"
    depends_on:
      - mysqldb
    networks:
      - mynetwork
networks: 
  mynetwork:

mysqldbflask-app都将加入mynetwork网络。

我通常会过度测试,因此我也会docker-compose exec flask-app bash并运行ping mysql命令(假设已安装ping),以确保两个容器之间具有网络连接。

此外,尽管我提到了links,但您应该避免使用它,因为它已被弃用,并且可能在将来的docker-compose版本中删除。

答案 1 :(得分:-1)

您的mysql服务在端口3333上有一个侦听器。

PORTS语法:HOST:CONTAINER(https://docs.docker.com/compose/compose-file/

在flask应用程序中,您尝试连接到端口3306。但是在默认的docker网络中,我假设没有mysqldb在端口3306中进行监听。

仅供参考: 在您的容器中尝试netstat -tulpn查看所有侦听器。