Install MariaDB di Debian 12: Perbedaan antara revisi
Tidak ada ringkasan suntingan |
(Perluas materi: CRUD database-tabel dengan contoh data siswa, user, backup-restore) |
||
| Baris 1: | Baris 1: | ||
Untuk menginstall MariaDB di Debian 12 caranya mudah. lakukan perintah berikut pada terminal linux. | |||
Update Sistem | |||
<code>robie@debian:~$ sudo apt update</code> | |||
Instal MariaDB | |||
<code>robie@debian:~$ sudo apt install mariadb-server mariadb-client -y</code> | |||
tunggu proses hingga selesai. | tunggu proses hingga selesai. | ||
Proses install akan terlihat seperti berikut | |||
robie@debian:~$ sudo apt install mariadb-server mariadb-client -y | |||
Reading package lists... Done | |||
Building dependency tree... Done | |||
Reading state information... Done | |||
The following additional packages will be installed: | |||
galera-4 gawk libaio1 libdbi-perl libhtml-template-perl libsigsegv2 | |||
mariadb-client-core mariadb-server-core socat | |||
... | |||
Setting up mariadb-server (1:10.11.4-1~deb12u1) ... | |||
Processing triggers for libc-bin ... | |||
robie@debian:~$ | |||
== Cek Service MariaDB == | |||
Untuk mengecek service mariadb sudah berjalan ketikkan perintah berikut | |||
<code>robie@debian:~$ sudo systemctl status mariadb</code> | |||
Jika instalasi berhasil, akan terlihat pesan <code>active (running)</code>. | |||
Agar otomatis jalan saat komputer hidup | |||
<code>robie@debian:~$ sudo systemctl enable mariadb</code> | |||
Untuk mengecek versi | |||
<code>robie@debian:~$ mariadb --version</code> | |||
== Masuk ke MariaDB lewat Terminal == | |||
Di Debian 12 user root memakai unix_socket, jadi masuk pakai sudo tanpa password | |||
<code>robie@debian:~$ sudo mariadb -u root -p</code> | |||
atau | |||
<code>robie@debian:~$ sudo mysql -u root</code> | |||
Jika berhasil akan terlihat seperti berikut | |||
Enter password: | |||
Welcome to the MariaDB monitor. Commands end with ; or \g. | |||
MariaDB [(none)]> | |||
Untuk keluar nanti ketik <code>EXIT;</code> | |||
== Mengecek Database == | |||
Setelah masuk ke <code>MariaDB [(none)]></code> ketikkan perintah berikut untuk melihat database yang ada | |||
<code>MariaDB [(none)]> SHOW DATABASES;</code> | |||
akan terlihat seperti berikut | |||
+--------------------+ | |||
| Database | | |||
+--------------------+ | |||
| information_schema | | |||
| mysql | | |||
| performance_schema | | |||
| sys | | |||
+--------------------+ | |||
== Membuat Database Baru == | |||
Contoh membuat database dengan nama db_sekolah | |||
<code>MariaDB [(none)]> CREATE DATABASE db_sekolah;</code> | |||
cek lagi dengan <code>SHOW DATABASES;</code> maka db_sekolah sudah ada. | |||
== Memilih Database == | |||
Untuk memakai database yang telah dibuat | |||
<code>MariaDB [(none)]> USE db_sekolah;</code> | |||
akan terlihat | |||
Database changed | |||
MariaDB [db_sekolah]> | |||
== Membuat Tabel == | |||
Contoh membuat tabel siswa ke database db_sekolah yang telah dipilih | |||
<code>MariaDB [db_sekolah]> CREATE TABLE siswa ( | |||
nis VARCHAR(10) PRIMARY KEY, | |||
nama VARCHAR(50), | |||
kelas VARCHAR(10), | |||
jurusan VARCHAR(20) | |||
);</code> | |||
Untuk mengecek tabel | |||
<code>MariaDB [db_sekolah]> SHOW TABLES;</code> | |||
Untuk melihat struktur tabel | |||
<code>MariaDB [db_sekolah]> DESCRIBE siswa;</code> | |||
== Menginputkan Data ke Tabel == | |||
Contoh input data siswa | |||
<code>MariaDB [db_sekolah]> INSERT INTO siswa (nis, nama, kelas, jurusan) VALUES ('001', 'Andi Pratama', 'XII', 'TJKT');</code> | |||
<code>MariaDB [db_sekolah]> INSERT INTO siswa (nis, nama, kelas, jurusan) VALUES ('002', 'Budi Santoso', 'XII', 'TJKT'), ('003', 'Siti Aminah', 'XI', 'TJKT');</code> | |||
Untuk melihat isi tabel | |||
<code>MariaDB [db_sekolah]> SELECT * FROM siswa;</code> | |||
akan terlihat seperti berikut | |||
+------+--------------+-------+---------+ | |||
| nis | nama | kelas | jurusan | | |||
+------+--------------+-------+---------+ | |||
| 001 | Andi Pratama | XII | TJKT | | |||
| 002 | Budi Santoso | XII | TJKT | | |||
| 003 | Siti Aminah | XI | TJKT | | |||
+------+--------------+-------+---------+ | |||
== Mengedit Data di Tabel == | |||
Contoh mengedit kelas Siti Aminah dari XI menjadi XII | |||
<code>MariaDB [db_sekolah]> UPDATE siswa SET kelas='XII' WHERE nis='003';</code> | |||
cek lagi dengan <code>SELECT * FROM siswa WHERE nis='003';</code> | |||
== Menghapus Data di Tabel == | |||
Contoh menghapus data Budi Santoso | |||
<code>MariaDB [db_sekolah]> DELETE FROM siswa WHERE nis='002';</code> | |||
cek lagi dengan <code>SELECT * FROM siswa;</code> maka tinggal 2 data. | |||
== Backup dan Restore Database == | |||
Lakukan backup sebelum menghapus tabel atau database. Harus keluar dulu dari mariadb dengan <code>EXIT;</code> | |||
<code>robie@debian:~$ sudo mysqldump -u root -p db_sekolah > backup-sekolah.sql</code> | |||
masukkan password root lalu cek hasil backup | |||
<code>robie@debian:~$ ls -lh backup-sekolah.sql</code> | |||
akan terlihat seperti berikut | |||
-rw-r--r-- 1 robie robie 2,5K Okt 13 10:00 backup-sekolah.sql | |||
Untuk melihat isi file backup | |||
<code>robie@debian:~$ cat backup-sekolah.sql</code> | |||
Untuk restore ke database baru. Contoh restore ke db_sekolah_baru | |||
<code>MariaDB [(none)]> CREATE DATABASE db_sekolah_baru;</code> | |||
<code>MariaDB [(none)]> EXIT;</code> | |||
<code>robie@debian:~$ sudo mysql -u root -p db_sekolah_baru < backup-sekolah.sql</code> | |||
cek hasil restore | |||
<code>robie@debian:~$ sudo mariadb -u root -p</code> | |||
<code>MariaDB [(none)]> USE db_sekolah_baru;</code> | |||
<code>MariaDB [db_sekolah_baru]> SHOW TABLES;</code> | |||
<code>MariaDB [db_sekolah_baru]> SELECT * FROM siswa;</code> | |||
maka data siswa akan kembali seperti semula. | |||
== Membuat User Baru dan Hak Akses == | |||
Agar tidak selalu memakai user root, buat user baru. Contoh membuat user guru dengan password 12345 | |||
<code>MariaDB [(none)]> CREATE USER 'guru'@'localhost' IDENTIFIED BY '12345';</code> | |||
Untuk melihat user yang ada | |||
<code>MariaDB [(none)]> SELECT User, Host FROM mysql.user;</code> | |||
akan terlihat seperti berikut | |||
+-------------+-----------+ | |||
| User | Host | | |||
+-------------+-----------+ | |||
| mariadb.sys | localhost | | |||
| root | localhost | | |||
| guru | localhost | | |||
+-------------+-----------+ | |||
Berikan hak akses ke database db_sekolah | |||
<code>MariaDB [(none)]> GRANT ALL PRIVILEGES ON db_sekolah.* TO 'guru'@'localhost';</code> | |||
<code>MariaDB [(none)]> FLUSH PRIVILEGES;</code> | |||
Untuk mengecek hak akses user guru | |||
<code>MariaDB [(none)]> SHOW GRANTS FOR 'guru'@'localhost';</code> | |||
Untuk mencoba login dengan user guru keluar dulu dengan <code>EXIT;</code> lalu ketikkan perintah berikut | |||
<code>robie@debian:~$ mariadb -u guru -p</code> | |||
masukkan password <code>12345</code> maka akan masuk | |||
Enter password: | |||
Welcome to the MariaDB monitor. | |||
MariaDB [(none)]> | |||
Jika ingin menghapus user | |||
<code>MariaDB [(none)]> DROP USER 'guru'@'localhost';</code> | |||
== Menghapus Tabel == | |||
<code>MariaDB [db_sekolah]> DROP TABLE siswa;</code> | |||
cek dengan <code>SHOW TABLES;</code> maka akan kosong <code>Empty set</code>. | |||
== Menghapus Database == | |||
<code>MariaDB [db_sekolah]> DROP DATABASE db_sekolah;</code> | |||
cek dengan <code>SHOW DATABASES;</code> maka db_sekolah sudah hilang. | |||
Untuk keluar dari mariadb | |||
<code>MariaDB [(none)]> EXIT;</code> | |||
[[Kategori:Linux]] | [[Kategori:Linux]] | ||
[[Kategori:ASJ TKJ]] | [[Kategori:ASJ TKJ]] | ||
Revisi per 13 Oktober 2026 03.55
Untuk menginstall MariaDB di Debian 12 caranya mudah. lakukan perintah berikut pada terminal linux.
Update Sistem
robie@debian:~$ sudo apt update
Instal MariaDB
robie@debian:~$ sudo apt install mariadb-server mariadb-client -y
tunggu proses hingga selesai.
Proses install akan terlihat seperti berikut
robie@debian:~$ sudo apt install mariadb-server mariadb-client -y Reading package lists... Done Building dependency tree... Done Reading state information... Done The following additional packages will be installed: galera-4 gawk libaio1 libdbi-perl libhtml-template-perl libsigsegv2 mariadb-client-core mariadb-server-core socat ... Setting up mariadb-server (1:10.11.4-1~deb12u1) ... Processing triggers for libc-bin ... robie@debian:~$
Cek Service MariaDB
Untuk mengecek service mariadb sudah berjalan ketikkan perintah berikut
robie@debian:~$ sudo systemctl status mariadb
Jika instalasi berhasil, akan terlihat pesan active (running).
Agar otomatis jalan saat komputer hidup
robie@debian:~$ sudo systemctl enable mariadb
Untuk mengecek versi
robie@debian:~$ mariadb --version
Masuk ke MariaDB lewat Terminal
Di Debian 12 user root memakai unix_socket, jadi masuk pakai sudo tanpa password
robie@debian:~$ sudo mariadb -u root -p
atau
robie@debian:~$ sudo mysql -u root
Jika berhasil akan terlihat seperti berikut
Enter password: Welcome to the MariaDB monitor. Commands end with ; or \g. MariaDB [(none)]>
Untuk keluar nanti ketik EXIT;
Mengecek Database
Setelah masuk ke MariaDB [(none)]> ketikkan perintah berikut untuk melihat database yang ada
MariaDB [(none)]> SHOW DATABASES;
akan terlihat seperti berikut
+--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | +--------------------+
Membuat Database Baru
Contoh membuat database dengan nama db_sekolah
MariaDB [(none)]> CREATE DATABASE db_sekolah;
cek lagi dengan SHOW DATABASES; maka db_sekolah sudah ada.
Memilih Database
Untuk memakai database yang telah dibuat
MariaDB [(none)]> USE db_sekolah;
akan terlihat
Database changed MariaDB [db_sekolah]>
Membuat Tabel
Contoh membuat tabel siswa ke database db_sekolah yang telah dipilih
MariaDB [db_sekolah]> CREATE TABLE siswa (
nis VARCHAR(10) PRIMARY KEY,
nama VARCHAR(50),
kelas VARCHAR(10),
jurusan VARCHAR(20)
);
Untuk mengecek tabel
MariaDB [db_sekolah]> SHOW TABLES;
Untuk melihat struktur tabel
MariaDB [db_sekolah]> DESCRIBE siswa;
Menginputkan Data ke Tabel
Contoh input data siswa
MariaDB [db_sekolah]> INSERT INTO siswa (nis, nama, kelas, jurusan) VALUES ('001', 'Andi Pratama', 'XII', 'TJKT');
MariaDB [db_sekolah]> INSERT INTO siswa (nis, nama, kelas, jurusan) VALUES ('002', 'Budi Santoso', 'XII', 'TJKT'), ('003', 'Siti Aminah', 'XI', 'TJKT');
Untuk melihat isi tabel
MariaDB [db_sekolah]> SELECT * FROM siswa;
akan terlihat seperti berikut
+------+--------------+-------+---------+ | nis | nama | kelas | jurusan | +------+--------------+-------+---------+ | 001 | Andi Pratama | XII | TJKT | | 002 | Budi Santoso | XII | TJKT | | 003 | Siti Aminah | XI | TJKT | +------+--------------+-------+---------+
Mengedit Data di Tabel
Contoh mengedit kelas Siti Aminah dari XI menjadi XII
MariaDB [db_sekolah]> UPDATE siswa SET kelas='XII' WHERE nis='003';
cek lagi dengan SELECT * FROM siswa WHERE nis='003';
Menghapus Data di Tabel
Contoh menghapus data Budi Santoso
MariaDB [db_sekolah]> DELETE FROM siswa WHERE nis='002';
cek lagi dengan SELECT * FROM siswa; maka tinggal 2 data.
Backup dan Restore Database
Lakukan backup sebelum menghapus tabel atau database. Harus keluar dulu dari mariadb dengan EXIT;
robie@debian:~$ sudo mysqldump -u root -p db_sekolah > backup-sekolah.sql
masukkan password root lalu cek hasil backup
robie@debian:~$ ls -lh backup-sekolah.sql
akan terlihat seperti berikut
-rw-r--r-- 1 robie robie 2,5K Okt 13 10:00 backup-sekolah.sql
Untuk melihat isi file backup
robie@debian:~$ cat backup-sekolah.sql
Untuk restore ke database baru. Contoh restore ke db_sekolah_baru
MariaDB [(none)]> CREATE DATABASE db_sekolah_baru;
MariaDB [(none)]> EXIT;
robie@debian:~$ sudo mysql -u root -p db_sekolah_baru < backup-sekolah.sql
cek hasil restore
robie@debian:~$ sudo mariadb -u root -p
MariaDB [(none)]> USE db_sekolah_baru;
MariaDB [db_sekolah_baru]> SHOW TABLES;
MariaDB [db_sekolah_baru]> SELECT * FROM siswa;
maka data siswa akan kembali seperti semula.
Membuat User Baru dan Hak Akses
Agar tidak selalu memakai user root, buat user baru. Contoh membuat user guru dengan password 12345
MariaDB [(none)]> CREATE USER 'guru'@'localhost' IDENTIFIED BY '12345';
Untuk melihat user yang ada
MariaDB [(none)]> SELECT User, Host FROM mysql.user;
akan terlihat seperti berikut
+-------------+-----------+ | User | Host | +-------------+-----------+ | mariadb.sys | localhost | | root | localhost | | guru | localhost | +-------------+-----------+
Berikan hak akses ke database db_sekolah
MariaDB [(none)]> GRANT ALL PRIVILEGES ON db_sekolah.* TO 'guru'@'localhost';
MariaDB [(none)]> FLUSH PRIVILEGES;
Untuk mengecek hak akses user guru
MariaDB [(none)]> SHOW GRANTS FOR 'guru'@'localhost';
Untuk mencoba login dengan user guru keluar dulu dengan EXIT; lalu ketikkan perintah berikut
robie@debian:~$ mariadb -u guru -p
masukkan password 12345 maka akan masuk
Enter password: Welcome to the MariaDB monitor. MariaDB [(none)]>
Jika ingin menghapus user
MariaDB [(none)]> DROP USER 'guru'@'localhost';
Menghapus Tabel
MariaDB [db_sekolah]> DROP TABLE siswa;
cek dengan SHOW TABLES; maka akan kosong Empty set.
Menghapus Database
MariaDB [db_sekolah]> DROP DATABASE db_sekolah;
cek dengan SHOW DATABASES; maka db_sekolah sudah hilang.
Untuk keluar dari mariadb
MariaDB [(none)]> EXIT;