Posted in

How to Install MySQL 8 on Ubuntu 24.04: A Step-by-Step Guide

How to Install MySQL 8 on Ubuntu 24.04: A Step-by-Step Guide
How to Install MySQL 8 on Ubuntu 24.04: A Step-by-Step Guide

Welcome to this comprehensive guide on installing MySQL 8 on Ubuntu 24.04. By following these straightforward steps, you will successfully set up a robust MySQL database server on your system. MySQL is the world’s most popular open-source relational database, essential for countless web applications, content management systems, and data-driven projects. Whether you’re a developer, a system administrator, or just curious, mastering its installation is a fundamental skill that opens doors to powerful data management capabilities.

Prerequisites for Installation

Before we begin, ensure you have the following:

  • An Ubuntu 24.04 server or desktop environment.
  • A non-root user with sudo privileges. This is crucial for security and best practice.
  • An active internet connection to download necessary packages.

Pro-tip: Always perform these installations in a test environment first if you’re working on a critical production system.

Step 1: Update Your System Packages

It’s always a good practice to update your system’s package list and upgrade existing packages to their latest versions before installing new software. This ensures the latest security patches and compatible libraries.

sudo apt update
sudo apt upgrade -y

Warning: The -y flag automatically confirms all prompts for package upgrades. While convenient, review the output if you have specific package versions you need to maintain.

Step 2: Install MySQL Server

Ubuntu’s default repositories offer the latest stable MySQL version. Install the mysql-server package, which includes the server daemon, client utilities, and libraries.

sudo apt install mysql-server -y

You might be prompted to set a root password during installation; if not, we’ll secure it in the next step.

Pro-tip: For specific MySQL versions not in default repos, add the official MySQL APT repository. For most, the default package is sufficient and easier to maintain.

Step 3: Secure Your MySQL Installation

After installation, run MySQL’s security script, mysql_secure_installation, to harden your setup. This script helps by:

  • Setting a root password.
  • Removing anonymous users.
  • Disabling remote root login.
  • Deleting the test database.
sudo mysql_secure_installation

You’ll be prompted to configure the VALIDATE PASSWORD COMPONENT. While LOW might be okay for Beginners, production systems should use MEDIUM or STRONG policies.

Follow the prompts, typically answering Y to remove anonymous users, disallow remote root login, remove the test database, and reload privilege tables.

Warning: Always use a strong, unique password for the MySQL root user.

Step 4: Verify MySQL Service Status

Once the installation and initial security setup are complete, verify that the MySQL service is running correctly.

sudo systemctl status mysql

You should see output indicating that the service is “active (running)”. Press q to exit the status view.

If not running, start it:

sudo systemctl start mysql

And enable it to start on boot:

sudo systemctl enable mysql

Pro-tip: systemctl is your go-to tool for managing services on modern Linux distributions like Ubuntu.

Step 5: Access the MySQL Shell

Now that MySQL is running, you can access its command-line interface. Log in as the root user for initial setup tasks.

sudo mysql -u root -p

You will be prompted to enter the root password you set during the mysql_secure_installation step. Once logged in, your prompt will change to mysql>.

To exit the MySQL shell, type exit; and press Enter.

Example: After logging in, you can see existing databases by typing SHOW DATABASES; (remember the semicolon!).

Step 6: Create a New MySQL User and Grant Privileges

Avoid using the root user for daily operations. Instead, create a dedicated user with specific privileges.

Create a User and Password

Log back into the MySQL shell as root:

sudo mysql -u root -p

Create a new user with a strong password. Replace 'your_username' and 'your_password':

CREATE USER 'your_username'@'localhost' IDENTIFIED BY 'your_password';

Warning: Use a very strong, complex password for all database users, especially in production.

Grant Privileges to the New User

Grant necessary privileges. For a web application, you might grant all privileges on a specific database. Let’s create my_app_db:

CREATE DATABASE my_app_db;
GRANT ALL PRIVILEGES ON my_app_db.* TO 'your_username'@'localhost';

Reload privilege tables:

FLUSH PRIVILEGES;

Then, exit the MySQL shell:

exit;

Pro-tip: 'localhost' restricts connections to the same machine. For remote access, use an IP address or '%' (use with caution and strong firewall rules).

Step 7: Test New User Access

Verify that your newly created user can log in and access the assigned database.

mysql -u your_username -p

Enter the password for your_username. Once logged in, try to access your database:

USE my_app_db;
SHOW TABLES;

If you see an empty set (because you haven’t created tables yet) or no errors, your user is correctly set up.

You have successfully installed MySQL 8 on your Ubuntu 24.04 system, secured it, and set up a dedicated user for your applications. From here, you can start creating databases, tables, and populating them with data for your projects. Explore MySQL Workbench for a graphical interface or connect your favorite programming language (Python, PHP, Node.js, etc.) to your new database server.

Leave a Reply

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