Tutorials  /  Databases

Install and Secure MySQL on Ubuntu 20.04

Ccentron Redaktion · October 2025 ·7 min read ·Databases, Tutorial

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.
DB

Matching infrastructure at centron

Databases need care in production: centron can take over updates, backups and monitoring – from a single instance to a full cluster. Explore managed clusters →

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

Console
$ sudo apt update

Install the MySQL server package

Console
$ sudo apt install mysql-server -y

Verify the installed MySQL version

Console
$ 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

Console
$ 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

Console
$ sudo systemctl start mysql

Check the MySQL server status

Console
$ 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

Console
$ 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

Console
$ sudo mysql

Set a strong password for the root user

Code
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

Code
mysql> FLUSH PRIVILEGES;

Exit the MySQL console

Code
mysql> EXIT;

Restart the MySQL service

Console
$ sudo systemctl restart mysql

Access MySQL

Log in to the MySQL server as root:

Console
$ 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:

Code
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:

Code
mysql> CREATE USER 'my_user'@'localhost' IDENTIFIED BY 'my_password';

Grant the user full privileges on the database

Code
mysql> GRANT ALL PRIVILEGES ON my_database.* TO 'my_user'@'localhost';

Allow the user to create databases

Code
mysql> GRANT CREATE ON *.* TO 'my_user'@'localhost';

Grant CRUD operations to the user

Code
mysql> GRANT SELECT, INSERT, UPDATE, DELETE ON *.* TO 'my_user'@'localhost';

Refresh privilege tables

Code
mysql> FLUSH PRIVILEGES;

Exit the MySQL console

Code
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:

Console
$ mysql -u my_user -p

Enter the password you assigned to my_user when prompted.

List available databases

Code
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:

Code
mysql> CREATE DATABASE example_centron;

Switch to the new database

Code
mysql> USE example_centron;

Create a sample table

Create a table with three columns to store various data types:

Code
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

Code
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:

Code
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.

Jetzt 200 € Guthaben sichern

Testen Sie Ihr Setup auf ccloud³

Registrieren Sie sich in der ccloud³ und erhalten Sie 200 € Startguthaben für Ihr Projekt – z. B. für eine PostgreSQL-VM mit automatischen Backups.

centron Redaktion Technische Redaktion

Das Redaktionsteam von centron schreibt Anleitungen aus dem Betriebsalltag: getestet auf unserer eigenen Plattform, betrieben im Rechenzentrum in Hallstadt bei Bamberg.

Kategorie Databases
Teilen
Noch offene Fragen?

Unser Team hilft Ihnen bei Ihrem konkreten Setup weiter – von Menschen, die die Plattform selbst betreiben.

War dieses Tutorial hilfreich?

Ihre Antwort wird anonym gespeichert und hilft uns, die Tutorials zu verbessern.

Kommentare

Noch keine Kommentare – stellen Sie die erste Frage zu diesem Tutorial.

Zum Kommentieren anmelden

Kommentare stehen centron-Kunden offen. Melden Sie sich in Ihrem Konto an, um eine Frage zu diesem Tutorial zu stellen.

Weiterlesen

Das könnte Sie auch interessieren

Jetzt kostenlos anfangen

Melden Sie sich an und erhalten Sie in den ersten 60 Tagen ein Guthaben von 200 € bei centron.

Dieses Werbeangebot gilt nur für neue Konten. Angebot ausschließlich für Gewerbetreibende.

Jetzt loslegen Sales kontaktieren