docker-compose run two instance of mysql - mysql

I want to run two instances of mysql using docker-compose.
I'm running MySQL in a Docker container and I have another Docker container running a python script to access the MySQL database.
One works fine on port 3306. In order to get two working, I thought I would just run the other one on a different port. But when I change it to a different port (e.g. 6603), but when I do, I get the below error:
mysql.connector.errors.InterfaceError: 2003: Can't connect to MySQL server on 'mysql:6603' (111 Connection refused)
I have read every question on s.o. I can find that seems relevant but none of the solutions work. I feel certain the fix will involve changing a line or two of configuration but I've spent many hours on this so far so any help would be greatly appreciated.
The docker-compose script is below (works fine if 6603 is replaced with 3306).
server:
build:
context: ../
dockerfile: Docker/ServerDockerfile
ports:
- "8080:8080"
links:
- mysql:mysql
volumes:
- ../py:/app
tty: true
mysql:
image: mysql
expose:
- "6603"
environment:
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: project
volumes:
- ./MySQL:/docker-entrypoint-initdb.d
- ./MySQL/data:/var/lib/mysql
And it is being accessed like this:
cnx = mysql.connector.connect(user='root', password='password',
host='mysql',
port="6603",
database='project')

Try to specify another port for MySQL by modifying its my.cnf file.

Have eventually found a couple of ways that work. The neatest one is for each app to create a network and connect the containers to it.
If each app uses a different network then mysql can run on 3306 on that Docker network and can be accessed on mysql://3306 from app1 and mysql2://3306 from app2. (Assuming you name you give the mysql service for app 2 is mysql2).
The Docker file with the new lines is below:
server:
build:
context: ../
dockerfile: Docker/ServerDockerfile
ports:
- "8080:8080"
volumes:
- ../py:/app
tty: true
networks:
-net
mysql2:
image: mysql
environment:
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: project
volumes:
- ./MySQL:/docker-entrypoint-initdb.d
- ./MySQL/data:/var/lib/mysql
networks:
-net
networks:
net:
driver: bridge
The Docker file for the second app is identical except the names are different (I put a 2 after each for simplicity).
server2:
build:
context: ../
dockerfile: Docker/ServerDockerfile
ports:
- "8081:8080"
volumes:
- ../py:/app
tty: true
networks:
-net2
mysql2:
image: mysql
environment:
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: project
volumes:
- ./MySQL:/docker-entrypoint-initdb.d
- ./MySQL/data:/var/lib/mysql
networks:
-net2
networks:
net2:
driver: bridge

Related

Docker Compose - Connection to Phpmyadmin and MysQL not working

I need a simple way with Docker-compose to create an environment with PHP, NGINX, MySQL and phpmyadmin.
I have already successfully created the PHP environment with NGINX.
Now I want to add a database with MySQL and phpmyadmin. These two components do not seem to work. For example, I cannot reach phpmyadmin through the specified port "8081". I reach my local servers with my local IP addresses and the ports at the end of the address.
As soon as I want to call phpmyadmin the browser window tells me "Can't connect to server".
Here is the docker-compose.yml file:
version: "3.9"
services:
web:
image: nginx:latest
ports:
- "8080:80"
volumes:
- ./src:/var/www/html
- ./default.conf:/etc/nginx/conf.d/default.conf
links:
- php-fpm
php-fpm:
image: php:8-fpm
volumes:
- ./src:/var/www/html
mysql:
image: mysql
container_name: mysql
environment:
MYSQL_ROOT_PASSWORD: '<mypassword>'
MYSQL_DATABASE: baton
MYSQL_USER: baton
MYSQL_PASSWORD: '<mypassword>'
ports:
- "3306:3306"
volumes:
- ./database/mysql:/var/lib/mysql
phpmyadmin:
image: phpmyadmin/phpmyadmin
container_name: pma
links:
- mysql
environment:
PMA_HOST: mysql
PMA_PORT: 3306
PMA_ARBITRARY: 1
restart: always
ports:
- 8081:80
Hope anyone can help!
Now I found the mistake I made to connect to the database via phpmyadmin. I got a second database, which already running on port 3306. I now switched to the existing database and now the connection works!

