Cara Install MySQL di Ubuntu 20.04

Tutorial Cara Install MySQL di Ubuntu 20.04 –
MySQL adalah salah satu sistem manajemen basis data relasional sumber terbuka yang paling populer dan banyak digunakan. Ini adalah pilihan yang baik jika kita tahu bahwa kita memerlukan database tetapi tidak tahu banyak tentang semua opsi yang tersedia.

Instalasi MySQL ini di test menggunakan Ubuntu server 20.04 pada virtual machine AWS EC2. Dan ini akan bekerja pada semua server/cloud hosting berbasis Ubuntu.

Hosting database MySQL di Ubuntu 20.04 memerlukan instalasi paket MySQL Server. Anda juga dapat mengakses database dari klien jarak jauh menggunakan MySQL Client.

Jika ingin menginstal MariaDB, lihat tutorial Cara Menginstal MariaDB di Ubuntu 20.04.

Ikuti langkah-langkah sederhana ini untuk menginstal MySQL Server di Ubuntu 20.04.

Apa saja yang dibutuhkan untuk Install MySQL?

  1. VPS/Cloud VPS atau virtual machine AWS EC2
  2. Ubuntu 20.04. Baca disini cara membuat Instance/virtual machine dengan Ubuntu
  3. SSH & User dengan privilege perintah sudo

Step 1: Update Package

Sebelum memulai instalasi, seperti biasa kita membutuhkan akses ke server via SSH. Setelah login ke server via SSH, pastikan dan biasakan untuk melakukan update package index. Agar semua package yang terinstall diperbarui ke versi yang baru.

sudo apt update
sqlinstall

Step 2: Install MySQL

Setelah perform update, install MySQL menggunakan perintah ini:

sudo apt install mysql-server

Note: Jika hanya ingin terhubung ke server MySQL jarak jauh atau remote database dari komputer, instalasi cukup menggunakan mysql client

sudo apt install mysql-client

MySQL service akan berjalan otomatis setelah di install, untuk verifikasi apakah sudah berjalan dan berfungsi gunakan perintah ini:

sudo systemctl status mysql
● mysql.service - MySQL Community Server                               Loaded: loaded (/lib/systemd/system/mysql.service; enabled; >     Active: active (running) since Sat 2021-08-28 18:08:10 UTC; >   Main PID: 44080 (mysqld)                                            Status: "Server is operational"                                    Tasks: 37 (limit: 1160)                                          Memory: 353.4M                                                    CGroup: /system.slice/mysql.service                                       └─44080 /usr/sbin/mysqld                                                                                               Aug 28 18:08:09 ip-172-xx-xx-xx systemd[1]: Starting MySQL Commun>Aug 28 18:08:10 ip-172-xx-xx-xx systemd[1]: Started MySQL Community 

Untuk mengecheck versi MySQL yang terinstall:

mysql --version
mysql  Ver 8.0.26-0ubuntu0.20.04.2 for Linux on x86_64 ((Ubuntu))

Step 3: Menambahkan Keamanan Instalasi MySQL

Gunakan perintah ini untuk menambahkan keamanan instalasi MySQL:

sudo mysql_secure_installation
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: 1
Please set the password for root here.                            
New password:

Re-enter new password:                                            
Estimated strength of the password: 25
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.

Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
Success.


Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.

Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y
Success.

By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.


Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y
 - Dropping test database...
Success.

 - Removing privileges on test database...
Success.

Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.

Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
Success.

All done!

Note: saya merekomendasikan untuk menjawab dengan “Y” atau yes pada keamanan instalasi MySQL.

Step 4: Test Koneksi MySQL

Login dengan root untuk mengakses MySQL:

sudo mysql -u root
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 17
Server version: 8.0.26-0ubuntu0.20.04.2 (Ubuntu)

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

Menambahkan User pada MySQL

Setelah berhasil akses MySQL, gunakan perintah ini untuk menambah user.

mysql> CREATE USER 'user_baru'@'localhost' IDENTIFIED BY 'Password_baru_1234?';

Password User: pada sesi keamanan instalasi MySQL kita menjawab skala password validasi 0-2, maka penggunaan password harus sesuai ketentuan yang kita buat. Misal saya menggunakan password validasi tingkat MEDIUM (1), maka password dalam syntax harus menggunakan minimal 8 karakter yang terdiri dari huruf campur, nomor, dan special characters (‘Password_baru_1234?’).

Check apakah user baru telah dibuat:

mysql> SELECT user FROM mysql.user;
+------------------+
| user             |
+------------------+
| debian-sys-maint |                                              | mysql.infoschema |                                              | mysql.session    |
| mysql.sys        |
| root             |
| user_baru        |                                              +------------------+                                              6 rows in set (0.00 sec)

Berikan full privilege seperti pada root:

mysql> GRANT ALL PRIVILEGES ON *.* TO 'user_baru'@'localhost' WITH GRANT OPTION;

Pastikan full privilege disimpan saat session ini:

mysql> FLUSH PRIVILEGES;

Saya menambahkan user baru dengan nama user_baru. Verifikasi privilege user yang baru dibuat:

mysql> SHOW GRANTS FOR 'user_baru'@'localhost';

Membuat Database pada MySQL

Untuk membuat database baru gunakan perintah ini:

mysql> CREATE DATABASE NAMA_DBKU;

Check apakah database berhasil dibuat, disini saya menggunakan nama websiteku:

mysql> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| websiteku          |
+--------------------+
5 rows in set (0.00 sec)

Dan keluar MySQL dengan perintah exit.

mysql> exit
Bye

Demikian Tutorial Cara Install MySQL di AWS EC2 Ubuntu 20.04.

Semoga bermanfaat.