MySQL Master Slave configuration using Docker-compose - mysql

Okay so I wanted to make a master slave replication for mysql for my website hosted in nginx run by php. I've managed to connect to the master mysql however, there seems to have some error on my slave container. I was wondering if there is anything wrong or what I should add in order to solve this issue.
You need to specify one of MYSQL_ROOT_PASSWORD, MYSQL_ALLOW_EMPTY_PASSWORD and MYSQL_RANDOM_ROOT_PASSWORD
2020-10-15 09:09:44+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 1:10.5.6+maria~focal started.
2020-10-15 09:09:45+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
2020-10-15 09:09:45+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 1:10.5.6+maria~focal started.
2020-10-15 09:09:45+00:00 [ERROR] [Entrypoint]: Database is uninitialized and password option is not specified
You need to specify one of MYSQL_ROOT_PASSWORD, MYSQL_ALLOW_EMPTY_PASSWORD and MYSQL_RANDOM_ROOT_PASSWORD
My docker-compose.yml file
version: "3.3"
services:
mysql:
image: mariadb:latest
restart: always
ports:
- "3306"
environment:
MYSQL_REPLICATION_MODE: master
MYSQL_REPLICATION_USER: repl_user
MYSQL_REPLICATION_PASSWORD: repl_password
MYSQL_ROOT_PASSWORD: rootpw
MYSQL_DATABASE: database
MYSQL_USER: user
MYSQL_PASSWORD: mypw
networks:
- my-net
mysql-slave:
image: mariadb:latest
restart: always
ports:
- "3306"
depends_on:
- mysql
environment:
MYSQL_REPLICATION_MODE: slave
MYSQL_REPLICATION_USER: repl_user
MYSQL_REPLICATION_PASSWORD: repl_password
MYSQL_MASTER_HOST: mysql
MYSQL_MASTER_PORT_NUMBER: 3306
MYSQL_MASTER_ROOT_PASSWORD: rootpw
MYSQL_ROOT_PASSWORD: rootpw
networks:
- my-net
myphp:
build: ./src/php
restart: always
expose:
- "9000"
volumes:
- ./src:/var/www/html
depends_on:
- mysql
- mysql-slave
networks:
- my-net
mynginx:
image: nginx
restart: always
volumes:
- ./src/nginx.ini:/etc/nginx/conf.d/default.conf
- ./src/php-fpm.conf:/etc/php/7.4/fpm/php-fpm.conf
- ./src/www.conf:/etc/php/7.4/fpm/pool.d/www.conf
- ./src:/var/www/html/
ports:
- "8080:80"
depends_on:
- myphp
networks:
- my-net
networks:
my-net:
driver: bridge

Related

Spring Boot + docker-compose + MySQL not able to connect

