简体   繁体   English

无法使用python访问mysql db(都在不同的容器上运行)

[英]unable to access mysql db using python (both running on different containers)

I am trying to connect to mysql db using a python program. 我正在尝试使用python程序连接到mysql db。 When run locally it works. 在本地运行时,它可以工作。

But while dockerizing the application I created, one container is for the python code and the other for the mysql db, when ran i this manner it fails to connect. 但是在对我创建的应用程序进行docker化时,一个容器用于python代码,另一个容器用于mysql db,以这种方式运行时,它无法连接。

Python_code: Python_code:

db.bind(provider='mysql', user='docker_root', password='password', host='db', database=database, port = 3306)

docker-compose: 泊坞窗 - 撰写:

version: "3"
  services:
    app:
      image: app:latest
      links:
        - db
      ports:
        - "8001:8081"
      environment:
        - DB_HOST= db

    db:
      image: mysql:5.7.26
      restart: always
      environment:
        MYSQL_DATABASE: 'my_db'
        MYSQL_USER: 'docker_root'
        MYSQL_PASSWORD: 'password'
        MYSQL_ROOT_PASSWORD: 'password'
      ports:
        - "3306:3306"
      volumes:
        - ./DB_config/:/etc/mysql/mysql.conf.d

And the docker-compose up fails with the eroor: 并且docker-compose up因eroor而失败:

pony.orm.dbapiprovider.OperationalError: (2003, "Can't connect to MySQL server on 'db' ([Errno 111] Connection refused)") pony.orm.dbapiprovider.OperationalError:(2003年,“无法连接到'db'上的MySQL服务器([Errno 111]连接被拒绝)”)

Where am I going wrong? 我要去哪里错了? Please advise! 请指教!

Try using container port 3306 - 尝试使用容器端口3306

db.bind(provider='mysql', user='docker_root', password='password', host='db', database=database, port = 3306)

Also, add depends_on attribute, you can remove the links attribute - 另外,添加depends_on属性,您可以删除links属性-

 depends_on:
    - db

YOu can use depends_on flag as mentioned in accepted answer. 您可以使用已接受答案中提到的depends_on标志。 If it does not solve your problem them use this approach. 如果不能解决您的问题,请使用此方法。

After starting the container, your server will try to connect to the database server. 启动容器后,您的服务器将尝试连接到数据库服务器。 sometimes database server may take some time to boot up and in this window, if the server tries to connect to database server it will face problems. 有时数据库服务器可能需要一些时间才能启动,并且在此窗口中,如果服务器尝试连接到数据库服务器,它将面临问题。 Try adding logic to reconnect database server after few seconds if the connection fails. 如果连接失败,请尝试添加逻辑以在几秒钟后重新连接数据库服务器。

try{
   connectToDatabase();
   }catch(error){
   waitForHalfMinute();
   connectToDatabase();
}

I would recommend you to exit your application in case it cannot connect to MySQL and set the restart policy to always, because depends_on does not guarantee that MySQL will be totally up when app starts but it is good to have it there. 我建议您在无法连接到MySQL的情况下退出您的应用程序,并将重新启动策略设置为always,因为depends_on不能保证在app启动时MySQL会完全启动,但是最好在app中安装它。

version: "3"
  services:
    app:
      image: app:latest
      restart: always
      links:
        - db
      ports:
        - "8001:8081"
      environment:
        - DB_HOST= db
      depends_on:
        - db

    db:
      image: mysql:5.7.26
      restart: always
      environment:
        MYSQL_DATABASE: 'my_db'
        MYSQL_USER: 'docker_root'
        MYSQL_PASSWORD: 'password'
        MYSQL_ROOT_PASSWORD: 'password'
      ports:
        - "3306:3306"
      volumes:
        - ./DB_config/:/etc/mysql/mysql.conf.d

And your application code should be something like: 您的应用程序代码应类似于:

try:
    db.bind(provider='mysql', user='docker_root', password='password', host='db', database=database, port = 3306)
except:
    # write some logs
    exit(1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM