How to deploy Spring boot app to Azure with MySQL Database

In this tutorial, we'll explore the steps to deploy Spring Boot application integrated with a MySQL database on Azure.

Prepare your spring boot application

  1. Create your spring boot application and include dependencies such as 'Spring Web', 'Spring Data JPA', 'Lombok' and 'MySQL Driver'

  2. Configure database connection. Modify application.properties or application.yml in your Spring Boot application to configure the database connection.

     spring.datasource.url=${AZURE_MYSQL_CONNECTIONSTRING}
     spring.datasource.username=${DB_USERNAME}
     spring.datasource.password=${DB_PASSWORD}
     spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQLDialect
     spring.jpa.hibernate.ddl-auto=update
    
  3. Push your spring boot application to a git. Execute this commands on command prompt:

    1. git init

    2. git branch -M main

    3. git remote add origin {name_of_your_repo}

    4. git push -u origin main

Set Up MySQL Database on Azure

  1. Create MySQL Database: Go to the Azure Portal, and create an Azure Database for MySQL.

  2. Fill in the form with the required details:

    • Subscription: Select your Azure subscription.

    • Resource group: Create a new one or select an existing resource group.

    • Server name: Provide a unique name which will be part of the hostname.

    • Location: Choose the Azure region closest to your users.

    • Version: Select the MySQL server version.

    • Admin username: Enter a username for the database administrator.

    • Password: Create and confirm password.

    • Pricing tier: Choose the Basic pricing tier

  3. Configure Firewall Rules: Make sure to set the firewall rules to allow access to the database. You can allow access to Azure services and/or specify IP ranges that can access the database. This you will find in networking section:

  4. Create the Database for the application. Go to the database section and create new database

  5. Access database from the browser or locally. If you want to access db locally use you can connect to MySQL server with the following command:

     mysql -h springboot-db.mysql.database.azure.com -u mhymkukmxx -p
    
  6. Access database from MySQL workbanch. To connect to workbench follow the steps bellow:

    • 1. Click the + symbol in the MySQL Connections tab to add a new connection.

      2. Enter a name for the connection in the Connection name field.

      3. Select Standard (TCP/IP) as the Connection Type.

      4. Enter hostname (ex: springboot-db.mysql.database.azure.com ).

      5. Enter username and then enter your Password.

      6. Go to the SSL tab and update the Use SSL field to Require.

      8. Click Test Connection to test the connection.

      9. If the connection is successful, click OK to save the connection.

  7. Connection string. You can find your connection string in connect section in connect your app part:

Deploy Your Application to Azure

  1. Create a Web App on Azure: Create a Web App and choose your: subscription, resource group (if there is no, create one), name for your app web app and choose region.

  2. Deploy the Application:

    • You can deploy your application through continuous integration pipelines like GitHub Actions.

    • Deployment from github:

      1. Click on Deployment Center from the sidebar.

      2. Choose GitHub as the source.

      3. Authenticate and authorize Azure to access your GitHub account

      4. Select the repository where your application's code is located and the branch to deploy from.

      5. Azure will detect the build pipeline based on the project type and will start an initial deployment using the latest commit from the main branch.

  3. Obtain Database Application Settings: Add application settings to match your application.properties in the app from step 1.

In the Configuration section click new application settings and add name and values for username, password etc. and option "deployment slot setting" should be ckecked.

Verify Deployment

  1. After deploying your application, access it via the URL provided by Azure App Service .

  1. Check with swagger to ensure that your application is connecting to the MySQL database and functioning as expected.