How to install MySQL on Ubuntu 24.04

Hosting an application or dynamic website requires you to set up a database on the backend to store user’s information. A popular option is MySQL, which is widely used for various projects with different web stacks.

To help you get started quicker, this tutorial will guide you through the steps to install MySQL on Ubuntu 24.04. We will also cover basic MySQL configuration to help you prepare your database for deployment.

Prerequisites for installing MySQL

Before installing MySQL, make sure you have a virtual private server (VPS) to host the database. It can be a dedicated machine or the same system that will run your web application so long as it has sufficient CPU cores, RAM, and storage.

MySQL doesn’t have a strict minimum hardware requirement. This will greatly depend on the amount of traffic and data you expect to handle. However, a good starting point would be a system with a single-core CPU, 2 GB of RAM, and 10 GB of storage.

If you don’t have a host machine, we recommend Hostinger’s Linux VPS solution, which starts at ₹439.00/month. In addition to reliable hardware, our VPS features like Kodee AI assistant and Browser Terminal can actually help simplify the MySQL installation process for beginners.

You can connect to your VPS as root directly from your browser using Browser Terminal and ask Kodee for guidance on managing your database. You will see these tools in action in the later sections.

In addition, make sure your system runs Ubuntu 24.04 to avoid compatibility issues and security risks that are often present in older versions. Hostinger users can easily install this version of Ubuntu without commands by using the operating system template.

Need simpler installation process?

Hostinger users can easily install MySQL on their VPS without commands by simply setting up the operating system template. However, this database is only available as a part of pre-configured web stacks and runs on Ubuntu 22.04.

Installing MySQL on a Ubuntu 24.04 VPS

Once you’re sure you meet the prerequisites, follow the steps below to install MySQL on Ubuntu 24.04.

1. Connect to your server

Since installing MySQL requires remotely running commands on your VPS, we must connect to it via SSH using PuTTY or Terminal.

If you use Terminal, run the following command to connect to your remote server. Remember to replace 185.185.185.185 with your VPS’ actual IP address:

ssh root@185.185.185.185

Alternatively, Hostinger users can use the built-in Browser terminal feature.

Simply open hPanelsidebarVPSManage and click the Browser terminal button on the top right. This tool automatically logs you in as root, making the process more efficient.

2. Update package index

Once logged in as root, let’s update your system’s package repository to ensure you get the latest version of MySQL. To do so, run the following command:

sudo apt update

While not mandatory, we also recommend upgrading installed packages to the latest version using this command:

sudo apt upgrade

Updating all packages before installing MySQL helps prevent incompatibility issues that might arise when using older software.

3. Install MySQL

Now that your system is ready, let’s install the MySQL server package by running the following command:

sudo apt install mysql-server

Press Y if your command line asks for confirmation. Wait until the installation finishes, which should take around 15 minutes, depending on your internet connection speed.

After the installation is complete, run the following command to verify that MySQL is properly configured:

mysql -V

If your command line returns the installed MySQL version number, the database server is installed. Otherwise, you will see the “Command not found error,” which means you should rerun the installation command.

Important! Note the uppercase V option. Using lowercase can return an error since Linux commands are case-sensitive.

4. Secure MySQL installation

MySQL will use the default configuration out of the box, which can be insecure. For example, it doesn’t set up a password for the root database user, allowing unauthorized parties to access the account.

To minimize the security risks, run the MySQL secure installation wizard by entering the following command:

sudo mysql_secure_installation

During the wizard, you can adjust several security configurations by answering the prompted questions. Here is what you can do during the process:

  • Validate password strength to prevent users from using weak credentials.
  • Disallow root login remotely, allowing database access only from localhost.
  • Delete anonymous user accounts, which enable all users to log in to your database. 
  • Remove the default test database that is accessible to all users.

5. Verify MySQL functionality

After installing MySQL, let’s verify whether it works properly. Start by checking whether it is running using this command:

sudo systemctl status mysql

You should see MySQL service status as Active (Running). Otherwise, you can start it manually with this command:

sudo systemctl start mysql

Alternatively, Hostinger users can quickly check the MySQL service status by asking Kodee AI assistant. Open hPanel and click Manage on your VPS. Navigate to the sidebar and select Kodee. Then, ask your question like so.

Now, let’s enter the MySQL shell. This is where you will run SQL statements to interact with your database, including creating a new table for fetching data. Here’s the command:

sudo mysql -u root

Enter the root password, and your command line should change to MySQL>. It means MySQL works properly, and you can start managing the database.

Basic MySQL configuration

You might need to change several basic MySQL configurations to ensure your database functions based on your requirements.

​​Changing the data directory

As your database gets populated with more data, it will need more storage space and resources for more intensive I/O operations. If the current hosting environment doesn’t have enough hardware allocation, your application might crash.

Changing the data directory resolves this issue by moving your database storage location elsewhere. For a fresh MySQL installation, doing so lets you separate the database from your server’s system files, preventing drive overload.

Here’s how to move your database directory:

  1. Log in to the MySQL root user account:
mysql -u root -p
  1. Run the following statement to check your active data directory location, which is /var/lib/mysql/ by default:
SELECT @@datadir;
  1. Type exit to quit the MySQL shell and return to the main command line.
  2. Stop your MySQL service temporarily to avoid data changes when you migrate it:
sudo systemctl stop mysql
  1. Move the data directory to the new location using rsync. Remember to replace the destination placeholder with the actual value:
sudo rsync -av /var/lib/mysql /path/destination
  1. Reconfigure MySQL to locate the data in the new directory. Start by opening the configuration file using a text editor like Nano:
sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf
  1. Enter your new data directory into the datadir variable like so:
datadir=/path/destination
  1. Change Ubuntu’s AppArmor configuration to allow MySQL to modify data in the new directory. Here’s the command:
echo "alias /var/lib/mysql/ -> /your/new/datadir/," >> /etc/apparmor.d/tunables/alias
  1. Reload AppArmor to apply the changes:
sudo /etc/init.d/apparmor reload
  1. Restart the MySQL service using the following command:
sudo systemctl start mysql

The steps to move the data directory to another server are similar, except you use a remote destination with rsync. You should also edit the server’s MySQL configuration file to point to the new storage.

Enabling remote access

By default, you can only access your database from the host server. MySQL blocks remote connections from external systems to prevent unauthorized access that might compromise security.

However, this behavior prevents you from integrating third-party programs, managing the database from another machine, or connecting applications hosted on another system, which might cause functionality issues.

To allow other systems to access your database, add their IP addresses to your MySQL configuration file. To learn more about the steps, read our tutorial on granting MySQL remote access.

Important! Since remote access on your MySQL database can expose your server to security risks, only enable it if needed.

If you need remote access, make sure your MySQL and firewall configuration only allow trusted IP addresses. Also, use secure passwords for all remote MySQL user accounts and avoid granting them all privileges unless necessary.

We also recommend enabling MySQL logging by editing the configuration file. This setting helps you identify malicious activities in your database more easily.

Once remote access is enabled, make sure you have created a MySQL user with the privilege to connect to the database server.

Log in to MySQL remotely by running the following command on the permitted machine. Replace username with the actual account name and enter your password:

mysql -u username -h your_vps_ip -p

To allow another system to access an existing database, add its IP address as the host. After logging in to the root MySQL user, enter the following:

UPDATE mysql.db SET Host='your_remote_system_ip' WHERE Db=ExistingDatabaseName;

Adjusting the MySQL configuration file

You can change various MySQL settings, including network and logging, by editing the configuration files.

Note that MySQL has several configuration files, and you must find out which one your database server is using by running the following command:

mysql --help | grep -A1 'Default options are read from the following files in the given order'

You should see several paths that indicate the global or user-specific configuration files. Check out the MySQL option file documentation to learn more about their differences.

For this tutorial, let’s edit the global configuration file, which is /etc/mysql/my.cnf. To do so, open it using a text editor:

sudo nano /etc/mysql/my.cnf

Since my.cnf has no configuration by default, you must manually add the settings. This file takes the following parameters:

  • #comment or ;comment – optional notes to explain your configuration. MySQL won’t read these comments. 
  • [group] – a group or program to which you want to apply the configuration. A group setting ends on the next [group] line. 
  • opt_name=value – your MySQL configuration parameter in a key-value pair. 

To check the available settings, run the following command. Under the Variables line, you will see the configuration parameters and their values:

mysql --help

Your MySQL database configuration might look like the following:

[client]
port=3306
socket=/tmp/mysql.sock

[mysqld]
port=3306
socket=/tmp/mysql.sock
key_buffer_size=16M
max_allowed_packet=128M

[mysqldump]
quick

Conclusion

Hosting a dynamic website or application requires setting up a database like MySQL on your server. To help you get started faster, we have explained the steps for installing it on Ubuntu 24.04. Here’s the recap:

  1. Connect to your server as root via SSH using PuTTY, Terminal, or Hostinger’s Browser terminal.
  2. Run the apt update command to renew your system’s repository, ensuring you get the newer MySQL version.
  3. Download and install the MySQL package using the apt install command.
  4. Run MySQL’s secure installation script to adjust your database security configuration.
  5. Verify MySQL functionality by checking the service status and entering the database shell. 

Optionally, you can modify the mysqld.cnf or my.cnf configuration files to adjust your MySQL settings. For example, you can change the default data directory, enable a remote connection, or open a different port for your database.

After installing MySQL, configure phpMyAdmin on your Ubuntu server so you can easily manage your database using a graphical interface.

How to install MySQL on Ubuntu FAQ

What versions of Ubuntu are compatible with MySQL installation?


Technically, you can install MySQL on any version of Ubuntu, including the older ones like 16.04. However, we don’t recommend doing so for the following reasons:

  • Incompatibility issues. Using a very old version of Ubuntu with newer MySQL releases might cause some features or functionality to not work properly.
  • Security risks. Old software commonly has unpatched safety flaws that cybercriminals might exploit.
  • Bugs. Legacy Ubuntu and MySQL might have bugs that can affect functionality.

To avoid these issues, we recommend always using the latest version of both software. For instance, it is best to use Ubuntu 24.04, 22.04, or 20.04 with the newest MySQL.

How do I uninstall MySQL?


To uninstall MySQL on Ubuntu, start by stopping the database service using the following command:

sudo systemctl stop mysql

Then, use APT to uninstall MySQL:

sudo apt remove mysql-client mysql-server -y

Finish the process by deleting leftover data like unnecessary packages and caches by running these commands sequentially:

sudo apt autoremove -y 

sudo apt autoclean -y

Can I install MySQL Workbench alongside MySQL Server?


Yes, you can. MySQL Workbench will only work if your system has the MySQL server. To set it up, download the installer using the following command:

sudo curl https://dev.mysql.com/get/mysql-apt-config_0.8.33-1_all.deb 

Unpack the installer by running this command:

sudo dpkg -i mysql-apt-config_0.8.33-1_all.deb 

Finish the package setup wizard by choosing your preferred configurations. Once finished, update your package manager:

sudo apt update

Install MySQL workbench from APT by running this command:

sudo apt install mysql-workbench-community -y

Author
The author

Aris Sentika

Aris is a Content Writer specializing in Linux and WordPress development. He has a passion for networking, front-end web development, and server administration. By combining his IT and writing experience, Aris creates content that helps people easily understand complex technical topics to start their online journey. Follow him on LinkedIn.