How to Install MySQL 8 on Ubuntu 22.04

You can install and configure MySQL by running a few commands on the terminal. This tutorial will show you how to install, uninstall, configure, and secure MySQL 8 in ubuntu 22.04 system using command line.

Here are some steps to install, configure, and secure MySQL 8 on ubuntu 22.04 using terminal or command line:

Step 1 – Update System Dependencies

First, open the terminal or cmd and run sudo apt update or sudo apt upgrade to update system packages:

sudo apt update
sudo apt upgrade

Step 2 – Install MySQL

To install MySQL database on Ubuntu 22.04, run “$ sudo apt-get install mysql-server” command on terminal:

You can

sudo apt install mysql-server

During the installation, you’ll be prompted to set a password for the MySQL root user. Make sure to choose a strong password and remember it, as you’ll need it to access MySQL.

To check the MySQL server status, use this command:

sudo service mysql status

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 Fri 2022-04-29 00:38:45 UTC; 11s ago
    Process: 13836 ExecStartPre=/usr/share/mysql/mysql-systemd-start pre (code=exited, statu>
   Main PID: 13844 (mysqld)
     Status: "Server is operational"
      Tasks: 38 (limit: 1151)
     Memory: 351.4M
        CPU: 1.043s
     CGroup: /system.slice/mysql.service
             └─13844 /usr/sbin/mysqld

Step 3 – Securing MySQL

After the installation, it’s recommended to run the MySQL security script to secure your MySQL installation. This script will prompt you to perform several security-related tasks, including removing anonymous users, disallowing remote root login, and more.

To secure MySQL by using this command:

sudo mysql_secure_installation

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

Press y if you want to set up the validate password plugin or any other key to move to the next step.

There are three levels of password validation policy, low, medium, and strong; is as follows:

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: 

Enter 2 for strong password validation.

On the next prompt, will be asked to set a password for the MySQL root user.

Please set the password for root here.

If you need to set up the validate password plugin, the script will show you the strength of your new password. Type y to confirm the password.

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

Next, this will 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.

Step 4 – Login to MySQL as root

In 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, execute the following command.

sudo mysql

Now you can change the authentication type which helps you to login to your MySQL server as root using an external program such as phpMyAdmin.

Here are two methods to access and use mysql in ubuntu:

Method 1: Create New user

This is the recommended option by creating a new dedicated administrative user with access to all databases:

CREATE USER 'admin'@'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON *.* TO 'admin'@'localhost' WITH GRANT OPTION;
FLUSH PRIVILEGES; 

Method 2: Change Authentication Method

To change the authentication method from auth_socket to mysql_native_password. Use this command with your password:

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

Step 5 – Uninstall MySQL Ubuntu

Need to uninstall MySQL in ubuntu, you can do it by these commands:

sudo apt remove --purge mysql-server mysql-client mysql-common
sudo apt autoremove
sudo apt autoclean

Conclusion

Congratulations! You’ve successfully installed MySQL 8 on your Ubuntu 22.04 system. You can now start using MySQL to manage your databases and data.

AuthorDevendra Dode

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *