hieuvnlabs logo
Hiusvu

How To Install MySQL on Ubuntu

Published on December 5, 2024

How To Install MySQL on Ubuntu

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 (SQL) to manage its data.

This tutorial walks you through installing MySQL 8.0 on an Ubuntu 20.04 server. By the end, you will have a working relational database that you can use to build your next website or application.

Prerequisites

To follow this tutorial, you will need:

  • One Ubuntu 20.04 server with a non-root user that has sudo privileges.

Step 1 — Installing MySQL

On Ubuntu 20.04 you can install MySQL using the APT package repository. At the time of writing, the version of MySQL available in the default Ubuntu repository is 8.0.27.

Update the package index on your server if you have not done so recently:

sudo apt update

Then install the mysql-server package:

sudo apt install mysql-server

Make sure the server is running with the systemctl start command:

sudo systemctl start mysql.service

These commands install and start MySQL, but they do not prompt you to set a password or make any other configuration changes. Because this leaves your installation insecure, we will address that in the next step.

Step 2 — Configuring MySQL

For fresh installations of MySQL you should run the security script that ships with the DBMS. It changes some of the less secure default options for things like remote root logins and sample users.

Warning: As of July 2022, running the mysql_secure_installation script will error out if you do not perform some configuration first. The script tries to set a password for the MySQL root account, but by default on Ubuntu this account is not configured to connect with a password.

Before July 2022 the script would silently fail after attempting to set the root password and continue with the rest of the prompts. Now, however, it returns the following error after you enter and confirm a password:

Output
... Failed! Error: SET PASSWORD has no significance for user 'root'@'localhost' as the authentication method used doesn't store authentication data in the MySQL server. Please consider using ALTER USER instead if you want to change authentication parameters.

New password:

This puts the script into a recursive loop that you can only escape by closing your terminal window.

Because mysql_secure_installation performs a number of other useful actions, it is still recommended to run it before you start using MySQL. To avoid the loop, though, you first need to adjust how the root MySQL user authenticates.

Open the MySQL prompt:

sudo mysql

Then run the following ALTER USER command to change the root user's authentication method to one that uses a password. This example switches to mysql_native_password:

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

After making this change, exit the MySQL prompt:

exit

You can now run mysql_secure_installation without issue.

Once the security script finishes, you can reopen MySQL and change the root user's authentication method back to the default, auth_socket. To authenticate as the root MySQL user using a password, run:

mysql -u root -p

Then switch back to the default authentication method with:

ALTER USER 'root'@'localhost' IDENTIFIED WITH auth_socket;

This means you can once again connect to MySQL as root using sudo mysql.

Run the security script with sudo:

sudo mysql_secure_installation

The script takes you through a series of prompts where you can change some of your MySQL installation's security options. The first prompt asks whether you would like to set up the Validate Password Plugin, which can be used to test the strength of new MySQL users' passwords before deeming them valid.

If you elect to set up the Validate Password Plugin, any MySQL user you create that authenticates with a password will be required to have a password that satisfies the policy you select. The strongest policy, which you select by entering 2, requires passwords to be at least eight characters long and include a mix of uppercase, lowercase, numeric and special characters.

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

Regardless of whether you set up the Validate Password Plugin, the next prompt asks you to set a password for the MySQL root user. Enter and confirm a secure password of your choice:

Output
Please set the password for root here.


New password:

Re-enter new password:

Note that even though you have set a password for the root MySQL user, this user is not currently configured to authenticate with a password when connecting to the MySQL shell.

If you used the Validate Password Plugin, you will receive feedback on the strength of your new password. The script then asks whether you want to continue with the password you just entered or enter a new one. If you are satisfied with the strength, enter Y to continue:

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. This removes some anonymous users and the test database, disables remote root logins, and loads the new rules so that MySQL immediately respects the changes you have made.

When the script finishes, your MySQL installation is secured. You can move on to creating a dedicated database user with the MySQL client.

Step 3 — Creating a Dedicated MySQL User and Granting Privileges

Upon installation, MySQL creates a root user account that you can use to manage your database. This user has full privileges over the MySQL server, meaning it has complete control over every database, table, user and so on. Because of this, it is best to avoid using this account outside of administrative functions. This step outlines how to use the root MySQL user to create a new user account and grant it privileges.

In Ubuntu systems running MySQL 5.7 (and later versions), the root MySQL user is set to authenticate using the auth_socket plugin by default rather than with a password. This plugin requires that the name of the operating system user that invokes the MySQL client matches the name of the MySQL user specified in the command, so you must invoke mysql with sudo to access the root MySQL user:

sudo mysql

Note: If you installed MySQL with another tutorial and enabled password authentication for root, you will need to use a different command to access the MySQL shell. The following runs the MySQL client with regular user privileges, and you will only gain administrator privileges within the database by authenticating:

mysql -u root -p

Once you have access to the MySQL prompt, you can create a new user with a CREATE USER statement. The general syntax is:

CREATE USER 'username'@'host' IDENTIFIED WITH authentication_plugin BY 'password';

