In this article, we will install the MySQL on docker, and configure additional account for specific database access only.

Install the MySQL on docker

  1. Check if you have installed the docker, by executing:
1
sudo docker --version

If it show the docker version, which means you have installed, if not you could install docker via the installation guide

  1. Pull the mysql image:
1
sudo docker pull mysql:latest
  1. Run a MySQL container with the following command:
1
sudo docker run -d --name mysql_container -e MYSQL_ROOT_PASSWORD={Your Root Account Password} -p 3306:3306 mysql:latest
  1. Verify that the MySQL container is running
1
sudo docker ps
  1. If you need make it always running
1
sudo docker update --restart=always mysql-container

Basic commands

  1. Entering the MySQL Console
1
sudo docker exec -it {Your Container ID} mysql -u root -p

and then entering you root password to enter the console.

  1. Create a new user with limited access to the specified databases:
1
2
3
CREATE USER 'new_user'@'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON {Database Name}.* TO 'new_user'@'localhost';
FLUSH PRIVILEGES;

If you wanna this additional account could be access externally, then replace localhost to %

Verify the user has been created successfully and has access to the specified databases:

1
SHOW GRANTS FOR 'new_user'@'localhost';

Exit the MySQL command line interface:

1
exit;