Hot Summer Sale - up to 36% OFF

How to Install MySQL on Ubuntu 24.04: Step-by-Step Tutorial

How to Install MySQL on Ubuntu 24.04: Step-by-Step Tutorial
Published on Aug 12, 2025 Updated on Aug 12, 2025

MySQL is a high-performance, relational database management system (RDBMS) that employs SQL syntax to store and manage data. It powers data-driven applications across various industries and has proven to be a reliable and robust database solution over the years.

This guide walks you step-by-step through MySQL installation on Ubuntu 24.04.

#Prerequisites

Here is a list of requirements before you get underway:

  • An instance of Ubuntu 24.04 LTS configured with SSH access

  • A sudo user configured on the VPS

  • Internet access

Deploy and scale your projects with Cherry Servers' cost-effective dedicated or virtual servers. Enjoy seamless scaling, pay-as-you-go pricing, and 24/7 expert support—all within a hassle-free cloud environment.

#How to Install MySQL on Ubuntu 24.04

We will demonstrate MySQL installation from the default APT repositories. In addition, you will learn how to harden its defenses according to best security practices and allow remote connections for carrying out routine tasks.

#Step 1: Update the local package index

At the top of the list is updating your local package index. To achieve this, run the command:

Command Line
sudo apt update

Head to the next step to install the MySQL server.

#Step 2: Install MySQL server

The Ubuntu repository provides the MySQL server package. To verify this, invoke the apt-cache command as follows.

Command Line
apt-cache search mysql-server 

The output confirms its availability.

check-mysql-server-package-ubuntu-repositories

To install the MySQL server package, run the command:

Command Line
sudo apt install mysql-server -y

This installs the MySQL server package alongside additional libraries, dependencies, and configuration files.

#Step 3: Verify MySQL server installation

To verify the MySQL server's installation, issue the command shown.

Command Line
mysql --version
Outputmysql  Ver 8.0.41-0ubuntu0.24.04.1 for Linux on x86_64 ((Ubuntu))

The installation spawns the MySQL systemd service, which starts automatically. You can confirm the service status as follows.

Command Line
sudo systemctl status mysql

check-mysql-server-status

MySQL listens on port 3306 and is bound to the localhost IP 127.0.0.1. To confirm this, invoke the ss command.

Command Line
sudo ss -antp | grep mysql

You should get output similar to what we have displayed.

OutputLISTEN 0      151               127.0.0.1:3306                 0.0.0.0:*     users:(("mysqld",pid=149925,fd=26))
LISTEN 0      70                127.0.0.1:33060                0.0.0.0:*     users:(("mysqld",pid=149925,fd=21))

#Step 4: Secure the MySQL Server

Data exposure is the last thing you want to deal with when going to production. It can translate to tremendous financial losses and reputational damage.

The default installation includes lax security settings. Thus, extra security hardening steps are necessary to guarantee a decent level of database security.

To secure the MySQL database server, run the command:

Command Line
sudo mysql_secure_installation

Set up a password validation policy to enforce the strongest password complexity rules ideal to mitigate password-guessing attempts.

Follow the prompt and select the highest password policy level, in this case, type 2 and hit ENTER.

configure-password-strength-mysql-server

The remaining section will require you to configure MySQL to the recommended practice. Be sure to type Y for every prompt. This will do the following:

Removes anonymous users from the database. This is mostly used for testing purposes and should be removed as it grants access to anyone to the database server without a password.

Disallows remote root login. This blocks remote root access, effectively mitigating the risk of brute-force attacks.

Removes 'test' database, a globally accessible database.

Applies the changes made above by reloading privilege tables.

secure-mysql-server

#Step 5: Configure authentication for MySQL root account

In the previous step, we took basic security-hardening measures to safeguard the server against potential breaches. However, the MySQL root account is not password-enabled by default, and anyone can log in as root. You want to avoid this situation at all costs.

To confirm this, you can try to log in as the MySQL root account user as shown:

Command Line
sudo  mysql -u root -p

In this step, we will configure a password for the MySQL root account to access the MySQL shell.

Command Line
sudo mysql 

Next, set the root password using the following ALTER USER query.

Command Line
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'SecurePassword';

Save the changes.

Command Line
FLUSH PRIVILEGES;

Finally, exit the MySQL shell.

Command Line
EXIT;

secure-mysql-root-account

Subsequent login attempts will prompt you for a password while authenticating as root.

#Step 6: Create a MySQL Admin user

MySQL installation ships with a root user account with absolute privileges over every database, and by extension, tables, users, and so forth. Best practice recommends creating a new user and granting it specific privileges for controlled access and operations on the database server.

Create a new database user as follows.

