How to Install MySQL on Ubuntu 24.04

In this tutorial, we will explain how to install MySQL on Ubuntu 24.04 OS. MySQL is an open-source relational database management system storing data in table structures under different data types. The data types are integer, string, float, datetime, varchar, etc. Every data type stores various kinds of information. For example, an integer is for numbers, a float is for decimal numbers, the string is intuitive and stores strings, DateTime stores the date in a unique format, and so on. Michael Widenius wrote MySQL in C and C++, and many database-driven web applications, including Drupal, Joomla, WordPress, and Magento, use it extensively. Global applications like Facebook, YouTube, and Twitter use MySQL for their database management needs.

In the following paragraphs, we will install MySQL and provide you with some basic commands. Installing MySQL on Ubuntu 24.04 is a straightforward process that may take up to 10 minutes.

Prerequisites

Step 1. Update the System

Before we start with the installation of MySQL, we will update the system packages to their latest versions available:

sudo apt update -y && sudo apt upgrade -y

Step2. Install MySQL database service

The latest Ubuntu 24.04 OS includes the MySQL package. To install MySQL, execute the following command:

sudo apt install mysql-server -y

Once installed to start and enable the MySQL service for an automatic start after the system reboot, execute the following commands:

sudo systemctl start mysql && sudo systemctl enable mysql

You should get the following lines after starting and enabling the service:

root@host:~# sudo systemctl start mysql && sudo systemctl enable mysql
Synchronizing state of mysql. service with SysV service script with /usr/lib/systemd/systemd-sysv-install.
Executing: /usr/lib/systemd/systemd-sysv-install enable mysql

To check the status of the service, you can execute the command below:

sudo systemctl status mysql

You should receive the following output:

root@host:~# sudo systemctl status mysql
● mysql.service - MySQL Community Server
     Loaded: loaded (/usr/lib/systemd/system/mysql.service; enabled; preset: enabled)
     Active: active (running) since Mon 2025-01-13 17:46:25 CST; 3min 18s ago
   Main PID: 346842 (mysqld)
     Status: "Server is operational"
      Tasks: 37 (limit: 4613)
     Memory: 364.0M (peak: 378.5M)
        CPU: 3.243s
     CGroup: /system.slice/mysql.service
             └─346842 /usr/sbin/mysqld

Jan 13 17:46:24 host.test.vps systemd[1]: Starting mysql.service - MySQL Community Server...
Jan 13 17:46:25 host.test.vps systemd[1]: Started mysql.service - MySQL Community Server.

To restart the MySQL service, you can use the following command:

sudo systemctl restart mysql

If you want to stop the MySQL service, you can use the command below:

sudo systemctl stop mysql

Step 3. Secure MySQL

To secure the MySQL database server after a fresh installation, set a strong MySQL root password. To do that, execute the following command, which will execute the security script:

sudo mysql_secure_installation

After this, there will be a couple of questions and steps that you need to complete to secure the MySQL service fully.

Press y|Y for Yes, any other key for No: Y

Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 2

Remove anonymous users? (Press y|Y for Yes, any other key for No) : Y

Disallow root login remotely? (Press y|Y for Yes, any other key for No) : Y

Remove test database and access to it? (Press y|Y for Yes, any other key for No) : Y

Reload privilege tables now? (Press y|Y for Yes, any other key for No) : Y

You have now completed and configured everything. In the final section, we will provide you with some basic and most important commands in MySQL.

Step 4. MySQL commands

As mentioned in the first paragraph, installing the MySQL service is a straightforward process. Now we will provide you with some basic MySQL commands for creating a database, creating a user, granting permission, making a dump of a database, and how to import as well.

Check the MySQL Version with the following command:

mysql -V

You should get output similar to this:

root@host:~# mysql -V
mysql  Ver 8.0.40-0ubuntu0.24.04.1 for Linux on x86_64 ((Ubuntu))

Execute the command mysql in your terminal to log in to the MySQL console. This action will produce the following output:

root@host:~# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 8.0.40-0ubuntu0.24.04.1 (Ubuntu)

Copyright (c) 2000, 2024, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 

Set the MySQL Root password and change the authentication method with the following command. This action will require the MySQL console to ask for the root password instead of allowing users to log in without it:

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'StrongR00tp@sw0rdHere!';

Now if you try to login only with mysql you will get the following message:

root@host:~# mysql
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

This tells us that the login requires specifying username and password. So, to log in as a root user, you can use the following command:

mysql -u root -p

The prompt will ask you to enter the root password you set before:

root@host:~# mysql -u root -p
Enter password:

Create a database with the following command:

CREATE DATABASE dbtest;

Create a database user with a strong password using the following command:

CREATE USER 'dbuser'@'localhost' IDENTIFIED BY 'Str0ngP@sswordHere!!';

Grant permissions for the user on the database with the command below:

GRANT ALL ON dbtest.* TO 'dbuser'@'localhost' WITH GRANT OPTION;

FLUSH PRIVILEGES instructs database to reload the in-memory copy of privileges from the privilege tables.

FLUSH PRIVILEGES;

After all these commands, your MySQL console should look like this:

mysql> CREATE DATABASE dbtest;
Query OK, 1 row affected (0.01 sec)

mysql> CREATE USER 'dbuser'@'localhost' IDENTIFIED BY 'Str0ngP@sswordHere!!';
Query OK, 0 rows affected (0.02 sec)

mysql> GRANT ALL ON dbtest.* TO 'dbuser'@'localhost' WITH GRANT OPTION;
Query OK, 0 rows affected (0.00 sec)

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.01 sec)

mysql>

To make a copy of the database, use the database dump feature. Perform the MySQL dump outside the MySQL console. First, exit the MySQL console:

mysql> exit;
Bye

Once logged out of the MySQL console, to make the MySQL dump execute the following command:

mysqldump -u root -p dbtest > dbtest.sql

MySQL import is process when you want to restore the database from the backup. Perform the MySQL import outside the MySQL console using the following command:

mysql -u root -p dbtest < dbtest.sql

The only difference is the mysql instead of mysqldump and the sign < instead of >.

Congratulations!

That’s it. You successfully installed MySQL database service on Ubuntu 24.04 OS.

If you liked this post about how to install MySQL on Ubuntu 24.04, please share it with your friends or leave a comment down below.

Leave a Comment