Can't run MYSQL container using docker-compose - mysql

I'm trying to run a MYSQL container for my project using docker-compose.
When I use the following command, everything works fine:
docker run --name mysql-db -e MYSQL_ROOT_PASSWORD=password -p 3306:3306 -d mysql:latest
But when I built this docker-compose file:
version: '3.8'
services:
mysql-db:
container_name: mysql-db
image: mysql:latest
environment:
- MYSQL_DATABASE='crm'
- MYSQL_ROOT_PASSWORD='password'
expose:
- "3306"
ports:
- "3306:3306"
The command:
docker-compose up --build
I got this message error:
mysql-db | '/var/lib/mysql/mysql.sock' ->
'/var/run/mysqld/mysqld.sock' mysql-db | Warning: Unable to
load '/usr/share/zoneinfo/iso3166.tab' as time zone. Skipping it.
mysql-db | Warning: Unable to load
'/usr/share/zoneinfo/leapseconds' as time zone. Skipping it. mysql-db
| Warning: Unable to load '/usr/share/zoneinfo/tzdata.zi' as time
zone. Skipping it. mysql-db | Warning: Unable to load
'/usr/share/zoneinfo/zone.tab' as time zone. Skipping it. mysql-db
| Warning: Unable to load '/usr/share/zoneinfo/zone1970.tab' as time
zone. Skipping it. mysql-db | ERROR 1064 (42000) at line 5: You
have an error in your SQL syntax; check the manual that corresponds to
your MySQL server version for the right syntax to use near '''' at
line 1 mysql-db exited with code 1
I also notice that there is this log right here says that I didn't set the MYSQL_ROOT_PASSWORD although I've already declared that
mysql-db | 2023-01-10T07:29:03.521464Z 6 [Warning] [MY-010453]
[Server] root#localhost is created with an empty password ! Please
consider switching off the --initialize-insecure option.
I'm extremely new to docker so hope you guys can help me.

I tried to reproduce with your compose file locally on my machine and it resulted into same error.
Only solution for this problem currently lies in the link shared by #SelVazi. We would need to build your custom mysql image and then use that to create mysql container.
Here is an example
Create a Dockerfile
I picked mysql image with tag 8 as I am running on Mac M1.
FROM mysql:8.0.31-debian
RUN apt-get update \
&& apt-get install --no-install-recommends -y tzdata \
&& apt-get clean \
&& rm -r /var/lib/apt/lists/*
Create Docker Compose file
version: '3.8'
services:
mysql-local:
build: .
image: mysql-local:latest
ports:
- "3307:3307"
container_name: mysql-local
# I am running on Mac M1 Apple Silicon, You might not need it.
platform: linux/x86_64
mysql-db:
container_name: mysql-db
image: mysql-local:latest
depends_on:
- mysql-local
environment:
- MYSQL_DATABASE='crm'
- MYSQL_ROOT_PASSWORD='password'
expose:
- "3306"
ports:
- "3306:3306"
Run compose file
docker-compose -f mysql-compose.yaml up --build

Related

Issue with mysql in docker-compose on Mac

I have an issue when trying to use a mysql docker container as part of a docker-compose on my MacBook (2,6 GHz 6-Core Intel Core i7 under Ventura 13.0.1). Here is what my docker-compose.yml file looks like:
version: '3.1'
services:
db:
image: mysql:8.0
environment:
- MYSQL_DATABASE=${MYSQL_DATABASE}
- MYSQL_USER=${MYSQL_USER}
- MYSQL_PASSWORD=${MYSQL_PASSWORD}
- MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD}
expose:
- 3306
healthcheck:
test: mysqladmin ping -h 127.0.0.1 -u ${MYSQL_USER} --password=${MYSQL_PASSWORD}
interval: 5s
timeout: 5s
retries: 55
restart: unless-stopped
flask:
build:
context: ./
dockerfile: Dockerfile
volumes:
- ./:/var/www/
working_dir: /var/www/
command: python3 app.py
depends_on:
- "db"
restart: unless-stopped
nginx:
image: nginx:1.13.7
container_name: nginx
volumes:
- ./nginx.conf:/etc/nginx/nginx.conf
- ./static:/var/www/html
ports:
- 80:80
depends_on:
- "flask"
restart: unless-stopped
When trying to run docker-compose up, the mysql container crashes, and I get the following error in the logs:
compose_2023-db-1 | 2023-02-09T22:33:31.523197Z 0 [ERROR] [MY-013236] [Server] The designated data directory /var/lib/mysql/ is unusable. You can remove all files that the server added to it.
compose_2023-db-1 | 2023-02-09T22:33:31.523257Z 0 [ERROR] [MY-010119] [Server] Aborting
compose_2023-db-1 | 2023-02-09T22:33:31.523345Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.32) MySQL Community Server - GPL.
As visible in my docker-compose.yml, I am not even trying to persist the database in a volume. I have actually tried, both using host file system and a docker volume, with the exact same error each time.
I have tried restarting Docker, my computer, updating Docker, removing all images and containers without success. I also tried several tags for mysql images (namely latest, 7.6 and 8.0) with the same result.
What really bugs me is that this exact same setup used to work fine until earlier today, but I am not able to tell what action caused it to start erroring out every time. More strangely, I can run a mysql container with a similar setup from with the following command:
-e MYSQL_ROOT_PASSWORD=root \
-e MYSQL_DATABASE=db \
-e MYSQL_USER=myuser \
-e MYSQL_PASSWORD=mypass \
-v $(pwd)/db:/var/lib/mysql \
-p 3306:3306 \
mysql:5.7
It seems that I am not the only one having this type of problem according to this issue on GitHub: https://github.com/docker-library/mysql/issues/757. As suggested on the last comment and since none of the pieces of advice I could find there worked for me, I would be super grateful for any leads on how to fix this.

MySQL error: Can't initialize batch_readline - may be the input source is a directory or a block device

Mysql 5.7 fails trying to setup init script while starting in docker container. System is Mac ARM64
Docker compose looks like this:
mysql:
image: mysql:5.7
container_name: mysql
environment:
MYSQL_ROOT_PASSWORD: 'root'
ports:
- '3306:3306'
expose:
- '3306'
volumes:
- ./init.sql:/docker-entrypoint-initdb.d/init.sql
- mysql:/var/lib/mysql:rw
As result Mysql throws error:
mysql | ERROR: Can't initialize batch_readline - may be the input source is a directory or a block device.
mysql exited with code 1
The problem is that docker compose doesn't recognize '.' as current directory.
Solution is:
volumes:
- $PWD/init.sql:/docker-entrypoint-initdb.d/init.sql

How to fix error "Database is uninitialized and password option is not specified"

I'm working on a Mac computer.
I tried to install a nginx server and a MySQL database with docker compose.
Here is the content docker-compose.xaml file:
version: '3'
services:
web:
image: nginx
db:
image: mysql
ports:
- "3307:3306"
environment:
- MYSQLROOT_PASSWORD=password
- MYSQL_USER=user
- MYSQL_PASSWORD=password
- MYSQL_DATABASE=demodb
When executing the next command: docker-compose -f docker-compose.yaml up
I get the next error message:
[ERROR] [Entrypoint]: Database is uninitialized and password option is
not specified db_1 | You need to specify one of the following: db_1 |
MYSQL_ROOT_PASSWORD db_1 | - MYSQL_ALLOW_EMPTY_PASSWORD db_1 | - MYSQL_RANDOM_ROOT_PASSWORD
I will enjoy if you can help me.
It seems like you forgot an underscore in MYSQL_ROOT_PASSWORD? Change it to:
environment:
- MYSQL_ROOT_PASSWORD=password

How do I fix "Table 'performance_schema.session_variables' doesn't exist" with Docker compose?

I am currently beginning experiments with docker-compse. I've created a minimal Flask + MySQL webservice which works perfectly fine without Docker. Now I would like to see how I can make deployment easy with Docker.
My docker-compose.yml looks like this:
version: '3'
services:
web:
build: .
command: app.py
ports:
- "8082:8082"
volumes:
- .:/code
links:
- db
hostname: myappserver
environment:
- MYSQL_ROOT_PASSWORD=p#ssw0rd123
- MYSQL_DATABASE=flask_db
- MYSQL_HOST=db
- MYSQL_PORT=3306
db:
image: mysql
ports:
- "3307:3306"
environment:
- MYSQL_ROOT_PASSWORD=p#ssw0rd123
- MYSQL_DATABASE=flask_db
- MYSQL_HOST=mysqlserver
When I run sudo docker-compose up --build I get
web_1 | sqlalchemy.exc.ProgrammingError: (mysql.connector.errors.ProgrammingError) 1146 (42S02): Table 'performance_schema.session_variables' doesn't exist [SQL: "SHOW VARIABLES LIKE 'sql_mode'"]
db_1 | 2017-07-21T22:27:26.553761Z 3 [Note] Aborted connection 3 to db: 'flask_db' user: 'root' host: '172.18.0.3' (Got an error reading communication packets)
What is the problem and how do I fix it?
(The complete project is here: https://github.com/MartinThoma/flask_mysql_dockerized - it's really tiny)
No duplicate of
1: I know if this was on my machine, that I could fix it with mysql_upgrade -u root -p --force and /etc/init.d/mysql start. But the point of using docker compose is to make it really simple to start a web service. One command. If I have to tell the user to login into the container, run those commands, restart the other container.. then I could simply ask him to install it on the main system / run docker containers manually.
So here is what I did to fix it, where I hope that this is possible to be done less manually:
Start the containers:
# docker-comopse up
Find the container ID of the mysql image:
# docker container ls
Enter the container
# sudo docker exec -it 4f7d3f0763ad bash
Upgrade
# mysql_upgrade -u root -p --force
Exit the container, then restart
# docker-compose up
Initialize the database:
# cd /docker-entrypoint-initdb.d/
# mysql -h localhost -u root -p
mysql> source flask_db.sql;
You need to add database to your mysql service:
volumes:
- ./flask_db.sql:/docker-entrypoint-initdb.d/flask_db.sql
Tip: It is recommended to use smaller images so that your image has only required basic components and free from extra non-essential stuff.

Docker mysql instant exit

I have a problem with Mysql Docker which exits when I run my docker-compose up command.
Here is my docker-compose.yml file :
version: "2"
services:
web:
build: ./app
links:
- "db-mongo:db-mongo"
- "db-mysql:db-mysql"
ports:
- "443:3000"
volumes:
- "./app:/src"
- "/src/.sass-cache"
- "/src/node_modules"
- "/src/lib"
db-mongo:
build: ./mongo
ports:
- "27017:27017"
volumes:
- "./mongo/db:/data"
db-mysql:
image: mysql
ports:
- "3306:3306"
volumes:
- "./mysql/db:/var/lib/mysql"
- "./mysql/log:/var/log/mysql"
- "./mysql/conf.d:/etc/mysql/conf.d"
environment:
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: testt
Folders ./mysql/db and ./mysql/log are empty.
When I run docker-compose up, here the output :
db-mysql_1 | Initializing database
server_db-mysql_1 exited with code 1
When I run docker ps -a :
0a5a7a643f18 mysql "docker-entrypoint.sh" 8 minutes ago Exited (1) 7 minutes ago server_db-mysql_1
The strange thing is if I run docker run -d --name=new-mysql -p 3306:3306 -v /var/www/server/mysql/db:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=password mysql it works...
And here the output of docker logs 97c -f:
Initializing database
Database initialized
MySQL init process in progress...
Warning: Unable to load '/usr/share/zoneinfo/Factory' as time zone. Skipping it.
Warning: Unable to load '/usr/share/zoneinfo/iso3166.tab' as time zone. Skipping it.
Warning: Unable to load '/usr/share/zoneinfo/leap-seconds.list' as time zone. Skipping it.
Warning: Unable to load '/usr/share/zoneinfo/posix/Factory' as time zone. Skipping it.
Warning: Unable to load '/usr/share/zoneinfo/right/Factory' as time zone. Skipping it.
Warning: Unable to load '/usr/share/zoneinfo/zone.tab' as time zone. Skipping it.
MySQL init process done. Ready for start up.
Why does my docker-compose can't keep alive my mysql docker ? I must missing something... Help ! Thanks
EDIT : It appears it is my conf.d folder which is making some problems, because when I remove volume - "./mysql/conf.d:/etc/mysql/conf.d", server_db-mysql keeps alive.
Here is the mysql/conf.d/my.cnf file content :
[mysqld]
general_log_file = /var/log/mysql/mysql.log
general_log = 1
Why does that file is crashing the mysql ?
It's a permission issue when mounting /var/log/mysql in your custom config file.
Please reference this issue #docker-mysql-146 for the solution:
The owner and group of the /var/log/mysql directory has to be as expected by mysql. Ensure that, execute:
docker exec mysql chown mysql:root /var/log/mysql