Skip to content

Latest commit

 

History

History
120 lines (88 loc) · 5.08 KB

File metadata and controls

120 lines (88 loc) · 5.08 KB

Exercise 1: Run MySQL in a Docker Container

This exercise will make you run MySQL in a Docker Container on your local developer computer.

Docker Cheat Sheet: Commands that might come in handy for the exercises.

Before we start, you might wonder why do this, if you already have MySQL installed on your computer.
There are several benefits. What if you quickly want to test another version of MySQL. This is super easy with Docker. You might need alternative databases, like MongoDB, Redis etc. Again super easy to get just the version you need via the right Docker Image. There is actually no need at all for you to install MySQL on your own computer. You can just run it in a container. This is also a good way to ensure that ALL developers are using the same version.

Preconditions

This assumes you have installed Docker locally on your developer computer.

Learning outcome

● Have a conceptual understanding of why virtualization is a key concept in modern software development
● Have installed Docker on your local developer computer
● Know how to install and run images in a local docker container
● Know how to install a MySQL Image and run it in a container exposing the internal MySQL port to your own liking.
● Know the basics required to navigate in a Linux Shell

Get the image and start the Container

1)
In a terminal on your local computer execute the following statements (on a remote Ubuntu Server and possibly also on Mac, you need to prefix the commands with sudo)

docker pull mysql:8.0.38

Verify that the image was installed
Info: You can replace 8.0.38 above (and in the following) with the tag "latest" to get the latest version of the MySQL image. However, it's a good practice to specify a version tag instead of using the latest, to ensure consistency across different environments. Also, as of this writing, 8.0.38 is the newest version supported by MySQL Workbench. If you install a 8.1.X version Workbench will complain.

2)
Now type the following to start the container. You can change the name my-docker-mysql to you own liking
Important: Everything below MUST be in one line, NO LINE-BREAKS!

docker run \--name my-docker-mysql \--restart unless-stopped \-p 3307:3306 \-e MYSQL\_ROOT\_PASSWORD=test-1234 \-d mysql:8.0.38

Verify - and ensure that you can answer all the following questions:

  • A new mysql 8.0.38 image was "downloaded"
  • A new container, named my-docker-mysql was created
  • What's the idea with the environment flag (-e)
  • What's the idea with --restart flag, and the suggested value
  • You can read the log files generated by MySQL
  • Explain why we use 3307
  • Verify that you can stop, start, restart the server

Access your MySQL server via a bash inside the container

3)
By now you have a running container, with a Linux image and MySQL installed. Let’s navigate into a terminal (bash window) inside this container and connect to the MySQL server
In your terminal type the following (remember sudo on Ubuntu and Mac)

docker exec \-it my-docker-mysql bash

This should open a terminal connected to the bash shell running INSIDE your container.

4)
In this terminal type the following to open a MySQL Client:

mysql \-u root \-p

Enter the password you selected when you started the container (test-1234, if you did CUT AND PASTE)

5)
Execute a few SQL commands, to convince yourself that we have a functional MySQL-client, for example:

show databases;  
create database dummy\_db;  
show databases;  
drop database dummy\_db  
show databases

6)
Now type exit to return to you bash terminal in the container.

7)
Type exit one more time to return to "your own" terminal.

Create a non-root user to use with our new containerized database

8)
Now, repeat all the steps above to login to the MySQL client in the container.

9)
Create a non-root user with credentials which we will use when we connect from our Spring Boot apps and Workbench. Replace user and password to your own liking.

CREATE USER 'a\_user'@'%' IDENTIFIED BY 'a\_safe\_password';  
GRANT ALL ON \*.\* TO 'a\_user'@'%';

10)
Verify that the user with the selected grants was created

SELECT User, Host FROM mysql.user;  
 show grants for 'a\_user'@'%';

11)
Finally, before you exit, create a small database.

Connect to your containerized MySQL server from Workbench and Java

12)
Open Workbench or whatever MySQL Client you are using, and create a new MySQL Connection
Give it a name, use 127.0.0.1, and enter the port you used when you started the container (3307), and the username and password for the non-root user created in step 9.
Verify that you can connect, and see the database you created in step 11.

13)
Open a (Spring Boot) Project that uses a MySQL database, change the connection parameters to match your containerized server, and verify that you can use the database you made in step 11.

>> Next