How To Install MySQL on Ubuntu 20.04 [Quickstart]

Introduction

MySQL is an open-source database management system, commonly installed as part of the popular LAMP (Linux, Apache, MySQL, PHP/Python/Perl) stack. It implements the relational model and uses Structured Query Language (better known as SQL) to manage its data.

This quickstart tutorial will explain how to install MySQL version 8 on an Ubuntu 20.04 server.

Prerequisites

To follow this tutorial, you will need:

Step 1 — Installing MySQL

To install MySQL, first update your server’s package index if you’ve not done so recently:

  • sudo apt update

Then install the mysql-server package:

  • sudo apt install mysql-server

Step 2 — Configuring MySQL

Run MySQL’s included security script with sudo:

  • sudo mysql_secure_installation

This will take you through a series of prompts where you can make some changes to your MySQL installation’s security options. The first prompt will ask whether you’d like to set up the Validate Password Plugin, which can be used to test the strength of your MySQL password.

If you elect to set up the Validate Password Plugin, the script will ask you to choose a password validation level, with the weakest being 0 and the strongest being 2:

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

The next prompt will be to enter and confirm a password for the MySQL root user:

Output
Please set the password for root here. New password: Re-enter new password:

The script will then ask if you want to continue with the password you just entered or if you want to enter a new one. If you’re satisfied with your password, enter Y to continue the script:

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

From there, you can press Y and then ENTER to accept the defaults for all the subsequent questions.

Step 3 — (Optional) Adjusting User Authentication and Privileges

In order to use a password to connect to MySQL as root, you will need to switch its authentication method from the default authentication plugin auth_socket to another one, such as caching_sha2_password or mysql_native_password. To do this, open up the MySQL prompt from your terminal:

  • sudo mysql

From there, run an ALTER USER statement to change which authentication plugin it uses and set a new password. Be sure to change password to a strong password of your choosing, and be aware that this command will change the root password you set in Step 2:

  • ALTER USER 'root'@'localhost' IDENTIFIED WITH caching_sha2_password BY 'password';

Note: caching_sha2_password is MySQL’s preferred authentication plugin, as it provides more secure password encryption than the older, but still widely used, mysql_native_password.

However, many PHP applications — phpMyAdmin, for example — don’t work reliably with caching_sha2_password. If you plan to use this database with a PHP application, you may want to set root to authenticate with mysql_native_password instead:

  • ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';

Then, run FLUSH PRIVILEGES which tells the server to reload the grant tables and put your new changes into effect:

  • FLUSH PRIVILEGES;

Check the authentication methods employed by each of your users again to confirm that root no longer authenticates using the auth_socket plugin:

  • SELECT user,authentication_string,plugin,host FROM mysql.user;
Output
+------------------+------------------------------------------------------------------------+-----------------------+-----------+ | user | authentication_string | plugin | host | +------------------+------------------------------------------------------------------------+-----------------------+-----------+ | debian-sys-maint | $A$005$lS|M#3K #XslZ.xXUq.crEqTjMvhgOIX7B/zki5DeLA3JB9nh0KwENtwQ4 | caching_sha2_password | localhost | | mysql.infoschema | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | caching_sha2_password | localhost | | mysql.session | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | caching_sha2_password | localhost | | mysql.sys | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | caching_sha2_password | localhost | | root | *3636DACC8616D997782ADD0839F92C1571D6D78F | caching_sha2_password | localhost | +------------------+------------------------------------------------------------------------+-----------------------+-----------+ 5 rows in set (0.00 sec)

Once you confirm this on your own server, you can exit the MySQL shell:

  • exit

Alternatively, you could connect to MySQL with a dedicated user instead of root. To create such a user, open up the MySQL shell once again:

  • sudo mysql

Note: If you have password authentication enabled for root, as described in the preceding paragraphs, you will need to run the following instead:

  • mysql -u root -p

From there, create a new user and give it a strong password:

  • CREATE USER 'sammy'@'localhost' IDENTIFIED BY 'password';

Then, grant your new user the appropriate privileges. For example, you could grant the user privileges to all tables within the database, as well as the power to add, change, and remove user privileges, with this command:

  • GRANT ALL PRIVILEGES ON *.* TO 'sammy'@'localhost' WITH GRANT OPTION;

Following this, you can exit the MySQL shell:

  • exit

Conclusion

You now have a basic MySQL setup installed on your server. Here are a few examples of next steps you can take:

0 Comments

Creative Commons License