Mysql docker container keeps restarting - mysql

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

Related

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

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.

mysql docker container exited with code 1

I want to run mysql on windows using docker container when i try use docker-compose up command on docker-compose file this the result.
> D:\dockerfiles>docker-compose up
db_1 | Initializing database
db_1 | 2018-10-08T09:00:29.024081Z 0 [Warning] Changed limits: max_open_files: 1024 (requested 5000)
db_1 | 2018-10-08T09:00:29.024224Z 0 [Warning] Changed limits: table_open_cache: 431 (requested 2000)
db_1 | 2018-10-08T09:00:29.024512Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
db_1 | 2018-10-08T09:00:29.028590Z 0 [ERROR] --initialize specified but the data directory has files in it. Aborting.
db_1 | 2018-10-08T09:00:29.028673Z 0 [ERROR] Aborting
db_1 |
dockerfiles_db_1 exited with code 1
and this my docker-compose.yml
version: '3.7'
services:
db:
image: mysql:5.7
restart: always
environment:
MYSQL_ROOT_PASSWORD: pass
MYSQL_DATABASE: star
MYSQL_USER: user
MYSQL_PASSWORD: pass
update of docker compose file
version: '3.7'
services:
mysqldb:
image: mysql:5.7
container_name: mysql_con1
command: --default-authentication-plugin=mysql_native_password
command: --disable-partition-engine-check
restart: always
environment:
MYSQL_ROOT_PASSWORD: pass
MYSQL_DATABASE: star
MYSQL_USER: user
MYSQL_PASSWORD: pass
volumes:
- "./:/var/lib/mysql"
networks:
- samplenet
networks:
samplenet:
driver: nat
There are some files in /var/lib/mysql directory. Remove everything from this directory.
Or highly recommended, use volumes in docker-compose.yml.
volumes:
- /my/own/datadir:/var/lib/mysql
Update:
I tested with the following compose file, it worked fine without any errors.
version: '3.7'
services:
db:
image: mysql:5.7
restart: always
volumes:
- ./data:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: pass
MYSQL_DATABASE: star
MYSQL_USER: user
MYSQL_PASSWORD: pass
For those who ran the container using the docker run command and added the --detach|-d flag; Remove and rerun the container without the -d flag.
This should provide the error message to why it fails.
I got the same behaviour defining restaart=always, If I remove it everything went fine, and later I run command
docker update --restart=always {{ID OF CONTAINER}}
Which makes it restart always. Hope this helps you.
Update Volume in docker-compose.yml
volumes:
- /my/own/datadir:/var/lib/mysql
Update: this compose file, it worked fine .
mysql:
image: mysql:5.7.22
container_name: mysql
restart: unless-stopped
tty: true
ports:
- "3307:3306"
volumes:
- /my/own/datadir:/var/lib/mysql
environment:
MYSQL_USERNAME: "root"
MYSQL_PASSWORD: "root"
MYSQL_ROOT_PASSWORD: "root"
MYSQL_DATABASE: test

docker-compose mysql container denies access to wordpress container

