Blogroll

Pages

Implementasi Sistem Database Terdistribusi Pada MySQL

Selasa, 10 Desember 2013


Sumber :  Dicky Rahardiantoro


Secara definisi replikasi memiliki pengertian sebagai suatu proses mencopy atau mentransfer data dari suatu database ke database lain yang tersimpan pada komputer berbeda. Bila menilik artikel yang saya tulis sebelumnya tentang Pengenalan Arsitektur Database, replikasi dapat difahami sebagai proses pengkopian dan pengelolaan objek-objek dari database yang membentuk suatu sistem database terdistribusi (Distributed Database).

Pada umumnya MySQL dipergunakan secara massal sebagai Database yang cukup handal dalam menangani sistem database terpusat, seperti kebanyakan sistem database yang digunakan untuk web site, content management system, dan lain-lain. Bahkan hampir seluruh penyedia layanan hosting menyertakan dukungan produk MySQL untuk kelengkapan service-nya.

Penggunaan MySQL untuk mendukung proses replikasi database pada saat artikel ini ditulis masih sangat jarang ditemui. Implementasi sistem database terdistribusi kebanyakan masih berkiblat pada software-software dengan bandrol yang tinggi seperti Oracle, SQL Server, IBM DB2 dan lain sebagainya.

MySQL dalam hal ini tentunya tidak mau ketinggalan. Mulai versi 5.0 MySQL sudah mendukung sistem replikasi yang mana sebuah database server yang berfungsi sebagai master dapat tereplikasi datanya ke dalam satu atau lebih database server yang difungsikan sebagai slave.

Model replikasi pada MySQL adalah Asynchronous, sehingga server dengan type slave tidak selalu harus terkoneksi secara permanen untuk menerima berbagai update pada database server master. Replikasi dapat diberlakukan pada sebagian table atau pada keseluruhan database, tergantung pada kebutuhan kita.

Untuk melakukan proses replikasi dibutuhkan :
  1. dua atau lebih komputer yang berfungsi sebagai server, satu server berperan sebagai master dan yang lainnya sebagai slave
  2. adanya koneksi jaringan baik melalui LAN, WAN ataupun Wireless (setting IP Address tidak akan dibahas pada artikel ini, pembaca dianggap sudah mengerti)

Setting Pada Komputer Master

(1) Buka file my.ini

(2) Jika anda menggunakan paket Wampserver, maka tambahkan opsi berikut ini pada bagian bawah tag [wampmysqld]
server-id = 1
log-bin

(3) Setelah itu restrart MySQL

(4) Kemudian masuk ke MySQL console dengan perintah :
mysql -u root -p {password yang sesuai};

Keterangan : tidak ada spasi antara -p dengan password

(5) Jalankan perintah :
GRANT REPLICATION SLAVE ON *.* TO 'slave_user'@'%' IDENTIFIED BY 'some_password';
keterangan : ganti dengan password yang sesuai

(6) Jalankan perintah :
FLUSH PRIVILEGES;
USE mydatabase;
FLUSH TABLES WITH READ LOCK;
SHOW MASTER STATUS;

keterangan : ganti mydatabase sesuai dengan nama database yang akan direplikasi

(7) perintah di atas akan menghasilkan contoh keluaran sebagai berikut : (Lain komputer akan lain keluaran)

+---------------+----------+--------------+------------------+
| File | Position | Binlog_do_db | Binlog_ignore_db |
+---------------+----------+--------------+------------------+
| mysql-bin.009 | 130 | mydatabase | |
+---------------+----------+--------------+------------------+
1 row in set (0.00 sec)


(8) Kemudian jalankan perintah unlock sebagai berikut :
UNLOCK TABLES;

(9) Langkah terakhir adalah keluar dari console dengan perintah :
quit;




Setting Pada Komputer Slave (bisa lebih dari 1 server)

(1) Buat database yang sama dengan milik master, misal : mydatabase
mysql -u root -p {password yang sesuai};
Create database mydatabase;
quit;

(2) Buka file my.ini

(3) Jika anda menggunakan paket Wampserver, maka tambahkan opsi berikut ini pada bagian bawah tag [wampmysqld]
server-id = 2
master-host=192.168.1.1
master-user=slave_user
master-password='some_password'

master-connect-retry=60
replicate-do-db=mydatabase

Misal :
komputer master memiliki IP 192.168.1.1

(3) Setelah itu restart MySQL

(4) Kemudian masuk lagi ke console dengan perintah :
mysql -u root -p {password yang sesuai};
STOP SLAVE;

