Issue with mysql in docker-compose on Mac - mysql

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.

Related

Can't run MYSQL container using docker-compose

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

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

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

Mysql refuses Connection to Adminer on Docker

I know this is a duplicate of this, but since that was never answered, I am re-posting this question.
I am trying to build a basic connection of php-apache and mysql containers.
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
# Exposing the ports doesn't help either
# ports:
# - 3306:3306
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 run a simple docker-compose up command, and access Adminer on localhost:8080.
However, despite using the default login details, i.e
server: db
username: root
password: example
I get the error SQLSTATE[HY000] [2002] Connection refused.
Screenshot:
I think the issue might be some configuration issue on my local machine, because I couldn't use docker based LAMP stacks made by other people too.
Any help is greatly appreciated,
Thank You!
Turns out Patience was the answer.
If I wait long enough(around 4.5 minutes), the mysql container lets out a small log detail.
db_1 | 2021-03-09T08:21:03.882088Z 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.
After that the login in works.
Note: There is a similar line that is logged, but that is regarding port 0, this is 3306. Attempting a failure immediately after that fails.
One must wait for the line with port 3306.
If any of you can give me feedback or advice on how I could make my mysql container initialize faster,
I'd greatly appreciate it.

docker-compose mysql persistent data

I'm new to docker and I'm following this guide to setup my mac (the guide is for linux but is not the point) for a PHP dev env with docker-compose.
The guide is quite good and everything seems to work correctly, there is just a part that doesn't seem to work.
If you go to Step 9 — Creating a User for MySQL, I'm running docker-compose up and I have these errors so I can't access the db container.
mysqld: Error on realpath() on '/var/lib/mysql-files' (Error 2 - No such file or directory)
db | 2020-04-16T18:22:19.603226Z 0 [ERROR] [MY-010095] [Server] Failed to access directory for --secure-file-priv. Please make sure that directory exists and is accessible by MySQL Server. Supplied value : /var/lib/mysql-files
db | 2020-04-16T18:22:19.603246Z 0 [ERROR] [MY-010119] [Server] Aborting
If I remove the - ./mysql/my.cnf:/etc/mysql/my.cnf from the volumes everything seem to work but then for some reason my laravel db is not created.
these command is not supposed to already create a DB?
environment:
MYSQL_DATABASE: laravel
MYSQL_ROOT_PASSWORD: root
another question is where the data is stored?
db:
...
volumes:
- dbdata:/var/lib/mysql
...
#Volumes
volumes:
dbdata:
driver: local
how it works that driver local and what is the location of it?
I just found out that the tutorial uses a wrong path, the correct path is:
- ./mysql/my.cnf:/etc/my.cnf
and not:
- ./mysql/my.cnf:/etc/mysql/my.cnf
docker-compose.yml
version: '3.7'
services:
db:
image: mysql:8.0.21
container_name: dbweb
restart: always
environment:
MYSQL_ROOT_PASSWORD: 'deivypassword'
TZ: America/New_York
ports:
# : < MySQL Port running inside container>
- '3306:3306'
# Where our data will be persisted
volumes:
- dbweb2:/var/lib/mysql
- ./my.cnf:/etc/mysql/my.cnf
Names our volume
volumes:
dbweb2: