django (mysql.连接器.错误.数据库错误)2005(HY000):未知MySQL服务器主机“db”(8)(此错误的背景信息位于:https://sqlalche.me/e/20/4xp6)

lrpiutwd  于 2023-02-17  发布在  Go
关注(0)|答案(1)|浏览(129)

我正在用Django开发一个项目,我想使用数据库来创建 Dataframe ,但我被主题问题(mysql.connector.errors.DatabaseError)2005(HY 000)卡住了:未知的MySQL服务器主机'db'(8)。(此错误的背景信息位于:https://sqlalche.me/e/20/4xp6)我应该如何修复它?
views.py

from sqlalchemy import create_engine, text
def user_detail(req, id):
    engine = create_engine("mysql+mysqlconnector://user:mariadb@db:9051/mariadb")
    query = "SELECT * FROM LoanParcel"
    df = pd.read_sql_query(sql=text(query), con=engine.connect())
    df = df.drop(["id", "date_add", "start_date", "description"], axis = 1)
    return render(req,'pages/user_detail.html')

docker-compose.yml

version: '3.7'
services:
  db:
    image: mariadb:10
    command: --default-authentication-plugin=mysql_native_password
    restart: always
    environment:
      - MYSQL_ROOT_PASSWORD=mariadb
      - MYSQL_DATABASE=mariadb
      - MYSQL_USER=mariadb
      - MYSQL_PASSWORD=mariadb
      - MARIADB_ROOT_PASSWORD=mysecretpassword
    ports:
      - 9051:3306
    volumes:
      - "mysqldata:/var/lib/mysql"  

  web:
    build: .
    restart: always
    command: python manage.py runserver 0.0.0.0:8000
    environment:
      - DATABASE_URL=mysql+mysqlconnector://user:mariadb@db:9051/mariadb
    ports:
      - "9052:8000"
    depends_on:
      - db
      
volumes:
  mysqldata:

停靠-组合ps命令

NAME                       COMMAND                  SERVICE             STATUS              PORTS
myproject-uborrowu-db-1    "docker-entrypoint.s…"   db                  running             0.0.0.0:3306->3306/tcp
myproject-uborrowu-web-1   "python manage.py ru…"   web                 running             0.0.0.0:8000->8000/tcp

相关问题