(5) Jalankan perintah :
CHANGE MASTER TO MASTER_HOST='192.168.1.1'
-> MASTER_USER='slave_user'
-> MASTER_PASSWORD=''
-> MASTER_LOG_FILE='mysql-bin.009'
-> MASTER_LOG_POS=130;

START SLAVE;

(6) Langkah terakhir adalah keluar dari console dengan perintah :
quit;


Jika perintah di atas dijalankan dengan benar, maka segala perubahan data yang terjadi pada database server master akan selalu diikuti oleh server lain yang berfungsi sebagai slave.


Using PHP To Backup MySQL Database




There are at least three ways to backup your MySQL Database :
  1. Execute a database backup query from PHP file.
  2. Run mysqldump using system() function.
  3. Use phpMyAdmin to do the backup.

 

Execute a database backup query from PHP file

Below is an example of using SELECT INTO OUTFILE query for creating table backup :
<?php
include 'config.php';
include 'opendb.php';

$tableName  = 'mypet';
$backupFile = 'backup/mypet.sql';
$query      = "SELECT * INTO OUTFILE '$backupFile' FROM $tableName";
$result = mysql_query($query);


include 'closedb.php';
?>
To restore the backup you just need to run LOAD DATA INFILE query like this :
<?php
include 'config.php';
include 'opendb.php';

$tableName  = 'mypet';
$backupFile = 'mypet.sql';
$query      = "LOAD DATA INFILE 'backupFile' INTO TABLE $tableName";
$result = mysql_query($query);


include 'closedb.php';
?>
It's a good idea to name the backup file as tablename.sql so you'll know from which table the backup file is


Run mysqldump using system() function

The system() function is used to execute an external program. Because MySQL already have built in tool for creating MySQL database backup (mysqldump) let's use it from our PHP script
<?php
include 'config.php';
include 'opendb.php';

$backupFile = $dbname . date("Y-m-d-H-i-s") . '.gz';
$command = "mysqldump --opt -h $dbhost -u $dbuser -p $dbpass $dbname | gzip > $backupFile";
system($command);

include 'closedb.php';
?>


Use phpMyAdmin to do the backup

This option as you may guessed doesn't involve any programming on your part. However I think i mention it anyway so you know more options to backup your database.
To backup your MySQL database using phpMyAdmin click on the "export" link on phpMyAdmin main page. Choose the database you wish to backup, check the appropriate SQL options and enter the name for the backup file.



Membuat Backup MYSQL Automatic



Sebagai admin sebuah server sudah sewajarnya jika kita membuat backup data yang ada dikomputer kita. Untuk ini berikut adalah cara untuk membuat backup mySQL secara otomatis.
permata kita buat dulu aplikasi setting yang bisa kita panggil :
# vi /root/mysqlbackup.sh
#!/bin/sh -x
# MySQL db backup script by rab
# Nama file : mysql_backup.sh
# user,pass dan host mysql
user=”root” -> isi dengan user root mysql anda
passwd=”sanyasyari” -> isi dengan password root mysql anda
host=”localhost”

# Menentukan direktori binary mysql,mysqldump,chown,chmod dan gzip

binmysql=”$(which mysql)”
binmysqldump=”$(which mysqldump)”
chown=”$(which chown)”
chmod=”$(which chmod)”
gzip=”$(which gzip)”

# Menentukan direktori parent backup
i=”/usr/backup”
# Menentukan direktori backup mysql (main directory)
j=”$i/mysql”

# Mendapatkan waktu berupa tanggal, bulan dan tahun dengan format d-m-Y

waktu=”$(date +”%d-%m-%Y”)”
# Hilangkan dulu direktori backup jika ada
if [ -d $j ];
then
rm -rf $j
echo “Direktori sudah ada, proses akan dilanjutkan”
else
echo “Direktori backup belum dibuat, proses akan dilanjutkan”
fi

# Membuat Direktori backup

mkdir -p $j

# Mendapatkan list database
cli=”$($binmysql -u $user -p$passwd -Bse ’show databases’)”
# Main loop
for db in $cli
do
FILE=”$j/$db.$host.$waktu.gz”
$binmysqldump -u $user -h $host -p$passwd $db | $gzip -9 > $FILE
done

# Mengganti permission root direktori backup, agar hanya user dan group root saja yang boleh mengaksesnya

$chown -R root:root $i
$chmod -R 0600 $i


