상세 컨텐츠

본문 제목

Running MySQL in Docker and Connecting with MySQLWorkbench

카테고리 없음

by BenzhaminKim 2020. 7. 15. 17:10

본문

In this post, I'm going to show you how to run MySQL server in Docker and connect it using MySQLWorkbench

 

First, you need to install docker on your machine.

 

https://www.docker.com/get-started

 

Get Started with Docker | Docker

Learn about the complete container solution provided by Docker. Find information for developers, IT operations, and business executives.

www.docker.com

You can download Docker Desktop from the link.

 

After you finish the installation, you need to customize docker resources for MySQL.

 

MySQL needs resources at leat 4GB memory. 

 

The resources are depends on your machine.

Second, we are going to pull the mysql image.

sudo docker pull mysql

 

Now run the following command.

sudo docker run --name mysql -p 3406:3306 -e MYSQL_ROOT_PASSWORD=anypassword -d mysql/mysql-server

 

--name: it's the name of your docker container

-p 3406:3306: This is the port you are going to connect your machine and the container. You're going to use 3406 port to connect 3306 port which is from the docker cotainer

-e MYSQL_ROOT_PASSWORD : This is to set a password for MySQL

-d : It's the name of the docker image you are going to use.

 

Log into MySQL within the docker container using the docker exec command:

 

$ sudo docker exec -it mysql bashbash-4.2# mysql -uroot -panypassword

 

Remember, when we created and ran the MySQL container, we provided MYSQL_ROOT_PASSWORD=anypassword.

Create a database and user, and grant privileges in MySQL (from within the container).

Log into MySQL if you haven’t already. After login, the mysql> prompt shows up:

bash-4.2# mysql -udev -psecret

 

I create a user named dev, grant all privileges, and quit.

 

Important: This step is required to log into MySQL from outside the container. The root user will not be able to log in from the host OS (Linux). Use % instead of localhost in dev@localhost.

 

mysql> CREATE USER 'dev'@'%' IDENTIFIED BY 'secret';

Query OK, 0 rows affected (0.00 sec)

mysql> GRANT ALL PRIVILEGES ON * . * TO 'dev'@'%';

Query OK, 0 rows affected (0.00 sec)

mysql> quit

 

Connect to MySQL running in Docker from MySQL Workbench. Open MySQL Workbench and click on + to add a new connection. Enter all the information as stated in the screenshot and click on Test Connection.

댓글 영역