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

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

Related

Docker MySql Operation Not Permitted

I have my docker compose as follows but am getting an operation not permitted mbind issue. It's been awhile but as far as I remember it was seeding with the db/init before. I have 2 sql files in there that I exported from a seeded db in the past via MySQL Workbench.
db_1 | 2022-05-17T03:52:16.766919Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.28' socket: '/var/run/mysqld/mysqld.sock' port: 3306 MySQL Community Server - GPL.
db_1 | mbind: Operation not permitted
db:
command: '--default-authentication-plugin=mysql_native_password'
environment:
- MYSQL_DATABASE=investing
- MYSQL_ROOT_PASSWORD=777
image: mysql:8.0.28
ports:
- "3307:3306"
restart: on-failure
volumes:
- "./db/init:/tmp/docker-entrypoint-initdb.d"
Very curious thing but apparently I now needed to add security options to docker compose. I haven't upgraded docker but I did upgrade my Linux OS. I can't think of any other major system changes to prompt the issue.
security_opt:
- seccomp:unconfined
db:
command: '--default-authentication-plugin=mysql_native_password'
environment:
- MYSQL_DATABASE=investing
- MYSQL_ROOT_PASSWORD=777
image: mysql:8.0.28
ports:
- "3307:3306"
restart: on-failure
security_opt:
- seccomp:unconfined
volumes:
- "./db/init:/tmp/docker-entrypoint-initdb.d"
My db/init is still no longer seeding but that may be out of the scope of this question.

Docker MySQL Container takes too long to initialize

Once I run docker-compose up, it takes over 4 minutes for my mysql container to log out the line.
db_1 | 2021-03-09T08:29:28.459612Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.23' socket: '/var/run/mysqld/mysqld.sock' port: 3306 MySQL Community Server - GPL.
The line is important because it is only after its respective process is finished, that I can make connections to the database.
This is my docker setup.
docker-compose.yml
version: '3.1'
services:
php:
build:
context: .
dockerfile: Dockerfile
ports:
- 80:80
volumes:
- ./src:/var/www/html
db:
image: mysql
command: --default-authentication-plugin=mysql_native_password
restart: always
environment:
MYSQL_ROOT_PASSWORD: example
adminer:
image: adminer
restart: always
ports:
- 8080:8080
Dockerfile
FROM php:7.2-apache
RUN docker-php-ext-install mysqli
I have an i7-2600k and 16GB of ram.
I don't think my PC is potato enough to be this slow.
Any help on how I can speed up this process?

MySQL Master Slave configuration using Docker-compose

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

Why does mariadb connection fail via docker?

My docker-compose.yml looks like this:
version: '3'
services:
database:
build:
context: ./database
environment:
- MYSQL_DATABASE=${DATABASE_NAME}
- MYSQL_USER=${DATABASE_USER}
- MYSQL_PASSWORD=${DATABASE_PASSWORD}
- MYSQL_ROOT_PASSWORD=${DATABASE_ROOT_PASSWORD}
ports:
- "3306:3306"
volumes:
- ./database/init.sql:/docker-entrypoint-initdb.d/init.sql
- ./database/data:/var/lib/mysql
php-fpm:
build:
context: ./php-fpm
depends_on:
- database
environment:
- APP_ENV=${APP_ENV}
- APP_SECRET=${APP_SECRET}
- DATABASE_URL=mysql://${DATABASE_USER}:${DATABASE_PASSWORD}#database:3306/${DATABASE_NAME}?serverVersion=5.7
volumes:
- ../src:/var/www
nginx:
build:
context: ./nginx
volumes:
- ../src:/var/www
- ./nginx/nginx.conf:/etc/nginx/nginx.conf
- ./nginx/sites/:/etc/nginx/sites-available
- ./nginx/conf.d/:/etc/nginx/conf.d
- ./logs:/var/log
depends_on:
- php-fpm
ports:
- "80:80"
- "443:443"
With this .env file:
DATABASE_NAME=testDB
DATABASE_USER=appuser
DATABASE_PASSWORD=apppassword
DATABASE_ROOT_PASSWORD=password
APP_ENV=dev
APP_SECRET=24e17c47430bd2044a61c131c1cf6990
Result after i ran docker-compose build and docker-compose-up:
Version: '10.5.4-MariaDB-1:10.5.4+maria~focal' socket: '/run/mysqld/mysqld.sock' port: 3306 mariadb.org binary distribution
database_1 | 2020-06-25 17:13:10 3 [Warning] Aborted connection 3 to db: 'unconnected' user: 'unauthenticated' host: 'symfonytestproject_php-fpm_1.symfonytestproject_default' (This connection closed normally without authentication)
And now the console stuck and i have to force a quit.
I tried always everything: removing volumes, rebuild volumes...
Iam very thankfull when anyone can help me with this problem.

Docker: "wait-for-it" script does not start the application when MySQL is ready

I am using Docker-Compose and wait-for-it to start my backend with Node.js once the MySQL service is ready to receive connections. My problem is that the script does not realize the connection is ready by itself. I can make it work if I set the timeout to 20s since MySQL would be running by then, but that could change in different environments and the app would crash if it took more time for a specific environment. I want it to check it periodically since I think it is the right thing to do.
This is my docker-compose.yml:
version: "3.8"
services:
app:
image: pfmc
ports:
- 4005:4005
working_dir: /usr/src/app
command: sh -c './wait-for-it.sh -t 0 db:3306 -- npm start'
environment:
MYSQL_ROOT_PASSWORD: pfmc123
MYSQL_HOST: mysql
MYSQL_USER: root
MYSQL_PASSWORD: pfmc123
MYSQL_DATABASE: pfmc
mysql:
image: mysql:5
command: --default-authentication-plugin=mysql_native_password
restart: always
volumes:
- ./db-startup:/docker-entrypoint-initdb.d
environment:
MYSQL_ROOT_PASSWORD: pfmc123
MYSQL_DATABASE: pfmc
The logs:
app_1 | wait-for-it.sh: waiting for db:3306 without a timeout
mysql_1 | 2020-05-25 19:59:58+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 5.7.30-1debian10 started.
mysql_1 | 2020-05-25 19:59:58+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
mysql_1 | 2020-05-25 19:59:58+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 5.7.30-1debian10 started.
mysql_1 | 2020-05-25 19:59:58+00:00 [Note] [Entrypoint]: Initializing database files
and it stays at this point:
mysql_1 | 2020-05-25T20:00:08.243448Z 0 [Note] mysqld: ready for connections.
mysql_1 | Version: '5.7.30' socket: '/var/run/mysqld/mysqld.sock' port: 3306 MySQL Community Server (GPL)
Thank you very much for your help.
You do have one issues in here: your MySQL container is a service named mysql not db you should either adapt the service to be named db or the wait-for-it invocation to poll on a connection to mysql:3306
All together, your docker-compose.yml should look like this – I added the fix with comment stating #fixme:
version: "3.8"
services:
app:
image: pfmc
ports:
- 4005:4005
working_dir: /usr/src/app
command: sh -c './wait-for-it.sh -t 0 db:3306 -- npm start'
environment:
MYSQL_ROOT_PASSWORD: pfmc123
# A first #fixme is here
MYSQL_HOST: db
MYSQL_USER: root
MYSQL_PASSWORD: pfmc123
MYSQL_DATABASE: pfmc
# And a second #fixme is here
db:
image: mysql:5
command: --default-authentication-plugin=mysql_native_password
restart: always
volumes:
- ./db-startup:/docker-entrypoint-initdb.d
environment:
MYSQL_ROOT_PASSWORD: pfmc123
MYSQL_DATABASE: pfmc