Database overwriting when creating mysql container

When i create database using sql script at the entry-point. and used MYSQL_DATABASE env variable the database is overwriting to empty database.

if i didnt provide the database env variable then dbuser don’t have access to the database create using sql script.

below is the part of mysql service config

    volumes:
      - .\schema.sql:/docker-entrypoint-initdb.d/0_init.sql
      - db-data:/var/lib/mysql
    environment:
      - MYSQL_DATABASE=short_url_db
      - MYSQL_USER=dbuser
      - MYSQL_PASSWORD=password
      - MYSQL_ROOT_PASSWORD=password

[note: there is only tables related schema inside the SQL script.]

Would be interesting to see your full docker-compose.yml, especially which image you use.

got it. Here is my findings.

[Note] [Entrypoint]: Creating database short_url_db
[Note] [Entrypoint]: Creating user dbuser
[Note] [Entrypoint]: Giving user dbuser access to schema short_url_db

[Note] [Entrypoint]: /usr/local/bin/docker-entrypoint.sh: running /docker-entrypoint-initdb.d/0_init.sql
[Note] [Entrypoint]: Entrypoint script for MySQL Server 8.2.0-1.el8 started.

because their is no IF NOT EXISTS Clause why creating database in the SQL script file. An error raised because the short_url_db database is already created first hence the tables are not created.

Error Problem
CREATE DATABASE short_url_db;

Error solved:
CREATE DATABASE IF NOT EXISTS short_url_db;