
Install MySQL in Docker
Download the images
Download images
1# Install latest version
2docker pull mysql/mysql-server
3
4# If want to install spesific version
5docker pull mysql/mysql-server:8.0.28Start Instance of MySQL
Run MySQL
1docker run --name='mysql' -d -p 3306:3306 mysql/mysql-serverdocker runthe command for starting the docker container from the image-name='mysql'after the argument, is defined the name of the container, without this argument name will be generated automaticallyda very important argument, which ensures, that the container in detached mode will be runningp 3306:3306mapping port from a docker container to host port. Because MySQL is running on port 3306 I mapped exactly this port outside of container.mysql/mysql-servername of our images — see section 1
You can also check your running container using the docker command.
Check running container
1docker psGet generated MySQL initial password so we can login to MySQL bash. to do it we can run command to see the log.
1docker logs mysql # mysql is the name of container
if you try to connect with some tools (for example DBeaver) it is not working. The reason is, that the root user has no corresponding permission. You must grant the permission. Due to start the command line in the container.
Log in to MySQL bash
Open mysql
1docker exec -it mysql bashdocker execexecution docker commanditallow tty and keep stdinmy_sql_containername of running containerbashbecause we want to start bash
Connect to DB
1# go to mysql directory
2cd /var/lib/mysql
3# connect to db
4mysql -u root -p
5
6# Then enter the initial password from step 3If want to change default password you can run this query after input initial password
Change root password
1ALTER USER 'root'@'localhost' IDENTIFIED BY 'newpassword';Now we are ready to check the list of users:
Check list user
1use mysql;
2select user from user;Create User Connection
In this step we make connection so we can connect to our database using tools like Dbeaver, SqlYog, etc.
Run this query to make new user
Create user connection
1CREATE USER 'dbeaver'@'%' IDENTIFIED BY 'dbeaver';
2GRANT ALL PRIVILEGES ON *.* TO 'dbeaver'@'%' WITH GRANT OPTION;
3FLUSH PRIVILEGES;In this query we make user dbeaver and password is dbeaver. we grant all access for this user
Finally, you are ready to connect with some database client. I have chosen for DBeaver, because I have used it for a time. In the next picture, you can see the configuration. It is not so complicated :)
