Install MySQL in Docker

My SQL and Docker

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.28

Start Instance of MySQL

Run MySQL

1docker run --name='mysql' -d -p 3306:3306 mysql/mysql-server
  1. docker run the command for starting the docker container from the image
  2. -name='mysql' after the argument, is defined the name of the container, without this argument name will be generated automatically
  3. d a very important argument, which ensures, that the container in detached mode will be running
  4. p 3306:3306 mapping port from a docker container to host port. Because MySQL is running on port 3306 I mapped exactly this port outside of container.
  5. mysql/mysql-server name of our images — see section 1

You can also check your running container using the docker command.

Check running container

1docker ps

Get 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
MySql default password

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 bash
  1. docker execexecution docker command
  2. it allow tty and keep stdin
  3. my_sql_container name of running container
  4. bash because 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 3

If 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 :)

Connect to mysql