How to Install MySQL 8 on Ubuntu 22.04

MySQL is an open-source, fast reliable, and flexible relational database management system, typically used with PHP. In this article, we are going to learn how to install MySQL 8 on Ubuntu 22.04. So, let’s get started.

Pre-requisites :

  • A system with Ubuntu 22.04 installed and running.

    • root or sudo user access to the system.

Once you're all set, we'll proceed with MySQL 8 installation and configuration.

Install MySQL 8

Let's install MySQL 8 using the below commands.

apt update

apt -y install mysql-server

Finally, you will want to secure your MySQL installation by issuing the following command.

mysql_secure_installation

Output:

root@crown:~# mysql_secure_installation

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
Please set the password for root here.

New password: 

Re-enter new password: 

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
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.

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! 
root@crown:~# 

To check the database server status.

systemctl status mysql

Output:

root@crown:~# sudo systemctl status mysql
● mysql.service - MySQL Community Server
     Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
     Active: active (running) since Fri 2022-04-08 15:37:05 UTC; 5min ago
    Process: 7669 ExecStartPre=/usr/share/mysql/mysql-systemd-start pre (code=exited, status=0/>
   Main PID: 7677 (mysqld)
     Status: "Server is operational"
      Tasks: 39 (limit: 1035)
     Memory: 355.8M
        CPU: 1.705s
     CGroup: /system.slice/mysql.service
             └─7677 /usr/sbin/mysqld

Apr 08 15:37:04 crown systemd[1]: Starting MySQL Community Server...
Apr 08 15:37:05 crown systemd[1]: Started MySQL Community Server.

Create Database

Let us begin with creating a Database and a user. We will then grant the required privileges to the user so it can interact with the Database:

mysql

CREATE USER 'test_user_crowncloud'@'localhost' IDENTIFIED BY "Password_crowncloud";

CREATE DATABASE test_db_crowncloud;

GRANT ALL PRIVILEGES ON test_db_crowncloud.* TO 'test_user_crowncloud'@'localhost';

FLUSH PRIVILEGES;

EXIT

The above commands will give complete access to the test_db_crowncloud Database to the user test_user_crowncloud.We would suggest using a strong and long password.

Check the version and newly created database using the below command,

mysql -V

Output:

root@crown:~# mysql -V
mysql  Ver 8.0.28-0ubuntu4 for Linux on x86_64 ((Ubuntu))

You can now connect to MySQL and review the existing databases on your database server by using the following command.

mysql -e "SHOW DATABASES;" -p

Output:

root@crown:~# mysql -e "SHOW DATABASES;" -p
Enter password: 
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| test_db_crowncloud |
+--------------------+
root@vps:~# 

Now you have successfully installed MySQL 8 on Ubuntu 22.04.