bind mount to mysql docker container not working - mysql

I have a mysql docker container with container_name=mysql1. When I run docker exec -it mysqlsh and look at the files using ls command, i see the following files.
bin boot dev docker-entrypoint-initdb.d entrypoint.sh etc healthcheck.sh home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
I want to bind mount some of these folders in my host machine, inside the directory where the docker-compose file is being run from.
version: '3.9'
services:
mysql1:
container_name: mysql1
image: mysql/mysql-server
ports:
- '3306:3306'
environment:
- MYSQL_ROOT_PASSWORD = 'root'
volumes:
- ./db:/etc
But tis is giving me the following error:
[+] Running 2/0
⠿ Network poc_1_default Created 0.0s
⠿ Container mysql1 Created 0.0s
Attaching to mysql1
mysql1 | [Entrypoint] MySQL Docker Image 8.0.28-1.2.7-server
mysql1 | [Entrypoint] Initializing database
mysql1 | [Entrypoint] No password option specified for new database.
mysql1 | [Entrypoint] A random onetime password will be generated.
mysql1 | 2022-02-05T16:36:18.356881Z 0 [System] [MY-013169] [Server] /usr/sbin/mysqld (mysqld 8.0.28) initializing of server in progress as process 18
mysql1 | 2022-02-05T16:36:18.359651Z 0 [ERROR] [MY-010124] [Server] Fatal error: Can't change to run as user 'mysql' ; Please check that the user exists!
mysql1 | 2022-02-05T16:36:18.359664Z 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.
mysql1 | 2022-02-05T16:36:18.359700Z 0 [ERROR] [MY-010119] [Server] Aborting
mysql1 | 2022-02-05T16:36:18.359754Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.28) MySQL Community Server - GPL.
mysql1 exited with code 1
What is happening and how do I fix this?

Related

Unable to start mysql docker client on Mac M1

I have a docker-compose configuration as follows:
mysql:
image: mysql/mysql-server:8.0.23
ports:
- ${DOCKER_HOST_MYSQL_PORT:-3306}:3306
command: --default-authentication-plugin=mysql_native_password
environment:
MYSQL_ROOT_PASSWORD: secret
MYSQL_DATABASE: ${DB_DATABASE}
volumes:
- ./storage/docker/mysql:/var/lib/mysql
networks:
then I ran the following: docker-compose up mysql
I'm getting the following error:
[+] Running 5/5
⠿ mysql Pulled 14.1s
⠿ 517a40cb4717 Pull complete 5.9s
⠿ 4a26b26e2152 Pull complete 10.0s
⠿ 14794e2450ed Pull complete 10.0s
⠿ 754b01a0f0ff Pull complete 10.0s
[+] Running 1/1
⠿ Container client-app-mysql-1 Recreated 0.4s
Attaching to client-app-mysql-1
client-app-mysql-1 | [Entrypoint] MySQL Docker Image 8.0.23-1.1.19
client-app-mysql-1 | [Entrypoint] Starting MySQL 8.0.23-1.1.19
client-app-mysql-1 | 2022-03-29T08:41:12.591047Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.23) starting as process 26
client-app-mysql-1 | 2022-03-29T08:41:12.597713Z 0 [Warning] [MY-010159] [Server] Setting lower_case_table_names=2 because file system for /var/lib/mysql/ is case insensitive
client-app-mysql-1 | 2022-03-29T08:41:12.611927Z 1 [System] [MY-011012] [Server] Starting upgrade of data directory.
client-app-mysql-1 | 2022-03-29T08:41:12.612425Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
client-app-mysql-1 | 2022-03-29T08:41:13.343343Z 1 [ERROR] [MY-012224] [InnoDB] Tablespace flags are invalid in datafile: ./ibdata1, Space ID:0, Flags: 21. Please refer to http://dev.mysql.com/doc/refman/8.0/en/innodb-troubleshooting-datadict.html for how to resolve the issue.
client-app-mysql-1 | 2022-03-29T08:41:13.344157Z 1 [ERROR] [MY-012237] [InnoDB] Corrupted page [page id: space=0, page number=0] of datafile './ibdata1' could not be found in the doublewrite buffer.
client-app-mysql-1 | 2022-03-29T08:41:13.344888Z 1 [ERROR] [MY-012930] [InnoDB] Plugin initialization aborted with error Data structure corruption.
client-app-mysql-1 | 2022-03-29T08:41:13.833188Z 1 [ERROR] [MY-011013] [Server] Failed to initialize DD Storage Engine.
client-app-mysql-1 | 2022-03-29T08:41:13.835836Z 0 [ERROR] [MY-010020] [Server] Data Dictionary initialization failed.
client-app-mysql-1 | 2022-03-29T08:41:13.837260Z 0 [ERROR] [MY-010119] [Server] Aborting
client-app-mysql-1 | 2022-03-29T08:41:13.838870Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.23) MySQL Community Server - GPL.
client-app-mysql-1 exited with code 1
I got into the same problem once moved to a M1 machine.
Didn't dig much into the issue, but got it working by attaching a docker volume and in this way the DB data are persistent
version: '3.7'
services:
dcm-mysql:
hostname: dcm-mysql
container_name: dcm-mysql
image: mysql:8.0.29-oracle
restart: always
environment:
MYSQL_DATABASE: dcm
MYSQL_USER: dcm
MYSQL_PASSWORD: dcm
MYSQL_ROOT_PASSWORD: root
ports:
- 3306:3306
volumes:
# - ../local_data/mdb/data:/var/lib/mysql
- dcm-mysql-data:/var/lib/mysql
networks:
- mariadb
volumes:
dcm-mysql-data:

