Instalasi Database Server (Ubuntu)

Muhammad Fajri
Monday, 05 June 2023

Bismillahirrahmanirrahim.

Setelah instalasi Web Server, yang merupakan langkah awal dari proses instalasi LAMP pada Sistem Operasi Ubuntu, selanjutnya instalasi Database Server. Umumnya, jika data yang dikelola tidak begitu besar, dapat memanfaatkan MySQL atau MariaDB.

Akhir-akhir ini, MariaDB menjadi paket default (bundling) dalam sebuah paket Server Side Scripting seperti XAMPP, namun tetap MySQL sebagai pendahulunya tetap dipertahankan dan juga terus dikembangkan secara bersama-sama.

Instal MySQL

MySQL merupakan Sistem Manajemen Basis Data Relasional (Relational Database Mana Management System) untuk membuat dan memelihara database perusahaan. MySQL kompatibel dengan semua platform OS utama, sehingga sangat cocok digunakan untuk pengembangan aplikasi berbasis web.

Untuk menginstal MySQL, ketikkan perintah:

sudo apt install mariadb-server -y

Berikut output beberapa baris terakhir perintah yang diketikkan.

Preparing to unpack .../7-mariadb-server_1%3a10.3.36-0+deb10u2_all.deb ...
Unpacking mariadb-server (1:10.3.36-0+deb10u2) ...
Setting up libconfig-inifiles-perl (3.000001-1) ...
Setting up libcgi-pm-perl (4.40-1) ...
Setting up libhtml-template-perl (2.97-1) ...
Setting up mariadb-server-core-10.3 (1:10.3.36-0+deb10u2) ...
Setting up galera-3 (25.3.25-2) ...
Setting up mariadb-client-core-10.3 (1:10.3.36-0+deb10u2) ...
Setting up libfcgi-perl (0.78-2+b3) ...
Setting up libterm-readkey-perl (2.38-1) ...
Setting up libdbi-perl:amd64 (1.642-1+deb10u2) ...
Setting up libcgi-fast-perl (1:2.13-1) ...
Setting up mariadb-client-10.3 (1:10.3.36-0+deb10u2) ...
Setting up libdbd-mysql-perl:amd64 (4.050-2) ...
Setting up mariadb-server-10.3 (1:10.3.36-0+deb10u2) ...
Created symlink /etc/systemd/system/mysql.service → /lib/systemd/system/mariadb.service.
Created symlink /etc/systemd/system/mysqld.service → /lib/systemd/system/mariadb.service.
Created symlink /etc/systemd/system/multi-user.target.wants/mariadb.service → /lib/systemd/system/mariadb.service.
Setting up mariadb-server (1:10.3.36-0+deb10u2) ...
Processing triggers for man-db (2.8.5-2) ...

Untuk melihat hasil instalasi MySQL atau MariaDB, ketikkan perintah:

mysql --version

Output:

mysql  Ver 15.1 Distrib 10.3.36-MariaDB, for debian-linux-gnu (x86_64) using readline 5.2

Mengatur Direktori Data MySQL

Untuk memulai service, atur direktori data MariaDB terlebih dahulu. Untuk melakukannya, jalankan perintah:

sudo mysql_install_db --user=mysql --basedir=/usr --datadir=/var/lib/mysql

Secara default, data MariaDB sudah terinstal pada direktori /var/lib/mysql/.

Aktifkan service MySQL

Gunakan perintah berikut untuk menjalankan ulang service MariaDB.

sudo systemctl restart mysqld

Untuk memeriksa apakah service MariaDB berhasil dijalankan atau belum, jalankan perintah:

sudo systemctl status mysqld

Contoh output:

● mariadb.service - MariaDB 10.3.36 database server
     Loaded: loaded (/lib/systemd/system/mariadb.service; enabled; vendor preset: enabled)
     Active: active (running) since Mon 2023-06-05 22:42:54 WITA; 9min ago
       Docs: man:mysqld(8)
             https://mariadb.com/kb/en/library/systemd/
    Process: 40077 ExecStartPre=/usr/bin/install -m 755 -o mysql -g root -d /var/run/mysqld (code=exited, 
    Process: 40078 ExecStartPre=/bin/sh -c systemctl unset-environment _WSREP_START_POSITION (code=exited,
    Process: 40080 ExecStartPre=/bin/sh -c [ ! -e /usr/bin/galera_recovery ] && VAR= ||   VAR=`cd /usr/bin
    Process: 40168 ExecStartPost=/bin/sh -c systemctl unset-environment _WSREP_START_POSITION (code=exited
    Process: 40170 ExecStartPost=/etc/mysql/debian-start (code=exited, status=0/SUCCESS)
   Main PID: 40130 (mysqld)
     Status: "Taking your SQL requests now..."
      Tasks: 31 (limit: 9179)
     Memory: 63.6M
     CGroup: /system.slice/mariadb.service
             └─40130 /usr/sbin/mysqld

Jun 05 22:42:54 fajri-PC systemd[1]: Starting MariaDB 10.3.36 database server...

Status Active: active (running) pada keterangan di atas menunjukkan service mysqld telah berhasil berjalan dengan baik.

Atur Password User Root MySQL

Seperti pemahaman kita bahwa sangat disarankan untuk membuat password database untuk user root. Dengan demikian, database yang ada tidak dapat diakses oleh pengguna tertentu saja.

Jalankan perintah:

sudo mysql_secure_installation

Contoh output:

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user. If you've just installed MariaDB, and
haven't set the root password yet, you should just press enter here.

Enter current password for root (enter for none): #Press enter
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

Set root password? [Y/n] #Type y
New password: #Type your password
Re-enter new password: #Retype password
Password updated successfully!
Reloading privilege tables..
 ... Success!


By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB 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? [Y/n] #Press enter
 ... 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? [Y/n] #Press enter
 ... Success!

By default, MariaDB 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? [Y/n] #Press enter
 - 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? [Y/n] #Press enter
 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!

Mengetes Database MySQL

Setelah menjalankan perintah sebelumnya, database yang ada tidak dapat diakses oleh user biasa. Untuk itu, akses database sebagai user yang memiliki hak akses root dengan menjalankan perintah sudo mysql -u root -p atau sudo su terlebih dahulu.

$ sudo mysql -u root -p
Enter password: #Type your database password

Jika berhasil, halaman awal MySQL ditampilkan.

Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 46
Server version: 10.3.36-MariaDB-0+deb10u2 Deepin 20.8

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

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

MariaDB [(none)]>

Jika semua langkah sudah dilakukan, maka database MySQL telah terinstal pada sistem.

Referensi