MySQL is the most popular open-source database management system. MySQL uses a relational database and Structured Query Language (SQL) to manage its data.

In this post, I’ll show you how to install MySQL on Ubuntu.

Installing MySQL

There are two ways to install MySQL on Ubuntu. First is to use one of the versions included in the APT package repository by default. Use the following command to find the MySQL version in the APT package repository.

To install MySQL included in the APT repository:

    1. Update the package index on your server

    1. Install the package

During installation, the installer will prompt you to create a root password. Type a secure one and ensure you remember it.

Note: If you are not sure about the version, you can omit the version and run sudo apt-get install mysql-server. This installs the latest version for your Linux distribution.

Installing a Newer MySQL Vwersion

If you want to install the latest MySQL version, such as MySQL 5.7 you’ll need to manually add the MySQL’s repository first. To do so, perform the following steps:

    1. Install the newer APT package repository from the MySQL APT repository page.

    1. Go through the prompt to select and apply the specific MySQL version to install.
    2. Update the package index on your server.

    1. Install the package.

Once installation is complete, MySQL should run automatically. You can check the status of MySQL using the following command.

In order to stop MySQL from the running state, run the following command:

To start MySQL, run the following command:

Refer to the official documentation here for various post-installation setup and configurations of MySQL.

0
Share

Out of the box, Spring Boot is very easy to use with the H2 Database. If the H2 database is found on your classpath, Spring Boot will automatically set up an in memory H2 database for your use. But what if you want to use MySQL? Naturally, Spring Boot has support for MySQL and a number of other popular relational databases.

Previously, I wrote about creating a web application using Spring Boot.  Let’s say we want to deploy this application to production and we’ve decided to use MySQL for the database. Changing Spring Boot from H2 to MySQL is easy to do.

MySQL Configuration

For this example, I’m using MySQL installed locally on my MacBook Pro. You’ll need to have a database defined for your use. For this example, I want to create a database for my use. Using the command prompt, you can log into MySQL with this command:

Use the following command to create a database.

You only need to use these commands if you want to use a new database. MySQL is a very robust database. The full capabilities of MySQL are beyond the scope of this tutorial.

MySQL Dependencies

First we need to add the MySQL database drivers to our project. You will need to add the following dependency to your Maven POM file.

POM.xml

Spring Boot Properties

We need to override the H2 database properties being set by default in Spring Boot. The nice part is, Spring Boot sets default database properties only when you don’t. So, when we configure MySQL for use. Spring Boot wont setup the H2 database anymore.

The following properties are need to configure MySQL with Spring Boot. You can see these are pretty standard Java data source properties. Since in my example project, I’m using JPA too, we need to configure Hibernate for MySQL too.

NOTE: If this was actually a production database, you not tell Hibernate to use the create-drop option. This tells Hibernate to recreate the database on startup. Definitely not the behavior we want. You can set this property to the following values: none, validate, update, create-drop. If this was actually a production database, you probably would want to use validate.

Running Spring Boot with MySQL

This is all that needs to be changed to use MySQL with Spring Boot. When you start the project now, MySQL will be used by the Spring Boot application for the database.

Free Introduction to Spring Tutorial

Are you new to the Spring Framework? Checkout my Free Introduction to Spring Online Tutorial.

 

Get The Source

The source code for this post is available on GitHub here. You can download the source and build the project using Maven.

1
Share