
MySQL is an open-source relational database management system (RDBMS) that uses Structured Query Language (SQL) to store and manage data. It is commonly used in web applications, data platforms, and software that requires structured data handling. Installing MySQL on Ubuntu 25.04 provides a stable, consistent and secure environment for database operations across various use cases.
This article explains how to install MySQL on Ubuntu 25.04, from system preparation to securing the MySQL installation. These steps are applied on whether you're setting up a fresh server or preparing a local development environment.
Prerequisites
Before you begin, you need to:
- Have access to an Ubuntu 25.04 instance as a non-root user with sudo privileges.
Install MySQL on Ubuntu 25.04
Ubuntu 25.04 includes MySQL packages in its default APT repositories. You can install the MySQL server directly without adding any external repositories. Follow the steps below to update your system index and install the MySQL database server on your Ubuntu instance.
Update the APT's server package index.
console$ sudo apt update
Install the MySQL server package.
console$ sudo apt install mysql-server -y
Verify that MySQL is installed and check its version.
console$ mysql --version
Your output should be similar to the one below:
mysql Ver 8.4.4-0ubuntu2 for Linux on x86_64 ((Ubuntu))
Manage the MySQL System Service
MySQL server runs as a systemd service named mysql
, which manages its background processes and startup behavior. Follow the steps below to enable the mysql
service to start at boot and verify that its status is active and running.
Enable MySQL to start automatically when the system boots.
console$ sudo systemctl enable mysql
Start the MySQL database service.
console$ sudo systemctl start mysql
View the MySQL service status.
console$ sudo systemctl status mysql
Your output should be similar to the one below:
● 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 the MySQL server is crucial to prevent unauthorized access and protect sensitive data. By default, the root
user has unrestricted access to the MySQL console. Follow the steps below to set a new root
password, disable insecure defaults, and improve the overall security of your MySQL server.
Run the following command to start the MySQL secure installation script.
console$ sudo mysql_secure_installation
Follow the prompts below to secure your MySQL server by enabling password validation, enforcing strong password policies, removing anonymous users, disabling remote root access and deleting test databases.
- VALIDATE PASSWORD COMPONENT: Enter Y and press Enter to enable password validation.
- Password Validation Policy: Enter 2 to enforce the usage of strong passwords.
- Remove anonymous users?: Enter Y to remove anonymous MySQL users.
- Disallow root login remotely?: Enter Y to disable root user remote MySQL access.
- Remove test database and access to it?: Enter Y to delete the test database.
- Reload privilege tables now?: Enter Y to apply the changes to the privilege tables.
Open the
mysqld.cnf
configuration file using any text editor such asnano
.console$ sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf
Add the following directive under the
[mysqld]
section to enable themysql_native_password
authentication plugin.inimysql_native_password=ON
Your
[mysqld]
section should look like below:ini... [mysqld] # # * Basic Settings # mysql_native_password=ON user = mysql ...
Restart MySQL to ensure the configuration changes takes effect.
console$ sudo systemctl restart mysql
Access the MySQL console as the
root
user.console$ sudo mysql
Set a strong password for the root user.
sqlmysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'your_strong_password';
Replace
your_strong_password
with a secure password that complies with your password policy.Execute the following to refresh MySQL’s user permissions.
sqlmysql> FLUSH PRIVILEGES;
Exit the MySQL database console.
sqlmysql> EXIT;
Access MySQL
Follow the steps below to access your MySQL server and manage databases and users.
Log in to the MySQL server as the
root
user.console$ mysql -u root -p
When prompted, enter the root password you created earlier.
Create a sample MySQL database named
app_data
.sqlmysql> CREATE DATABASE app_data;
Create a new MySQL user
app_user
with a secure password.sqlmysql> CREATE USER 'app_user'@'localhost' IDENTIFIED BY 'your_secure_password';
Replace
your_secure_password
with a strong password based on your password policy.Grant the database user
app_user
full privileges to theapp_data
database.sqlmysql> GRANT ALL PRIVILEGES ON app_data.* TO 'app_user'@'localhost';
Grant the database user
app_user
permission to create new databases.sqlmysql> GRANT CREATE ON *.* TO 'app_user'@'localhost';
Grant the database user
app_user
permissions to perform CRUD operations on all databases.sqlmysql> GRANT SELECT, INSERT, UPDATE, DELETE ON *.* TO 'app_user'@'localhost';
Reload the MySQL privilege tables to apply the new user changes.
sqlmysql> FLUSH PRIVILEGES;
Exit the MySQL database console.
sqlmysql> EXIT;
Create a Sample MySQL Database
After creating the app_user
database user, you can use it to create and manage databases without root privileges. In this section, you will log in as app_user
, create a new database, add a table, and populate it with sample data.
Log in to the MySQL database server as the user
app_user
.console$ mysql -u app_user -p
Enter the password you configured for
app_user
when prompted.List all databases accessible to the current user.
sqlmysql> SHOW DATABASES;
Your output should be similar to the one below:
+--------------------+ | Database | +--------------------+ | app_data | | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 5 rows in set (0.00 sec)
Create a new database named
app_demo
.sqlmysql> CREATE DATABASE app_demo;
Switch to the new database context.
sqlmysql> USE app_demo;
Create a table named
items
to store sample data, including an auto-incrementing ID, name, description, and a timestamp.sqlmysql> CREATE TABLE items ( id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255) NOT NULL, description TEXT, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP );
Insert sample records into the
items
table.sqlmysql> 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.');
Query the
items
table to verify that the data was inserted correctly.sqlmysql> SELECT * FROM items;
Your output should be similar to the one below:
+----+------------+--------------------------+---------------------+ | 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 database console.
sqlmysql> EXIT;
Conclusion
You have installed and secured MySQL on Ubuntu 25.04, created a dedicated database user with granular privileges, and used that user to create and manage a sample database. MySQL is a robust and scalable open-source relational database management system (RDBMS) that integrates seamlessly with modern application stacks such as LAMP and LEMP. It is also used as the database component for dynamic websites and server-side applications. To learn more about MySQL features, refer to the official MySQL documentation.
No comments yet.