MySQL MariaDB

How to install mysql on ubuntu 20.04

MySQL is an open-source and the most popular relational database management system, which is based on SQL means Structured Query Language. Its speed and efficiency make it the best choice for various purposes in which data warehousing, logging, and e-commerce applications are included. MySQL implements the relational model and is commonly installed on an integral part of LAMP stacks. It is also used for web databases. The two packages are available on Ubuntu. The first one is MySQL client and the other package is MySQL server software. The MySQL client is used to connect with the MySQL server and the MySQL server software is used to host various databases.

In this article, we will go through the process of installing MySQL on Ubuntu 20.04 LTS server. After completing the installation of MySQL, we will host it using the terminal.

You must have one non-root administrative user on your Ubuntu 20.04 server. Launch terminal application using Ctrl + Alt + t. You can also open it using the application search bar.

To install MySQL, you need to perform the following steps on Ubuntu 20.04 LTS:

Step 1: Installing MySQL server on Ubuntu 20.04 LTS

To host a MySQL database on your Ubuntu 20.04 system, you will install the MySQL Server package. You will install MySQL using the Apt package repository of Ubuntu 20.04. At the time of writing this tutorial, My SQL version 8.0.19 is available in the Ubuntu repository.

It is an ethical practice to update the Apt package manager on your server before going on with the process. So, use the following command to update packages:

$ sudo apt update

After that, you will install the package of ‘mysql-server’. For this purpose, you need to execute the following command:

$ sudo apt install mysql-server

The above-mentioned command will install MySQL on Ubuntu 20.04. However, the level of security of this command is demanding. Therefore, we will make the installation secure in the next stage of the process.

Step 2: Verify MySQL service status

Once the installation of MySQL server is completed, the service of MySQL starts automatically. You can also verify the status of MySQL service by using the following command:

$ sudo systemctl status mysql

Step 3: Secure Configuration of MySQL

In this step, you will run the security script to make the installation secure. When this script runs on your terminal, it changes some less secure features such as remote root logins. Use the following command for secure configurations:

$ sudo mysql_secure_installation

You will press ‘y’ to allow the installation of the ‘validate password plugin’. The plugin for validating passwords will be configured, which is basically used to not only test the password strength of MySQL users but also improve the security as well.

There are three different levels of the password validation policy which are low, medium, and strong. You will select option ‘2’ for the strong password. On the next user prompt, you will set the password of the MySQL server for the root users.

If you already set up the plugin for password validation, then the script will display the new password strength. Type ‘y’ to confirm the new password.

In the next user prompts, you need to confirm the following questions:

  1. Do you want to remove the anonymous user?
  2. Restrict root user access to the local machine?
  3. Remove the test database?
  4. Reload privilege tables?

You should type ‘y’ to answer all questions and proceed further.

Step 4: Login root to MySQL

MySQL client utility is used to interact with MySQL server using the command line. This client utility installed as a dependency of the MySQL server package.

On Ubuntu 20.04, the root user of MySQL server 8.0 is authenticated by the default plugin auth_socket. This plugin is used to authenticate the users that connect the localhost through the file Unix socket. Now, you need to open MySQL and execute the following command to log in as root user type on MySQL server:

$ sudo mysql

Now, we will go through the basic SQL command syntax that will help you to start working on it. To Create a new database use the following command:

mysql> CREATE DATABASE SAMREENA_DB;

In the above-mentioned command, the name of the database is ‘SAMREENA_DB’. You can change the name of the database as per your requirements.

Next, you will check which authentication method each of your MySQL users accounts by using the following command:

mysql> SELECT user,authentication_string,plugin,host FROM mysql.user;

You can create a new user account that will have certain privileges to the database. The syntax is as follows:

mysql> CREATE USER 'samreena'@'localhost' IDENTIFIED BY 'password';

Now, execute FLUSH PRIVILEGES command which acknowledges the server to reload the grant tables and make new changes:

mysql> FLUSH PRIVILEGES;

To exit from the MySQL shell, you need to execute the following command:

mysql> exit

In this article, you have learned how to install the MySQL server on Ubuntu 20.04 LTS. Moreover, you also learned various SQL commands for database and user creation. In the next step, you can explore how to manage MySQL databases and user accounts using the command line. If you have any queries related to this article then you can give your feedback via comments.

Valuable Links

About the author

Karim Buzdar

Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications. As an IT engineer and technical author, he writes for various web sites. He blogs at LinuxWays.