mysql docker container exited with code 1 - mysql

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

Related

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: "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

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.

Unknown authentication method error while using wordpress and mysql docker images

I am trying to install wordpress with mysql via docker but I have communication errors between the container of the DB and the wordpress container.
here is my docker-compose.yml file
version: '3'
services:
db:
image: mysql:latest
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
links:
- 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:
If I execute the "docker-compose up -d" command and that I open the logs of the wordpress container I have this error:
...
wordpress_1 | Warning: mysqli::__construct(): The server requested authentication method unknown to the client [caching_sha2_password] in Standard input code on line 22
wordpress_1 |
wordpress_1 | Warning: mysqli::__construct(): (HY000/2054): The server requested authentication method unknown to the client in Standard input code on line 22
wordpress_1 |
wordpress_1 | MySQL Connection Error: (2054) The server requested authentication method unknown to the client
...
I added command: '--default-authentication plugin=mysql_native_password' in the db section and i changed the mysql version to 5.7 but it did not help me solve the problem.
I also visited these discussions :
Wordpress on docker-compose no run
https://serverfault.com/questions/880773/unable-to-access-wordpress-site-created-as-a-docker-stack/880777#880777
https://github.com/docker-library/wordpress/issues/313
Thank you in advance for your proposals.
It seems that WordPress/PHP doesn't support MySQL v8 yet. Even wordpress docker image readme suggests to use MySQL v5.7. After changing mysql version to v5.7, you might notice that MySQL container crashes with an error similar to below:
...
db_1 | 2019-01-28T18:45:24.611045Z 0 [ERROR] [FATAL] InnoDB: Table flags are 0 in the data dictionary but the flags in file ./ibdata1 are 0x4800!
db_1 | 2019-01-28 18:45:24 0x7f00e013a740 InnoDB: Assertion failure in thread 139641736111936 in file ut0ut.cc line 942
db_1 | InnoDB: We intentionally generate a memory trap.
...
To fix this, easiest way is to delete the MySQL docker volume using docker-compose down -v. After that, docker-compose up -d should work.
If that still doesn't work, use the docker-compose example from https://hub.docker.com/_/wordpress/. Adding it here in case it gets pulled down in future.
version: '3.1'
services:
wordpress:
image: wordpress
restart: always
ports:
- 8080:80
environment:
WORDPRESS_DB_HOST: db
WORDPRESS_DB_USER: exampleuser
WORDPRESS_DB_PASSWORD: examplepass
WORDPRESS_DB_NAME: exampledb
db:
image: mysql:5.7
restart: always
environment:
MYSQL_DATABASE: exampledb
MYSQL_USER: exampleuser
MYSQL_PASSWORD: examplepass
MYSQL_RANDOM_ROOT_PASSWORD: '1'