How to Install MySQL on Ubuntu 22.04 | 7 Steps

November 6th, 2023
How to Install MySQL on Ubuntu 22.04 | 7 Steps

MySQL is one of the most widely used databases favored by developers due to the balance of features, performance, and ease of use, delivering high performance, scalability, security, flexibility, and low costs. In this tutorial, we will show you how to install MySQL on Ubuntu 22.04 in seven steps. We include installation, configuration, securing MySQL, and creating a new user.

What is MySQL?

MySQL is one of the most popular open-source database management systems. It is a component of the famous LAMP (Linux, Apache, MySQL, PHP) stack and has supported major web applications and platforms for nearly two decades.

This open-source database management system comes with several storage engines, including InnoDB, the transaction-safe (ACID compliant) engine, and MyISAM, with a smaller footprint, often used for read-only situations. MySQL also supports replication, which can be used for long-distance data distribution, data security, and scaling out your architecture.

Prerequisites

To follow this tutorial, you will need the following:

How to install MySQL on Ubuntu 22.04: Step-by-step

Follow the below seven steps to install MySQL on Ubuntu 22.04. We will first upgrade all the system packages and then install, configure, and secure MySQL, after which we will create a new user and privileges on a newly created database. Lastly, we will also cover MySQL basic service management. Let’s dive right in.

Step 1: Install MySQL server on Ubuntu

First, let’s update the list of packages and upgrade them:

sudo apt update
sudo apt upgrade

As the system is upgraded, we can install the latest MySQL proposed by Ubuntu.

sudo apt install mysql-server -y

Note that the package installed is mysql-server. This will install both the MySQL server and the client. The -y at the end of the command allows us to proceed to the installation by automatically answering the installation procedure by answering yes.

Step 2: Start MySQL server

Now that we have installed MySQL, we will start the service using the following command:

sudo service mysql start

We now check the status of the service:

sudo service mysql status

mysql status

As we can see, MySQL is now up and running.

Step 3: Set initial MySQL root password

By default, MySQL does not come with a password for the root account. We will now set one. This is a good security practice, which is also needed before using the MySQL secure installation script.

Let’s first connect to the database using the MySQL client:

sudo mysql

sudo mysql

Now that we are connected, we can set a password for the root account using the following command:

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'mypassword';

Once you set the password, the MySQL client can no longer connect using only the sudo mysql command.

Step 4: Secure MySQL server

As the database is a crucial part of an application, you need to secure it. MySQL helps you do that by providing the mysql_secure_installation command.

We will now launch the secure installation script:

sudo mysql_secure_installation

The screenshot below shows the different questions asked by MySQL. Fill in and adjust based on your needs:

mysql secure installation

Let’s reconnect to the server using the new root password set during the secure installation procedure.

sudo mysql -u root -p

You should now see the MySQL prompt:

mysql -user root

Step 5: Add a new user

The installation is now complete. In order to connect any application to your database, it is recommended to create a database and a specific user and grant that user access to this database. This allows us to manage our data better.

We will create the user first:

CREATE USER 'didier'@'localhost' IDENTIFIED WITH mysql_native_password BY '%$HmbKe#XeVtn8i%mX$Ha&v2on%crUWc';

Please note that the user can only connect to the database via 'localhost'. If you want the user to be able to connect from anywhere, use % instead of localhost. Be aware that this is insecure as there is no restriction based on where clients can connect.

We then flush all the privileges. This will refresh MySQL and allow us to use that user immediately.

flush privileges;

mysql user

Step 6: Create MySQL database and grant privileges

We now proceed to create a MySQL database myapp with the following command:

 create database myapp;

We then grant the previously created user all the privileges on this database:

grant all privileges on myapp.* to 'didier'@'localhost';

mysql create database

You can now do any operation on the database.

Step 7: Stop and restart MySQL

While managing your application, you will need to stop or restart your MySQL database.

Use the following command to stop MySQL:

sudo service mysql stop

To restart MySQL, use this command:

sudo service mysql restart

Conclusion

In this comprehensive seven-step tutorial, we've covered how to install MySQL on Ubuntu 22.04. MySQL is a fundamental component of the LAMP stack and an integral part of many web applications and platforms. You can get further information about MySQL in the official MySQL documentation.

With over 20 years in IT, Didier has been creating technical documentation for companies, catering to both technical and non-technical audiences. Didier is an expert in Linux system administration, DevOps, cloud computing, cybersecurity, IT consulting, management consulting, technical writing, Diataxis framework, Doc-as-Code, UX Writing, Jamstack, MkDocs, Docker, containers, open-source, SDLC, and Python programming. His hands-on technical expertise, coupled with his strong communication skills, enables him to bridge the gap between developers and end-users. Didier creates user guides, API References, end-user documentation, how-tos, and tutorials. He is an expert in authoring using modern technologies such as Markdown, Mermaid, and static-site generators. Didier also utilizes frameworks and methodologies such as Diaxiatis and Doc-as-code, applying structured writing techniques. Currently, Didier works as a freelance technical writer and documentation consultant, assisting organizations in creating comprehensive and easy-to-understand documentation for their software and web applications. In his previous roles, Didier worked as a system and applications engineer and implemented style guides, tone and voice best practices, and documentation processes that streamline their release cycles. Didier resides in Vacoas, Mauritius.

Cloud VPS - Cheaper Each Month

Start with $9.99 and pay $0.5 less until your price reaches $6 / month.

We use cookies to ensure seamless user experience for our website. Required cookies - technical, functional and analytical - are set automatically. Please accept the use of targeted cookies to ensure the best marketing experience for your user journey. You may revoke your consent at any time through our Cookie Policy.
build: 372cf894.659