Skip to content

Setting up multiple mysql users with passwords in ENV file #781

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
LaurentNoyon opened this issue Aug 16, 2021 · 3 comments
Closed

Setting up multiple mysql users with passwords in ENV file #781

LaurentNoyon opened this issue Aug 16, 2021 · 3 comments
Labels
question Usability question, not directly related to an error with the image

Comments

@LaurentNoyon
Copy link

See here : Setting up multiple mysql users without storing passwords #213

I'm having difficulties applying the advices of the topic above. I'm reposting my problem because the issue is closed and has no visibility, here is my Dockerfile :

FROM mysql:5.7

ADD docker/mysql/script.sql /docker-entrypoint-initdb.d/script.sql
ADD docker/mysql/init_script_database.sh /docker-entrypoint-initdb.d/init_script_database.sh
RUN chmod -R 775 /docker-entrypoint-initdb.d

Here is my init_script_database.sh :

mysql --protocol=socket -uroot -p$MYSQL_ROOT_PASSWORD <<EOSQL
CREATE USER 'myuser_bis'@'%' IDENTIFIED WITH mysql_native_password AS '$MYSQL_SECOND_PASSWORD';GRANT USAGE ON *.* TO 'myuser_bis'@'%' REQUIRE NONE WITH MAX_QUERIES_PER_HOUR 0 MAX_CONNECTIONS_PER_HOUR 0 MAX_UPDATES_PER_HOUR 0 MAX_USER_CONNECTIONS 0;
CREATE DATABASE IF NOT EXISTS `myuser_bis`;
GRANT ALL PRIVILEGES ON `myuser\_bis`.* TO 'myuser_bis'@'%';
EOSQL

Here is my docker-compose :

[...]
# MySQL container
mysql:
    build:
        context: ../
        dockerfile: ./docker/mysql/Dockerfile
    volumes:
        # Prevent mysql data to be erased on restart
        - mysql_volume:/var/lib/mysql
    # Connect to "mysql_network" network, as defined below
    networks:
        - mysql_network
    # Pass a list of environment variables to the container
    environment:
        TZ: "Europe/Rome"
        MYSQL_ALLOW_EMPTY_PASSWORD: "no"
        MYSQL_ROOT_PASSWORD: "${MYSQL_ROOT_PASSWORD}"
        MYSQL_USER: "myuser"
        MYSQL_PASSWORD: "${MYSQL_PASSWORD}"
        MYSQL_DATABASE: "mybase"
        MYSQL_SECOND_PASSWORD: "${MYSQL_SECOND_PASSWORD}"
    command: [
        'mysqld',
        '--character-set-server=utf8mb4',
        '--collation-server=utf8mb4_general_ci',
        '--innodb-use-native-aio=0' # this has been added to prevent bugs mysqli::real_connect(): php_network_getaddresses: getaddrinfo failed
    ]
[...]

Finally I have env file with all the variables (properly read).

While running docker-compose up, I get no errors. I get "mybase" database, "myuser" user, but the user "myuser_bis" is missing and its database too. Also, "mybase" database is empty (as if script.sql was not read).
If I comment the line "ADD docker/mysql/init_script_database.sh /docker-entrypoint-initdb.d/init_script_database.sh" from my Dockerfile, I'll will get "mybase" database filled with the sql script I provided, and "myuser" user.

How can I make my shell script work in order to add my second user ?

Thanks in advance for your help.

(also I'm using compose-down and prune volumes each time to be sure I get a fresh start)

@tianon
Copy link
Member

tianon commented Aug 16, 2021

I would suggest posting to a dedicated support forum, such as the Docker Community Forums, the Docker Community Slack, or Stack Overflow.

@wglambert wglambert added the question Usability question, not directly related to an error with the image label Aug 16, 2021
@wglambert
Copy link

Closing since these repos aren't really a user-help forum

@yosifkit
Copy link
Member

When a container is started for the first time, a new database with the specified name will be created and initialized with the provided configuration variables. Furthermore, it will execute files with extensions .sh, .sql and .sql.gz that are found in /docker-entrypoint-initdb.d.

- Docker Hub docs

So if your volume was already initialized from a previous docker-compose up then it will be unchanged. You must first delete the volume (back up any important data first) and start again. (See also, docker-library/postgres#203 (comment))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Usability question, not directly related to an error with the image
Projects
None yet
Development

No branches or pull requests

4 participants