连接docker中的mysql uri获取地址不可用错误

t3irkdon  于 2021-06-18  发布在  Mysql
关注(0)|答案(0)|浏览(244)

我正在处理我的 flask 应用程序。我让我的dockerfile工作,现在我只需要连接我的mysql数据库。在 flask 里,我经常这样做 __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)]>

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题