MySQL MariaDB

How to install and set up MySQL Database on Ubuntu 22.04

In the modern big data ecosystem, MySQL is one of the most well-known technologies. It is also referred to as one of the most popular and effective databases across many industries. To master this MySQL database management system, only a basic understanding is required. Even if you are unfamiliar with relational systems, you can create secure, fast, and powerful storage systems.

This write-up will discuss the procedure to install and set up the MySQL database on Ubuntu 22.04. So, let’s start!

How to install MySQL database on Ubuntu 22.04

To install MySQL database on Ubuntu 22.04, you must follow the below-given step-by-step instructions.

Step 1: Update system repositories

Press “CTRL+ALT+T” to open the terminal of the Ubuntu 22.04 and run the below-given command to update system repositories:

$ sudo apt update

Step 2: Install MySQL database on Ubuntu 22.04

In the next step, install MySQL database on your system with the help of the following command:

$ sudo apt install mysql-server

Wait for a few minutes as the installation of the MySQL database management system will take some time to complete:

Step 3: Verify MySQL service status

Then, check the status of the “mysql” service:

$ systemctl is-active mysql

The given output signifies that “mysql” service is currently active and running on our Ubuntu 22.04 system:

Step 4: Configure MySQL server

Now, write out the below-given command for performing an initial and interactive configuration of the MySQL server:

$ sudo mysql_secure_installation

In the configuration manual, you will be first asked to set the level for the password validation policy. Input a number from the given menu according to your password validation requirements:

After doing so, type out the new password for “root” and enter “y” to continue with the provided password:

Also, set the other configuration options accordingly:

Step 5: Log in to MySQL server

Next, log in to the MySQL server for setting the default authentication method to “mysql_native_password” and specify a native password for the root:

$ sudo mysql

As you can see, the following query will set the root password to “Password123#@!” and the authentication method to “mysql_native_password”:

> ALTER USER 'root'@'localhost'

IDENTIFIED WITH mysql_native_password BY 'Password123#@!';

Reload the grant tables in the MySQL database so that the changes can be applied without restarting the “mysql” service:

> FLUSH PRIVILEGES;

Again try to login into the MySQL database with the password you have set previously:

The above-given error-free output signifies that we have successfully installed MySQL database on our Ubuntu 22.04 system.

Lastly, to assure that MySQL service is active, execute the following command:

$ systemctl status mysql.service

Now, move ahead and try to set up a MySQL database on your Ubuntu system.

How to set up MySQL Database on Ubuntu 22.04

To set up MySQL database on Ubuntu 22.04, firstly login to the MySQL server using the following command:

$ sudo mysql

Then, we will create a new database named “database1”:

> CREATE DATABASE database1;

To utilize the created MySQL “database1”, we will write-out the following command:

> USE database1;

You can create a new user account and assign the required privileges to it. In our case, we are creating a user “new_user” with the password “Pd123#@!”. Also adding the “%” permits us to remotely access the database from anywhere:

CREATE USER 'new_user'@'%' IDENTIFIED BY 'Pd123#@!';

In the next step, grant permission to the “new_user” account for viewing and modifying the “database1”:

GRANT ALL PRIVILEGES ON *.* TO 'new_user'@'%' WITH GRANT OPTION;

Lastly, save all of the added changes and exit from the MySQL window:

> FLUSH PRIVILEGES;

> EXIT

We have compiled the method to install and set up the MySQL database on Ubuntu 22.04. Give it a try and work on this fantastic database management system.

Conclusion

To install a MySQL database on Ubuntu 22.04, first, execute the system repositories and then execute the “$ sudo apt-get install mysql-server” command. After that, log in to the MySQL server using “$ sudo mysql” and create a database user, assign privileges to it, and save all of the added changes. This write-up discussed the method to install and set up MySQL databases on Ubuntu 22.04.

About the author

Sharqa Hameed

I am a Linux enthusiast, I love to read Every Linux blog on the internet. I hold masters degree in computer science and am passionate about learning and teaching.