Run 2 docker mysql containers simultaneously with docker-compose - mysql

I need help setting up 2 mysql docker containers at the same time.
Right now I spin up my mysql docker container via docker-compose like this:
version: '3.3'
services:
db:
image: mysql:5.7
restart: always
environment:
MYSQL_DATABASE: my_database
MYSQL_USER: user
MYSQL_PASSWORD: ********
MYSQL_ROOT_PASSWORD: ********
MYSQL_ROOT_USER: root
ports:
# <Port exposed> : < MySQL Port running inside container>
- '3308:3306'
What I've tried is copy-pasting the docker-composse.yml into a different folder and changing it a little
version: '3.3'
services:
db:
image: mysql:5.7
restart: always
environment:
MYSQL_DATABASE: my_other_database
MYSQL_USER: user
MYSQL_PASSWORD: ********
MYSQL_ROOT_PASSWORD: ********
MYSQL_ROOT_USER: root
ports:
# <Port exposed> : < MySQL Port running inside container>
- '3310:3307'
I run docker-compose up for both of them and both start up normally. I can connect to port 3308 just fine but I cannot connect to port 3310. I get
Can not read response from server.
Expected to read 4 bytes, read 0 bytes before connection was unexpectedly lost.
How do I set up my docker-compose.yml files so I get 2 mysql containers that run simultaneously?

I think you have the wrong Port on the second config. Mysql runs on port 3306
Change 3310:3307 to 3310:3306.
The whole config:
version: '3.3'
services:
db:
image: mysql:5.7
restart: always
environment:
MYSQL_DATABASE: my_other_database
MYSQL_USER: user
MYSQL_PASSWORD: ********
MYSQL_ROOT_PASSWORD: ********
MYSQL_ROOT_USER: root
ports:
# <Port exposed> : < MySQL Port running inside container>
- '3310:3306'

Related

Docker connect to mysql container via remote software

