How to Install MySQL on Ubuntu 22.04

Published on

5 min read

How to Install MySQL on Ubuntu 22.04

MySQL is one of the most popular open-source relational database management systems. It is fast, easy to manage, scalable, and an integral part of the popular LAMP and LEMP stacks. MySQL can run on any platform including, MacOS, Linux, and Windows.

This article explains how to install and secure MySQL version 8.0 on an Ubuntu 22.04 machine. Upon completion, you will have a fully functional database server that you can use for your projects.

Prerequisites

To follow this guide you must be logged in as a user with sudo privileges .

Installing MySQL on Ubuntu

At the time of writing this article, the latest version of MySQL available in the Ubuntu 22.04 repositories is MySQL version 8.0.

Start by updating the local package index:

sudo apt update

Next, you can install MySQL by typing:

sudo apt install mysql-server

Once the installation is completed, the MySQL service will start automatically. To verify that the MySQL server is running, type:

sudo systemctl status mysql

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

● mysql.service - MySQL Community Server
     Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
     Active: active (running) since Wed 2023-11-29 16:50:28 UTC; 49s ago
    Process: 2238 ExecStartPre=/usr/share/mysql/mysql-systemd-start pre (code=exited, status=0/SUCCESS)
   Main PID: 2246 (mysqld)
     Status: "Server is operational"
      Tasks: 38 (limit: 2220)
     Memory: 365.3M
        CPU: 1.199s
     CGroup: /system.slice/mysql.service
             └─2246 /usr/sbin/mysqld
     ...

If for some reason the server fails to start, you can troubleshoot the issue by looking in your server logs:

sudo journalctl -u mysql

Securing MySQL

MySQL installation comes with a script named mysql_secure_installation that allows you to improve the database server security.

Invoke the script without arguments:

sudo mysql_secure_installation

You will be asked to configure the VALIDATE PASSWORD PLUGIN , which is used to test the strength of the MySQL users’ passwords and improve the security:

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, medium, and strong. Press y if you want to set up the validate password plugin or any other key to move to the next step.

If you decided to use the component, the script will ask you to select the level of the password’s complexity and strength. Generally its good to choose either medium or strong level:

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

Next, you’ll be asked to remove the anonymous user, restrict root user access to the local machine, remove the test database, and reload privilege tables. You should answer y to all questions.

Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
Success.

Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.

Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y
Success.

By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.

Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y
 - Dropping test database...
Success.

 - Removing privileges on test database...
Success.

Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.

Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
Success.

All done! 

Login as root

You can interact with the MySQL server from the command line using the MySQL client utility, which is installed as a dependency of the MySQL server package.

On MySQL 8.0, the root user is authenticated by the auth_socket plugin by default.

The auth_socket plugin authenticates users that connect from the localhost through the Unix socket file. This means that you can’t authenticate as root by providing a password.

To log in to the MySQL server as the root user, type:

sudo mysql

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

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 8.0.35-0ubuntu0.22.04.1 (Ubuntu)

Copyright (c) 2000, 2023, Oracle and/or its affiliates.

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> 

If you want to login to your MySQL server as root using an external program such as phpMyAdmin, you have two options.

The first option is to change the authentication method from auth_socket to mysql_native_password. You can do that by running the following command:

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

The second recommended option is to create a new dedicated administrative user with access to all databases:

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

Conclusion

We have shown you how to install MySQL on Ubuntu 22.04. Now that your database server is up and running, your next step could be to learn how to manage MySQL user accounts and databases .

If you have any questions or feedback, feel free to comleavement.