MariaDB / mariadb-docker

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

mysql: command not found #524

Closed Tanguy-Magnaudet closed 11 months ago

Tanguy-Magnaudet commented 11 months ago

Hello,

I'm trying to init my db with a .sh file (because I want to fetch Env variable), put it tells me mysql command doesn't exists. :

  mariadb:
    container_name: mariadb
    build:
      context: .
      dockerfile: ./docker/Dockerfile-mariadb.dockerfile
    restart: always
    networks:
      - heracles
    environment:
      - MYSQL_RANDOM_ROOT_PASSWORD=yes
      - MYSQL_USER=$DB_USER
      - MYSQL_PASSWORD=$DB_PASSWORD
      - MYSQL_DATABASE=$DB_NAME
      - DB_USER=$DB_USER
      - DB_PASSWORD=$DB_PASSWORD
      - DB_NAME=$DB_NAME
      - DB_HERACLES=$DB_HERACLES
    volumes:
      - "./data/sql:/var/lib/mysql"

The Dockerfile :

FROM mariadb:11.0.2

# Add your shell script into the container
ADD ./scripts/initdb.sh /docker-entrypoint-initdb.d/initdb.sh

# Give it the necessary permissions
RUN chmod +x /docker-entrypoint-initdb.d/initdb.sh

And the initdb.sh :

mysql -u root -e "CREATE DATABASE IF NOT EXISTS $DB_NAME; GRANT ALL PRIVILEGES ON $DB_NAME.* TO $DB_USER@'%' IDENTIFIED BY '$DB_PASSWORD';"
mysql -u root -e "CREATE DATABASE IF NOT EXISTS $DB_HERACLES; GRANT ALL PRIVILEGES ON $DB_HERACLES.* TO $DB_USER@'%' IDENTIFIED BY '$DB_PASSWORD';"
mysql -u root -e "FLUSH PRIVILEGES;"

Any idea why it doesn't work ?

dr-m commented 11 months ago

MariaDB/server@306e439c6d19b578fdd12b640a6f9a81ed2737e1 in MariaDB Server 10.4 introduced symbolic links such as mariadb for mysql. Later, MariaDB Server 10.5 renamed the executables to MariaDB names and introduced symbolic links such as mysql for mariadb. Finally, in MariaDB/server@9656356b550079234185f430fe6b4190f435f3e6 (MariaDB Server 11.0) the symbolic links were moved into a separate package.

grooverdan commented 11 months ago

And the MariaDB image doesn't include the links: https://mariadb.org/mariadb-server-docker-official-images-healthcheck-without-mysqladmin/

A way as an init.sh without the execute permissions is:

 docker_process_sql <<-EOSQL
CREATE DATABASE IF NOT EXISTS $DB_NAME;
GRANT ALL PRIVILEGES ON $DB_NAME.* TO $DB_USER@'%' IDENTIFIED BY '$DB_PASSWORD';"
CREATE DATABASE IF NOT EXISTS $DB_HERACLES;
GRANT ALL PRIVILEGES ON $DB_HERACLES.* TO $DB_USER@'%' IDENTIFIED BY '$DB_PASSWORD';"
EOSQL

The docker_process_sql is part of the entrypoint and without execute permissions on the script it is run in the same context.

Note the outstanding feature request https://jira.mariadb.org/browse/MDEV-27638 if you want to make a contribution to the docker entrypoint to support this natively. FLUSH PRIVILEGES isn't needed after creating grants or users.