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

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

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x