Mariadb docker container Can't connect to MySQL server on host (111 Connection refused) with Python

后端 未结 2 2043
死守一世寂寞
死守一世寂寞 2021-01-24 23:51

I am trying the connect mariadb with python using docker-compose:

docker-compose.yml

    version: \'2\'
    services:
      mariadb:
        image: bitna         


        
2条回答
  •  礼貌的吻别
    2021-01-25 00:24

    This problem is due to python containers executing prior then properly executing the database container. To remove this issue use below lines of code before mysql connection.

    import time
    time.sleep(1)
    

    Your code should look like this:

    import time
    time.sleep(1)
    
    mydb = mysql.connector.connect(
      host="",
      user="",
      passwd="",
      database = "",
      buffered=True
    )
    
    cursor = mydb.cursor()
    

    It worked in my case. Hope it will work for you as well.

提交回复
热议问题