My Frequent Command on MySQL 8.0 Administration

List of available users

SELECT User, Host FROM mysql.user;

Show privileges of an account

show grants for exampleuser@localhost;

Contoh:

mysql> show grants for epintaruser@localhost;

+------------------------------------------------------------------+
| Grants for epintaruser@localhost |
+------------------------------------------------------------------+
| GRANT USAGE ON *.* TO `epintaruser`@`localhost` |
| GRANT ALL PRIVILEGES ON `epintar`.* TO `epintaruser`@`localhost` |
+------------------------------------------------------------------+
2 rows in set (0.00 sec)

Alter user password

ALTER USER 'userName'@'localhost' IDENTIFIED BY 'New-Password-Here';

Contoh:

mysql> alter user 'moodle_user'@'%' identified by 'p4sswordku';

Query OK, 0 rows affected (0.00 sec)

mysql>

Grant privileges of a database to a user

Syntax:

GRANT <privileges> ON <database>.<object> TO '<user>'@'<host>';

Sebelumnya harus sudah ada terlebih dahulu proses pembuatan user dengan host yang sama

Contoh:

mysql> GRANT ALL PRIVILEGES ON digitoskelasku.* TO 'moodle_user'@'%';
Query OK, 0 rows affected (0.00 sec)

Create alternate Super Admin Users

Using root account for daily activities is not recommended, so we need to have other user account that has superadmin privileges

mysql> create user admin@localhost identified by '@dminpassw0rd';
mysql> grant all privileges on *.* to admin@localhost;

Install php7.4 dan php8.0 di Ubuntu 22.04

Mengapa perlu memasang lebih dari satu versi PHP ?

Pada masa peralihan teknologi ada kalanya kita memerlukan kedua versi exist bersama-sama, karena bisa jadi sebagian aplikasi masih menggunakan php7.4 tapi pada versi aplikasi yang lebih baru atau aplikasi yang lain yang berjalan di server yang sama menggunakan php yang versinya lebih baru, pada tulisan ini kita pakai php8.x. Nanti pengaturan versi PHP mana yang digunakan dilakukan pada file konfigurasi nginx nya walaupun tetap memililh salah satu untuk versi defaultnya

sudo apt install software-properties-common ca-certificates lsb-release apt-transport-https 
LC_ALL=C.UTF-8 sudo add-apt-repository ppa:ondrej/php 
sudo apt update 
sudo apt install php7.4
sudo apt install php8.0

Mengatur default php version

sudo update-alternatives --config php

Akan muncul menu seperti di bawah ini:

There are 2 choices for the alternative php (providing /usr/bin/php).

Selection Path Priority Status
------------------------------------------------------------
* 0 /usr/bin/php8.0 80 auto mode
1 /usr/bin/php7.4 74 manual mode
2 /usr/bin/php8.0 80 manual mode

Press <enter> to keep the current choice[*], or type selection number:

Tips untuk mengamankan server Moodle

Berikut beberapa tips untuk mengamankan sebuah Moodle Server. Pendekatannya dapat dari arah Operating System, Server-server (web server, db server, cache, dll), Instalasi Moodle, server setup dan configuration

#1 Anti clickjacking

Menambahkan X-Frame-Options dalam HTTP header berguna untuk mengamankan NGINX dari serangan Clickjacking

The X-Frame-Options pada HTTP response header dapat digunakan untuk mengindikasikan apakah browser diizinkan untuk membuka sebuah halaman web luar ke dalam sebuah frame atau iframe .

Apakah anda pernah mencoba untuk meng”embed” situs Google.com di website anda dalam sebuah frame ? Dipasikan itu tidak bisa Anda lakukan karena memang di protect, dan hal yang sama dapat Anda lakukan juga pada website Anda. 

Ada tiga pilihan pengaturan untuk X-Frame-Options:

  1. SAMEORIGIN: This setting will allow the page to be displayed in a frame on the same origin as the page itself.
  2. DENY: This setting will prevent a page displaying in a frame or iframe.
  3. ALLOW-FROM URI: This setting will allow a page to be displayed only on the specified origin.

