How to Install MySQL 8 on Ubuntu 21.10

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 21.10. So, let’s get started.

Pre-requisites :

  • A system with Ubuntu 21.10 installed and running.

  • root access to the system.

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

Add Repository

Let us begin with adding MySQL Dev apt repository.

apt update

apt -y  install wget

wget https://repo.mysql.com//mysql-apt-config_0.8.18-1_all.deb

dpkg -i mysql-apt-config_0.8.18-1_all.deb

Select Ubuntu hirsute,

image

image

image

Install MySQL 8

Let's install MySQL 8 using the below commands.

apt update

apt -y install mysql-server

Select OK,

image

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

mysql_secure_installation

Output:

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

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 below command,

mysql -v 

Output:

root@vps:~# mysql -v
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 12
Server version: 8.0.28-0ubuntu0.21.10.3 (Ubuntu)

Copyright (c) 2000, 2022, 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.

Reading history-file /root/.mysql_history
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>

Once secured, you can connect to MySQL and review the existing databases on your database server by using the following command.

mysql -e "SHOW DATABASES;" -p

Output:

root@vps:~# 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 21.10.