I have a problem with mysql 5.7 container denying access to wordpress container. I'm using docker-compose and I'm running docker on Mac OSX. Docker should be on latest version available.
Here's my docker-compose.yml
version: '2'
services:
wordpress:
depends_on:
- db
image: wordpress:latest
container_name: wordpress
ports:
- "8000:80"
- "443:443"
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_NAME: blog
WORDPRESS_DB_USER: blog_admin
WORDPRESS_DB_PASSWORD: userpasswd
networks:
- wordpress_net
db:
image: mysql:5.7
container_name: db
ports:
- "3306:3306"
volumes:
- db_data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: rootpasswd
MYSQL_DATABASE: blog
MYSQL_USER: blog_admin
MYSQL_PASSWORD: userpasswd
networks:
- wordpress_net
networks:
wordpress_net:
volumes:
db_data:
Logs from db container are:
2017-05-12T23:28:06.138429Z 321 [Note] Access denied for user 'blog_admin'#'172.19.0.3' (using password: YES)
Logs from wordpress container are:
MySQL Connection Error: (1045) Access denied for user 'blog_admin'#'172.19.0.3' (using password: YES)
Warning: mysqli::mysqli(): (HY000/1045): Access denied for user 'blog_admin'#'172.19.0.3' (using password: YES) in - on line 22
docker ps:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1b02f0146fe7 wordpress:latest "docker-entrypoint..." 25 minutes ago Up 26 seconds 0.0.0.0:443->443/tcp, 0.0.0.0:8000->80/tcp wordpress
5d932ed6c269 mysql:5.7 "docker-entrypoint..." 25 minutes ago Up 25 minutes 0.0.0.0:3306->3306/tcp db
What have I tried:
Restarting docker host.
docker-compose rm -v and then docker-compose up -d again.
Logging in with those user credentials and root credentials outside of wordpress container.
Removing docker images and pulling them again from scratch.
Using root credentials in WORDPRESS_DB_HOST, WORDPRESS_DB_USER
I can see all the env vars for db when I connect to db container. Wordpress container keeps restarting it self. I saw one answer on stack overflow which recommended flushing privileges and setting new user account but I want to know if I'm doing something wrong that could cause this problem to appear again on other machine.
Change:
WORDPRESS_DB_USER: blog_admin
WORDPRESS_DB_PASSWORD: userpasswd
To:
WORDPRESS_DB_USER: root
WORDPRESS_DB_PASSWORD: rootpasswd
And then:
docker-compose up -d --build
Your username Blog_admin doesn't have access to create database.
What have I done:
docker-compose rm -v hasn't worked for me as I've always used docker-compose down to shutdown containers. And I think this is the root of the problem.
I deleted the folder with my docker-compose.yml and created a new one.
Then I created a compose file with just the config for mysql container, launched it and tried to connect to the mysql server as root.
It worked. Then I had to stop the container with docker stop containerID.
Then I ran docker-compose rm -v(For some reason rm -v works only when you stop the container. Not when you use docker-compose down this caused the db's state to persist, as I used a volume for the db container) and completed the yml file with the wordpress container config.
I've ended up with something like this:
version: '2'
services:
wordpress:
image: wordpress:latest
container_name: wordpress-blog
depends_on:
- mysql
ports:
- "8000:80"
- "443:443"
restart: always
environment:
WORDPRESS_DB_HOST: mysql
WORDPRESS_DB_USER: admin
WORDPRESS_DB_PASSWORD: password
WORDPRESS_DB_NAME: wordpress
mysql:
image: mysql:5.7
container_name: mysql-db
ports:
- "3306:3306"
restart: always
environment:
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: wordpress
MYSQL_USER: admin
MYSQL_PASSWORD: password
NOTE: I had a problem previously not only connecting to the database from the wordpress container, but also from the db container itself. The method I described above helped me to solve this issue.
Simple solution
change
volumes:
- db_data:/var/lib/mysql
to something like
volumes:
- db_data:/var/lib/mysqlx

Docker Compose mysql import .sql

I'm having trouble importing an .sql dump file with docker-compose. I've followed the docs, which apparently will load the .sql file from docker-entrypoint-initdb.d. However, when I run docker-compose up, the sql file is not copied over to the container.
I've tried stopping the containers with -vf flag, but that didn't work either. Am I doing something wrong in my .yml script?
I have dump.sql in the directory database/db-dump/ in the root where my compose file is.
frontend:
image: myimage
ports:
- "80:80"
links:
- mysql
mysql:
image: mysql
ports:
- "3306:3306"
environment:
MYSQL_ROOT_PASSWORD: rootpass
MYSQL_USER: dbuser
MYSQL_PASSWORD: userpass
MYSQL_DATABASE: myimage_db
volumes:
- ./database/db-dump:/docker-entrypoint-initdb.d
This worked for me,
version: '3.1'
services:
db:
image: mysql
command: --default-authentication-plugin=mysql_native_password
restart: always
volumes:
- ./mysql-dump:/docker-entrypoint-initdb.d
environment:
MYSQL_ROOT_PASSWORD: example
MYSQL_DATABASE: ecommerce
adminer:
image: adminer
restart: always
ports:
- 8080:8080
mysql-dump must be a directory. All the .sql's in the directory will be imported.
After many attempts with the volumes setting i found a workaround
I created another image based on mysql with the following in the Dockerfile
FROM mysql:5.6
ADD dump.sql /docker-entrypoint-initdb.d
Then removed the volumes from compose and ran the new image
frontend:
image: myimage
ports:
- "80:80"
links:
- mysql
mysql:
image: mymysql
ports:
- "3306:3306"
environment:
MYSQL_ROOT_PASSWORD: rootpass
MYSQL_USER: dbuser
MYSQL_PASSWORD: userpass
MYSQL_DATABASE: myimage_db
This way the dump is always copied over and run on startup
This appears on the documentation page of Docker MySQL image: https://hub.docker.com/_/mysql/
Initializing a fresh instance
When a container is started for the first time, a new database with
the specified name will be created and initialized with the provided
configuration variables. Furthermore, it will execute files with
extensions .sh, .sql and .sql.gz that are found in
/docker-entrypoint-initdb.d. Files will be executed in alphabetical
order. You can easily populate your mysql services by mounting a SQL
dump into that
directory
and provide custom
images with contributed
data. SQL files will be imported by default to the database specified
by the MYSQL_DATABASE variable.
Mysql database dump schema.sql is residing in the /mysql-dump/schema.sql directory and it creates tables during the initialization process.
docker-compose.yml:
mysql:
image: mysql:5.7
command: mysqld --user=root
volumes:
- ./mysql-dump:/docker-entrypoint-initdb.d
environment:
MYSQL_DATABASE: ${MYSQL_DATABASE}
MYSQL_USER: ${MYSQL_USER}
MYSQL_PASSWORD: ${MYSQL_PASSWORD}
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
I was having a similar issue with mysql where I would mount a local directory at /configs/mysql/data containing a mydatabasedump.sql file via docker-compose to the docker-entrypoint-initdb.d volume,
the file would get loaded on to the container but not execute or populate the database when the container initialized. My intial docker-compose.yml looke like this:
#docker-compose.yml
version: '3'
services:
db:
build: ./build/mysql/ #this is pointing to my Dockerfile
container_name: MYSQL_Database
restart: always
environment:
MYSQL_PORT: 3306
MYSQL_ROOT_PASSWORD: admin
MYSQL_DATABASE: my_app_database
MYSQL_USER: admin
MYSQL_PASSWORD: admin
volumes:
- ./configs/mysql/data:/docker-entrypoint-initdb.d:
I found two working solutions for this problem:
The first came after I logged in the running container and confirmed that mydatabasedump.sq file was present and executable in the container's docker-entrypoint-initdb.d directory; I created and added
a bash script to my local /configs/mysql/data directory called dump.sh that excuted after the container was initialized. It contains a single mysql command that copies my_database_dump.sql to my_app_database.
The bash script looks like this
#!/bin/bash
#dump.sh
mysql -uadmin -padmin my_app_database < my_database_dump.sql
#end of dump.sh
I executed this script via my Dockerfile in the ENTRYPOINT directive like this:
#Dockerfile
FROM mysql:5.5
ENTRYPOINT [ "dump.sh" ]
EXPOSE 80
#end of Dockerfile
After realizing the initial issue was due to the volumes being mouted after the cotainer is built and therefore not intilizing the database with the dump file (or executing any scripts in that directory) at boot time, the second solution was simply to
move the volumes directive in my compose-file above the built directive. This worked and allowed me to remove the dump.sh scrip and the DOCKERENTRY directive in my Dockerfile.
The modified docker-compose.yml looks like this
#docker-compose.yml
version: '3'
services:
db:
volumes:
- ./configs/mysql/data:/docker-entrypoint-initdb.d
build: ./build/mysql/ #this is pointing to my Dockerfile
container_name: MYSQL_Database
restart: always
environment:
MYSQL_PORT: 3306
MYSQL_ROOT_PASSWORD: admin
MYSQL_DATABASE: my_app_database
MYSQL_USER: admin
MYSQL_PASSWORD: admin
I also have this problem. I mount a local directory at ./mysql-dump containing a init.sql file via docker-compose to the docker-entrypoint-initdb.d volume, the file would get loaded on to the container but not execute or populate the database when the container initialized.
My intial docker-compose.yml looke like this:
mysqld:
image: mysql
container_name: mysqld
volumes:
- ./mysql/data:/var/lib/mysql
- ./mysql/my.cnf:/etc/my.cnf
- ./init:/docker-entrypoint-initdb.d
env_file: .env
restart: always
environment:
- MYSQL_ROOT_PASSWORD=123456
- MYSQL_DATABASE=fendou
command: --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
--default-authentication-plugin=mysql_native_password
but it doesn't work for me.
I found another working solutions for this problem:
add --init-file /data/application/init.sql to mysql command.change above configuration like
mysqld:
image: mysql
container_name: mysqld
volumes:
- ./mysql/data:/var/lib/mysql
- ./mysql/my.cnf:/etc/my.cnf
# - ./init:/docker-entrypoint-initdb.d
env_file: .env
restart: always
environment:
- MYSQL_ROOT_PASSWORD=123456
- MYSQL_DATABASE=fendou
command: --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
--default-authentication-plugin=mysql_native_password
--init-file /docker-entrypoint-initdb.d/init.sql #attention here
hope it help for you
I wanted to keep the original setup of the container, so I tried a restore on the already running container. This seemed to work:
cat dump.sql | docker-compose exec -T db mysql -h localhost -u root -psomewordpress -v
But it was very slow and the verbose output seemed to be buffered, so I tried:
docker-compose cp dump.sql db:/tmp/
docker-compose exec db sh -c "mysql -h localhost -u root -psomewordpress -v < /tmp/dump.sql"
Which at least provided faster feedback.
Might be useful for someone? Looks like it was mainly slow because I used --skip-extended-insert on the dump, without the extended inserts it went faster 🙂