Install and Configure MySQL on Ubuntu 25.04
MySQL is a free and open-source relational database management system (RDBMS) that utilizes Structured Query Language (SQL) to organize and manipulate data. It’s widely adopted in web applications, data-driven platforms, and software solutions that require structured data management. Installing MySQL on Ubuntu 25.04 ensures a reliable, secure, and consistent setup suitable for diverse use cases.
This guide walks you through installing MySQL on Ubuntu 25.04—from preparing the system to securing the installation. The steps below are applicable whether you’re configuring a new server or creating a local development setup.
Prerequisites
Before starting, ensure you have the following:
- Access to an Ubuntu 25.04 instance with a non-root user account that has sudoprivileges.
Install MySQL on Ubuntu 25.04
Ubuntu 25.04 provides MySQL packages in its default APT repositories, so you can install MySQL directly without external sources. Follow these steps to update your package index and install the MySQL database server on your system.
Step 1: Update the APT Package Index
$ sudo apt update
Step 2: Install the MySQL Server Package
$ sudo apt install mysql-server -y
Step 3: Verify the Installation
Once installed, check that MySQL is running correctly and confirm the installed version.
$ mysql --version
Your output should resemble the following:
mysql  Ver 8.4.4-0ubuntu2 for Linux on x86_64 ((Ubuntu))
Manage the MySQL System Service
The MySQL server operates as a systemd service named mysql, responsible for handling background processes and startup routines. The commands below show how to enable MySQL to start automatically, launch the service, and check its operational status.
Enable MySQL at Boot
$ sudo systemctl enable mysql
Start the MySQL Service
$ sudo systemctl start mysql
Check the MySQL Service Status
$ sudo systemctl status mysql
A successful status output looks similar to this:
● mysql.service - MySQL Community Server
     Loaded: loaded (/usr/lib/systemd/system/mysql.service; enabled; preset: enabled)
     Active: active (running) since Wed 2025-04-23 15:45:51 UTC; 1min 8s ago
 Invocation: e97531cc30e94807a7a293e576cb307d
   Main PID: 6489 (mysqld)
     Status: "Server is operational"
      Tasks: 34 (limit: 8761)
     Memory: 437.1M (peak: 450.4M)
        CPU: 2.069s
     CGroup: /system.slice/mysql.service
             └─6489 /usr/sbin/mysqld
Secure the MySQL Server
Securing your MySQL installation is essential to prevent unauthorized access and protect confidential data. By default, the root account has unrestricted access to the MySQL shell. Follow these instructions to define a secure root password, disable insecure defaults, and strengthen your server’s overall security posture.
Step 1: Run the MySQL Secure Installation Script
$ sudo mysql_secure_installation
Follow the on-screen prompts to apply the following security configurations:
- VALIDATE PASSWORD COMPONENT: Type Yto enable password validation.
- Password Validation Policy: Enter 2to enforce strong password usage.
- Remove anonymous users?: Enter Yto delete anonymous accounts.
- Disallow root login remotely?: Enter Yto block remote root logins.
- Remove test database and access to it?: Enter Yto remove the test database.
- Reload privilege tables now?: Enter Yto apply all changes.
Step 2: Configure MySQL Authentication Settings
Open the MySQL configuration file using your preferred text editor, such as nano:
$ sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf
Add the following directive under the [mysqld] section to enable the mysql_native_password authentication plugin:
mysql_native_password=ON
Your [mysqld] section should resemble the following:
...
[mysqld]
#
# * Basic Settings
#
mysql_native_password=ON
user            = mysql
...
Step 3: Restart the MySQL Service
Restart MySQL to apply the configuration changes:
$ sudo systemctl restart mysql
Step 4: Set a Strong Root Password
Access the MySQL console as the root user:
$ sudo mysql
Assign a robust password to the root user:
mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'your_strong_password';
Replace your_strong_password with a secure password that adheres to your organization’s password standards.
Reload user permissions to apply the update:
mysql> FLUSH PRIVILEGES;
Exit the MySQL console:
mysql> EXIT;
Access MySQL
Follow the steps below to access your MySQL server, manage users, and administer databases.
Step 1: Log In as Root
$ mysql -u root -p
When prompted, enter the root password you configured earlier.
Step 2: Create a Database and User
Create a sample database named app_data:
mysql> CREATE DATABASE app_data;
Create a new MySQL user named app_user with a secure password:
mysql> CREATE USER 'app_user'@'localhost' IDENTIFIED BY 'your_secure_password';
Replace your_secure_password with a strong, policy-compliant password.
Step 3: Grant User Privileges
Give app_user full privileges on the app_data database:
mysql> GRANT ALL PRIVILEGES ON app_data.* TO 'app_user'@'localhost';
Grant permission to create new databases:
mysql> GRANT CREATE ON *.* TO 'app_user'@'localhost';
Grant CRUD permissions on all databases:
mysql> GRANT SELECT, INSERT, UPDATE, DELETE ON *.* TO 'app_user'@'localhost';
Reload privileges to activate the changes:
mysql> FLUSH PRIVILEGES;
Exit the MySQL console:
mysql> EXIT;
Create a Sample MySQL Database
Now that the app_user account is set up, you can use it to create and manage databases without root privileges. Follow the steps below to log in as app_user, create a new database, and populate it with sample data.
Step 1: Log In as app_user
$ mysql -u app_user -p
Step 2: View Available Databases
mysql> SHOW DATABASES;
Example output:
+--------------------+
| Database           |
+--------------------+
| app_data           |
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)
Step 3: Create and Populate a Database
Create a new database named app_demo:
mysql> CREATE DATABASE app_demo;
Switch to the new database:
mysql> USE app_demo;
Create a table named items with an auto-increment ID, name, description, and timestamp:
mysql> CREATE TABLE items (
         id INT AUTO_INCREMENT PRIMARY KEY,
         name VARCHAR(255) NOT NULL,
         description TEXT,
         created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
         );
Insert sample data into the table:
mysql> INSERT INTO items (name, description) VALUES 
         ('Item One', 'This is the first item.'),
         ('Item Two', 'This is the second item.'),
         ('Item Three', 'This is the third item.');
Verify the inserted records:
mysql> SELECT * FROM items;
Example output:
+----+------------+--------------------------+---------------------+
| id | name       | description              | created_at          |
+----+------------+--------------------------+---------------------+
|  1 | Item One   | This is the first item.  | 2025-04-23 18:02:49 |
|  2 | Item Two   | This is the second item. | 2025-04-23 18:02:49 |
|  3 | Item Three | This is the third item.  | 2025-04-23 18:02:49 |
+----+------------+--------------------------+---------------------+
3 rows in set (0.00 sec)
Exit the MySQL console:
mysql> EXIT;
Conclusion
You have now installed and secured MySQL on Ubuntu 25.04, created a dedicated database user with specific privileges, and used that user to build and manage a sample database. MySQL remains one of the most dependable and scalable open-source relational database management systems, integrating seamlessly with modern stacks such as LAMP and LEMP. It powers countless web applications and server-side solutions. For further details and advanced configuration, refer to the official MySQL documentation.