After CREATE USER, you specify a username. This is immediately followed by an @ sign and then the hostname from which this user will connect. If you only plan to access this user locally from your Ubuntu server, you can specify localhost. Wrapping both the username and host in single quotes is not always necessary, but it can help prevent errors.

You have several options when it comes to choosing your user's authentication plugin. The auth_socket plugin mentioned previously can be convenient, as it provides strong security without requiring valid users to enter a password to access the database. But it also prevents remote connections, which can complicate things when external programs need to interact with MySQL.

As an alternative, you can leave out the WITH authentication_plugin portion of the syntax entirely to have the user authenticate with MySQL's default plugin, caching_sha2_password. The MySQL documentation recommends this plugin for users who want to log in with a password due to its strong security features.

Run the following command to create a user that authenticates with caching_sha2_password. Be sure to change sammy to your preferred username and password to a strong password of your choosing:

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

Note: There is a known issue with some versions of PHP that causes problems with caching_sha2_password. If you plan to use this database with a PHP application, such as phpMyAdmin, you may want to create a user that authenticates with the older, though still secure, mysql_native_password plugin instead:

CREATE USER 'sammy'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';

If you are not sure, you can always create a user that authenticates with caching_sha2_plugin and then ALTER it later on with this command:

ALTER USER 'sammy'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';

After creating your new user, you can grant them the appropriate privileges. The general syntax for granting user privileges is:

GRANT PRIVILEGE ON database.table TO 'username'@'host';

The PRIVILEGE value in this example syntax defines what actions the user is allowed to perform on the specified database and table. You can grant multiple privileges to the same user in one command by separating each with a comma. You can also grant a user privileges globally by entering asterisks (*) in place of the database and table names. In SQL, asterisks are special characters used to represent "all" databases or tables.

To illustrate, the following command grants a user global privileges to CREATE, ALTER and DROP databases, tables and users, as well as the power to INSERT, UPDATE and DELETE data from any table on the server. It also grants the user the ability to query data with SELECT, create foreign keys with the REFERENCES keyword and perform FLUSH operations with the RELOAD privilege. However, you should only grant users the permissions they need, so feel free to adjust your own user's privileges as necessary.

You can find the full list of available privileges in the official MySQL documentation.

Run this GRANT statement, replacing sammy with your own MySQL user's name, to grant these privileges to your user:

GRANT CREATE, ALTER, DROP, INSERT, UPDATE, INDEX, DELETE, SELECT, REFERENCES, RELOAD on *.* TO 'sammy'@'localhost' WITH GRANT OPTION;

Note that this statement also includes WITH GRANT OPTION. This allows your MySQL user to grant any permissions that it has to other users on the system.

Warning: Some users may want to grant their MySQL user the ALL PRIVILEGES privilege, which provides broad superuser privileges akin to the root user's, like so:

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

Such broad privileges should not be granted lightly, as anyone with access to this MySQL user will have complete control over every database on the server.

Following this, it is good practice to run the FLUSH PRIVILEGES command. This frees up any memory that the server cached as a result of the preceding CREATE USER and GRANT statements:

FLUSH PRIVILEGES;

Then you can exit the MySQL client:

exit

In the future, to log in as your new MySQL user, use a command like the following:

mysql -u sammy -p

The -p flag causes the MySQL client to prompt you for your MySQL user's password in order to authenticate.

Finally, let's test the MySQL installation.

Step 4 — Testing MySQL

Regardless of how you installed it, MySQL should have started running automatically. To test this, check its status:

systemctl status mysql.service

You will see output similar to the following:

Output
 mysql.service - MySQL Community Server
     Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
     Active: active (running) since Tue 2020-04-21 12:56:48 UTC; 6min ago
   Main PID: 10382 (mysqld)
     Status: "Server is operational"
      Tasks: 39 (limit: 1137)
     Memory: 370.0M
     CGroup: /system.slice/mysql.service
             └─10382 /usr/sbin/mysqld

If MySQL is not running, you can start it with sudo systemctl start mysql.

For an additional check, you can try connecting to the database using the mysqladmin tool, which is a client that lets you run administrative commands. For example, this command says to connect as a MySQL user named sammy (-u sammy), prompt for a password (-p) and return the version. Be sure to change sammy to the name of your dedicated MySQL user, and enter that user's password when prompted:

sudo mysqladmin -p -u sammy version

You should see output similar to this:

Output
mysqladmin  Ver 8.0.19-0ubuntu5 for Linux on x86_64 ((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.

Server version        8.0.19-0ubuntu5
Protocol version    10
Connection        Localhost via UNIX socket
UNIX socket        /var/run/mysqld/mysqld.sock
Uptime:            10 min 44 sec

Threads: 2  Questions: 25  Slow queries: 0  Opens: 149  Flush tables: 3  Open tables: 69  Queries per second avg: 0.038

This means MySQL is up and running.

Conclusion

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

  • Pair it with Nginx and PHP-FPM to complete a LEMP stack. See How To Run Multiple PHP Versions on Ubuntu.
  • Set up regular backups with mysqldump.
  • Create a separate database and user for each application you host.