Mysql operational error (2003, "Can't connect to MySQL server on '127.0.0.1' ([Errno 111] Connection refused)")

huangapple go评论63阅读模式
英文:

Mysql operational error (2003, "Can't connect to MySQL server on '127.0.0.1' ([Errno 111] Connection refused)")

问题

I need to connect Mysql database to FastAPI. Mysql uses custom port (17001). Also I need to use docker-compose.

Database:

from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker

DB_USER = 'api'
DB_PASSWORD = 'test'
DB_HOST = '127.0.0.1'
DATABASE = 'main'
DB_PORT = 17001

connect_uri = f'mysql+pymysql://{DB_USER}:{DB_PASSWORD}@{DB_HOST}:{DB_PORT}/{DATABASE}'

engine = create_engine(connect_uri)
Base = declarative_base()

from . import Models

Base.metadata.create_all(bind=engine)

SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)

def get_db():
    db = SessionLocal()
    try:
        yield db
    finally:
        db.close()

Docker-compose:

version: "3.8"

services:
  database:
    image: mysql:8.0
    restart: always
    environment:
      MYSQL_DATABASE: "main"
      MYSQL_ROOT_PASSWORD: "test"
      MYSQL_USER: "api"
      MYSQL_PASSWORD: "test"
      MYSQL_TCP_PORT: 17001
    ports:
      - "17001:17001"
    volumes:
      - ./MySQLData:/var/lib/mysql
    networks:
      - main
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:17001"]
      interval: 10s
      timeout: 10s
      retries: 10
  api:
    restart: on-failure
    build:
      dockerfile: ./api/Dockerfile
    volumes:
      - type: bind
        source: ./api
        target: /app
    ports:
      - 17002:17002
    depends_on:
      database:
        condition: service_healthy
    networks:
      - main

networks:
  main:

./api/Dockerfile:

FROM python:3.9
WORKDIR /code
COPY ./api/requirements.txt /code/requirements.txt
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
COPY ./api /code/app
CMD ["python3", "/code/app/Main.py"]

When I am running this using docker compose up, I get this error:

sqlalchemy.exc.OperationalError: (pymysql.err.OperationalError) (2003, "Can't connect to MySQL server on '127.0.0.1' ([Errno 111] Connection refused)")

When I am running MySQL using docker compose, then I am running the API by hand, everything works fine.

How do I solve this issue?

英文:

I need to connect Mysql database to FastAPI. Mysql uses custom port (17001). Also I need use docker-compose.

Database:

from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker

DB_USER = 'api'
DB_PASSWORD = 'test'
DB_HOST = '127.0.0.1'
DATABASE = 'main'
DB_PORT = 17001

connect_uri = f'mysql+pymysql://{DB_USER}:{DB_PASSWORD}@{DB_HOST}:{DB_PORT}/{DATABASE}'

engine = create_engine(connect_uri)
Base = declarative_base()

from . import Models

Base.metadata.create_all(bind=engine)

SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)

def get_db():
    db = SessionLocal()
    try:
        yield db
    finally:
        db.close()

Docker-compose:

version: "3.8"

services:
  database:
    image: mysql:8.0
    restart: always
    environment:
      MYSQL_DATABASE: "main"
      MYSQL_ROOT_PASSWORD: "test"
      MYSQL_USER: "api"
      MYSQL_PASSWORD: "test"
      MYSQL_TCP_PORT: 17001
    ports:
      - "17001:17001"
    volumes:
      - ./MySQLData:/var/lib/mysql
    networks:
      - main
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:17001"]
      interval: 10s
      timeout: 10s
      retries: 10
  api:
    restart: on-failure
    build:
      dockerfile: ./api/Dockerfile
    volumes:
      - type: bind
        source: ./api
        target: /app
    ports:
      - 17002:17002
    depends_on:
      database:
        condition: service_healthy
    networks:
      - main

networks:
  main:

./api/Dockerfile:

FROM python:3.9
WORKDIR /code
COPY ./api/requirements.txt /code/requirements.txt
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
COPY ./api /code/app
CMD ["python3", "/code/app/Main.py"]

When I am running this using docker compose up, I gets this error:

sqlalchemy.exc.OperationalError: (pymysql.err.OperationalError) (2003, "Can't connect to MySQL server on '127.0.0.1' ([Errno 111] Connection refused)")

When I am running MySQL using docker compose, then I am running api by hand, all works fine.

How do I solve this issue?

答案1

得分: 0

我忘记了 --build。为了运行,我使用 docker compose up --build

英文:

I have forgotten about --build. To run, I uses docker compose up --build.

huangapple
  • 本文由 发表于 2023年5月22日 23:21:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76307681.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定