SQL script isn't executed during launching of mysql container - mysql

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 ?

Related

How to overwrite mysql sql_mode parameter in docker-compose

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 ???

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

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#'%';

connect to dockerized sql from host: Access denied

I'm sorry to open one more of those 'can't connect to my database' questions, but after searching for hours I just don't get it.
I want a dockerized database to be used by the (non dockerized) invoicePlane app.
So I'm using the following docker-compose.yml
version: '2'
services:
db:
image: mariadb:latest
#also tried mysql:latest
restart: always
environment:
MYSQL_DATABASE: 'invoiceplane_db'
MYSQL_USER: 'invoiceplane'
MYSQL_PASSWORD: 'pass1'
MYSQL_ROOT_PASSWORD: 'pass0'
expose:
- '3306'
volumes:
myserver/path:/var/lib/mysql
volumes:
invoiceplane-db:
It starts with sudo docker-compose -f docker-compose.yml up
From inside the container, everything works fine. I can connect, e.g. via mysql -uroot -ppass0, and see
+--------------+-----------+
| User | Host |
+--------------+-----------+
| invoiceplane | % |
| root | % |
| root | localhost |
+--------------+-----------+
3 rows in set (0.004 sec)
Now I want to access the db from the host, trying
mysql invoiceplane_db_1:3306 -uroot -ppass0
where invoiceplane_db_1 is the container's name.
but I'm always getting:
ERROR 1045 (28000): Access denied for user 'root'#'localhost' (using password: YES)
Through my websearch I gained the impression that this may have to do with the bind-address in the /etc/mysql/my.cnf of the container, which was stated here, for example. But I was not able to get it right. Initially, the parameter was commented out. I set it to bind-address = 127.0.0.1 and restarted the container. It did not help.
Am I missing something here?
This is my working docker-composer for mariadb:
db:
image: mariadb:10.4
ports:
- "3306:3306"
volumes:
- db-storage:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: root-pass
MYSQL_USER: my-user
MYSQL_PASSWORD: my-user-pass
MYSQL_DATABASE: my-database
From the looks of it, seems like you are missing the port mapping: 3306:3306

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.