在docker中连接MySQL URI获取地址不可用错误

时间:2018-11-08 03:35:08

标签: mysql docker flask docker-compose flask-sqlalchemy

我正在对flask应用进行docker化。我已使Dockerfile正常工作,现在只需要连接MySQL数据库即可。在烧瓶中,我以前在__init.py___中执行以下操作:

# SQL configs
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://username:password@localhost/db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False

db = SQLAlchemy(app)

这在我的网络服务器上有效,有没有办法通过docker实现相同的目的?

运行docker-compose up时出现以下错误

app_1_fb82702505ba | [2018-11-08 04:02:27 +0000] [10] [ERROR] Exception in worker process
app_1_fb82702505ba | Traceback (most recent call last):
app_1_fb82702505ba |   File "/home/supv/venv/lib/python3.5/site-packages/pymysql/connections.py", line 582, in connect
app_1_fb82702505ba |     **kwargs)
app_1_fb82702505ba |   File "/usr/local/lib/python3.5/socket.py", line 712, in create_connection
app_1_fb82702505ba |     raise err
app_1_fb82702505ba |   File "/usr/local/lib/python3.5/socket.py", line 703, in create_connection
app_1_fb82702505ba |     sock.connect(sa)
app_1_fb82702505ba | OSError: [Errno 99] Address not available

这是我的docker-compose.yml:

version: "2"
services:
  app:
    build: ./
    links:
      - db
    ports:
      - "5000:5000"

  db:
    image: mysql:5.7
    ports:
      - "32000:3306"
    environment:
      MYSQL_ROOT_PASSWORD: pass
    volumes:
      - ./db:/docker-entrypoint-initdb.d/:ro

此外,我可以通过命令行手动连接到mysql:

# mysql --host=127.0.0.1 --port=32000 -u root -p
Enter password:
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.24 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MySQL [(none)]>

0 个答案:

没有答案