0

I want to create an MariaDB instance where I have a user which can create and alter databases. This is my docker compose file:

version: '3.1'


services:

  db:
    image: mariadb
    restart: always
    environment:
      MARIADB_ROOT_PASSWORD: example
      MARIADB_USER: user
      MARIADB_PASSWORD: example
    command: >
      bash -c echo "GRANT ALL PRIVILEGES ON *.* TO 'user'@'localhost' WITH GRANT OPTION;" mysql -u root -pexample -h localhost 
    volumes:
      - C:\db-data:/var/lib/mysql
    ports:
      - "3306:3306"

  adminer:
    image: adminer
    restart: always
    ports:
      - 8080:8080

I am getting the following error when executing "CREATE DATABASE DB;" from my python application running on the host system:

Access denied for user 'user'@'%' to database 'DB'

I can open a terminal and log into the database and grant privileges manually but I just want to run docker compose create/start and have everything ready. And I want to be able to create new databases from my python application as well. I have a bash command defined in my docker compose file in order to set privileges but it seems like it has not been ran.

Michael
  • 892
  • 2
  • 10
  • 28
  • Your `command:` is the _only_ thing your container will do; with the specific syntax you show, it will print a blank line (the result of `bash -c echo` with some ignored arguments) and immediately exit. You need to use a different initialization mechanism. This command runs _instead of_ starting a database. The linked question describes the `/docker-entrypoint-initdb.d` mechanism, but also see the [`mariadb` Docker Hub image page](https://hub.docker.com/_/mariadb) for environment-variable settings that can automatically set up a database user. – David Maze Mar 11 '23 at 12:46
  • Thanks! I ended up with "./init.sql:/docker-entrypoint-initdb.d/init.sql" under volumes with "GRANT ALL PRIVILEGES ON *.* TO 'user'@'%'" as its content. – Michael Mar 11 '23 at 13:09

0 Answers0