Daniel Tran
Daniel Tran

Reputation: 69

Docker compose: MariaDB got an error with init.sql / entrypoint

have the following service in my docker compose file:

  database:
    image: mariadb
    container_name: database
    environment:
      MYSQL_ROOT_PASSWORD: 7ctDGg5YUwkCPkCW
    entrypoint:
      sh -c "echo 'CREATE DATABASE IF NOT EXISTS users;
                    CREATE DATABASE IF NOT EXISTS data;
                    CREATE DATABASE IF NOT EXISTS wordpress;
                    CREATE USER IF NOT EXISTS 'keycloak'@localhost IDENTIFIED BY 'jk2zKvGkJXBsrNMV';
                    GRANT ALL PRIVILEGES ON 'users'.* TO 'keycloak'@localhost; 
                    CREATE USER IF NOT EXISTS 'wordpress'@localhost IDENTIFIED BY 'QKJFUfZbv7jMB5ba';
                    GRANT ALL PRIVILEGES ON 'wordpress'.* TO 'wordpress'@localhost;' > /docker-entrypoint-initdb.d/init.sql;
      /usr/local/bin/docker-entrypoint.sh --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
      "
    ports:
      - 3306:3306
    volumes:
      - database_data:/var/lib/mysql
    networks:
      - backend-network

The mariadb service start ups without problems, but when it comes to the point where it is trying to run the init.sql I got the following error message:

database     | 2020-06-13 18:20:03+00:00 [Note] [Entrypoint]: /usr/local/bin/docker-entrypoint.sh: running /docker-entrypoint-initdb.d/init.sql
database     | ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'jk2zKvGkJXBsrNMV' at line 1
database exited with code 1

Hopefully there is some one out there, who is able to tell me whats wrong.

So far, Daniel

Upvotes: 1

Views: 1968

Answers (1)

David Maze
David Maze

Reputation: 159030

Take the content of your echo statement and move it to a separate file; for example, init.sql in the current directory. You can bind-mount that specific file into the /docker-entrypoint-initdb.d directory:

database:
  image: mariadb
  environment:
    MYSQL_ROOT_PASSWORD: 7ctDGg5YUwkCPkCW
  ports:
    - 3306:3306
  volumes:
    - database_data:/var/lib/mysql
    - ./init.sql:/docker-entrypoint-initdb.d/init.sql

What's actually happening with the command you're showing is that you're using single quotes as both a shell quoting delimiter and an SQL quoting delimiter.

echo '...IDENTIFIED BY 'something';'

In this case the single quotes around the password end the shell quoting and just get lost

# same as:
echo ...IDENTIFIED\ BY\ something\;

If you really needed to do it this way you could use the list format for the command to spell out the words specifically (without needing to separately quote them), and then use YAML block scalar syntax to avoid needing to separately quote the command, at which point you can use double quotes to quote the echo argument

command:
  - sh
  - -c
  - >-
      echo "... IDENTIFIED BY 'password'; ..."...

But it will be more understandable and less fragile to just split this out into a separate file. Avoid writing complex scripts inline in YAML files.

Upvotes: 2

Related Questions