Can't connect to MySQL container from other container

I'm trying to connect my FASTAPI app container to a MySQL database container using the docker-compose file. In the Docker documentation it says that docker creates a default network for both containers. However, I would like to use a pre-existing network that I've created(app-net).
This is my docker-compose file:
version: '3.4'
services:
mysql:
image: mysql:latest
command: mysqld --default-authentication-plugin=mysql_native_password
container_name: mysql
restart: always
ports:
- 3307:3306
environment:
MYSQL_ROOT_PASSWORD: password
volumes:
- mysql-data:/var/lib/mysql
app:
build: .
image: app:1.0
container_name: app
ports:
- 8000:8000
environment:
PASSWORD: password
USERNAME: root
networks:
default:
external: true
name: app-net
volumes:
mysql-data:
driver: local
this is the output I get when i run docker inspect mysql -f "{{json .NetworkSettings.Networks }}":
{"app-net":{"IPAMConfig":null,"Links":null,"Aliases":["mysql","5e998f9fb646"],"NetworkID":"7f60c83e4c88d25e674461521446ec9fa98baca8639c782c79671c4fcb77ba88","EndpointID":"","Gateway":"","IPAddress":"","IPPrefixLen":0,"IPv6Gateway":"","GlobalIPv6Address":"","GlobalIPv6PrefixLen":0,"MacAddress":"","DriverOpts":null}}
However, when I run each container individually using CMD with the --network app-net the output is different:
{"app-net":{"IPAMConfig":null,"Links":null,"Aliases":["46157e588c87"],"NetworkID":"7f60c83e4c88d25e674461521446ec9fa98baca8639c782c79671c4fcb77ba88","EndpointID":"6a6922a9a6ea8f9d113447decbbb927cb93ddffd3b9563ee882fa2e44970cde5","Gateway":"172.20.0.1","IPAddress":"172.20.0.2","IPPrefixLen":16,"IPv6Gateway":"","GlobalIPv6Address":"","GlobalIPv6PrefixLen":0,"MacAddress":"02:42:ac:14:00:02","DriverOpts":null}}
In my app code in order to connect the mysql server, I specified the container name as the hostname since they are supposed to share the same network. But, as I mentioned it seems both containers can't talk to each other.
I'm pretty sure that is the reason I can't connect the database through my app and get that error when I run:
docker-compose -f docker-compose.yml up
I get this error:
sqlalchemy.exc.OperationalError: (_mysql_exceptions.OperationalError) (2005, "Unknown MySQL server host 'mysql' (0)")
What am I missing?
If the error you're getting is specifically "unknown host" or something similar, this can happen if your application container starts before the database container exists. You can work around this situation by telling Compose about the dependency:
version: '3.8'
services:
mysql: { ... }
app:
depends_on:
- mysql
This has two key effects. If you try to start only the application container docker-compose up app, it will also start the mysql container, following the depends_on: chain. When Compose does start containers, it at least creates the mysql container before creating the app container.
Note that this doesn't guarantee the database will actually be running by the time your application starts up. If you do encounter this, you will get a different error like "connection refused". You can work around this by embedding a script in your image that waits for the database to be available, or by using a version 2 Compose file with health-check support; see Docker Compose wait for container X before starting Y for more details on this specific problem.
You could add the key networks to both containers, this way:
version: '3.4'
services:
mysql:
image: mysql:latest
command: mysqld --default-authentication-plugin=mysql_native_password
container_name: mysql
restart: always
ports:
- 3307:3306
environment:
MYSQL_ROOT_PASSWORD: password
volumes:
- mysql-data:/var/lib/mysql
networks:
- app-net
app:
build: .
image: app:1.0
container_name: app
ports:
- 8000:8000
environment:
PASSWORD: password
USERNAME: root
networks:
- app-net
networks:
default:
external: true
name: app-net
volumes:
mysql-data:
driver: local

