How To Install MySQL 5.7 and 8.0 In Ubuntu 20.04

Install MySQL 5.7 and 8.0 In Ubuntu

Introduction

In this tutorial, we will learn how to install MySQL 5.7 and 8.0 in Ubuntu 20.04.

MySQL is an open-source relational database server tool for Linux operating systems. It is used worldwide in web-based technology. MySQL is an important part of LAMP. MySQL is used to create and store the information in the Database in the form of tables (rows and columns). Mostly used Database servers are MySQL and MariaDB. If you want to install MariaDB.  

Prerequisites

  • An Ubuntu 20.04 running system
  • Access of Root User or a User with sudo privileges.

Remove old MySQL Repositories for install MySQL 5.7 and 8.0 in Ubuntu 20.04.

Before, starting the installation process removes the default MySQL repositories.

To remove the default Repositories, follow the below command:

sudo apt-get purge mysql-apt-config

Install the MySQL in Ubuntu 20.04

Choose the MySQL version as per your requirement. We cannot install multiple MySQL in Linux operating system. So, choose the MySQL version carefully as per your website requirement.

Install MySQL 5.7

  • Update the Server.
sudo apt update
  • Install wget package.
sudo apt install wget -y
  • Download the MySQL 5.7 Repositories
wget https://dev.mysql.com/get/mysql-apt-config_0.8.12-1_all.deb
  • Enable the Repositories.
sudo dpkg -i mysql-apt-config_0.8.12-1_all.deb
  • Choose the Ubuntu Bionic option.
Upgrade MySQL Server from 5.7 to 8 Ubuntu 18.04
  • Choose the MySQL 5.7 version, then select OK and press Enter.
How to install MySQL on Ubuntu -
  • Now we need to add the security key for the Repository.
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 467B942D3A79BD29
  • Update the MySQL Repository.
sudo apt-get update
  • Check the MySQL 5.7 successfully added in repository or not.
sudo apt-cache policy mysql-server
  • Install the MySQL 5.7:
sudo apt install -f mysql-client=5.7.32-1ubuntu18.04 mysql-community-server=5.7.32-1ubuntu18.04 mysql-server=5.7.32-1ubuntu18.04 -y

If it shows Error then check the MySQL version from the output shown by the above command.

  • Set a very strong password for MySQL server. You can use Random Password Generator to generate a strong password. If you use a normal password then after installation it will not allow you to login. It will show an error
    Access denied for user 'root'@'localhost'
Upgrade MySQL Server from 5.7 to 8 Ubuntu 18.04.

Read Also: How to Add and Delete Users on CentOS 8

Install MySQL 8.0

  • Update the Server’s resources:
sudo apt update -y
  • Install wget package on the server:
sudo apt install wget -y
  • Download the MySQL 8.0 Repositories file.
wget https://dev.mysql.com/get/mysql-apt-config_0.8.12-1_all.deb
  • Enable the Repositories for the MySQL installation:
sudo dpkg -i mysql-apt-config_0.8.12-1_all.deb
  • Choose the Ubuntu Bionic option in the Dialog Box.
Install MySQL 5.7 and 8.0 In Ubuntu
  • Choose the MySQL 8.0 version, then select OK and press the Enter Button.
Add MySQL Repository
  • Now we need to add the security key for the Repository.
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 467B942D3A79BD29
  • Again Update the MySQL Repository.
sudo apt-get update
  • Run the below command to start the installation process of the MySQL 8.0 server:
apt-get install mysql-server
  • Set a very strong password for MySQL server. You can use Random Password Generator to generate a strong password. If you use a normal password then after installation it will not allow you to login. It will show an error
    Access denied for user 'root'@'localhost'
Install MySQL 5.7 and 8.0 In Ubuntu

Start the MySQL Service

  • Enable the MySQL service. It helps the server to automatically restart the MySQL service in the case of Server reboot (restart).
systemctl enable mysql
  • Restart the MySQL service
systemctl restart mysql
  • Check the status of MySQL service
systemctl status mysql
Install MySQL 5.7 and 8.0 In Ubuntu 20.04

Secure MySQL Server

You have to perform some tasks to secure the MySQL server.

  • Run the below command to secure the MySQL server:
mysql_secure_installation
  • It will ask you some questions, answer them as per your requirement.
Install MySQL 5.7 and 8.0 In Ubuntu 20.04

Enter your current MySQL root password.

Install MySQL 5.7 and 8.0 In Ubuntu 20.04

If you want to use this component, Type y and press Enter. It is used to check the strength of your password.

Install MySQL 5.7 and 8.0 In Ubuntu 20.04

If you want to set a new password, you can set a strong password from here. Password must be very strong. Use Random Password Generator to generate a strong password.

Install MySQL 5.7 and 8.0 In Ubuntu 20.04

To remove the Anonymous User, type yand press Enter key.

Install MySQL 5.7 and 8.0 In Ubuntu 20.04

If you want to disallow Remote access on the server then type y and press Enter button.

Install MySQL 5.7 and 8.0 In Ubuntu 20.04

To remove the test databases, type y and press Enter button.

Install MySQL 5.7 and 8.0 In Ubuntu 20.04

Enter y and press Enter button to save the changes.

Remove the MySQL Server

If you want to remove the MySQL server from Ubuntu 20.04 then you have to follow the below commands:

  • To remove the MySQL server.
 sudo apt-get remove --purge mysql-server mysql-client mysql-common

Enter Y to continue.

  • To remove all the Directories of MySQL. Select Yes and Press Enter button.
Install MySQL 5.7 and 8.0 In Ubuntu 20.04
  • Run the below commands to completely clean remaining data related to MySQL.
sudo apt-get remove --purge mysql*
sudo apt-get autoclean
sudo apt-get autoremove

Conclusion

In this tutorial, we have learned how we can install and remove the different versions of the MySQL server with the help of Repositories in Ubuntu 20.04. MySQL is one of the most popular packages used to manage the database.

If you guys have any queries related to this tutorial, let me know in the comments.