Reputation: 143
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?
Upvotes: 5
Views: 12376
Reputation: 895
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
Upvotes: 7