1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] mysql.connector.errors.DatabaseError: 2005 (HY000): Unknown MySQL server host 'db' (2)

Discussão em 'Python' iniciado por Stack, Setembro 17, 2024.

  1. Stack

    Stack Membro Participativo

    • I've dockerized my django web app.
    • Whenever i run "docker-compose up" everything works fine and it opens locally
    • But when i run "python manage.py runserver" it pops up an error mysql.connector.errors.DatabaseError: 2005 (HY000): Unknown MySQL server host 'db' (2)
    • This is the same error it is showing when i deployed it to azure
    • When docker-compose up runs without error locally and the web app works well

    here is my docker-compose.yml file

    services:
    db:
    image: mysql
    ports:
    - '3306:3306'
    environment:
    MYSQL_DATABASE: 'app'
    MYSQL_USER: 'root'
    MYSQL_PASSWORD: 'aspilos'
    MYSQL_ROOT_PASSWORD: 'aspilos'
    web:
    build: .
    command: python manage.py runserver 0.0.0.0:8000
    volumes:
    - .:/aspilos
    ports:
    - "8000:8000"
    depends_on:
    - db


    Here is my utils.py file

    import mysql.connector
    mydb = mysql.connector.connect(
    host="db",
    user="root",
    passwd="aspilos",
    database="aspilos_log",
    auth_plugin="mysql_native_password"
    )
    mycursor = mydb.cursor()
    mycursor.execute("SELECT CONCAT('+', PHONE_NUMBER) FROM category2")
    results = mycursor.fetchall()
    for i in zip(*results):
    number = list(i)
    number1 = '+2348076548894'
    print (number)


    Here is my settings.py file

    DATABASES = {
    'default': {
    'ENGINE': 'django.db.backends.mysql',
    'NAME': 'app',
    'USER': 'root',
    'PASSWORD': 'aspilos',
    'HOST': 'db',
    'PORT': '3306',
    },
    }

    Continue reading...

Compartilhe esta Página