mysql docker container - grant user privileges - warning

Viewed 11426

I have started the standard mysql docker container and now want to create a user and grant privileges to him. But there happens nothing and i get this warning:
MySQL is started in --skip-name-resolve mode; you must restart it without this switch for this grant to work

I am not so familiar with mysql - so what should i do here?

A much better solution would be to start this container with an additional sql script.
What should i do to start the container with an script like this:

CREATE USER 'myuser'@'localhost' IDENTIFIED BY 'myuser';
GRANT ALL PRIVILEGES ON * . * TO 'myuser'@'localhost';

That is my actual docker command:
docker run -p 3306:3306 --name mysql-server -e MYSQL_ROOT_PASSWORD=root -d mysql:latest

Could some one help me here?

1 Answers

I have got into the same situation, in my case I have to start my standard MySQL container with readonly user with SELECT only privilege. I have created a SQL script and copy to a folder /docker-entrypoint-initdb.d/ inside an image. All SQL files inside /docker-entrypoint-initdb.d/ the directory would be executed by default when MySQL container boots. I have included my working code snippets which would suffice for the requirement, just modify the SQL in the privileges.sql file as per your need. Additionally, As you have started with --skip-name-resolve mode, MySQL does not consider the DNS lookup values and look for IP address which is default 127.0.0.1. For the generic purpose, I have used % (any valid host)

privileges.sql

CREATE USER 'readonly'@'%' IDENTIFIED BY 'readonly';
GRANT SELECT ON *.* TO 'readonly'@'%';
FLUSH PRIVILEGES;

Dockerfile-mysql

FROM mysql:latest
ENV MYSQL_ROOT_PASSWORD root
COPY ./schemapath/privileges.sql /docker-entrypoint-initdb.d/

Finally, execute below docker comments to create a custom MySQL image and run it as a container in detached mode.

docker build -t custom-mysql . --file ./Dockerfile-mysql
docker run -d --name custom-mysql-container -p 127.0.0.1:3306:3306 custom-mysql

you could validate the above commands using the following steps:

docker exec -it mysql-it-container /bin/bash
mysql -uroot -proot

mysql> show grants for 'readonly';
+---------------------------------------+
| Grants for readonly@%                 |
+---------------------------------------+
| GRANT SELECT ON *.* TO 'readonly'@'%' |
+---------------------------------------+
1 row in set (0.01 sec)

Related