connecting to a mysql container: what's the equivalent of localhost inside a docker compose project?

I have this docker-compose.yml file, which has one service (log_app) trying to write to a mysql database (mydb).
version: '3.4'
services:
mydb:
container_name: mysql_data
restart: always
image: mysql:8.0.22
environment:
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PW}
MYSQL_DATABASE: ib
MYSQL_PASSWORD: ${MYSQL_PW}
volumes:
- ./init.sql:/docker-entrypoint-initdb.d/init.sql
- ./mysql-data:/var/lib/mysql
tws:
build: .
container_name: ib_logger_app
volumes:
- ./ib/IBController.ini:/root/IBController/IBController.ini
- ./ib/jts.ini:/root/Jts/jts.ini
environment:
TRADING_MODE: ${TWS_TRADING_MODE}
TWSUSERID: ${TWS_USER_ID}
TWSPASSWORD: ${TWS_PASSWORD}
FIXUSERID:
FIXPASSWORD:
XVFB_ARGS: -ac -screen 0 1024x768x16 +extension RANDR
restart: always
ports:
- 5901:5900
depends_on:
- mydb
log_app:
build: log_app/ib_client
environment:
- IB_GATEWAY_URLNAME=tws
- IB_GATEWAY_URLPORT=4004
- MKT_DATA_TYPE=4
restart: on-failure
depends_on:
- tws
- mydb
volumes:
mysql-data:
When I run this on a bare metal server, everything works fine, but in that situation, I connect to localhost on port 3306. This doesn't seem to work here, though. I get
Can't connect to MySQL server on 'localhost' (99)
I've been reading that 3306 is still the right port, but localhost and 127.0.0.1 are wrong now. Neither works for me.
I've also seen some answers on this site that recommend changing localhost to host.docker.internal, but that gives me this:
Can't connect to MySQL server on 'host.docker.internal' (111)
The host name will be the container name as defined in your docker-compose file.
In your example, you should use mydb:3306 instead of localhost:3306
EDIT:
one way to ensure that is to exec into the container that you want to connect from and run:
telnet mydb 3306
and see if it works (but t might require additional installation of telnet).

