Home > other >  How to add DBA account in docker-compose.yml?
How to add DBA account in docker-compose.yml?

Time:10-23

In the following docker-compose.yml, how can I add DBA account (for example, username: abc password:123) with 'GRANT ALL PRIVILEGES' to it in MySQL?

I have tried adding 'MYSQL_DBA_USER: abc MYSQL_DBA_PASSWORD: 123' under 'MYSQL_PASSWORD: secret' but it does not work.

services:
 mysql-server:
    container_name: mysql
    ports:
        - "18080:8080"
    environment:
        MYSQL_ROOT_PASSWORD: 12345
        MYSQL_DATABASE: wordpress
        MYSQL_USER: wordpress_user
        MYSQL_PASSWORD: secret
    image: mysql/mysql-server
 wordpress:
    image: wordpress:latest
    container_name: wordpress
    ports:
        - "20080:80"
    environment:
        WORDPRESS_DB_HOST: mysql-server:3306
        WORDPRESS_DB_USER: wordpress_user
        WORDPRESS_DB_PASSWORD: secret
    depends_on:
    - mysql-server

CodePudding user response:

If you map any SQL script files ending in .sql or .sh into the directory /docker-entrypoint-initdb.d in the container, they will be run when the database is created.

So if you create a script called create-dba-user.sql in the same directory as your docker-compose file and add a volume mapping to your docker-compose file like this

mysql-server:
    container_name: mysql
    ports:
        - "18080:8080"
    environment:
        MYSQL_ROOT_PASSWORD: 12345
        MYSQL_DATABASE: wordpress
        MYSQL_USER: wordpress_user
        MYSQL_PASSWORD: secret
    volumes:
        - ./create-dba-user.sql:/docker-entrypoint-initdb.d/create-dba-user.sql
    image: mysql/mysql-server

Then your script will be run when the database is created. If you have an existing database, then it won't be run. But your compose file looks like you don't persist the database, so you'll have a fresh database on each run.

  • Related