How to Install MySQL 8.0 on Ubuntu 24.04

How to Install MySQL 8.0 on Ubuntu 24.04

MySQL 8.0 is one of the most popular relational database management systems, known for its reliability and performance. In this guide, we will walk you through two methods of installing MySQL 8.0 on Ubuntu 24.04: using the Ubuntu APT repositories and using the MySQL Community Server. This step-by-step tutorial aims to provide clear instructions and commands, making it easy for beginners and experienced users alike to set up MySQL.

Prerequisites

Before you begin, ensure that you have:

  • A system running Ubuntu 24.04.
  • A user account with sudo privileges.
  • An internet connection.

Method 1: Install MySQL 8.0 via Ubuntu APT Repositories

Step 1: Update Package Index

First, it’s essential to update your package index to ensure you have the latest information on available packages.

sudo apt update

Step 2: Install MySQL Server

Now, install MySQL server by running the following command:

sudo apt install mysql-server

This command will install MySQL 8.0 along with its dependencies. During the installation, you might be prompted to configure the MySQL server. Follow the on-screen instructions to set a root password.

Step 3: Secure MySQL Installation

Once the installation is complete, it’s important to run a security script that will remove some insecure default settings and improve the overall security of your MySQL installation.

sudo mysql_secure_installation

During this process, you’ll be prompted to:

  • Set the root password (if you haven’t already).
  • Remove anonymous users.
  • Disallow remote root login.
  • Remove test databases.
  • Reload privilege tables.

Follow the prompts to enhance your MySQL security.

Step 4: Verify Installation

To check if MySQL is installed and running, execute:

sudo systemctl status mysql

You should see a message indicating that the MySQL service is active (running). You can also log in to the MySQL shell with:

sudo mysql -u root -p

Method 2: Install MySQL 8.0 with MySQL Community Server

If you prefer to install the latest version directly from the MySQL Community Server, follow these steps.

Step 1: Install Prerequisites

First, install the necessary prerequisites by running the following command in your terminal:

sudo apt install dirmngr ca-certificates software-properties-common apt-transport-https curl lsb-release

This command installs essential tools to manage repositories and handle GPG keys.

Step 2: Import the MySQL GPG Key

Next, you need to import the MySQL GPG key to ensure the authenticity of the packages. Run the following command:

curl -fsSL http://repo.mysql.com/RPM-GPG-KEY-mysql-2023 | sudo gpg --dearmor | sudo tee /usr/share/keyrings/mysql.gpg > /dev/null

This command downloads the GPG key and adds it to your system’s keyring.

Step 3: Add the MySQL 8.0 Repository

Now, add the MySQL 8.0 repository to your system. Use the following command:

echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/mysql.gpg] http://repo.mysql.com/apt/ubuntu $(lsb_release -cs) mysql-8.0" | sudo tee -a /etc/apt/sources.list.d/mysql.list

This command creates a new list file for MySQL in your sources list.

Step 4: Update Package Index

After adding the repository, update your package index to include the new MySQL packages:

sudo apt update

Step 5: Install MySQL Server

Now, you can install the MySQL Community Server package with the following command:

sudo apt install mysql-community-server

During the installation, you will be prompted to set a root password. Enter a strong password and confirm it. If you prefer passwordless login using UNIX socket-based authentication, you can leave the password blank.

Step 6: Secure MySQL Installation

After the installation is complete, it’s crucial to enhance the security of your MySQL installation. Run the security script:

sudo mysql_secure_installation

Follow the prompts to:

  • Set the root password (if not already set).
  • Remove anonymous users.
  • Disallow remote root login.
  • Remove the test database.
  • Reload privilege tables.

This step is essential for securing your database server.


How to Uninstall MySQL 8.0 on Ubuntu 24.04

If you’ve decided to remove MySQL 8.0 from your Ubuntu 24.04 system, the process is straightforward. Below are the detailed steps to ensure a clean uninstallation.

Step 1: Stop the MySQL Service

Before uninstalling MySQL, you need to stop the MySQL service to ensure no processes are running.

sudo systemctl stop mysql --now

Step 2: Remove MySQL Server Packages

Next, use the following commands to remove MySQL packages. Start with the MySQL Community Server package:

sudo apt autoremove mysql-community-server

Then, remove the MySQL server package:

sudo apt autoremove mysql-server

Step 3: Remove MySQL APT Repository

If you installed MySQL via the APT repository, it’s a good idea to remove the MySQL repository from your sources list to avoid any future conflicts.

sudo rm /etc/apt/sources.list.d/mysql.list

Step 4: Clean Up Residual Configuration Files (Optional)

If you want to ensure that all configuration files are removed, you can purge MySQL packages:

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

Step 5: Remove Database Files (Optional)

If you wish to completely remove all MySQL data, you can delete the database files. Be cautious with this step, as it will delete all your databases.

sudo rm -rf /var/lib/mysql

Step 6: Update Package Index

Finally, update your package index to reflect the changes:

sudo apt update

Conclusion

Congratulations! You have successfully installed MySQL 8.0 on Ubuntu 24.04 using either the Ubuntu APT repositories or the MySQL Community Server. With MySQL up and running, you can start developing your applications or managing your databases.

By following this guide, you should have a robust MySQL setup that can serve as the backbone for your web applications and data management needs. If you found this guide helpful, consider sharing it to help others in the community!

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top