I am trying sample application using spring boot + docker-compose + mysql. Below is my docker-compose,yml file
mysql:
image: mysql:latest
container_name: mysql-db
restart: always
command: --default-authentication-plugin=mysql_native_password
ports:
- "33061:3306"
networks:
- spring-boot-mysql-net
environment:
MYSQL_DATABASE: practice_db
MYSQL_ROOT_PASSWORD: root
volumes:
- ./database_storage:/docker-entrypoint-initdb.d
practice-service:
container_name: practice-service
build:
context: ./
dockerfile: Dockerfile
ports:
- "8080:8080"
depends_on:
- mysql
networks:
- spring-boot-mysql-net
restart: on-failure
command: sh -c './wait-for mysql:3306 -- npm start'
phpMyAdmin:
image: phpMyAdmin/phpMyAdmin
container_name: phpMyAdmin
restart: always
depends_on:
- mysql
environment:
PMA_HOST: database
PMA_PORT: 3306
ports:
- "9091:80"
networks:
spring-boot-mysql-net:
driver: bridge
application. Properties
spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://mysql:3306/practice_db
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
logging.level.org.hibernate.SQL=DEBUG
logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE
spring.jpa.hibernate.naming.physical-
strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
But my spring boot application giving error while connecting to MySQLDB
2023-02-11 16:31:10 com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
2023-02-11 16:31:10 The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
2023-02-11 16:31:10 Caused by: java.net.ConnectException: Connection refused
Also I am trying to run phpMyAdmin but that is also not connecting to MySQL. I am not sure MySQL container is starting or not because I can see logs of it as -
2023-02-11 16:31:42 2023-02-12T00:31:42.117825Z 0 [System] [MY-010931] [Server]
/usr/sbin/mysqld: ready for connections. Version: '8.0.32' socket: '
/var/run/mysqld/mysqld.sock' port: 3306 MySQL Community Server - GPL.
2023-02-11 16:31:30 2023-02-12 00:31:30+00:00 [Note] [Entrypoint]: Temporary server started.
2023-02-11 16:31:30 '/var/lib/mysql/mysql.sock' -> '/var/run/mysqld/mysqld.sock'
2023-02-11 16:31:36 2023-02-12 00:31:36+00:00 [Note] [Entrypoint]: Creating database
practice-db
2023-02-11 16:31:36
2023-02-11 16:31:36 2023-02-12 00:31:36+00:00 [Note] [Entrypoint]: Stopping temporary server
2023-02-11 16:31:40 2023-02-12 00:31:40+00:00 [Note] [Entrypoint]: Temporary server stopped
2023-02-11 16:31:40
2023-02-11 16:31:40 2023-02-12 00:31:40+00:00 [Note] [Entrypoint]: MySQL init process done.
Ready for start up.
I have example in github which works:
https://github.com/armdev/docker-mysql
My docker compose:
version: '3'
services:
mysqlnode:
image: mysqlnode
build: ./mysqlnode
container_name: "mysqlnode"
environment:
- MYSQL_ROOT_PASSWORD=root
- MYSQL_USER=admin
- MYSQL_PASSWORD=root
- MYSQL_DATABASE=flownet
volumes:
- /opt/mysql/logs/:/opt/mysql/logs
- /opt/mysql/data:/var/lib/mysql
ports:
- 3306:3306
networks:
- flownet
phpmyadmin:
image: phpmyadmin/phpmyadmin
container_name: "phpmyadmin"
links:
- mysqlnode
ports:
- 9191:80
environment:
MYSQL_USERNAME: root
MYSQL_ROOT_PASSWORD: root
PMA_HOST: mysqlnode
networks:
- flownet
networks:
flownet:
driver: bridge
MySQL user/password root:root

How to configure a pre-existing image with Docker Compose

I have an app that uses the MySQL image, and I need to supply to configure its bind-address. What is the best way of doing this? I have tried creating a file mysqld.cnf containing the below information, but can’t seem to get this working using the below code. You can see from the versions this is an old application, should that make a difference.
When starting the server, I still see:
[Note] Server hostname (bind-address): '*'; port: 3306
Is there either a way to make the below work, or another way to configure MySQL's bind-address?
mysqld.cnf:
[mysqld]
bind-address=0.0.0.0
docker-compose.yml:
version: '3.9'
services:
db:
image: mysql:5.7
volumes:
- devdb:/var/lib/mysql
- ./mysql.cnf:/etc/mysqld/conf.d/mysqld.cnf
environment:
MYSQL_DATABASE: 'pi3'
MYSQL_USER: 'dev'
MYSQL_PASSWORD: 'dev'
MYSQL_ROOT_PASSWORD: 'root'
ports:
- "33312:3306"
app:
build: .
volumes:
- .:/usr/src/app
- ./node_modules:/usr/src/app/node_modules
- ./logs:/usr/src/app/logs
- ${SSH_AUTH_SOCK}:/ssh-agent
ports:
- '3000:3000'
depends_on:
- db
environment:
- DATABASE_NAME=collabor8
- DATABASE_HOST=db
- DATABASE_USERNAME=root
- DATABASE_PASSWORD=root
- SSH_AUTH_SOCK=/ssh-agent
volumes:
devdb:

Mysql docker container keeps restarting