I have created my docker environment, but is having trouble connecting to mysql via remote software e.g. Workbench or Sequel Pro. Below is my yml file for mysql specific.
mysql-dev:
image: mysql:8.0.17
container_name: bcdb
volumes:
- mysql-dev:/var/lib/mysql
environment:
MYSQL_ALLOW_EMPTY_PASSWORD: "no"
MYSQL_ROOT_PASSWORD: "rootpwd"
MYSQL_USER: 'testuser'
MYSQL_PASSWORD: 'testpassword'
MYSQL_DATABASE: 'testdb'
MYSQL_ROOT_HOST: '%'
ports:
- "3301:3306"
Note: The mysql-dev is connected to volumes mysql-dev:
No matter if I type in as hostname:
localhost, 127.0.0.1, (ip address of the mysql server) etc. there is no connection available. Error message is: "MySQL said: Can't connect to MySQL server on '127.0.0.1' (61)"
Just a thought: Do I need to expose the ports in the Dockerfile, or am I missing something basic?
I'm starting the container by typing: "docker-compose up -d" - nothing else atm.
Try bridge network, here is a working yaml config,
version: "3"
services:
mysql-dev:
image: mysql:5
container_name: bcdb
# volumes:
# - mysql-dev:/var/lib/mysql
environment:
MYSQL_ALLOW_EMPTY_PASSWORD: "no"
MYSQL_ROOT_PASSWORD: "rootpwd"
MYSQL_USER: 'testuser'
MYSQL_PASSWORD: 'testpassword'
MYSQL_DATABASE: 'testdb'
MYSQL_ROOT_HOST: '%'
ports:
- "3301:3306"
networks:
- mysql-main
networks:
mysql-main:
driver: bridge
Test connection:
➜ ~ nc 127.0.0.1 3301
J
5.7.34OEaDdC���;2
Bpq?7O{mysql_native_password
^C
➜ ~
For the details, you can refer this excellent blog post.

Cannot connect to MySQL service in MySQL docker container?

So, I've got a MySQL docker container on which I run a script to write into the database, but when I execute the script, my script throws
mysql.connector.errors.DatabaseError: 2003 (HY000): Can't connect to MySQL server on '127.0.0.1' (111)
Do I have to start the MySQL service in the Dockerfile?
docker-compose.yml:
version: '3.3'
services:
db:
build: .
restart: always
environment:
MYSQL_ROOT_PASSWORD: test
MYSQL_DATABASE: test
MYSQL_USER: test
MYSQL_PASSWORD: test
volumes:
- ./db_data:/docker-entrypoint-initdb.d
First you need to expose the MySQL port 3306 or any other port you set for the MySQL
and then you can get help from commmand parameter.
A working sample would be like this:
version: '3.1'
services:
db:
image: mysql
container_name: mysql_test
command: --default-authentication-plugin=mysql_native_password
restart: always
environment:
MYSQL_ROOT_PASSWORD: root_password
MYSQL_DATABASE: test
MYSQL_USER: test_user
MYSQL_PASSWORD: test_password
ports:
- 30001:3306
expose:
- 30001
volumes:
- my-db:/var/lib/mysql
networks:
- test
networks:
test:
volumes:
my-db:
driver: local
With this sample code you will be able to connect with localhost:30001 to your database from host machine and mysql_test:3306 from other containers within the same network.
If you're trying to execute script in your host, you need to export port 3306
version: '3.3'
services:
db:
build: .
restart: always
environment:
MYSQL_ROOT_PASSWORD: test
MYSQL_DATABASE: test
MYSQL_USER: test
MYSQL_PASSWORD: test
ports:
- 3306:3306
volumes:
- ./db_data:/docker-entrypoint-initdb.d
If you're trying to execute inside docker (with a docker exec for example), check if container is up with docker ps

can not connect to mysql in docker's bridge network

I created a MySQL container but can only connect to the database from the host.
I start the MySQL container in this file:
docker-composer.yml:
version: '3.3'
services:
db:
image: mysql:5.7
ports:
- "3307:3306"
volumes:
- db_data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: somewordpress
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
wordpress:
depends_on:
- db
image: wordpress:latest
ports:
- "8000:80"
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
WORDPRESS_DB_NAME: wordpress
volumes:
db_data: {}
when run docker-compser up , I got this error:
MySQL Connection Error: (2002) No route to host
But I can succeed to connect to the container from the host, like this:
mysql -u root -p -h 127.0.0.1 -P 3307
You can try to connect without a port specifying:
WORDPRESS_DB_HOST: db
It turns out to be a firewall problem. I am using fedora 32 system. And the docker interface using the default zone which will block 80 port and 3306 port.
So, after I bind my docker interface to trusted zone and added some port everything worked.

Docker can't connect to MySQL running docker-compose up

I am using this Docker config: https://github.com/romaricp/kit-starter-symfony-4-docker
I start the environment by using:
docker-compose build
followed by:
docker-compose up -d
Everything is running fine but there is a problem with MySQL service. I get an Symfony error:
"An exception occurred in driver: could not find driver"
as well as PMA error when I try to login to DB:
mysqli_real_connect(): php_network_getaddresses: getaddrinfo failed:
Name does not resolve
Any idea how could I make it work?
docker-compose.yml:
version: '3'
services:
apache:
build: .docker/apache
container_name: sf4_apache
ports:
- 80:80
volumes:
- .docker/config/vhosts:/etc/apache2/sites-enabled
- .:/home/wwwroot/sf4
depends_on:
- php
mysql:
image: mysql
container_name: sf4_mysql
volumes:
- .docker/data/db:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: sf4
MYSQL_USER: sf4
MYSQL_PASSWORD: sf4
php:
build: .docker/php
container_name: sf4_php
volumes:
- .:/home/wwwroot/sf4
environment:
- maildev_host=sf4_maildev
depends_on:
- maildev
- mysql
phpmyadmin:
image: phpmyadmin/phpmyadmin
container_name: sf4_phpmyadmin
ports:
- 8080:80
links:
- mysql
maildev:
image: djfarrelly/maildev
container_name: sf4_maildev
ports:
- 8001:80
Also I opened the mysql logs and I see this:
2019-04-07T12:00:30.943414Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.15' socket: '/var/run/mysqld/mysqld.sock' port: 3306 MySQL Community Server - GPL.
Maybe the port is wrong and that's why I can't connect?
For phpmyadmin service, i think you should set the PMA_HOST and PMA_PORT environment variables like this:
phpmyadmin:
image: phpmyadmin/phpmyadmin
container_name: sf4_phpmyadmin
environment:
PMA_HOST: mysql
PMA_PORT: 3306
ports:
- 8080:80
links:
- mysql
Your mysql container should have the command instruction at the start to set the authentication plugin (there is an issue with the connectors with mysql 8), more details here
mysql:
image: mysql
container_name: sf4_mysql
command: "--default-authentication-plugin=mysql_native_password"
volumes:
- .docker/data/db:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: sf4
MYSQL_USER: sf4
MYSQL_PASSWORD: sf4
in your Symfony app, the connection string is located in .env file, and it should have the following format :
DATABASE_URL=mysql://mysql_user:mysql_user_password#mysql_host:mysql_port/db_name
mysql_user: your mysql user (ex: root)
mysql_user_password: the user's password (ex: root)
mysql_host: is should contain your mysql container service name
located in your docker-compose.yml file (mysql in this case)
mysql_port: mysql container internal port (3306, in this case)
db_name: the database you want to connect to.
the DATABASE_URL can look like this :
DATABASE_URL=mysql://root:root#mysql:3306/sf4
stop your containers after these changes and start them up again.
Hope this will help.
You have to expose the mysql port 3306:
mysql:
image: mysql
container_name: sf4_mysql
volumes:
- .docker/data/db:/var/lib/mysql
restart: always
ports:
- "3306:3306"
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: sf4
MYSQL_USER: sf4
MYSQL_PASSWORD: sf4
networks:
- default
I think you're having trouble linking the containers. I suggest that you use a custom network instead of linking. That all containers can see each other

docker phppyadmin container Access denied for user 'root'

I have installed docker on digitalocean droplet successfully and below shows my docker-compose.yml configurations:
version: '2.1'
services:
mysql:
build:
context: ./docker/mysql
image: mysql:latest
ports:
- 3306:3306
environment:
MYSQL_ROOT_PASSWORD: root_pass
MYSQL_USER: root
MYSQL_PASSWORD: root_pass
volumes:
- mysqldata:/var/lib/mysql
phpmyadmin:
image: phpmyadmin/phpmyadmin
container_name: phpmyadmin
links:
- mysql:db
environment:
MYSQL_USERNAME: root
MYSQL_ROOT_PASSWORD: root_pass
restart: always
ports:
- 8080:80
And once I up the docker services everything works fine as you can see by below screen capture.
And I can access my mysql database inside terminal perfectly with my user credentials.
But the problem is when I try to access phpmyadmin with droplet_ip:8080 its says:
#1045 - Access denied for user 'root'#'172.18.0.4' (using password: YES)
And here I used same username (root) password (root_pass) as well.
Any suggestions regarding this problem would be grateful. Thank you.
The following works for me
version: '3.1'
volumes:
mysql-volume:
services:
mysql:
image: mysql
container_name: mysql
volumes:
- mysql-volume:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: rootpass
MYSQL_DATABASE: db
MYSQL_USER: user
MYSQL_PASSWORD: supersecret
phpmyadmin:
image: phpmyadmin/phpmyadmin
container_name: phpmyadmin
environment:
PMA_HOST: mysql
MYSQL_USER: user
MYSQL_PASSWORD: supersecret
ports:
- 80:80
depends_on:
- mysql
don't mix up root user and MySQL user. In my example above I use the MySQL user to login with phpadmin. if you want to login using your root user you don't have to specify MySQL user and it will look like this (you don't need to specify a user for phpmyadmin because it's always root):
version: '3.1'
volumes:
mysql-volume:
services:
mysql:
image: mysql
container_name: mysql
volumes:
- mysql-volume:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: rootpass
MYSQL_DATABASE: db
phpmyadmin:
image: phpmyadmin/phpmyadmin
container_name: phpmyadmin
environment:
PMA_HOST: mysql
MYSQL_ROOT_PASSWORD: rootpass
ports:
- 80:80
depends_on:
- mysql
Also very important, remove your mysql volume when you want to recreate the whole setup. (docker volume rm ..). Because maybe your mysql is started with the same volume again after making changes.
I got the access denied error because I had a dollar sign in my password, which the docker compose file parses as variable substitution (I've only seen the ${} syntax) on this page you will read "Both $VARIABLE and ${VARIABLE} syntax are supported."
Therefore if I had for example this as my database password in the docker compose file: hello$world, the world variable would get substituted, of course I don't have a 'world' variable, the password that would be set in that case would be hello, so without knowing the docker compose syntax you would be trying to log in with hello$world, and you'd be denied access.