Install and Configure MySQL on Ubuntu 20.04
MySQL is a widely used open-source relational database that efficiently stores and manages data. Installing it on Ubuntu 20.04 creates a reliable environment for building and securing database-driven applications.
This guide explains how to install and configure MySQL on Ubuntu 20.04. For instructions tailored to a different system, you can also refer to our guide on installing MySQL on Debian 12.
Prerequisites
Before starting, ensure you have the following:
- An Ubuntu 20.04 server.
- SSH access to the server as a non-root user with sudo privileges.
- The server updated with the latest packages.
Install MySQL on Ubuntu 20.04
MySQL is included in the default APT repositories for Ubuntu 20.04. Update your package index and install the latest MySQL server version using the APT package manager.
Update the server package index
$ sudo apt update
Install the MySQL server package
$ sudo apt install mysql-server -y
Verify the installed MySQL version
$ mysql --version
Example output:
mysql Ver 8.0.41-0ubuntu0.20.04.1 for Linux on x86_64 ((Ubuntu))
Manage the MySQL System Service
MySQL runs as the mysql system service on Ubuntu 20.04. Enable it to start at boot and check its status to manage the database processes.
Enable MySQL to start at boot
$ sudo systemctl enable mysql
Example output:
Synchronizing state of mysql.service with SysV service script with /usr/lib/systemd/systemd-sysv-install.
Executing: /usr/lib/systemd/systemd-sysv-install enable mysql
Start the MySQL server
$ sudo systemctl start mysql
Check the MySQL server status
$ sudo systemctl status mysql
Example output:
● mysql.service – MySQL Community Server
Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
Active: active (running) since Sun 2025-04-06 10:58:51 UTC; 5min ago
Main PID: 17189 (mysqld)
Status: “Server is operational”
Tasks: 39 (limit: 9415)
Memory: 365.9M
CGroup: /system.slice/mysql.service
└─17189 /usr/sbin/mysqld
Secure the MySQL Server
Securing MySQL helps protect your databases from unauthorized access. Set a strong root password and remove insecure defaults using the steps below.
Run the MySQL secure installation script
$ sudo mysql_secure_installation
Follow these steps to secure your MySQL server:
- Enable password validation: Enter y and press Enter to activate password validation.
- Set strong password policy: Enter 2 to enforce the use of strong passwords.
- Set a new password: Provide a strong password for the root user (it will be verified).
- Confirm password: Enter y to confirm the new root password.
- Remove anonymous users: Enter y to revoke access from unauthorized users.
- Disable remote root login: Enter y to prevent root user login from remote hosts.
- Delete test databases: Enter y to remove any test databases.
- Reload privilege tables: Enter y to apply the changes immediately.
Log in to the MySQL server as root
$ sudo mysql
Set a strong password for 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 according to your password policy.
Reload the MySQL privilege tables
mysql> FLUSH PRIVILEGES;
Exit the MySQL console
mysql> EXIT;
Restart the MySQL service
$ sudo systemctl restart mysql
Access MySQL
Log in to the MySQL server as root:
$ mysql -u root -p
Enter the root user password you set earlier when prompted.
Create a new database
For example, create a database named my_database:
mysql> CREATE DATABASE my_database;
Create a new MySQL user
Create a user named my_user and set a strong password, replacing my_password with a secure password according to your password policy:
mysql> CREATE USER 'my_user'@'localhost' IDENTIFIED BY 'my_password';
Grant the user full privileges on the database
mysql> GRANT ALL PRIVILEGES ON my_database.* TO 'my_user'@'localhost';
Allow the user to create databases
mysql> GRANT CREATE ON *.* TO 'my_user'@'localhost';
Grant CRUD operations to the user
mysql> GRANT SELECT, INSERT, UPDATE, DELETE ON *.* TO 'my_user'@'localhost';
Refresh privilege tables
mysql> FLUSH PRIVILEGES;
Exit the MySQL console
mysql> EXIT;
Create a Sample MySQL Database
Non-privileged MySQL users, such as my_user, can create databases and tables. In this example, my_user creates a database, adds a table, inserts records, and retrieves them.
Log in as the MySQL user
Log in to the MySQL database console using the my_user account you created earlier:
$ mysql -u my_user -p
Enter the password you assigned to my_user when prompted.
List available databases
mysql> SHOW DATABASES;
Example output:
+——————–+
| Database |
+——————–+
| information_schema |
| my_database |
| mysql |
| performance_schema |
| sys |
+——————–+
Create a new database
For example, create a database called example_centron:
mysql> CREATE DATABASE example_centron;
Switch to the new database
mysql> USE example_centron;
Create a sample table
Create a table with three columns to store various data types:
mysql> CREATE TABLE sample_table (
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 sample_table (name, description) VALUES
('Sample Item 1', 'This is the first sample item.'),
('Sample Item 2', 'This is the second sample item.'),
('Sample Item 3', 'This is the third sample item.');
Select and verify the records
Retrieve all records from the table to confirm the data insertion:
mysql> SELECT * FROM sample_table;
Example output:
+—-+————–+——————————-+———————+
| id | name | description | created_at |
+—-+————–+——————————-+———————+
| 1 | Sample Item 1| This is the first sample item.| 2024-06-19 10:00:00 |
| 2 | Sample Item 2| This is the second sample item.| 2024-06-19 10:00:00|
| 3 | Sample Item 3| This is the third sample item. | 2024-06-19 10:00:00|
+—-+————–+——————————-+———————+
Conclusion
You have successfully installed MySQL on Ubuntu 20.04, secured it for authenticated access, and created a sample database with a table and records. MySQL integrates seamlessly with modern web applications and can serve as a dedicated database server or as the backend for dynamic stacks such as LAMP. For additional details, consult the official MySQL documentation.


