From 0fdc2e4a52356807ee7b33501804997c8e4225e0 Mon Sep 17 00:00:00 2001
From: Marcelo C Barbosa <marcelo.barbosa@brasilparalelo.com.br>
Date: Tue, 13 Sep 2022 16:21:27 -0300
Subject: [PATCH 1/2] added docker compose and docker commands to run mysql
 using a public mysql image, rather than having to install it locally

---
 Readme.md                                 | 28 +++++++++++++++-------
 aliases.sh                                |  2 ++
 mysql.yaml                                | 29 +++++++++++++++++++++++
 src/main/resources/application.properties |  7 +++---
 4 files changed, 55 insertions(+), 11 deletions(-)
 create mode 100644 aliases.sh
 create mode 100644 mysql.yaml

diff --git a/Readme.md b/Readme.md
index dd07c08..996c3ca 100644
--- a/Readme.md
+++ b/Readme.md
@@ -4,11 +4,11 @@ Build Restful CRUD API for a simple Note-Taking application using Spring Boot, M
 
 ## Requirements
 
-1. Java - 1.8.x
+1. Java - 1.8.x --> actually 18.0.2
 
-2. Maven - 3.x.x
+2. Maven - 3.x.x --> actually 3.8.6
 
-3. Mysql - 5.x.x
+3. Mysql - 5.x.x -> actually 8.0.30
 
 ## Steps to Setup
 
@@ -18,16 +18,27 @@ Build Restful CRUD API for a simple Note-Taking application using Spring Boot, M
 git clone https://github.com/callicoder/spring-boot-mysql-rest-api-tutorial.git
 ```
 
-**2. Create Mysql database**
+**2. Create Mysql database in Docker**
 ```bash
-create database notes_app
+source aliases.sh
+mysql_up
+mysql_get_in
 ```
 
-**3. Change mysql username and password as per your installation**
+**3 Configure mysql and create testuser with testpassword**
+```bash
+# root and mypassword configured when running mysql via Docker
+mysql -uroot -pmypassword
+
+# now inside mysql shell
+create database notes_app;
+create user 'testuser'@'%' identified by 'testpassword';
+grant all on notes_app.* to 'testuser'@'%';
+```
 
-+ open `src/main/resources/application.properties`
++ Corresponding configuration in `src/main/resources/application.properties`
 
-+ change `spring.datasource.username` and `spring.datasource.password` as per your mysql installation
++ A Adminer UI interface to interact with MySQL will be available on `localhost:8081`
 
 **4. Build and run the app using maven**
 
@@ -60,6 +71,7 @@ The app defines following CRUD APIs.
 
 You can test them using postman or any other rest client.
 
+Note that each note shall receive a body with title and content as a JSON.
 ## Learn more
 
 You can find the tutorial for this application on my blog -
diff --git a/aliases.sh b/aliases.sh
new file mode 100644
index 0000000..ce52fa4
--- /dev/null
+++ b/aliases.sh
@@ -0,0 +1,2 @@
+alias mysql_up="sudo docker compose -f mysql.yaml up"
+alias mysql_get_in="sudo docker exec -it mysqlcontainer bash"
\ No newline at end of file
diff --git a/mysql.yaml b/mysql.yaml
new file mode 100644
index 0000000..eb4ce12
--- /dev/null
+++ b/mysql.yaml
@@ -0,0 +1,29 @@
+version: "3.9"
+# https://hub.docker.com/_/mysql
+# https://find10.medium.com/how-to-connect-a-mysql-docker-container-with-a-local-spring-boot-application-9366707dce0d
+services:
+
+  db:
+    image: mysql:8.0.30
+    container_name: mysqlcontainer
+    # NOTE: use of "mysql_native_password" is not recommended: https://dev.mysql.com/doc/refman/8.0/en/upgrading-from-previous-series.html#upgrade-caching-sha2-password
+    # (this is just an example, not intended to be a production configuration)
+    command: --default-authentication-plugin=mysql_native_password
+    restart: always
+    environment:
+      MYSQL_ROOT_PASSWORD: mypassword
+    ports:
+      - "3306:3306"
+    expose:
+      - "3306"
+    volumes:
+      - my-db:/var/lib/mysql
+
+  adminer:
+    image: adminer
+    restart: always
+    ports:
+      - "8081:8080"
+
+volumes:
+  my-db:
diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties
index d357d46..48ed098 100644
--- a/src/main/resources/application.properties
+++ b/src/main/resources/application.properties
@@ -1,7 +1,8 @@
 ## Spring DATASOURCE (DataSourceAutoConfiguration & DataSourceProperties)
-spring.datasource.url = jdbc:mysql://localhost:3306/notes_app?useSSL=false&serverTimezone=UTC&useLegacyDatetimeCode=false
-spring.datasource.username = root
-spring.datasource.password = callicoder
+spring.datasource.url = jdbc:mysql://localhost:3306/notes_app
+## ?useSSL=false&serverTimezone=UTC&useLegacyDatetimeCode=false
+spring.datasource.username = testuser
+spring.datasource.password = testpassword
 
 
 ## Hibernate Properties

From 8be103f4fc96fed721d5e91a67c8777a8089a911 Mon Sep 17 00:00:00 2001
From: Marcelo C Barbosa <marcelo.barbosa@brasilparalelo.com.br>
Date: Tue, 13 Sep 2022 16:30:14 -0300
Subject: [PATCH 2/2] updating README.md

---
 Readme.md | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/Readme.md b/Readme.md
index 996c3ca..87b3150 100644
--- a/Readme.md
+++ b/Readme.md
@@ -1,6 +1,6 @@
-# Spring Boot, MySQL, JPA, Hibernate Rest API Tutorial
+# Spring Boot, MySQL (via Docker), JPA, Hibernate Rest API Tutorial
 
-Build Restful CRUD API for a simple Note-Taking application using Spring Boot, Mysql, JPA and Hibernate.
+Build Restful CRUD API for a simple Note-Taking application using Spring Boot, Mysql (via Docker), JPA and Hibernate.
 
 ## Requirements
 
@@ -15,7 +15,7 @@ Build Restful CRUD API for a simple Note-Taking application using Spring Boot, M
 **1. Clone the application**
 
 ```bash
-git clone https://github.com/callicoder/spring-boot-mysql-rest-api-tutorial.git
+git clone https://github.com/mcbarb/spring-boot-mysql-rest-api-tutorial.git
 ```
 
 **2. Create Mysql database in Docker**
