Category: Software Development
Anything related to software development.
Linux Bash Tips
Eksekusi perintah pada file-file tertentu
Rekursif perintah untuk mendafpatkan file-file dengan tipe tertentu, lalu mengeksekusi perintah tertentu pada setiap file tersebut
# find /directory/of/interest/ -type f -iname "*.sh" -exec chmod +x {} \;
Mengubah mod dari sebuah file yang berisi daftar file
# chmod g+w `cat daftar_nama_file.txt`
daftar_nama_file.txt
file1.abc
file2.xyz
dst.abx
How to Get Current Date and Time in Bash Script
#!/bin/bash
current_date_time=$(date)
echo "Current date and time: $current_date_time"
# Output: Current date and time: Sun Feb 28 23:26:13 UTC 2023
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:
Disable tombol download HTML5 video
<video width="600" height="350" controls controlsList="nodownload">
<source data-src="video1.ogg" type="video/mp4">
</video>
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:
- tidak memuat perintah CREATE [table “” not found /]
- kalau ada data yang akan di insert, ternyata sudah ada, maka proses insert record data terkait akan di lewatkan
- complete-insert …
Kasus #2 – replace
mysqldump --no-create-info --replace pintar_db > pintar_db-20230827-1554.replace.data
File backup yang dihasilkan:
- tidak memuat perintah CREATE [table “” not found /]
- 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:
- Semua table di dump tapi tidak akan dumping table t1,t2 dan t3
References:
User dan Role di Postgresql
Pengertian role di postgresql
Roles ini berbeda dengan pengertian role di sistem operasi Unix. Disini tidak ada pembedaan antara users dan groups. Roles dapat diubah-ubah, dan lebih flexible. Misalnya role dapat menjadi anggota dari role yang lain. Role juga dapat memiliki objek dan access control terhadap object2 tersebut. Secara default setiap user memiliki role sendiri.
Melihat user yang sudah ada :
postgres=# SELECT rolname FROM pg_roles; rolname ---------- postgres (1 row)
Sebuah role adalah entitas yang dapat memiliki objek database, dan memiliki wewenang terhadap database. Role juga dapat dianggap seperti sebuah “user”, sebuah “group” atau keduanya sekaligus tergantung bagaimana menggunakannya.
Membuat user baru artinya sama dengan membuat role baru :
postgres=# CREATE ROLE admin; CREATE ROLE postgres=# SELECT rolname FROM pg_roles; rolname ---------- postgres admin (2 rows)
Sekarang terlihat sudah ada 2 buah user/role : postgres dan admin.
Memberikan password pada user/role :
postgres=# ALTER ROLE admin WITH PASSWORD 'adminpassword'; ALTER ROLE
MySQL: Duplicate table
Duplikasi struktur table nya saja tanpa data
CREATE TABLE new_table LIKE original_table;
Perintah di atas akan membuat duplikat struktur table new_table yang sama persis dengan original_table.
Untk menyalin datanya dapat menggunakan perintah sebagai berikut:
INSERT INTO new_table SELECT * FROM original_table;
Reference: https://popsql.com/learn-sql/mysql/how-to-duplicate-a-table-in-mysql
Insert MySQL Table from *.csv file
Setup local infile
Sebelum dapat melakukan load data secara local ke dalam database, maka perlu memastikan bahwa load infile dalam kondisi aktif
Periksa status local infile
mysql> show global variables like 'local_infile';
Akan memberikan informasi seperti di bawah ini:
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| local_infile | OFF |
+---------------+-------+
1 row in set (0.00 sec)
Untuk mengaktifkan gunakan perintah berikut:
mysql> set global local_infile=true;
Setelah itu silahkan connect ke database dengan parameter local infile
mysql --local_infile=1 -u root -ppassword DB_name
Dan lakukan load data local, seperti:
mysql> load data local infile 'path/file_name.extention' into table table_name;
INSERT
Misalkan kita memiliki table berikut ini:
mysql> desc temp_user_info_data;
+------------+------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+------------+------+-----+---------+----------------+
| id | bigint(10) | NO | PRI | NULL | auto_increment |
| userid | bigint(10) | NO | MUL | 0 | |
| fieldid | bigint(10) | NO | | 0 | |
| data | longtext | NO | | NULL | |
| dataformat | tinyint(2) | NO | | 0 | |
+------------+------------+------+-----+---------+----------------+
Catatan: untuk melihat info lebih detail tentang table dan bagaimana table tersebut di buat, Anda dapat menggunakan perintah:
mysql> desc namatable
atau
mysql> show create table namatable;
Kemudian file data kita diletakkan di /root dengan nama file data *.csv: contoh.csv seperti berikut:
5791,23,11
19766,23,11
Lalu lakukan load data:
mysql> load data local infile '/root/contoh.csv' into table temp_user_info_data fields terminated by ',' (userid,fieldid,data);
Query OK, 2 rows affected (0.01 sec)
Records: 2 Deleted: 0 Skipped: 0 Warnings: 0
Jika data Anda memiliki satu baris pertama yang berisi nama kolom, maka berikan ignore 1 lines seperti berikut:
mysql> load data local infile '/root/contoh.csv' into table temp_user_info_data fields terminated by ',' ignore 1 lines (userid,fieldid,data);
Switch php version
Berikut adalah untuk switch dari satu versi php ke versi lainnya
sudo update-alternatives --config php
Setelah itu akan muncul pilihan sebagai berikut (tergantung php version yang sudah/pernah di install):
There are 5 choices for the alternative php (providing /usr/bin/php).
Selection Path Priority Status
------------------------------------------------------------
0 /usr/bin/php8.1 81 auto mode
1 /usr/bin/php7.1 71 manual mode
2 /usr/bin/php7.2 72 manual mode
* 3 /usr/bin/php7.4 74 manual mode
4 /usr/bin/php8.0 80 manual mode
5 /usr/bin/php8.1 81 manual mode
Press <enter> to keep the current choice[*], or type selection number:
Reference: https://php.tutorials24x7.com/blog/how-to-switch-php-version-on-ubuntu-20-04-lts