The Container keeps restarting.
I tried
docker-compose down -v
docker volume rm
The container was working fine earlier.
Logs
2021-03-27 13:16:08+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.23-1debian10 started.
2021-03-27 13:16:08+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
2021-03-27 13:16:08+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.23-1debian10 started.
2021-03-27 13:16:08+00:00 [ERROR] [Entrypoint]: MYSQL_USER="root", MYSQL_USER and MYSQL_PASSWORD are for configuring a regular user and cannot be used for the root user
Remove MYSQL_USER="root" and use one of the following to control the root user password:
- MYSQL_ROOT_PASSWORD
- MYSQL_ALLOW_EMPTY_PASSWORD
- MYSQL_RANDOM_ROOT_PASSWORD
Docker-compose.yml
mysql:
image: mysql:8.0
ports:
- 3306:3306
expose:
- "3306"
cap_add:
- SYS_NICE # CAP_SYS_NICE
volumes:
- ./cache/mysql:/var/lib/mysql
- ./conf-mysql.cnf:/etc/mysql/conf.d/mysql.cnf
environment:
- MYSQL_ROOT_PASSWORD=root
- MYSQL_PASSWORD=root
- MYSQL_USER=root
- MYSQL_DATABASE=mydb
restart: unless-stopped
Simply remove the MYSQL_USER and it will work fine because the root user gets created automatically.
PS. This seems to be a problem with a newer docker version because this used to work before and not throw an error.
User root is reserved and already created with mysql when it's up.
MYSQL_USER must be a different name, not root.
I faced the exact same problem and here is how I fixed it.
Go to your docker-compose.yml file and make the following changes:
"MYSQL_USER: root" to "MYSQL_ROOT_USER: root" then delete the
previous one.
"MYSQL_PASSWORD: YourPasseord" to "MYSQL_ROOT_PASSWORD:
YourPasseord" then delete the previous one.
Example:Here is my configuration...
database_server:
image: mysql:8.0
container_name: mysql
restart: always
environment:
MYSQL_DATABASE: DB_epraca
MYSQL_ROOT_USER: root
MYSQL_ROOT_PASSWORD: Password
MYSQL_ROOT_HOST: localhost
There was recent change how the official mysql docker which caused this issue. For further details you can check this PR on github.
For a quick solution you should remove MYSQL_USER=root and your docker-compose.yaml file should look something like this
mysql:
image: mysql:8.0
ports:
- 3306:3306
expose:
- "3306"
cap_add:
- SYS_NICE # CAP_SYS_NICE
volumes:
- ./cache/mysql:/var/lib/mysql
- ./conf-mysql.cnf:/etc/mysql/conf.d/mysql.cnf
environment:
- MYSQL_ROOT_PASSWORD=root
- MYSQL_DATABASE=mydb
restart: unless-stopped
Only update your the .env file:
DB_USERNAME=sail
DB_PASSWORD=password

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

How to establish database connection from wordpress docker

I try running a docker compose wordpress by using this guide: https://docs.docker.com/compose/wordpress/
This is the yaml file as described in the guide:
version: '3.3'
services:
db:
image: mysql:5.7
volumes:
- db_data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: wordpress
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
WORDPRESS_DEBUG: "true"
volumes:
db_data: {}
After I run my
"docker-compose up -d"
command, I go to "http://localhost:8000/" in my browser and get the white page with "Error establishing a database connection". According to the guide, wordpress should show me the 5 minute Installation already at this point. When I run the container with wordpress debug true, this error message is shown then:
Warning: mysqli_real_connect(): (HY000/2002): Connection refused in /var/www/html/wp-includes/wp-db.php on line 1612
Connection refused
I now use
docker exec it container_id /bin/bash
and type "mysql -p". Now I use the MYSQL_ROOT_PASSWORD from the docker compose file but I get access denied ("Access denied for user 'root'#'localhost' (using password: YES)")
I am not sure what I did earlier, but at some point it worked and I listed the databases and the mysql.users and the db and user were there.
So I dont even know, what the problem here is...
And why can I not access as root anymore? Does anyone know what to do?
EDIT: changed port back to 3306, I tried 3308 just to see if that may be a port issue
I found another post and they used this yaml. Still not sure why this works, but it does.
version: '3.3'
services:
db:
image: mysql:5.7
volumes:
- db_data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: wordpress
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
volumes:
db_data: {}
Check the logs of a service. To check the services, do:
$ docker-compose logs
...
db_1 | 2022-10-21 2:08:08 0 [Note] InnoDB: Buffer pool(s) load completed at 221021 2:08:08
db_1 | 2022-10-21 2:08:08 0 [Note] mysqld: ready for connections.
db_1 | Version: '10.6.4-MariaDB-1:10.6.4+maria~focal' socket: '/run/mysqld/mysqld.sock' port: 3306 mariadb.org binary distribution
For example, If you have a mariadb service named db and it doesn't print that way, you might have to wait for it to print.