Initialising a mysql database and persist data using Docker compose

I am trying to run a web application that uses two docker containers: one contains the application code and the other container is a mysql database instance.
I am trying to populate the db with an sql file and also try to persist the database data through a volume. I am encountering an issue with the sql init file being copied into the mysql container but not being executed.
This is how my docker-compose.yml looks like:
version: "3.7"
services:
db:
image: mysql:8.0.19
command: '--default-authentication-plugin=mysql_native_password'
restart: always
volumes:
- db-data:/var/lib/mysql
- ./db:/docker-entrypoint-initdb.d/:ro
environment:
- MYSQL_ROOT_PASSWORD=password
networks:
- backend
app:
build: app
restart: always
depends_on:
- db
ports:
- 5000:5000
networks:
- backend
- frontend
volumes:
- ./app/src:/code
volumes:
db-data:
networks:
backend:
frontend:
And this is the log when starting the mysql container
2021-04-17 12:34:01+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.19-1debian10 started.
2021-04-17 12:34:01+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
2021-04-17 12:34:01+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.19-1debian10 started.
2021-04-17T12:34:01.466444Z 0 [Warning] [MY-011070] [Server] 'Disabling symbolic links using --skip-symbolic-links (or equivalent) is the default. Consider not using this option as it' is deprecated and will be removed in a future release.
2021-04-17T12:34:01.466615Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.19) starting as process 1
2021-04-17T12:34:01.891144Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
2021-04-17T12:34:01.895163Z 0 [Warning] [MY-011810] [Server] Insecure configuration for --pid-file: Location '/var/run/mysqld' in the path is accessible to all OS users. Consider choosing a different directory.
2021-04-17T12:34:01.916128Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.19' socket: '/var/run/mysqld/mysqld.sock' port: 3306 MySQL Community Server - GPL.
2021-04-17T12:34:02.051164Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Socket: '/var/run/mysqld/mysqlx.sock' bind-address: '::' port: 33060
It does not look like the init.sql file is being executed. Also from the mysql container CLI I can see that the 'init.sql' file has been copied.
# cd docker-entrypoint-initdb.d
# ls
init.sql
# mysql -u root -p
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| example |
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.01 sec)
I am quite new to Docker and Docker compose but I cannot figure out how to make this work.

