Using Docker To Run MySQL Server In Your Development Environment

Using Docker To Run MySQL Server In Your Development Environment

3 Comments

Today I’m going to show you how can you can use Docker to run MySQL Server in your development environment. Using Docker to run MySQL is much easier than manually installing it and for when you have multiple instances of MySQL running at the same time. I’m assuming that you already have Docker Community Edtion install on your machine. If you do not have Docker installed follow the instruction here for your operating system.

Install MySQL Using Docker

If you search online for how to install MySQL using Docker, you will see many different examples on how to do this. I’m going to show how to do it so that you can connect to the database from your IDE and from the applications you are developing.

Type this command at the terminal:

$  docker run -p 3306:3306 --name hb-mysql-example -e MYSQL_ROOT_PASSWORD=Buster -d mysql

Let’s  cover what each part of this command is doing:

  1.  Run is creating a new Docker container running MySQL. The mysql at the end of the command is saying to use the latest version of MySQL. This command will also download the MySQL image if itis not already downloaded.
  2. -P is telling the container to map port 3306 of your localhost to port 3306 of the container.
  3. -Name is setting the name of the container to hb-mysql-model.
  4. -E is creating the environment variable MYSQL_ROOT_PASSWORD and setting it’s value to Buster.
  5. -D this is telling Docker to run the container in the background.

List Running Docker Containers

You use this following command to list which containers are running:

$ docker ps

This command will output something like this to the terminal:

Starting, Restarting and Stopping Docker Containers

Containers do not start up by default when you turn on your machine or do a restart of your machine. Here I will show you how to start, stop and restart containers.

Starting a Container

To start a container, you use this command: docker start containerName.

To start the hb-mysql-example container use this command:

$ docker start hb-mysql-example

 

Restarting a Container

To restart a container, you use this command: docker restart containerName.

To restart the hb-mysql-example container use this command:

$ docker restart hb-mysql-example

 

Stopping a Container

To stop a container, you use this command: docker stop containerName.

To stop the hb-mysql-example container use this command:

$ docker stop hb-mysql-example

 

Connecting To MySQL  Container From IntelliJ

Here I will how to connect to MySQL using IntelliJ. If you do not have IntelliJ, you can download it here.

Follow these steps:

  1. Open IntelliJ
  2. Click on the database tab on the right side of the window.
  3. Click on the “+” button to add a new database connection.
  4. Select MySql from the list of database servers.
  5. Fill in the host, database name, username and password like in the image below and click the OK button.

You are now connected to MySQL and can start working with MySQL.

Conclusion

In this post, you learned how to install MySQL using docker, how to start, restart, and stop containers. You also learn how to connect to MySQL from IntelliJ.

 

Originally published at fluentjava.com

About Harrison Brock

Java and Go Software Engineer.

You May Also Like

3 comments on “Using Docker To Run MySQL Server In Your Development Environment

  1. April 15, 2020 at 11:06 am

    Where will be the data stored? There is no volume.

    Reply
    • April 15, 2020 at 3:21 pm

      Inside of a temporary volume. You’d need to map a volume if persistence is needed.

      Reply
  2. July 1, 2021 at 8:56 am

    Hi, what name sould be insert in intelliJ? hb-mysql-example or mysql? I am confused^^

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.