Skip to content

Commit 8a318ee

Browse files
This file is used to demonstrate MySQL database in Docker Container
0 parents  commit 8a318ee

5 files changed

Lines changed: 173 additions & 0 deletions

File tree

README.md

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
# Exercise 8.3 - Relational Database
2+
3+
Create a relational database container and create and access the case study database.
4+
5+
### Step 1
6+
7+
Open the Cloud Platform Console at https://console.cloud.google.com. Go to Compute Engine and VM Instances.
8+
Start the VM if it isn’t running and connect using SSH.
9+
10+
### Step 2
11+
12+
Change to the lab directory.
13+
`cd`
14+
`cd devops-lesson-8/lab-8.3`
15+
16+
### Step 3
17+
18+
Initialize directory structure
19+
20+
Create a Docker data volume to hold the database.
21+
22+
`docker volume create --name monitoring_data`
23+
24+
Confirm that the data volume was created.
25+
26+
`docker volume ls`
27+
28+
Examine the script that will run and create the scase study database structure.
29+
30+
`cat runserver_first`
31+
32+
Run the script to create a container with MySQL running and create the database.
33+
34+
`./runserver_first`
35+
36+
You will need to monitor the logs to see when MySQL has completed creating
37+
the student database and is waiting for connections. This may take several minutes
38+
to complete.
39+
`docker logs mysql`
40+
41+
When you see the following in the logs you may continue. Run the command until you see.
42+
43+
`mysqld: ready for connections.`
44+
45+
Find the IP address of the server.
46+
`docker inspect mysql`
47+
48+
### Step 4
49+
50+
Load data into the student database
51+
52+
Examine the script that will run the MySQL client. Notice that we are using the same image.
53+
`cat runclient`
54+
55+
Run the MySQL client container.
56+
`./runclient`
57+
58+
You will be placed inside the client container running the Bash command shell.
59+
You can now type commands to use the database. You may need to change the IP address to that of the server.
60+
`mysql -h 172.17.0.2 -u student -p monitoring`
61+
62+
You will be prompted for the password. Enter 'student' as the password
63+
`Enter password:`
64+
65+
When you connect you will see the MySQL client prompt
66+
`mysql>`
67+
68+
At the prompt read in the definition for the employee information.
69+
`mysql> source /data/docker.sql;`
70+
71+
### Step 5
72+
73+
Examine the database tables in the database by selecting the first 10 rows from each
74+
75+
`mysql> show tables;`
76+
77+
Get the content of the docker table.
78+
79+
`mysql> select * from docker;`
80+
81+
Insert another row of data and query the table again.
82+
83+
### Step 5
84+
85+
Clean up after the lab
86+
87+
Exit from the MySQL client
88+
`mysql> quit;`
89+
90+
Exit from the client container
91+
`exit`
92+

runclient

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#! /bin/bash
2+
3+
docker run --rm -it -v $(pwd)/sql:/data mysql:5.7 /bin/bash
4+

runserver_first

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#! /bin/bash
2+
#
3+
# File: runserver
4+
#
5+
# Purpose: Runs the MySQL server in a Docker container
6+
#
7+
# NB: Uses a Docker volume named mysql_data to contain database data
8+
#
9+
10+
export MYSQL_IMAGE=mysql
11+
export MYSQL_VERSION=5.7
12+
export ROOT_PASSWORD=rootpw
13+
export STUDENT_USER=student
14+
export STUDENT_PASSWORD=student
15+
export STUDENT_DATABASE=monitoring
16+
export MYSQL_SERVER_CONTAINER_NAME=mysql
17+
export MYSQL_SERVER_EXTERNAL_PORT=3306
18+
export MYSQL_SERVER_INTERNAL_PORT=3306
19+
20+
docker rm -f mysql
21+
22+
docker run -d \
23+
--name mysql \
24+
-p $MYSQL_SERVER_EXTERNAL_PORT:$MYSQL_SERVER_INTERNAL_PORT \
25+
-e MYSQL_DATABASE=$STUDENT_DATABASE \
26+
-e MYSQL_USER=$STUDENT_USER \
27+
-e MYSQL_PASSWORD=$STUDENT_PASSWORD \
28+
-e MYSQL_ROOT_PASSWORD=$ROOT_PASSWORD \
29+
-v monitoring_data:/var/lib/mysql \
30+
${MYSQL_IMAGE}:${MYSQL_VERSION}
31+
docker ps

runserver_first.save

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
i#! /bin/bash
2+
#
3+
# File: runserver
4+
#
5+
# Purpose: Runs the MySQL server in a Docker container
6+
#
7+
# NB: Uses a Docker volume named mysql_data to contain database data
8+
#
9+
10+
export MYSQL_IMAGE=mysql
11+
export MYSQL_VERSION=5.7
12+
export ROOT_PASSWORD=rootpw
13+
export STUDENT_USER=student
14+
export STUDENT_PASSWORD=student
15+
export STUDENT_DATABASE=monitoring
16+
export MYSQL_SERVER_CONTAINER_NAME=mysql
17+
export MYSQL_SERVER_EXTERNAL_PORT=8080
18+
export MYSQL_SERVER_INTERNAL_PORT=8080
19+
20+
docker rm -f mysql
21+
22+
docker run -d \
23+
--name mysql \
24+
-p $MYSQL_SERVER_EXTERNAL_PORT:$MYSQL_SERVER_INTERNAL_PORT \
25+
-e MYSQL_DATABASE=$STUDENT_DATABASE \
26+
-e MYSQL_USER=$STUDENT_USER \
27+
wq
28+
-e MYSQL_PASSWORD=$STUDENT_PASSWORD \
29+
-e MYSQL_ROOT_PASSWORD=$ROOT_PASSWORD \
30+
-v monitoring_data:/var/lib/mysql \
31+
${MYSQL_IMAGE}:${MYSQL_VERSION}
32+
docker ps

sql/docker.sql

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
DROP TABLE IF EXISTS docker;
2+
3+
CREATE TABLE docker (
4+
id INT NOT NULL AUTO_INCREMENT,
5+
event_time DATETIME NOT NULL,
6+
severity VARCHAR(40) NOT NULL,
7+
message VARCHAR(200) NOT NULL,
8+
9+
PRIMARY KEY (id)
10+
) ;
11+
12+
INSERT INTO docker (id,event_time,severity,message)
13+
VALUES (1,NOW(),'INFO','Initial configuration');
14+

0 commit comments

Comments
 (0)