DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Software Design and Architecture
  3. Cloud Architecture
  4. Build a Java and MySQL App on VMs and Containers in Azure

Build a Java and MySQL App on VMs and Containers in Azure

Here's a straightforward guide to building Java apps with a MySQL DB on the Microsoft Azure cloud using Spring Boot, VMs, and containers.

Brian Benz user avatar by
Brian Benz
·
Apr. 16, 18 · Tutorial
Like (4)
Save
Tweet
Share
9.71K Views

Join the DZone community and get the full member experience.

Join For Free

Greetings, Java devs!  It's a thrill for me to post my first article on DZone. In this post, I’ll show you how to retrieve a simple Java App from GitHub and connect it to the Azure Database for MySQL. In part two (on my blog), I’ll show you how to deploy the application to a Linux VM on Azure, and in part three (also on my blog), we’ll deploy a Docker container on Azure App Service Web Apps.

The application uses Spring Boot to package and deploy the app and uses embedded Tomcat as the app engine. It connects to MySQL via a connection string, which we will adapt to connect to the new Azure MySQL service.

Steps for Today

  • Create a MySQL database in Azure
  • Connect the app to the Azure MySQL database

In the next two posts, we will:

  • Deploy the app to an Azure Linux VM
  • Deploy the app to Docker via Azure Container Service

Prerequisites

  • A Git Client
  • Java 7 JDK or above
  • The Azure CLI 2.0
  • An Azure account (Free Trial here)

Clone the GitHub Sample

From the command prompt, navigate to a working directory and clone the sample repository.

git clone https://github.com/bbenz/spring-boot-todo


Configure the app to use the MySQL database

Create an Azure MySQL Database With the Azure CLI

Next up, let’s create an Azure Database for MySQL instance using the Azure CLI. We will Use the Azure CLI 2.0 in a terminal window to create the resource group and a MySQL instance.

Log In and Create a Resource Group

Log in to your Azure subscription with the az login command, then follow the on-screen directions.

Create an Azure Resource Group

Azure resource groups manage Azure services together as a unit. Each resource group must have a location. To see all possible values you can use for — location, use the az appservice list-locations command.

The following example creates an Azure resource group in the North Europe region.

az group create — name myResourceGroup — location “North Europe”


Create a MySQL Server

Create a server in Azure Database for MySQL.

Substitute your own unique MySQL server name where you see the <mysql_server_name> placeholder. This name is part of your MySQL server’s hostname, <mysql_server_name>.mysql.database.azure.com, so it needs to be globally unique. Also substitute <admin_user> and <admin_password> with your own values.

az mysql server create — name <mysql_server_name> — resource-group myResourceGroup — location “North Europe” — admin-user <admin_user> — admin-password <admin_password>


When the MySQL server is created, the Azure CLI returns information similar to this example:

{

    “administratorLogin”: “admin_user”,

    “administratorLoginPassword”: null,

    “fullyQualifiedDomainName”: “mysql_server_name.mysql.database.azure.com”,

    “id”: “/subscriptions/00000000–0000–0000–0000–000000000000/resourceGroups/myResourceGroup/providers/Microsoft.DBforMySQL/servers/mysql_server_name”,

    “location”: “northeurope”,

    “name”: “mysql_server_name”,

    “resourceGroup”: “mysqlJavaResourceGroup”,

}


Configure a MySQL Firewall

Create a firewall rule for your MySQL server to allow client connections by using the az mysql server firewall-rule create command. Here’s an example that creates a firewall rule for a broad range of IP addresses (You will likely want to narrow your actual firewall IP address range):

az mysql server firewall-rule create — name allIPs — server <mysql_server_name> — resource-group myResourceGroup — start-ip-address 0.0.0.0 — end-ip-address 255.255.255.255


Configure the Azure MySQL Database

Connect to the MySQL server using the values you specified previously for <admin_user> and <mysql_server_name>.

mysql -u <admin_user>@<mysql_server_name> -h <mysql_server_name>.mysql.database.azure.com -P 3306 -p


In the mysqlprompt, create a database and a table for the to-do items.

CREATE DATABASE tododb;


Create a database user and give it all privileges in the tododb database. Replace the placeholders <Javaapp_user> and <Javaapp_password> with your own unique app name:

CREATE USER ‘<Javaapp_user>’ IDENTIFIED BY ‘<Javaapp_password>’;

GRANT ALL PRIVILEGES ON tododb.* TO ‘<Javaapp_user>’;


Exit your server connection by typing “quit”.

Update the Values in the application.properties File

Update the following values in src/main/resources/application.properties:

spring.datasource.url=jdbc:mysql:// >@<mysql_server_name>.mysql.database.azure.com:3306/tododb

spring.datasource.username=adminname@<mysql_server_name>

spring.datasource.password=password

spring.datasource.driver-class-name=com.mysql.jdbc.Driver

spring.jpa.hibernate.ddl-auto=update


Build and Run the Sample

Build and run the sample locally using the Maven wrapper included in the repo:

mvn package spring-boot:run


In a browser, open http://localhost:8080to work with the todo app.

Next up, in part two (on my blog), I show you how to deploy the application to a Linux VM on Azure, and in part three (also on my blog) I deploy a Docker container on Azure App Service Web Apps.

azure app MySQL Docker (software) Java (programming language) Build (game engine)

Published at DZone with permission of Brian Benz. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Multi-Cloud Integration
  • How To Choose the Right Streaming Database
  • 11 Observability Tools You Should Know
  • DevOps for Developers: Continuous Integration, GitHub Actions, and Sonar Cloud

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: