How to overwrite mysql sql_mode parameter in docker-compose - mysql

I wrote docker compose file
version: '3.9'
services:
db:
image: mysql:5.7
restart: always
environment:
- MYSQL_ROOT_PASSWORD=root
ports:
- 3306:3306
volumes:
- mysql_db:/var/lib/mysql
- ./my.conf:/etc/mysql/conf.d/my.cnf
volumes:
mysql_db:
And my.conf
[mysqld]
sql-mode="TRADITIONAL,ALLOW_INVALID_DATES"
But after restart container parameters in my.conf file was added to default sql_mode
mysql> show variables like "sql_mode";
+---------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Variable_name | Value |
+---------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| sql_mode | STRICT_TRANS_TABLES,STRICT_ALL_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ALLOW_INVALID_DATES,ERROR_FOR_DIVISION_BY_ZERO,TRADITIONAL,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION |
+---------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0,00 sec)
How to overwrite default parameters and left only present in my.conf ???

Related

mysql docker container : create database, user and execute commands automatically

I have followed this post create database and table automatically with docker-compose to execute a script at startup of my container.
The database is created and a user i defined in the docker-compose is created, my database is not populated like i have set in the sql script.
Can anyone help me?
here is the dockerfile:
FROM phpmyadmin
COPY DockerCreateAllTablesDBwithData.sql /docker-entrypoint-initdb.d/initdb.sql
Here is the docker-compose.yml:
---
version: '3.7'
services:
db:
image: mysql:8-debian
container_name: db
restart: always
networks:
- network_app
cap_add:
- SYS_NICE
environment:
- MYSQL_ROOT_PASSWORD=password
- MYSQL_USER=admin
- MYSQL_PASSWORD=password
- MYSQL_DATABASE=CantineTest
- TZ='Europe/Paris'
volumes:
- mydatavolume:/var/lib/mysql
phpmyadmin:
build:
context: .
dockerfile: ./Dockerfile
container_name: phpmyadmin
restart: always
ports:
- 8080:80
depends_on:
- db
# volumes:
# - ./data/certbot/conf:/etc/letsencrypt
networks:
- network_app
environment:
- PMA_HOST=db
- TZ="Europe/Paris"
#volumes:
# frontendbuild:
# name: frontendbuild
networks:
network_app:
name: network_app
volumes:
mydatavolume:
And here is the DockerCreateAllTablesDBwithData.sql:
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET AUTOCOMMIT = 0;
START TRANSACTION;
SET time_zone = "+00:00";
--
-- Bdd : `CantineTest`
--
create database CantineTest;
use CantineTest;
DROP TABLE IF EXISTS `Alias`;
CREATE TABLE `Alias` (
`AliasID` smallint(5) UNSIGNED NOT NULL,
`AliasName` varchar(50) NOT NULL,
`AliasDescription` varchar(255) DEFAULT NULL,
`AliasMailingList` mediumtext NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
You have added the DB script to a wrong place. Must be added to the docker-composer where you have MySQL. Add as a volume
volumes:
- ./mysql-dump:/docker-entrypoint-initdb.d
Create a mysql-dump folder where you have docker-compose and add your SQL script to it.

docker compose documentation tutorial, Unknown database 'todos'

i followed docker compose tutorial
https://docs.docker.com/get-started/08_using_compose/
to create composer file for todo app with mysql service
i did the same docker compose as the tutorial
but it gave me this error
app-mysql-1 | 2022-05-28T21:55:26.950553Z 3 [Note] Unknown database 'todos'
app-app-1 | Error: ER_BAD_DB_ERROR: Unknown database 'todos'
i executed mysql container services to see if todos database created or not
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
docker compose
version: "3.7"
services:
app:
image: node:12-alpine
command: sh -c "yarn install && yarn run dev"
ports:
- 3000:3000
working_dir: /app
volumes:
- ./:/app
environment:
MYSQL_HOST: mysql
MYSQL_USER: root
MYSQL_PASSWORD: secret
MYSQL_DB: todos
mysql:
image: mysql:5.7
volumes:
- todo-mysql-data:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: secret
MYSQL_DATABASE: todos
volumes:
todo-mysql-data:
Running just the mysql portion of your docker-compose file seems to have worked for me.
version: "3.7"
services:
mysql:
image: mysql:5.7
volumes:
- todo-mysql-data:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: secret
MYSQL_DATABASE: todos
volumes:
todo-mysql-data
You might want to try deleting the volume and recreating your containers.
docker volume rm todo-mysql-data
docker-compose up --force-recreate
Thanks Hambrook i found that there was a volume with the same name i tried to create
docker volume ls
DRIVER VOLUME NAME
local app_todo-mysql-data
local todo-mysql-data
so i removed it and write command again
docker compose up
and it's work

SQL script isn't executed during launching of mysql container

I'm very new at docker and docker-composer. I'm trying to make a mysql container which will be used by a php-apache container.
I have a SQL script which create a database and insert values.
I want my mysql container to execute this SQL script at his launching, so the app will be able to make queries on it. here's my docker-compose file :
version: "3.7"
services:
mysql:
image: mysql:latest
container_name: mysql
restart: always
volumes:
- db-volume:/var/lib/mysql
- ./bdd.sql:/docker-entrypoint-initdb.d/bdd.sql
environment:
MYSQL_ROOT_PASSWORD: test
MYSQL_DATABASE: webreathe
app:
image: toto:latest
container_name: toto
restart: always
volumes:
- ./:/var/www/webreathe
ports:
- 8080:80
depends_on:
- mysql
volumes:
db-volume:
I launch it with docker-compose up -d
and I want to see if my database was created so I execute inside of the mysql container :
mysql -u root -p test -e "show databases;"
but the result is :
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| test |
+--------------------+
So I wonder if the bdd.sql file was in the /docker-entrypoint-initdb.d directory
and yes it is : ls docker-entrypoint-initdb.d => bdd.sql
Do you know why it doesn't execute the script ?

Can't create db in docker-entrypoint-initdb.d with mysql docker container

I created a docker-compose.yml.
version: "3.7"
services:
db:
container_name: mysql
image: mysql
ports:
- 3306:3306
volumes:
- ./sql:/docker-entrypoint-initdb.d
- ./mysql:/var/lib/mysql
environment:
MYSQL_ROOT_USER : root
MYSQL_ROOT_PASSWORD: root_pass
MYSQL_DATABASE: wp1
MYSQL_USER: wp
MYSQL_PASSWORD: pass
restart: always
And in sql directory, I put a init.sql.
CREATE DATABASE IF NOT EXISTS wp2;
Then run docker-compose up.
In the log there is a entry for the init script.
mysql | 2020-04-08 02:17:32+00:00 [Note] [Entrypoint]: /usr/local/bin/docker-entrypoint.sh: running /docker-entrypoint-initdb.d/init.sql
But the database wp2 is not created.
$ docker exec -it mysql bash
# mysql -u wp -ppass
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| wp1 |
+--------------------+
How can I create a database with mysql container init script?
Your wp user doesn't have access rights on wp2.
Append to your sql file:
GRANT ALL on wp2.* to wp#'%';

Why is MYSQL_DATABASE env var being ignored by MySQL docker container?

I have the following docker-compose.yml
version: "3.7"
services:
db:
container_name: db
image: mysql
ports:
- "3306:3306"
environment:
MYSQL_ROOT_PASSWORD: "mysql"
MYSQL_DATABASE: "mydb"
security_opt:
- seccomp:unconfined
volumes:
- ./supplied/init:/docker-entrypoint-initdb.d/:ro
When I run 'docker-compose up' It see this :
Recreating db ... done
Attaching to db
If I check the available databases I see:
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| db |
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
Why is it ignoring my MYSQL_DATABASE environment variable?
I'm running on windows 10.
EDIT :
This looks like some sort of caching issue. I changed the service name and container_name in the docker-compose file and it started working...
from https://github.com/docker-library/mysql/issues/180:
Important to note that the image entrypoint script will never make
changes to an existing database. If you mount an existing data
directory into var/lib/mysql, options like MYSQL_ROOT_PASSWORD will
have no effect
My issue was that the volume already existed. When I removed the volume with docker-compose down -v
When I brought it up, it would created the database.
You should use the --force-recreate flag:
--force-recreate Recreate containers even if their configuration
and image haven't changed.