In this tutorial, we will learn how to install Mysql on Docker.
Please follow the steps to install docker using this tutorial Install Docker on Ubuntu
Install Mysql using Docker:
To install the desired version of MySQL first, visit docker mysql image section where we can find the available MySQL version. In this tutorial, we are using MySQL version 5.7.
Use the following command on the server to install the MySQL image
docker pull mysql:5.7
Use your own version instead of 5.7
This will pull the image and install Mysql inside docker. We can verify the image using the following command
docker images
The output of the above command will be similar to this
mysql 5.7 314749b3a5c 17 hours ago 431MB
Create Mysql Container:
Let’s create the MySQL container. To create a MySQL container, use the following command.
docker run --name=cn-mysql -d -p 3316:3306 --env="MYSQL_ROOT_PASSWORD=root1249" mysql:5.7
Note: use your own MySQL root password instead of root1249 and the port instead of 3316 and container name instead of cn-mysql
This will create the container. We can see the container using the following command
docker container ls -a
OR
docker ps -a
Enter into the Mysql server:
If we want to do the regular MySQL operation, then we need to enter the MySQL server inside docker as below
docker exec -it elint-mysql bash
Now login into MySQL using the command
mysql -u root -p
Use your own MySQL password which is used while creating a container i.e root1249
Now we can do operations like creating databases and users.
Creating Mysql User:
In order to create a MySQL user and give permission visit the tutorial, Create Mysql User and Grant Privileges
Let's create a user
create user 'username'@'%' identified by 'password';
Note: change your own username and password. If you need to connect your created user from any client, you should create the user with “%” which signifies, that you can connect this user from any host.
Now, let's grant privileges
grant all privileges on *.* to username;
Use the previously created username
flush privileges;