Berikut contoh penulisan pada file konfigurasi nginx di block [server]

add_header X-Frame-Options "SAMEORIGIN";

#2 Mengamankan direktori-direktori yang terbuka

Biasanya direktori yang ada di Moodle kebanyakan di isi dengan file index.html yang kosong untuk memberikan kesan bahwa direktori tersebut walaupun ada, tapi isinya kosong. Untuk itu disaranakn mengahpus semua file index.html dari direktori-direktori dimaksud.

#3 Mengamankan file-file tertentu dari akses melalui browser

Walaupun file-file seperti *.txt, *.md tidak berisi informasi-iinformasi yang sensitif, tapi demi kemanan yang lebih baik, akses terhadap file-file seperti ini sebaiknya tetap dihindari. Karena letak file-file tersebut tersebar di berbagai direktori dibawah direktori utama moodle, maka secara cepat, kita dapat melakukan pembatasan itu dengan cara pengaturan file konfigurasi di Nginx config atau di nginx virtual host configuration file(s)

Konfigirasi berikut ini akan mengamankan file-file dengan ekstensi *.txt|git|md|json. Letakkan kode berikut di file config dari virtualhost config nya, yang biasanya diletakkan di /etc/nginx/site-enable/*

location ~.(git|txt|md|json)$ {
deny all;
return 404;
}

#4 Menutup akses ke direktori-direktori yang di hidden

Demikian pula direktori-direktori yang di hidden, biasanya nama direktori di awalai dengan tanda “.” (titik). Tapi ada juga direktori yang diawalai tanda titik, tapi tidak boleh di block/deny, misalnya direktori .well-known. Direktori ini digunakan oleh Let’s Encrypt

Maka konfigirasi file konfigurasi nginx perlu ditambahkan perintah sebagai berikut:

 location ~ /\.(?!well-known).* {
deny all;
access_log off;
log_not_found off;
}

#5 Menyembunyikan identitas web server yang digunakan yang akan muncul ketika terjadi error

Masih akan berlanjut ….

Reference:

Cross-site Scripting (XSS) in Moodle

https://geekflare.com/add-x-frame-options-nginx/

https://webdock.io/en/docs/how-guides/security-guides/how-to-configure-security-headers-in-nginx-and-apache?irclickid=zw5Xe0R39xyPWxVyFWSd%3AWiYUkH2G4yNqyHQXQ0&irgwc=1&utm_medium=2334778&utm_source=Impact&utm_campaign=Online%20Tracking%20Link#2.-content-security-policy-csp

https://pdfs.semanticscholar.org/1aed/91301bea6030195332a990fd8d666693072c.pdf

https://serverfault.com/questions/755662/nginx-disable-htaccess-and-hidden-files-but-allow-well-known-directory

SQL Injection false positive (https://github.com/zaproxy/zaproxy/issues/3662)

Moodle File Permissions Recomendation

1. moodledata directory dan semua all konten-kontennya (dan subdirectories, termausk sessions):

owner: apache user (apache, httpd, www-data, whatever; see above)
group: apache group (apache, httpd, www-data, whatever; see above)
permissions: 700 on directories, 600 on files

2. moodle directory dan semua konten-kontent nya dan subdirektori (termasuk: config.php):

owner: root
group: root
permissions: 755 on directories, 644 on files.

Jika dimungkinkan login secara lokal oleh user biasa, maka ada 2 (dua) hal yang perlu diatur:

owner: root
group: apache group (apache, httpd, www-data, whatever; see above)
permissions: 750 on directories, 640 on files.

Reference:

https://docs.moodle.org/403/en/Security_recommendations#Most_secure.2Fparanoid_file_permissions

Resize Guest Disk Virtual Machine

Ringkasan langkah:

  1. Pastikan lokasi disk image yang akan diperbesar kapasitasnya
  2. Tentukan cara yang akan digunakan untuk memperbesar VM nya
    • Lihat informasi partisi pada disk image,
  3. Rename disk image lama, buat Disk image baru dengan ukuran yang diinginkan
  4. Buat disk baru
  5. Jalankan perintah virt-resize

1. Informasi tentang disk image

# virsh dumpxml <GuestName>

contoh:

# virsh dumpxml Digitos_U22_5
<domain type='kvm'>
  <name>Digitos_U22_5</name>
  <uuid>0d737f69-3200-440f-8117-8506bacde339</uuid>
  <metadata xmlns:libosinfo="http://libosinfo.org/xmlns/libvirt/domain/1.0" xmlns:cockpit_machines="https://github.com/cockpit-project/cockpit-machines">
    <libosinfo:libosinfo>
      <libosinfo:os id="http://ubuntu.com/ubuntu/22.04"/>
    </libosinfo:libosinfo>
    <cockpit_machines:data>
      <cockpit_machines:has_install_phase>false</cockpit_machines:has_install_phase>
      <cockpit_machines:install_source_type>file</cockpit_machines:install_source_type>
      <cockpit_machines:install_source>/server_images/ubuntu-22.04.3-live-server-amd64.iso</cockpit_machines:install_source>
      <cockpit_machines:os_variant>ubuntu22.04</cockpit_machines:os_variant>
    </cockpit_machines:data>
  </metadata>
  <memory unit='KiB'>2097152</memory>
  <currentMemory unit='KiB'>2097152</currentMemory>
  <vcpu placement='static'>2</vcpu>
  <os>
    <type arch='x86_64' machine='pc-q35-rhel8.6.0'>hvm</type>
  </os>
  <features>
    <acpi/>
    <apic/>
    <vmport state='off'/>
  </features>
  <cpu mode='host-model' check='partial'/>
  <clock offset='utc'>
    <timer name='rtc' tickpolicy='catchup'/>
    <timer name='pit' tickpolicy='delay'/>
    <timer name='hpet' present='no'/>
  </clock>
  <on_poweroff>destroy</on_poweroff>
<on_reboot>restart</on_reboot>
  <on_crash>destroy</on_crash>
  <pm>
    <suspend-to-mem enabled='no'/>
    <suspend-to-disk enabled='no'/>
  </pm>
  <devices>
    <emulator>/usr/libexec/qemu-kvm</emulator>
    <disk type='block' device='disk'>
      <driver name='qemu' type='raw' cache='none' io='native'/>
      <source dev='/dev/vmdiskpool/vmdisk5'/>
      <target dev='vda' bus='virtio'/>
      <address type='pci' domain='0x0000' bus='0x04' slot='0x00' function='0x0'/>
    </disk>
...

Terlihat bahwa disk image ada di : /dev/vmdiskpool/vmdisk5

2. Lihat informasi partisi pada disk image

# virt-df -h

Misalnya :

# virt-df -h -a /dev/vmdiskpool/vmdisk5 
Filesystem                                Size       Used  Available  Use%
vmdisk5-100g:/dev/sda2                    1.7G       130M       1.5G    8%
vmdisk5-100g:/dev/ubuntu-vg/ubuntu-lv
                                          8.0G       4.2G       3.4G   53%

3. Rename disk image

lvrename /dev/vmdiskpool/vmdisk5 /dev/vmdiskpool/vmdisk5.backup

4. Buat Disk baru

# lvcreate -L <new-size>G -n <nama-disk> <disk-pool>/dev/vg_guests

contoh:

# lvcreate -L 100G -n vmdisk5 /dev/vmdiskpool

5. Resize partisi (optional) dan salin partisi lama ke disk image baru

Bagi yang belum terinstall virt-resize, untuk pengguna Ubuntu, Debian dapat memasangnya sbb:

# apt-get install libguestfs-tools

Berikut penggunaannya:

# virt-resize /dev/vmdiskpool/vmdisk5-backup /dev/vmdiskpool/vmdisk5 --resize /dev/sda2=1.8G  --expand /dev/sda3 --LV-expand /dev/ubuntu-vg/ubuntu-lv

Output nya kira-kira akan seperti ini:

[   0.0] Examining /dev/vmdiskpool/vmdisk5-backup
**********

Summary of changes:

/dev/sda1: This partition will be left alone.

/dev/sda2: This partition will be resized from 1.8G to 1.8G.  The 
filesystem ext4 on /dev/sda2 will be expanded using the ‘resize2fs’ 
method.

/dev/sda3: This partition will be resized from 8.2G to 98.2G.  The LVM PV 
on /dev/sda3 will be expanded using the ‘pvresize’ method.

/dev/ubuntu-vg/ubuntu-lv: This logical volume will be expanded to maximum 
size.  The filesystem ext4 on /dev/ubuntu-vg/ubuntu-lv will be expanded 
using the ‘resize2fs’ method.

**********
[   3.7] Setting up initial partition table on /dev/vmdiskpool/vmdisk5
[  14.9] Copying /dev/sda1
[  14.9] Copying /dev/sda2
 100% ⟦▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒⟧ 00:00
[  18.7] Copying /dev/sda3
 100% ⟦▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒⟧ 00:00
[  46.2] Expanding /dev/sda2 using the ‘resize2fs’ method
[  46.3] Expanding /dev/sda3 using the ‘pvresize’ method
[  46.3] Expanding /dev/ubuntu-vg/ubuntu-lv using the ‘resize2fs’ method

Resize operation completed with no errors.  Before deleting the old disk, 
carefully check that the resized disk boots and works correctly.

Reference:

mysqldump in practice

Default

Kasus #1

# mysqldump --no-create-info --complete-insert --insert-ignore pintar_db > pintar_db-20230824-2208.insert-ignore.data

File backup yang dihasilkan:

  1. tidak memuat perintah CREATE [table “” not found /]
  2. kalau ada data yang akan di insert, ternyata sudah ada, maka proses insert record data terkait akan di lewatkan
  3. complete-insert …

Kasus #2 – replace

mysqldump --no-create-info --replace pintar_db > pintar_db-20230827-1554.replace.data

File backup yang dihasilkan:

  1. tidak memuat perintah CREATE [table “” not found /]
  2. kalau ada data yang akan di insert, ternyata sudah ada, maka data tersebut akan direplace oleh data yang baru datang

Kasus #3 – dump table tertentu

Jika Anda akan dumping tabel-tabel t1, t2, dan t3 dari mydb:

mysqldump -u... -p... mydb t1 t2 t3 > mydb_tables.sql

Sebaliknya, Jika Anda akan dumping semua tabel kecuali tabel-tabel t1, t2, dan t3 dari mydb:

DBTODUMP=mydb
SQL="SET group_concat_max_len = 10240;"
SQL="${SQL} SELECT GROUP_CONCAT(table_name separator ' ')"
SQL="${SQL} FROM information_schema.tables WHERE table_schema='${DBTODUMP}'"
SQL="${SQL} AND table_name NOT IN ('t1','t2','t3')"
TBLIST=`mysql -u... -p... -AN -e"${SQL}"`
mysqldump -u... -p... ${DBTODUMP} ${TBLIST} > mydb_tables.sql

Note: Anda dapat mengubah SQL="${SQL} AND table_name NOT IN ('t1','t2','t3')" to SQL="${SQL} AND table_name NOT LIKE 'foo\_%'". Anda juga dapat mengganti kondisi menjadi ‘%foo%’ untuk mendapatkan semua table yang menggandung kosa-kata ‘foo’ dalam nama tabelnya (termasuk didalamnya seperti: ‘food’, ‘fool’, dst).

Kasus #4 – ignore table(s)

Cara lain jika Anda akan dumping semua tabel kecuali tabel-tabel t1, t2, dan t3 dari mydb:

mysqldump -uusername -ppassword dbname \
  --ignore-table=schema.t1    \
  --ignore-table=schema.t2    \
  --ignore-table=schema.t3 > mysqldump.sql

File backup yang dihasilkan:

  1. Semua table di dump tapi tidak akan dumping table t1,t2 dan t3

References:

  1. https://man7.org/linux/man-pages/man1/mysqldump.1.html
  2. https://dba.stackexchange.com/questions/9306/how-do-you-mysqldump-specific-tables
1 2 3 4 13