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.
Join the DZone community and get the full member experience.
Join For FreeGreetings, 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 mysql
prompt, 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:8080
to 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.
Published at DZone with permission of Brian Benz. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments