简体   繁体   English

如何使用 pymsql 连接到 docker 容器上的 mariadb

[英]How to connect to mariadb on a docker container using pymsql

I am using a Docker container to connect to mysql but I am getting this following error.我正在使用 Docker 容器连接到 mysql 但我收到以下错误。

error:错误:

pymysql.err.OperationalError: (2003, "Can't connect to MySQL server on 'localhost' ([Errno 99] Cannot assign requested address)")

I am not sure about 'localhost' I am using a linode/Digital Ocean instance.我不确定 'localhost' 我使用的是 linode/Digital Ocean 实例。

Test class (test_mysql.py)测试 class (test_mysql.py)

import pymysql
class TestDBConnectionFromDocker(object):
    def test_docker(self):
        connection = pymysql.connect(
                host='localhost', 
                user='root', 
                password='myodesi123', 
                port=6603)
        assert connection == True

docker-compose.yaml docker-compose.yaml

version: '3.1'
services:
  test:
    build: .
    links:
      - mariadb
    volumes:
      - .:/sp_odesi_report_project
    environment:
      - MYSQL_ROOT_PASSWORD=root
      - MYSQL_DATABASE=sp_odesi
      - MYSQL_USER=odesi
      - MYSQL_PASSWORD=myodesi123
    stdin_open: true
    ports:
      - 6603:3306
    depends_on:
      - mariadb
    tty: true

  mariadb:
    image: mariadb:latest
    restart: unless-stopped
    container_name: mariadb-odesi
    environment:
      - MYSQL_ROOT_PASSWORD=root
      - MYSQL_DATABASE=sp_odesi
      - MYSQL_USER=odesi
      - MYSQL_PASSWORD=myodesi123
    expose:
      - 3306

Dockerfile Dockerfile

 FROM python:3.7.6-buster

 RUN mkdir /sp_odesi_report_project/
 COPY ./test_mysql.py /sp_odesi_report_project/

 RUN pip install --upgrade pip
 RUN pip3 install pymysql==0.9.3
 RUN pip3 install pytest

 WORKDIR /sp_odesi_report_project/

 CMD "pytest"
 ENV PYTHONDONTWRITEBYTECODE=true

Command line命令行

docker-compose build

docker-compose run test sh

pytest -v

inspect检查

[root@li1975-241 sp_odesi]# docker container inspect c3e | grep -C 2 '3306'
            "AttachStderr": false,
            "ExposedPorts": {
                "3306/tcp": {}
            },
            "Tty": false,
--
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {
                "3306/tcp": null
            },

Your connection settings are not quite right.您的连接设置不太正确。 You had:你有过:

connection = pymysql.connect(
                host='localhost', 
                user='root', 
                password='myodesi123', 
                port=6603)

However that does not match the way you have setup the dB in your docker-compose file for the MariaDB service.但是,这与您在 docker-compose 文件中为 MariaDB 服务设置 dB 的方式不匹配。 Instead it should be:相反,它应该是:

connection = pymysql.connect(
                host='mariaDB', 
                user='root', 
                password='root', 
                port=3306)

Alternatively you could make your docker-compose match the settings you have in your connection string.或者,您可以使您的 docker-compose 与您在连接字符串中的设置相匹配。 (Using root as password is probably not good). (使用 root 作为密码可能不好)。

Use mariadb (ie the hostname of the container running your database) instead of localhost .使用mariadb (即运行数据库的容器的主机名)而不是localhost

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

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