#Selesai
Setelah kita membuat diatas maka tekan Esc + :wq
lalu jangan lupa set chmod 700 di file mysqlbackup.sh
#chmod 700 mysqlbackup.sh
Kemudian kita setting di crontab anda :
#> crontab -e
Tambahkan :
59 3 * * *  root /root/mysql_backup.sh 2>&1
59 -> menit 59
3 -> jam tiga
maka backup akan terjadi setiap hari pada pukul 3:59 pagi
oh iya hasil dari sistem ini adalah :
1. File akan terbackup di folder /user/backup/mysql dan file hanya 1 per nama
2. File sudah di comprese dengan .gz
Simple yach..
Mudah-mudahan berguna ya..

Regards,
Sany Asy’ari, S.Kom


Menghapus Semua Tabel di Database

Tuesday, June 5th, 2007
Dalam MySQL, untuk menghapus sebuah tabel dapat menggunakan perintah DROP TABLE. Tapi bagaimana untuk menghapus semua tabel yang ada di database? Sementara ini perintah untuk menghapus semua tabel ini belum ada di MySQL. Lho, lantas bagaimana kalau kita ingin menghapus semua tabel? Apa dihapus satu persatu? Aduh, cape de…
Untuk mengatasi permasalahan diatas kita bisa menggunakan mysqldump dan sedikit shell script. Dapat dilihat pada contoh berikut :
mysqldump [-u username] [-ppassword] nama_database --no-data
--add-drop-table | grep ^DROP | xargs -0 mysql [-u username]
[-ppassword] nama_database -e
Penggunaan :
  • Opsi -u diatas bersifat optional. Artinya jika database anda tidak dimiliki oleh user tertentu opsi -u ini tidak diikut sertakan.
  • Opsi -p diatas bersifat optional. Artinya jika user dari database anda tidak memiliki password maka opsi -p ini tidak diikut sertakan.
  • nama_database diganti sesuai dengan database anda
  • Perintah ini hanya dapat dijalankan di unix shell dengan asumsi anda sudah menginstall mysqldump.


Mengaktifkan Absolute URL di TinyMCE

Tuesday, July 3rd, 2007
Jika anda menggunakan TinyMCE sebagai WYSIWYG editor, secara default URL dari image yang anda masukkan (http://example.com/main/image/example.jpg) akan di ubah menjadi Relative URL (../image/example.jpg).
Hal ini tentu menjadi masalah jika mencoba menampilkan image tersebut di root direktori (http://example.com) yang menyebabkan image tidak bisa ditampilkan. Hal ini juga yang saya alami kemarin, dan setelah googling menemukan jawaban bahwa Relative URL di TinyMCE itu bisa dinon aktifkan.
Untuk menon aktifkan Relative URL tersebut dapat dilakukan dengan cara menset relative_url menjadi false pada method tinyMCE.init()
tinyMCE.init({
/** other configs */
relative_urls : false
});
Pada method mcImageManager.init() relative_urls juga diset menjadi false.
mcImageManager.init({
relative_urls : false
});
Jika anda menghadapi permasalahan yang sama, cara diatas bisa dicoba.


Manipulasi Data Menggunakan Tabel Pada Database Lain di MySQL

Thursday, February 14th, 2008
MySQL mengizinkan kita untuk memanipulasi data menggunakan data dari database lain. Dengan catatan user kita memiliki akses ke database tersebut. Hal ini bisa dilakukan dengan menyertakan nama database di depan nama tabel. Berikut dua contoh untuk perintah SQL memanipulasi data dari database lain.
CREATE TABLE SELECT
CREATE TABLE  `tost`.`test` SELECT * FROM `test`.`test`
Hasil dari perintah SQL diatas akan membuat table test di database tost yang tabelnya itu (bentuk dan juga isinya) diduplikasi dari tabel test di datbase test.
UPDATE TABLE
UPDATE `tost`.`test`, `test`.`test`
SET `tost`.`test`.`text` = `test`.`test`.`text`
WHERE `tost`.`test`.`id` = `test`.`test`.`id`
Hasil dari perintah SQL diatas akan mengupdate isi field text di table test pada database tost agar sama dengan isi field text di table test pada database test.
Saya rasa kedua contoh perintah SQL diatas sudah cukup, dan mudah - mudahan postingan ini bisa menjawab pertanyaan



Home / SQL





How to Back Up and Restore a MySQL Database
Print
Email

If you're storing anything in MySQL databases that you do not want to lose, it is very important to make regular backups of your data to protect it from loss. This tutorial will show you two easy ways to backup and restore the data in your MySQL database. You can also use this process to move your data to a new web server.
Back up From the Command Line (using mysqldump)
If you have shell or telnet access to your web server, you can backup your MySQL data by using the mysqldump command. This command connects to the MySQL server and creates an SQL dump file. The dump file contains the SQL statements necessary to re-create the database. Here is the proper syntax:
$ mysqldump --opt -u [uname] -p[pass] [dbname] > [backupfile.sql]
  • [uname] Your database username
  • [pass] The password for your database (note there is no space between -p and the password)
  • [dbname] The name of your database
  • [backupfile.sql] The filename for your database backup
  • [--opt] The mysqldump option
For example, to backup a database named 'Tutorials' with the username 'root' and with no password to a file tut_backup.sql, you should accomplish this command:
$ mysqldump -u root -p Tutorials > tut_backup.sql
This command will backup the 'Tutorials' database into a file called tut_backup.sql which will contain all the SQL statements needed to re-create the database.
With mysqldump command you can specify certain tables of your database you want to backup. For example, to back up only php_tutorials and asp_tutorials tables from the 'Tutorials' database accomplish the command below. Each table name has to be separated by space.
$ mysqldump -u root -p Tutorials php_tutorials asp_tutorials > tut_backup.sql
Sometimes it is necessary to back up more that one database at once. In this case you can use the --database option followed by the list of databases you would like to backup. Each database name has to be separated by space.
$ mysqldump -u root -p --databases Tutorials Articles Comments > content_backup.sql
If you want to back up all the databases in the server at one time you should use the --all-databases option. It tells MySQL to dump all the databases it has in storage.
$ mysqldump -u root -p --all-databases > alldb_backup.sql
The mysqldump command has also some other useful options:
--add-drop-table: Tells MySQL to add a DROP TABLE statement before each CREATE TABLE in the dump.
--no-data: Dumps only the database structure, not the contents.
--add-locks: Adds the LOCK TABLES and UNLOCK TABLES statements you can see in the dump file.
The mysqldump command has advantages and disadvantages. The advantages of using mysqldump are that it is simple to use and it takes care of table locking issues for you. The disadvantage is that the command locks tables. If the size of your tables is very big mysqldump can lock out users for a long period of time.
Back up your MySQL Database with Compress
If your mysql database is very big, you might want to compress the output of mysqldump. Just use the mysql backup command below and pipe the output to gzip, then you will get the output as gzip file.
$ mysqldump -u [uname] -p[pass] [dbname] | gzip -9 > [backupfile.sql.gz]
If you want to extract the .gz file, use the command below:
$ gunzip [backupfile.sql.gz]
Restoring your MySQL Database
Above we backup the Tutorials database into tut_backup.sql file. To re-create the Tutorials database you should follow two steps:
  • Create an appropriately named database on the target machine
  • Load the file using the mysql command:
$ mysql -u [uname] -p[pass] [db_to_restore] < [backupfile.sql]
Have a look how you can restore your tut_backup.sql file to the Tutorials database.
$ mysql -u root -p Tutorials < tut_backup.sql
To restore compressed backup files you can do the following:
gunzip < [backupfile.sql.gz] | mysql -u [uname] -p[pass] [dbname]
If you need to restore a database that already exists, you'll need to use mysqlimport command. The syntax for mysqlimport is as follows:
mysqlimport -u [uname] -p[pass] [dbname] [backupfile.sql]
Backing Up and Restoring using PHPMyAdmin
It is assumed that you have phpMyAdmin installed since a lot of web service providers use it. To backup your MySQL database using PHPMyAdmin just follow a couple of steps:
  • Open phpMyAdmin.
  • Select your database by clicking the database name in the list on the left of the screen.
  • Click the Export link. This should bring up a new screen that says View dump of database (or something similar).
  • In the Export area, click the Select All link to choose all of the tables in your database.
  • In the SQL options area, click the right options.
  • Click on the Save as file option and the corresponding compression option and then click the 'Go' button. A dialog box should appear prompting you to save the file locally.
Restoring your database is easy as well as backing it up. Make the following:
  • Open phpMyAdmin.
  • Create an appropriately named database and select it by clicking the database name in the list on the left of the screen. If you would like to rewrite the backup over an existing database then click on the database name, select all the check boxes next to the table names and select Drop to delete all existing tables in the database.
  • Click the SQL link. This should bring up a new screen where you can either type in SQL commands, or upload your SQL file.
  • Use the browse button to find the database file.
  • Click Go button. This will upload the backup, execute the SQL commands and re-create your database.



Tidak ada komentar:

Posting Komentar