How To Install MySQL on Ubuntu 24.04


MySQL is an open-source data management system for web services. It is frequently used in the LAMP stack, specifically in Linux systems. It uses the SQL and relational model for data management-related applications.

This article will help you to set up MySQL version 8.0 on the Ubuntu 24.04 system. Once done, you’ll have a functional relational database ready for your upcoming website or application development.

Table of Contents

1. How to Install MySQL on Ubuntu 24.04

To install MySQL server on Ubuntu 24.04 you can use the default apt package manager. Before you can proceed with the MySQL installation, first update your system packages. This will make sure the MySQL version downloaded is up-to-date:

sudo apt update

Now install the mysql-server package:

sudo apt install mysql-server

To verify the MySQL installed version, you can run this command:

mysqld --version

Once the installation is done, the MySQL starts automatically. To check if MySQL is active, run:

sudo systemctl status mysql

If the MySQL server is disabled, start it by running this command:

sudo systemctl start mysql.service

2. Configuring MySQL Server

After installing MySQL, the next step is to set MySQL on Ubuntu 24.04 and perform some initial security setups for it. These steps include setting up a strong password for MySQL and removing unnecessary permissions and databases.

Now secure the MySQL installation:

sudo mysql_secure_installation

From the above output, you can see that MySQL is trying to establish the connection without any password.

Now we will secure the MySQL by assigning a password and removing the root accounts that have access permission from outside. We will also set up a root password for the root user.

The sudo mysql_secure_installation command has different purposes for enhancing the MySQL installation security.

Set Root Password

The first step after running the secure command is password validation. Password will help your system to maintain the MySQL server security.

Press the y key to confirm the password validation.

Now, depending on the system, you might face two different scenarios. In the first case, you may be asked to enter the password. You have three different options for password policy. You can either set a low, medium, or strong password. Enter the number for the option you like and press enter.

Enter password and retype it to confirm its validation.

In the second case, the command might skip setting a password altogether. Instead, it uses the operating system credentials for authentication.

The root user can log in without providing a password. By default, the auth_socket method is set for authentication.

Note: Even if auth_socket is used, you can still set a password after logging in to the MySQL server using the command:

ALTER USER root@localhost IDENTIFIED BY [password]

This command will produce nothing on the terminal.

Remove Anonymous User Accounts

The command also removes any anonymous user accounts. These accounts have no authentication requirements and can pose a security risk.

To remove the anonymous user, you can simply type y and press enter.

Disallow Root Login Remotely

When you’re setting up a system, it’s recommended to limit the root user’s remote access. This will reduce the risk of unauthorized login attempts. Even if someone manages to sneak onto your network, they won’t be able to log in as the root user without physical access or specific network permissions. So consider this step while setting MySQL, especially in production environments.

Remove Test Database

By default, MySQL includes a test database that all users, including anonymous ones, can access. The MySQL secure command removes this test database to prevent unauthorized access.

Reload Privileges Tables

After making these changes, the command reloads the privileges tables to ensure that the new settings are applied.

3. Testing MySQL Server

Finally, to test the MySQL server login to the MySQL server by running this command:

sudo mysql -u root

The command allows the root user (the highest-level user) to connect to the MySQL database server. If the root user has a password, enter it. Once the password is provided, the root user gains full control over the database. They can then use SQL commands to manage and retrieve the database data.

4. Uninstallation of MySQL From Ubuntu 24.04

To uninstall MySQL from Ubuntu 24.04 first make sure that you have taken the backup of all of your data.

Once done, the first step is to stop the MySQL service:

sudo systemctl stop mysql

Now remove all the MySQL-related packages by running this command:

sudo apt-get purge mysql-server mysql-client mysql-common mysql-server-core-* mysql-client-core-*

If you’ve changed the database location in your MySQL configuration, replace /var/lib/mysql accordingly:

sudo rm -rf /etc/mysql /var/lib/mysql

After MySQL uninstallation, you can perform some optional cleanup. To remove the unnecessary packages:

sudo apt autoremove

After uninstallation of MySQL, you may need to remove some unresolved dependencies. These dependencies usually come when you install any package and are needed for the working of MySQL server and its database. After running the above command, these no longer-needed packages or dependencies will be auto-removed.

 

To clear the apt cache, run:

sudo apt autoclean

After removing MySQL, you have to clean the system cache and add the MySQL-required dependencies that may later conflict with any other package. So by running the autoclean, the local repository of retrieved packages is auto-cleaned.

MySQL has been successfully uninstalled from your Ubuntu system.

Conclusion

MySQL is an open-source service for database management. Like any other platform, you can also install MySQL on Ubuntu 24.04. To install it on Ubuntu, you can run the apt install command. After the installation, enable MySQL by running the systemctl command. Once done, the MySQL server is ready for use. Also, make sure to configure the root password and remove all unnecessary and anonymous users. After doing all these steps, reload the privileges table, so all changes are applied successfully. For further info on installation steps, read the article.

Print Friendly, PDF & Email
Categories