SQLSTATE[HY000] [2002] Connection refused (SQL: select * from information_schema.tables where table_schema = ms_api_shop

I am using Lumen with Docker to create simple API for authentication. After installing LumenPassport, I cannot migrate the database. I can easily connect to the MySQL db with Dbeaver.
I have already created one Lumen Docker project for the same purpose, it is the second. The first one worked without a problem. Moreover, I have checked the MySQL databases, ms_api_shop was there
Errors:
Here is my docker-compose
services:
nginx:
build:
context: .
dockerfile: docker/Nginx.Dockerfile
image: nginx
ports:
- 8092:80
depends_on:
- fpm
volumes:
- ./:/var/www/lumen-docker
links:
- mysql
fpm:
build:
context: .
dockerfile: docker/fpm.Dockerfile
volumes:
- ./:/var/www/lumen-docker
depends_on:
- mysql
links:
- mysql
mysql:
image: mysql:5.7
ports:
- 33006:3306
environment:
- MYSQL_ROOT_PASSWORD=
- MYSQL_DATABASE=ms_api_shop
- MYSQL_ROOT_USER=
volumes:
- mysql-data:/var/lib/mysql
volumes:
mysql-data:
And env:
DB_HOST=mysql
DB_PORT=33006
DB_DATABASE=ms_api_shop
DB_USERNAME=
DB_PASSWORD=
In your docker-file, you are binding the 33006 container port to the 3306 of the host port. In case you want to access the MySQL, you should use 3306 not 33006 as you did in your .env
I have a Laravel app running in Docker and a part of my docker config looks like:
But I personally feel that they should be within a docker network, look at the last two lines of the code in my docker-compose.yml below:
mysql:
image: mysql:5.7.29
container_name: mysql
restart: unless-stopped
tty: true
ports:
- "3306:3306"
environment:
MYSQL_DATABASE: homestead
MYSQL_USER: homestead
MYSQL_PASSWORD: secret
MYSQL_ROOT_PASSWORD: secret
SERVICE_TAGS: dev
SERVICE_NAME: mysql
volumes:
- ./mysql:/var/lib/mysql
networks:
- laravel
According to my knowledge, docker containers can communicate with each other when they are on the same network. You seem to have not connected these two docker in docker-compose yet. To get network information from a container, you can use the below command.
$ docker inspect --format='{{json .NetworkSettings.Networks}}' <docker_container_name>
In case you need to connect these two containers, follow these steps:
First, you create a network
$ docker network create my-net
Second, you connect container to the network.
$ docker network connect my-net <docker_container_name>
Don't forget to connect these two containers to the network.

Docker MYSQL '[2006] MySQL server has gone away'

i have a little problem with mysql server.
i created my docker-compose.yml, but when i want to access to phpMyAdmin (localhost:8080), an error message appeared sais that:
"phpMyAdmin tried to connect to the MySQL server, and the server rejected the connection. Please check the values ​​of host, username and password in the configuration and make sure they match the information provided by the MySQL server administrator".
Here is my docker-compose file and thanks for helping me
version: '2'
services:
apache:
image: rafaelcgstz/magento2
# build: .
ports:
- 80:80
- 9001:9000
# - "35729:35729" # live reload
volumes:
- ./src:/var/www/html
- ~/.composer:/var/www/.composer
- ~/.npm:/var/www/.npm
# - ~/.nvm:/var/www/.nvm
environment:
XDEBUG_CONFIG: "remote_host=localhost"
PHP_IDE_CONFIG: "serverName=Docker"
depends_on:
- db
links:
- db
networks:
- magento-network
db:
image: mariadb
ports:
- 3300:3306
environment:
- MYSQL_ROOT_PASSWORD=root
- MYSQL_DATABASE=magento
- MYSQL_USER=magento
- MYSQL_PASSWORD=magento
volumes:
- dbdata:/var/lib/mysql
networks:
- magento-network
phpmyadmin:
image: phpmyadmin/phpmyadmin
environment:
- PMA_HOST=db
- PMA_USER=root
- PMA_PASSWORD=root
- MYSQL_ROOT_PASSWORD=root
ports:
- 8080:80
networks:
- magento-network
redis:
image: redis
ports:
- 6379
networks:
- magento-network
redis-session:
image: redis
ports:
- 6379
networks:
- magento-network
mailhog:
image: mailhog/mailhog
ports:
- 1025:1025
- 8025:8025
networks:
- magento-network
networks:
magento-network:
driver: bridge
volumes:
dbdata:
driver: local
Seems like you have a typo in mariadb service definition:
ports:
- 3300:3306
You configured port mapping so that container is reachable at 3300 but you did not pass this information to PHPMyadmin. As a result connection attempt just times out.
Side note: you do not need to expose port for database at all - other containers will communicate with it using Docker's virtual network and for local access you can use docker container -it exec <container-id> mysql... or docker-compose exec db mysql...
while it appears there is a typo for the port with this post I want to point out that it's important to make sure that your user has access 'TO' and 'FROM' the appropriate IP.
This is an (wide open -adjust as needed) example for updating the access domain when running adminer / phpadmin via docker:
GRANT ALL ON . TO 'admin'#'172.18.0.0/255.255.255.0' IDENTIFIED BY 'password' WITH GRANT OPTION;
p.s. I'm adding this answer here because I also landed on this page with the same error.
I solved my problem with this command:
docker-compose destroy
sudo rm -rf db/
docker-compose up -d
docker-compose web
composer install
symfony d:m:m --no-interaction
symfony d:f:l --no-interaction