2

I want to add MariaDB to my docker compose file, like this:

version: '3'
services:
    mariadb:
        image: 'mariadb:10.3'
        ports:
            - "3306:3306"
        volumes:
            - "${PWD}/data/mariadb:/var/lib/mysql:rw"
        environment:
            MYSQL_ROOT_PASSWORD: 'rootpass'
            MYSQL_ALLOW_EMPTY_PASSWORD: 'yes'
            MYSQL_USER: 'dev'

Now I can connect using -uroot -p and entering the rootpass password, but I can't connect using -udev and no password. It says:

ERROR 1045 (28000): Access denied for user 'dev'@'172.31.0.1' (using password: NO)

I also tried adding MYSQL_PASSWORD: ~ or MYSQL_PASSWORD: '' to the environment variables, but that also doesn't work.

It works when I add an actual password MYSQL_PASSWORD: 'dev', but I can't get it working without password. Does anyone know the magic combination of env vars?

The reason I want to achieve this, is because my team has this set up currently in a different way and everyone has a .my.cnf file containing user=dev and password=. So it would be nice if everyone could keep this local config unchanged.

1 Answer 1

3

The code from the entrypoint is:

    if  [ -n "$MARIADB_PASSWORD" ] || [ -n "$MARIADB_PASSWORD_HASH" ] && [ -n "$MARIADB_USER" ]; then
        mysql_note "Creating user ${MARIADB_USER}"
        if [ -n "$MARIADB_PASSWORD_HASH" ]; then
            createUser="CREATE USER '$MARIADB_USER'@'%' IDENTIFIED BY PASSWORD '$MARIADB_PASSWORD_HASH';"
        else
            # SQL escape the user password, \ followed by '
            local userPasswordEscaped
            userPasswordEscaped=$( docker_sql_escape_string_literal "${MARIADB_PASSWORD}" )
            createUser="CREATE USER '$MARIADB_USER'@'%' IDENTIFIED BY '$userPasswordEscaped';"
        fi

        if [ -n "$MARIADB_DATABASE" ]; then
            mysql_note "Giving user ${MARIADB_USER} access to schema ${MARIADB_DATABASE}"
            userGrants="GRANT ALL ON \`${MARIADB_DATABASE//_/\\_}\`.* TO '$MARIADB_USER'@'%';"
        fi
    fi

So the MARIADB_PASSWORD (and the old MYSQL_PASSWORD that gets copied to MARIADB_PASSWORD) must not be empty for it to create a user.

MARIADB_ALLOW_EMPTY_ROOT_PASSWORD (aka MYSQL_ALLOW_EMPTY_PASSWORD) only ever applied to the root password hence the name change.

One way is to use the /docker-entrypoint-initdb.d to have a reset_pass.sql file:

SET PASSWORD FOR dev@`%` = PASSWORD('')
3
  • Thank you for clearing it up. So it is either having this initial SQL file in every project, or just stop using an empty password/no password :D
    – 7ochem
    Commented Mar 16, 2023 at 13:06
  • this is clearly a missing feature - where can the feature be requested? Commented Nov 18 at 15:40
  • Feature requests can be written on github for MariaDB Docker or JIRA (MDEV project, Docker component).
    – danblack
    Commented Nov 18 at 21:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.