Database

How to Install MySQL Server on Ubuntu 20.04

In the following article, you will learn how to install MySQL Server on Ubuntu 20.04. MySQL Server is powerful and widely used by individuals as well as larger organizations. Installing MySQL Server on Ubuntu is pretty easy. In this example, I am running Ubuntu 20.04 server installed on my TrueNAS server. I remotely connect to the server from a Windows machine using Termius.

Steps to Install MySQL Server on Ubuntu 20.04

Step 1. Update Ubuntu

It is important to update the Ubuntu so we have the latest available packages and software. To do this, simply use the apt update command as follow

sudo apt update && sudo apt upgrade

Step 2. Install MySQL Server on Ubuntu 20.04

Use the following command to install MySQL Server

sudo apt install mysql-server

Answer y and then press Enter to confirm. The installation will start.

Step 3. Configure MySQL Server

After the installation completed, now configure MySQL Server with this command

sudo mysql_secure_installation

You will need to specify the root password during this step. When completed, you can start connect to the MySQL console using this command

sudo mysql -u root -p

Enter your MySQL root password and then you should be able to log in to the console as follow

To exit from the console, type:

exit;

Step 4. Enable Remote Access to the MySQL Server

By default, MySQL Server can be accessed from the localhost only. To enable remote access to the MySQL Server, we need to edit the mysqld.cnf. Usually this can be located under /etc/mysql/mysql.conf.d/ directory

sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf

Now find the following line

bind-address = 127.0.0.1
mysqlx-bind-address = 127.0.0.1

To be able to access the server from the network, we can change it to

bind-address = 0.0.0.0
mysqlx-bind-address = 127.0.0.1

This means the server can be accessed from anywhere. Now restart MySQL server

sudo systemctl restart mysql

To start the MySQL server during boot, you need to invoke this command

sudo systemctl enable mysql

Step 5. Create a New Database

Now we are going to create a new database. First, login to the console

sudo mysql -u root -p

Let’s create a new database called “db01”.

CREATE DATABASE db01;

Now check the databases we have

SHOW DATABASES;

Step 6. Create a New MySQL User

To create a new user, you can use this command in the MySQL console

CREATE USER 'dhani'@'localhost' IDENTIFIED WITH mysql_native_password BY '12345';

Now grant the database to the new user. In this example, I will grant all privileges to this user

GRANT ALL PRIVILEGES ON *.* TO dhani@localhost;

At this point, we have successfully installed MySQL Server on Ubuntu 20.04, create a new database and new user and also enable remote access to the MySQL server.

Post Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.