Jun 21, 2023 4 min read

How to Install MySQL on Ubuntu 20.04

Install MySQL on Ubuntu 20.04 with our step-by-step tutorial. It is a popular open-source RDBMS used for storing and retrieving data.

Install MySQL on Ubuntu 20.04
Table of Contents

Choose a different version or distribution

Introduction

Before we begin talking about how to install MySQL on Ubuntu 20.04, let's briefly understand – What is MySQL?

MySQL is a popular open-source relational database management system (RDBMS) used for storing and retrieving data. It is widely used by businesses and websites to manage their data efficiently. MySQL provides a reliable, scalable, and fast solution for handling large volumes of data.

With its user-friendly interface and robust features, MySQL offers seamless integration, high performance, and excellent security. Whether you are a beginner or an expert, MySQL is a trusted choice for effective data management.

In this tutorial, we will explain how to install and secure MySQL on Ubuntu 20.04. We will also address a few FAQs on how to install MySQL on Ubuntu 20.04.

Advantages of MySQL

  1. Open-source: MySQL is freely available, making it cost-effective for businesses.
  2. Scalability: MySQL can handle large datasets and supports high traffic websites.
  3. Performance: It offers fast data processing and efficient query execution.
  4. Security: MySQL ensures data integrity and provides robust security measures.
  5. Ease of use: With its user-friendly interface, MySQL is accessible to both beginners and experts in data management.

Prerequisites to Install MySQL on Ubuntu 20.04

Install MySQL on Ubuntu

The most recent version of MySQL that is accessible via the Ubuntu repository at the time this article is being written is MySQL version 8.0. Run the following commands to install it:

sudo apt update
sudo apt install mysql-server

The MySQL service will start automatically when the installation is finished. Type the below command to see whether the MySQL server is active.

sudo systemctl status mysql

The output should show that the service is enabled and running:

Output

● mysql.service - MySQL Community Server
     Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
     Active: active (running) since Tue 2020-04-28 20:59:52 UTC; 10min ago
   Main PID: 8617 (mysqld)
     Status: "Server is operational"
     ...

Secure MySQL

A script called MySQL_secure_installation that is included with the MySQL installation makes it simple to increase the security of the database server.

Call the script without providing any arguments:

sudo mysql_secure_installation

You will be prompted to set up the VALIDATE PASSWORD PLUGIN, which is used to strengthen passwords for MySQL users and increase security:

Output

Securing the MySQL server deployment.

Connecting to MySQL using a blank password.

VALIDATE PASSWORD COMPONENT can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD component?

Press y|Y for Yes, any other key for No: y

The password validation policy has three tiers: low, medium, and strong. If you wish to go to the next stage, press y to configure the verify password plugin or any other key:

Output

There are three levels of password validation policy:

LOW    Length >= 8
MEDIUM Length >= 8, numeric, mixed case, and special characters
STRONG Length >= 8, numeric, mixed case, special characters and dictionary                  file

Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 2

You will be prompted to choose a password for the MySQL root user at the following prompt:

Output

Please set the password for root here.


New password: 

Re-enter new password: 

Your new password's strength will be shown by the script if you have the validate password plugin configured. To confirm the password, enter y:

Output

Estimated strength of the password: 50 
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y

You'll then be prompted to delete the test database, disable root user access to the local computer, remove the anonymous user, and reload the privilege tables. Each question requires a y response.

Login as root

The MySQL client tool, which is installed as a dependency of the MySQL server package, may be used to communicate with the MySQL server from the command line.

The auth_socket plugin by default authenticates the root user in MySQL 8.0.

Users that connect from localhost using the Unix socket file are verified by the auth_socket plugin. This implies that using a password to log in as root is not possible.

Type the following to access the MySQL server as the root user:

sudo mysql

You will be presented with the MySQL shell, as shown below:

Output

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 12
Server version: 8.0.19-0ubuntu5 (Ubuntu)

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

There are two ways to log in as root to your MySQL server, using an external tool like PhpMyAdmin.

The first one is to switch from auth_socket to mysql_native_password as the authentication mechanism. You may do it by executing the below command:

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'very_strong_password';
FLUSH PRIVILEGES;

The second suggested course of action is to set up a brand-new administrator user with full access to all databases:

GRANT ALL PRIVILEGES ON *.* TO 'administrator'@'localhost' IDENTIFIED BY 'very_strong_password';

FAQs to Install MySQL on Ubuntu 20.04

What is the default root password after installing MySQL?

During the installation, you will be prompted to set a root password. Use that password to access MySQL as the root user.

How can I start or stop the MySQL service?

To start MySQL, use the command sudo systemctl start mysql. To stop it, use sudo systemctl stop mysql.

How can I check if MySQL is running on my Ubuntu system?

Run the command sudo systemctl status mysql to check the status of the MySQL service.

How do I log in to MySQL from the command line?

Type mysql -u username -p in the terminal, replacing "username" with your MySQL username. Enter your password when prompted.

How can I create a new MySQL user?

Log in to MySQL as the root user and use the command CREATE USER 'username'@'localhost' IDENTIFIED BY 'password'; to create a new user.

How can I import a MySQL database dump file?

Use the command mysql -u username -p database_name < dump_file.sql to import a MySQL database dump file.

How do I uninstall MySQL from Ubuntu 20.04?

Run the command sudo apt remove --purge mysql-server to uninstall MySQL from your Ubuntu 20.04 system.

Conclusion

We hope this detailed tutorial helped you install MySQL on Ubuntu 20.04.

If you have any queries, please leave a comment below and we’ll be happy to respond to them

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to DevOps Tutorials - VegaStack.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.