docker-compose mysql persistent data - mysql

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:

Related

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

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.

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'

Docker on Windows gives error when trying to start mysql container

I'm new on Docker and trying now to create my first docker-compose file.
The apache works so far but i'm struggeling a bit with mysql.
Here is my docker-compose.yaml
version: '3'
services:
web:
build: .
ports:
- "8083:80"
volumes:
- ./public_html/:/usr/local/apache2/htdocs/
networks:
- appnet
db:
image: mysql
volumes:
- db-data:/var/lib/mysql
command: --default-authentication-plugin=mysql_native_password
restart: always
environment:
MYSQL_ROOT_PASSWORD: root
networks:
- appnet
adminer:
image: adminer
restart: always
ports:
- 8080:8080
volumes:
db-data:
networks:
appnet:
The error is:
[ERROR] [MY-010457] [Server] --initialize specified but the data directory has files in it. Aborting.
I alread tried to remove the volume totally or added a /data at the end. But it gives me all the time the same error. Also Google didn't give me any good hints.
Does someone has an idea?
I figured out now. I'm running Docker on a Windows Machine, so i had to reset the credentials ond Docker App and type in again. Now it works. Hope that will help others as well. It seems on Docker for Windows, when something not work as expected it make sense to reset the credentials.

mysql 5.7 data directory has files in it

Following the tutorial at:
https://docs.docker.com/compose/wordpress/
Running docker-compose up I get:
Initializing database
[Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
[ERROR] --initialize specified but the data directory has files in it. Aborting.
[ERROR] Aborting
Have tried adding arguments as suggested here:
https://github.com/docker-library/mysql/issues/186
https://github.com/docker-library/mysql/issues/69
But this doesn't seems to work:
services:
db:
image: mysql:5.7
command: ["mysqld", "--ignore-db-dir=lost+found", "--explicit_defaults_for_timestamp"]
volumes:
- db_data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: somewordpress
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
Is there another solution?
I solved it using:
docker volume prune
Remove all unused local volumes. Unused local volumes are those which
are not referenced by any containers
https://docs.docker.com/engine/reference/commandline/volume_prune/