Connecting Python and MySQL in docker / docker-compose

LubieCiastka

Before, I created named volume and database: 'mysql-db1' using other mysql container.

I can't connect to database from python.

My .yml file :

    version: '3.6'

    services: 
      python:
        image: python:latest
        ports:
         - '80:80'
        volumes:
          - type: bind
            source: .
            target: /scripts
        command: tail -f /dev/null
        links:
          - 'mysql'

      mysql:
        image: mysql/mysql-server:latest
        ports:
         - '3306:3306'
        environment:
          MYSQL_ROOT_PASSWORD: root
        volumes:
          - type: volume
            source: mysql-db1
            target: /var/lib/mysql


    volumes: 
      mysql-db1:
        external: true

My simply python code:

    import mysql.connector

    cnx = mysql.connector.connect(user='root', password='root', host='mysql', database='test1')
    cnx.close()

I can enter the database using:

    $ docker-compose exec mysql bash
    # mysql -uroot -proot -Dtest1

Error:

    mysql.connector.errors.DatabaseError: 1130 (HY000): Host '172.18.0.3' is not allowed to connect to this MySQL server

Where is a problem?

Dmitrii G.

root user is not allowed to access the db externally by default. Use image environment variables to create a user and use that:

db:
restart: always
image: mariadb
container_name: myapp_db
environment:
  - MYSQL_USER=myuser
  - MYSQL_PASSWORD=mypass
  - MYSQL_DATABASE=mydb
  - MYSQL_ALLOW_EMPTY_PASSWORD=yes
ports:
  - 3306:3306

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Docker Compose : connecting python container with postgresql container

Problem in Connecting FastAPI Server and MySQL databse using Docker-Compose

Connecting to MySQL from Flask Application using docker-compose

Connecting to MySql on Docker

connecting to a docker-compose mysql container denies access but docker running same image does not

Wordpress not connecting to database with nginx, letsencrypt, and docker compose

Connecting to RabbitMQ container with docker-compose

Connecting PHP-FPM with Nginx in Docker Compose

Connecting go and postgres with docker-compose

Connecting Rails and Postgres containers without Docker Compose

docker-compose error connecting to redis + sidekiq

Docker connecting to docker mysql (jdbc url)

Drupal 8 is not connecting to Mysql in Docker

Connecting to a mysql running on a Docker container

Docker Connecting PHP container to MySQL

Docker error connecting to host mysql

Docker Compose - Python Instances

Laravel is not connecting to database on Docker/ docker-compose on Windows

Connecting FTP container works with docker-compose and not with docker run

Connecting separate WordPress Docker-Compose Stacks to an external MySQL and phpMyAdmin Containers

Issue connecting Node server to MySQL when both images are running on docker-compose

Connecting to MySql 8 from ASP.NET Core 2 web app whithin docker-compose

The dreaded Java SpringBoot app not connecting to MySQL with Docker-compose java.net.ConnectException: Connection refused

Connecting SQL Server on Docker to Python

Connecting to Redis cluster (in Docker) with Python

How to containerize MySQL using Docker for Docker Compose?

Using selenium/standalone-chrome in docker-compose connecting with Python's selenium

MySql 8.0.0 and Docker compose problems

NodeJS + Mysql with Docker Compose 2