Docker MySQL container not binding to local port correctly

I'm trying to connect my local 3306 to my docker containers 3306 using docker-compose. This was all working correctly until I encountered an error with MySQL when I tried changing a table name using workbench. Since then I've been unable to connect back to the container with the correct data.
Here is my docker-compose.yml:
version: "3.3"
services:
web_old:
build:
context: ./
dockerfile: ./html/Dockerfile
container_name: mackglobal_old
depends_on:
- db
volumes:
- ./:/var/www
# - ./config/000-default.conf:/etc/apache2/000-default.conf
ports:
- 8000:80
db:
container_name: mysql8_old
image: mysql:8.0.23
environment:
MYSQL_USER: "admin" # same as production so when we create views it doesnt cause definer errors
MYSQL_PASSWORD: "root"
MYSQL_ROOT_PASSWORD: "root"
volumes:
- ./mysql:/var/lib/mysql
- ./my.cnf:/etc/mysql/conf.d/my.cnf
ports:
- "3306:3306"
# command: mysqld --innodb_force_recovery=6 # USE THIS LINE IF THE MYSQL CONTAINER CRASHES TO RECOVER DATA IN READ
When connecting to the database in my application I am met with this PHP PDO error:
Connection failed: SQLSTATE[HY000] [1045] Access denied for user 'admin'#'172.18.0.3' (using password: YES)
When I connect to the mysql instance inside the docker container I am able to access the server using the username and password in the docker-compose file and all of my databases and tables are inside the instance. But when connecting to the instance inside workbench every query I make is met with:
Error Code: 1046. No database selected Select the default DB to be used by double-clicking its name in the SCHEMAS list in the sidebar.
and when running SHOW databases; I only get information_schema
Here are the details I've been using to connect to the instance:
Hostname: MacBook-Pro-2.local (also tried: localhost, 127.0.0.1)
Port: 3306 (I've also changed the port to 3307 in the compose file and get the same errors)
Username: admin
Password: root
Here is my.cnf:
[mysqld]
default_time_zone='+00:00'
sql_mode=""
character-set-server=utf8mb4
collation-server=utf8mb4_unicode_ci
init-connect='SET NAMES UTF8;'
innodb-flush-log-at-trx-commit=0
lower_case_table_names=2
and here is the log when running docker-compose up --build:
mysql8_old | 2021-02-16 20:15:38+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.23-1debian10 started.
mysql8_old | 2021-02-16 20:15:38+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
mysql8_old | 2021-02-16 20:15:38+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.23-1debian10 started.
mysql8_old | 2021-02-16T20:15:39.468943Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.23) starting as process 1
mysql8_old | 2021-02-16T20:15:39.506466Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
mysql8_old | 2021-02-16T20:15:40.895843Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
mysql8_old | 2021-02-16T20:15:41.681805Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Bind-address: '::' port: 33060, socket: /var/run/mysqld/mysqlx.sock
mysql8_old | 2021-02-16T20:15:41.848877Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
mysql8_old | 2021-02-16T20:15:41.849540Z 0 [System] [MY-013602] [Server] Channel mysql_main configured to support TLS. Encrypted connections are now supported for this channel.
mysql8_old | 2021-02-16T20:15:41.867833Z 0 [Warning] [MY-011810] [Server] Insecure configuration for --pid-file: Location '/var/run/mysqld' in the path is accessible to all OS users. Consider choosing a different directory.
mysql8_old | 2021-02-16T20:15:42.028150Z 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.
I should also note that the last environment I used was mysql:8.0.21 but I arrived at the same errors
Any help is greatly appreciated
Addition:
I've also tried restarting docker service and my laptop as well
When running network utility on port 3306 while containers are active:
Port scanner
EDIT:
Turns out my folder which holds the mysql data ./mysql in my docker compose file is now corrupt (without error) and I'm still unable to access anything in recovery mode. Luckily I had a backup but I lost all my recent changes that I made. I still am unsure what went wrong so I'm leaving this unsolved
In my docker-compose.yml, I made my mysql container like this:
mysql8-service:
image: mysql:8
container_name: mysql8-container
ports:
- "4306:3306"
volumes:
- ./mysql:/var/lib/mysql
command: --default-authentication-plugin=mysql_native_password --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
restart: always # always restart unless stopped manually
environment:
MYSQL_USER: root
MYSQL_ROOT_PASSWORD: secret
MYSQL_PASSWORD: secret
networks:
- nginx-php74-mysql8-node
It make me able to save my database in the folder of my app, and my credentials are good.

What's default password in docker container mysql-server when you don't set one?

I am try to run a mysql container and connect with mysql client then:
I've used following commands:
docker run --name=mysql -d mysql/mysql-server:latest
docker exec -it mysql mysql -uroot -p
According to tutorial, the last command allow me configure database password but When I introduce the first password it fails...
The first password was root and I get an unusual error, then I try with admin, admin an even my linux user password but They don't work...
I would like to know what's the error?
There are couple of ways to see the password.
First Approach - Don't Run container in daemon mode
Check the below command
docker run --name=mysql mysql/mysql-server:latest
and this will print the password in the terminal as look at the below logs
2020-05-28T23:41:01.418347Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Socket: '/var/run/mysqld/mysqlx.sock'
2020-05-28T23:41:01.666070Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
2020-05-28T23:41:01.714420Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.20' socket: '/var/lib/mysql/mysql.sock' port: 0 MySQL Community
Server - GPL.
Warning: Unable to load '/usr/share/zoneinfo/iso3166.tab' as time zone. Skipping it.
Warning: Unable to load '/usr/share/zoneinfo/leapseconds' as time zone. Skipping it.
Warning: Unable to load '/usr/share/zoneinfo/tzdata.zi' as time zone. Skipping it.
Warning: Unable to load '/usr/share/zoneinfo/zone.tab' as time zone. Skipping it.
Warning: Unable to load '/usr/share/zoneinfo/zone1970.tab' as time zone. Skipping it.
[Entrypoint] GENERATED ROOT PASSWORD: PopiKQIjAS#OGj3x]yJOBLEn80p
[Entrypoint] ignoring /docker-entrypoint-initdb.d/*
2020-05-28T23:41:06.208480Z 10 [System] [MY-013172] [Server] Received SHUTDOWN from user root. Shutting down mysqld (Version: 8.0.20).
2020-05-28T23:41:07.861667Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.20) MySQL Community Server - GPL.
[Entrypoint] Server shut down
[Entrypoint] Setting root user as expired. Password will need to be changed before database can be used.
[Entrypoint] MySQL init process done. Ready for start up.
[Entrypoint] Starting MySQL 8.0.20-1.1.16
2020-05-28T23:41:08.534785Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.20) starting as process 1
2020-05-28T23:41:08.549216Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2020-05-28T23:41:09.135591Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2020-05-28T23:41:09.369412Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Socket: '/var/run/mysqld/mysqlx.sock' bind-address: '::' port: 33060
2020-05-28T23:41:09.448584Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
2020-05-28T23:41:09.500464Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.20' socket: '/var/lib/mysql/mysql.sock' port: 3306 MySQL Communi
ty Server - GPL.
Second Approach - Run container in daemon mode & fetch the password from logs
Check the below command to run the container
docker run -d --name=mysql mysql/mysql-server:latest
and then run the below command to fetch the password
docker logs mysql 2>&1 | grep GENERATED
Output of above command is:
[Entrypoint] GENERATED ROOT PASSWORD: PopiKQIjAS#OGj3x]yJOBLEn80p
Once you have the password by one of the above-mentioned methods, you can then login with the below command using that password
docker exec -it mysql mysql -uroot -p
When asked, enter the generated root password (see the instructions above on how to find it). Because the MYSQL_ONETIME_PASSWORD option is true by default, after you have connected a mysql client to the server, you must reset the server root password by issuing this statement:
mysql> ALTER USER 'root'#'localhost' IDENTIFIED BY 'password';
Substitute password with the password of your choice. Once the password is reset, the server is ready for use.
Ref: https://hub.docker.com/r/mysql/mysql-server/
docker inspect <container_name_here> command output shows root password among other params
When starting a mysql container for the first time, there is no default password. The password you set while running the container is the default password that will be assigned to your root.
Starting a MySQL instance is simple:
$ docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:tag
where some-mysql is the name you want to assign to your container, my-secret-pw is the password to be set for the MySQL root user and tag is the tag specifying the MySQL version you want. See the list above for relevant tags.
Following the documentation in: https://dev.mysql.com/doc/refman/8.0/en/docker-mysql-getting-started.html
You get the password with command:
docker logs mysql 2>&1 | grep GENERATED
Example output:
GENERATED ROOT PASSWORD: Axegh3kAJyDLaRuBemecis&EShOs
There is no default password, try login in using:
sudo docker exec -it mysql mysql -u root

mysql docker container not getting started ,mysqld: Can't create/write to file '/var/lib/mysql/is_writable' (OS errno 13 - Permission denied)

i've created a docker compose file to run the mysql docker container and using the database scripts initializing the database.
While starting the container i am getting the below error.
account-service-db_1 | Initializing database
account-service-db_1 | mysqld: Can't create/write to file '/var/lib/mysql/is_writable' (OS errno 13 - Permission denied)
account-service-db_1 | 2019-05-11T16:58:35.265463Z 0 [Warning] [MY-011070] [Server] 'Disabling symbolic links using --skip-symbolic-links (or equivalent) is the default. Consider not using this option as it' is deprecated and will be removed in a future release.
account-service-db_1 | 2019-05-11T16:58:35.265550Z 0 [System] [MY-013169] [Server] /usr/sbin/mysqld (mysqld 8.0.16) initializing of server in progress as process 15
account-service-db_1 | 2019-05-11T16:58:35.268464Z 0 [ERROR] [MY-010460] [Server] --initialize specified but the data directory exists and is not writable. Aborting.
account-service-db_1 | 2019-05-11T16:58:35.268472Z 0 [ERROR] [MY-013236] [Server] Newly created data directory /var/lib/mysql/ is unusable. You can safely remove it.
account-service-db_1 | 2019-05-11T16:58:35.269284Z 0 [ERROR] [MY-010119] [Server] Aborting
account-service-db_1 | 2019-05-11T16:58:35.270510Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.16) MySQL Community Server - GPL.
docker-swarm-account-service-mysql-demo_account-service-db_1 exited with code 1
After research many people suggested to add user: "1000:50" in compose file will solve the issue .but after adding also i am getting the same error.
please find the my docker-compose file below
version: '3.7'
services:
account-service-db:
user: "1000:50"
image: mysql
environment:
- MYSQL_DATABASE=account-service
- MYSQL_USER=mysqluser
- MYSQL_PASSWORD=mysqlpassword
- MYSQL_ROOT_PASSWORD=mysqlpassword
volumes:
- "/Users/admin/dockerfiles/docker-swarm-account-service-mysql-demo/scripts/:/docker-entrypoint-initdb.d/"
- "/Users/admin/dockerfiles/docker-swarm-account-service-mysql-demo/account:/var/lib/mysql"
ports:
- "3306:3306"
deploy:
replicas: 1
placement:
constraints:
- node.labels.mysql.node == true
restart_policy:
condition: on-failure
delay: 10s
#max-attempts: 3
i am executing the above scenario in mac machine. Please help me why its behaving like this after giving all necessary permissions to volume and scripts folder.
The problem seems to be with your account folder. It might already be in use or corrupted. Create a new folder and test.
Remove double quotes from environment values in compose file.
If the folder on host that is being mounted does not have the required write permissions:
ssh to your host/node
chmod -R a+rwx /path/to/account/folder