Home / Deploying a MySQL database backed Java app to Tomcat on Azure with Azure DevOps

Overview

In this lab, you will learn how you can use Azure Pipelines to deploy a Java web application to Apache Tomcat with a MySQL database on Azure. Apache Tomcat is an open-source Java Servlet Container developed by the Apache Software Foundation (ASF). MySQL is a very popular open-source relational database management system.

For this lab, you will use Azure App Service and Azure Database for MySQL, a relational database service based on the open source MySQL Server engine. It is a fully managed database as a service, capable of handing mission-critical workload with predictable performance and dynamic scalability.

What’s covered in this lab

This lab will show how you will

  • Create a new Azure App Service with a MYSQL database server and configure the web app to use Apache Tomcat
  • Use Azure App Service Task to deploy a WAR file

Before you begin

  1. Refer the Getting Started page before you follow the exercises.

  2. Use MyShuttle as a template to provision the new Azure DevOps project using the Azure DevOps Demo Generator.

Exercise 1: Creating Azure Web App and MySQL database

  1. Click the below button to provision a Website and MySQL Database together to start developing even faster.

    Tomcat Configuration

  2. This template provides two MySQL options :

    • Azure Database for MySQL provides a managed database service for app development. You will choose this option.
    • MySQL In app is an App Service feature where MySQL database is created for your web app. In this scenario, MySQL server is running on the same instance side by side with your web server hosting the site. This is recommended for development purposes and has no additional cost.

    Create Web App+MySQL

  3. Wait for the Web App and the database to be provisioned. It roughly takes 3-5 minutes.

  4. Navigate to the resource group that you have created. You should see a Azure Database for MySQL server provisioned. Select the database server.

    Resource Group

  5. Select Properties. Save the SERVER NAME and SERVER ADMIN LOGIN NAME to a notepad.

    Database properties

    In this example, the server name is myshuttle-1-mysqldbserver.mysql.database.azure.com and the admin user name is mysqldbuser@myshuttle-1-mysqldbserver.

Exercise 2: Updating the App Settings for the Web App

Next, navigate to the Web app that you have created. As you are deploying a Java application, you need to change the web app’s web container to Apache Tomcat.

  1. Click Application Settings. To change it to Tomcat, you will first need to install Java. Select a Java Version to install and then change Web container to use Apache Tomcat. For this purpose of the lab, you will choose Java 8 and Apache Tomcat 9.0 though the version number would not matter much for the simple app that we are deploying.

    Setting Web container to Tomcat

  2. Click on Save and wait for the update to be applied. The web page will now look like the below image.

    Default Java App

    Next, you need to update the connection strings for the web app to connect to the database correctly. There are multiple ways you can do this - but for the purpose of this lab, you will take a simple approach by updating it directly on the Azure portal.

  3. From the Azure portal, select the Web app you provisioned. Select Application Settings and scroll down to the Connection Strings section.

  4. Add a new MySQL connection string with MyShuttleDb as the name, paste the following string for the value and replace MySQL Server Name, your user name and your password with the appropriate values -

    jdbc:mysql://{MySQL Server Name}:3306/alm?useSSL=true&requireSSL=false&autoReconnect=true&user={your user name}&password={your password}

    DB Connection

    • MySQL Server Name : Value that you copied previously from the MySQL server Properties.
    • your user name : Value that you copied previously from the MySQL server Properties.
    • your password : Value that you provided during the creation of MYSQL database server in the Deploy to Azure phase.
  5. Click on Save to save the connection string.

     String conStr = System.getenv("MYSQLCONNSTR_MyShuttleDb");
    

You have now setup and configured all the resources that is needed to deploy and run the MyShuttle application.

Exercise 3: Deploy the changes to Web App

  1. Select Pipelines and then, Builds. Choose the build MyShuttleBuild and click Edit Pipeline to view the build definition.

    Builds

    The lab uses the standard Maven build template to compile the code, copy and publish the resulting artifacts for deployment. An additional file which is copied here is the CreateMYSQLDB.sql file which creates a MYSQL database and inserts a few records into it during the deployment.

  2. Click Queue to queue the build and wait for the build to complete.

    Queue Build Queue Build 2

  3. Once the build succeeds, Select Releases under Pipelines.

  4. Select MyShuttle Release and click Edit Pipeline to open the release definition.

    Edit MyShuttle Release Definition

  5. Make sure the artifact is pointing to the Build artifact as shown below. If you are following this lab from Jenkins hands-on-lab, make sure the artifact is pointing to Jenkins.

    Team Build Artifact

  6. Click Tasks, select Azure-Dev and choose the Azure subscription details from the drop down. Click Authorize and login to your Azure subscription in the pop-up window. Provide or choose the created App Service Name with the web app that you created previously in this lab.

    Link Parameters

  7. Click Tasks and select Execute Azure MySQL : SqlTaskFile task and provide the following details.

    • Azure Subscription Details : Select the appropriate subscription.
    • Host Name : Select the MySQL Database server host name that was created.
    • Server Admin Login : Provide the SERVER ADMIN LOGIN NAME that you noted down previously.
    • Password : Provide the password that you created during the creation of Azure Web App + MYSQL database server in the Azure portal.

    Execute Azure MySQL Task

  8. Select the Deploy Azure App Service task and ensure that the created App service name is reflected correctly.

  9. Click on Save and then +Release | Create Release to start a new release

    MyShuttle Release Definition

  10. Wait for the release to complete. Then navigate to the Web App and select the URL from the overview blade. Add /myshuttledev context to the URL. For instance - http://myshuttle1.azurewebsites.net/myshuttledev

  11. Select Login and try logging in to the site with any one of the following credentials.

    Username Password
    barney barneypassword
    fred fredpassword
  12. If your database was setup correctly and the connection parameters are valid, you should be able to login to the portal.

    MyShuttle page after login

Summary

In this lab, you have learnt how to deploy a Tomcat based Java application with MySQL database on Azure with Azure Pipelines.