Command Line
CREATE USER 'cherry_user'@'localhost' IDENTIFIED WITH mysql_native_password BY 'YourSecurePassword';

Next, assign the desired privileges to the user. The query below assigns SELECT, UPDATE, INSERT, and RELOAD privileges to the user on all databases :

Command Line
GRANT SELECT, UPDATE, INSERT, RELOAD  ON *.* TO 'cherry_user'@'localhost' WITH GRANT OPTION;

Run the SHOW GRANTS query to display the privileges and roles granted to the MySQL user.

Command Line
SHOW GRANTS FOR 'cherry'@'localhost';

show-privileges-for-mysql-user

To grant all privileges to the user, effectively making them the root user, run:

Command Line
GRANT ALL PRIVILEGES ON *.* TO 'cherry_user'@'localhost' WITH GRANT OPTION;

Finally, flush privileges and exit the MySQL shell.

Command Line
FLUSH PRIVILEGES;
Command Line
EXIT;

Once created, you can now log in as the administrative user with the password you used.

Command Line
mysql -u cherry_user -p

login-as-mysql-regular-user

#Step 7: Configure remote connections for MySQL

When administering your database server, some situations call for external database access, especially for routine tasks. Since MySQL is bound to the loopback address, you need to modify this and set it to accept remote connections.

Therefore, edit the default configuration file: mysqld.cnf file.

Command Line
sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf

Locate the bind-address directive and set it to 0.0.0.0.

Command Line
bind-address: 0.0.0.0

Save the changes and exit.

Next, re-initialize the systemd configuration.

Command Line
sudo systemctl daemon-reload

And restart the MySQL server.

Command Line
sudo systemctl restart mysql

The next step is to modify the MySQL user's host from localhost to the external IP address of the remote server. So, log back to MySQL and run the following query. The remote_server_ip is the IP of the remote host that will access the MySQL server

Command Line
RENAME USER 'cherry_user'@'localhost' TO 'cherry_user'@'remote_server_ip';

Next, flush privileges and exit.

Command Line
FLUSH PRIVILEGES;
Command Line
EXIT;

Allow database traffic on port 3306 if UFW is enabled.

Command Line
sudo ufw allow 3306

You can grant a remote connection to a specific remote IP host as follows.

Command Line
sudo ufw allow from remote_IP_address to any port 3306

And finally, be sure to reload the firewall for the rules to take effect.

Command Line
sudo ufw reload

Head to the remote server and proceed to access the database as follows. Replace cherry_user and mysql_database_ip with your MySQL user and server IP address.

Command Line
mysql -u cherry_user -h database_server_ip -p

login-remotely-as-mysql-regular-user

#Step 8: Review installation with MySQL tuner

Assessing the state of your MySQL installation is generally recommended. One utility that can help achieve this is MySQL Tuner. This is a Perl script that quickly reviews your installation and offers suggestions on how to fine-tune your database server. It uncovers things such as:

Security issues, such as weak passwords or users without passwords Metrics for storage engines such as MyISAM, TokuDB, and Galera Performance metrics. It reports on slow queries, memory usage, table cache, etc. ThreadPool metrics Replication metrics

To install MySQL Tuner, run the command:

Command Line
sudo apt install mysqltuner -y

Once installed, run the script:

Command Line
sudo mysqltuner 

Here’s a snippet of the expected output

mysqltuner-command

Here is an excerpt of some of the general recommendations made, including variables that need adjustment for optimal performance.

mysqltuner-recommendations

#Conclusion

We have successfully installed MySQL on Ubuntu 24.04 LTS. In addition, we performed basic database hardening using a simple script and configured the database server to allow connections from remote sources. We hope you can seamlessly set up and configure your MySQL database server.

Cloud VPS Hosting

Starting at just $3.24 / month, get virtual servers with top-tier performance.

Share this article

Related Articles

Published on Jun 7, 2021 Updated on Jun 29, 2022

AlmaLinux Review: a CentOS Clone Supported by CloudLinux

AlmaLinux is an open-source Linux distribution focused on long-term stability, that is a 1:1 binary compatible fork of Red Hat Enterprise Linux (RHEL)

Read More
Published on May 31, 2022 Updated on May 5, 2023

A Complete Guide to Linux Bash History

Learn how to work with Bash history to become more efficient with any modern *nix operating system.

Read More
Published on Jan 26, 2022 Updated on Jun 15, 2023

How to Use Cron to Automate Linux Jobs on Ubuntu 20.04

Learn how to use Cron - the most popular Linux workload automation tool that is widely used in Linux community - to automate Linux jobs on Ubuntu 20.04.

Read More
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: 3dfb3dc94.1333