Install and Setup MySQL on Ubuntu 20.04 LTS

Daniel Santoso
2 min readJul 6, 2021

--

In this article, will show you how to install and setup MySQL on Ubuntu 20.04 LTS Server.

STEP 1 (Install MySQL)

Install MySQL by typing in the terminal :

$ sudo apt update
$ sudo apt install mysql-server

STEP 2 (Configuring MySQL)

After installed, you need to setup the security for MySQL. By run this command, you will be given a series of prompt to choose security preferences. You can specify your preferences for this setup :

$ sudo mysql_secure_installation

output :

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

STEP 3 (Create User and Set Previlege)

Login to MySQL as root user using the password that you set before :

$ sudo mysql -u root -p

after that, type this command to create a new MySQL user. You can create a username as you want, the default host is ‘localhost’, but it’s ok if you only want using username :

mysql> CREATE USER 'username'@'host' IDENTIFIED BY 'password';

Grant previlege to database tables :

mysql> GRANT PREVILEGE ON database.table TO 'username'@'host';

or you can grant a user with specific permission by using this command :

mysql> GRANT CREATE, ALTER, DROP, INSERT, UPDATE, DELETE, SELECT, REFERENCES, RELOAD on *.* TO user'@'localhost' WITH GRANT OPTION;

to free up server’s memory after create user and grant statements:

mysql> FLUSH PRIVILEGES;

in the future, to login as a specific user you can use this command :

$ sudo mysql -u daniel -p

STEP 4 (Testing MySQL)

$ sudo systemctl status mysql.service

output if running :

mysql.service - MySQL Community Server
Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
Active: active (running) since Tue 2020-04-21 12:56:48 UTC; 6min ago
Main PID: 10382 (mysqld)
Status: "Server is operational"
Tasks: 39 (limit: 1137)
Memory: 370.0M
CGroup: /system.slice/mysql.service
└─10382 /usr/sbin/mysqld

to start MySQL :

$ sudo systemctl start mysql

to stop MySQL :

$ sudo systemctl stop mysql

to restart MySQL :

$ sudo systemctl restart mysql

--

--

Responses (1)