MariaDB / mariadb-docker

Docker Official Image packaging for MariaDB
https://mariadb.org
GNU General Public License v2.0
755 stars 436 forks source link

Document bash in docker-entrypoint-initdb.d #537

Closed hyamanieu closed 8 months ago

hyamanieu commented 8 months ago

I wanted to run a bash script creating database and loading various data in a specific way, rather than just having .sql dumps.

I added the script to a folder moutned on /docker-entrypoint-initdb.d It would say it cannot connect to "127.0.0.1". The same script would work after the container was started if executed manually from within the container (docker exec...).

It took me a while to make my script work as mysql --host="127.0.0.1" would not work and I looked into the code of the docker-entrypoint.sh to understand how it's done, and sourced it to execute its functions, but it was unsuccessfull.

My script would finally work while using "localhost" rather than "127.0.0.1".

I believe it should be documented how to execute a mysql command in a bash script mounted on /docker-entrypoint-initdb.d. I may have missed it.

tanji commented 8 months ago

@hyamanieu This happens because of a MySQL/MariaDB behavior and has nothing to do with the entrypoint itself. If you specify host=localhost the client will use Unix socket, whereas if you use 127.0.0.1 it will use a TCP connection.

hyamanieu commented 8 months ago

Ok, that's quite specific.

I'm not exactly sure why local TCP wouldn't work before CMD is executed. I can decipher that there is a two step process, as socket connection is specified in docker-entrypoint.sh and mysqld is in CMD. TCP works after the container has started and is healthy. So I believe it's still somewhat linked, right?

Anyways , perhaps other people fall on this issue in the future and it helps.

tanji commented 8 months ago

@hyamanieu, if you wanted to connect with local TCP, you would need to create a user that would allow you to do so, e.g. username@127.0.0.1. By default, only root@localhost exists, which, in MariaDB 10.4.3 and later uses the unix_socket plugin authentication, meaning that no password is needed when the system user is root. More explanations here: https://mariadb.com/kb/en/authentication-plugin-unix-socket/

hyamanieu commented 8 months ago

Thanks for your explanations. I guess we can close

grooverdan commented 8 months ago

The other way is scripts (end with .sh) without execute permission are source in the docker-entrypoint-initdb.d.

Then your script can do colourful things like reuse the entrypoints functions (the docker_* and mysql_* ones are the ones I'll try very hard to keep API compatible) like:

docker_process_sql <<<'SELECT 1'

And other variants documented in the entrypoint.

hyamanieu commented 8 months ago

The other way is scripts (end with .sh) without execute permission are source in the docker-entrypoint-initdb.d.

Then your script can do colourful things like reuse the entrypoints functions (the docker_* and mysql_* ones are the ones I'll try very hard to keep API compatible) like:

docker_process_sql <<<'SELECT 1'

And other variants documented in the entrypoint.

This is what I have tried doing and it was not working. I've read the comments on this topic for the postgres docker image which is similar. Missing was the $SOCKET env var when sourcing the entrypoint.

Anyways, mysql on localhost works.