Category:Database
Contents
- 1 Database MySQL
- 2 Membuat database
- 3 Membuat user database
- 4 Mengizinkan user untuk mengakses database
- 5 Membuat Backup Database
- 6 Download / Backup / Export Database melalui phpMyAdmin
- 7 Upload / Restore / Import Database melalui phpMyAdmin
- 8 Pindah Directory MySQL (Untuk client VDS)
- 9 Splitting Huge MySQL Dump For Easy Restore
Database MySQL
Untuk dapat menggunakan database MySQL, anda harus membuat database-nya terlebih dahulu. Lalu, buatlah user spesifik yang diset untuk bisa mengakses database tersebut. Setelah itu anda bisa memanfaatkan database dalam aplikasi anda dengan user dan database yang telah dibuat.
Membuat database
1. Untuk mengakses Menu Maintenance MySQL, klik "MySQL Databases" pada cPanel. 2. Masukkan nama database yang ingin anda buat pada kolom “New Database: “. 3. Nama database anda nantinya adalah $NAMAUSER_$DB, contohnya: cbncloud_data1 (dimisalkan user akun hosting anda adalah "cbncloud" dan nama database yang anda buat adalah "data1"). 4. Klik "Create Database". 5. Untuk menghapus database yang telah dibuat, klik Delete Database pada tabel Database
Membuat user database
1. Masih pada halaman yang sama saat membuat database di cPanel 2. Masukkan nama user yang ingin dibuat pada kolom "Username: ". 3. Masukkan password kemudian klik Create User. 4. Nama user anda nantinya adalah $NAMAUSER_$USERDB, contohnya: cbncloud_user1. 5. Untuk menghapus user tersebut, klik tombol X di kolom Delete pada tabel Current Users. 6. Untuk mengubah password user yang telah dibuat, klik nama user pada tabel Current Users,
masukkan password lama dari user tersebut, kemudian buat password baru lalu klik Change Password.
Mengizinkan user untuk mengakses database
1. Masuk ke "MySQL Databases" pada halaman utama cPanel. 2. Pilih user yang dimaksud pada drop-down menu di sebelah kata "User: ". 3. Pilih nama database yang boleh diakses oleh user yang telah dipilih di atas pada drop-down menu di sebelah kata "Database: ". 4. Pilih hak (provilege) apa saja yang diberikan pada user tersebut terhadap database yang telah dipilih sebelumnya. 5. Klik "Make Changes".
Membuat Backup Database
1. Untuk membuat backup file database di cPanel, klik Backups. 2. Dowload database yang diinginkan dengan memilih / klik nama database pada bagian Download a MySQL Database Backup. Akan muncul jendela baru yang menentukan apakah file ingin di simpan atau tidak (Cancel). 3. Setelah klik Ok, maka database akan mulai di download ke lokasi penyimpanan di komputer anda. 4. Jika ingin mengembalikan atau meng-upload database dari storage / komputer ke cPanel, masih pada halaman yang sama saat melakukan Backup database, klik Restore a MySQL Database. 5. Klik tombol Browse untuk memilih database mana yang akan di-upload / restore. 6. Carilah file database dengan format *.sql.gz, kemudian klik Open. 7. Klik Upload untuk mengeksekusi Restore Database.
Download / Backup / Export Database melalui phpMyAdmin
1. Klik phpMyAdmin pada tab Databases di halaman utama cPanel. 2. Pilih nama database yang ingin di-backup, pilih semua tabel (Check All) atau tabel tertentu yang ingin di-backup, kemudian klik Export di menu bagian atas.
Ada 2 metode untuk melakukan backup, yaitu:
- Quick – display only the minimal option - Custom – display all possible option *Jika memilih Custom, anda dapat melakukan pengaturan mengenai penyimpanan backup file tersebut.
3. Kemudian tentukan format dari file database yang akan di-backup. 4. Klik Go yang ada dibagian bawah, pada jendela baru, pilih Save File dan klik Ok.
Upload / Restore / Import Database melalui phpMyAdmin
1. Klik Import lalu klik Browse untuk menentukan database mana yang akan di-upload. 2. Tentukan format file database yang diinginkan kemudian klik Go di bagian bawah halaman.
PS: Setiap akun hosting memiliki batasan jumlah database MySQL yang boleh dibuat, disesuaikan dengan paket hosting yang dipilih. Lihat kembali jumlah database yang anda miliki jika suatu saat gagal membuat database baru.
Pindah Directory MySQL (Untuk client VDS)
Secara default, direktori untuk tempat penyimpanan data MySQL adalah /var/lib/mysql Misal, anda ingin memindahkan lokasi file tersebut ke direktori /db/mysql, maka: - Stop servis MySQL: /etc/init.d/mysql stop - Ubah kepemilikan file ke user mysql: chown –R mysql:mysql /db/mysql - Copy semua file yang ada di direktori /var/lib/mysql ke /db/mysql: cp –R –p /var/lib/mysql /db/mysql - Edit my.cnf : o Datadir = /var/lib/mysql o Socket = /var/lib/mysql.sock Menjadi o Datadir = /db/mysql o Socket = /db/mysql/mysql.sock
Kemudian tambahkan baris berikut, [client] Socket = /db/mysql/mysql.sock - Restart servis MySQL: /etc/init.d/mysql restart Lakukan pengetesan, misal, dengan membuat database baru kemudian cek apakah data tersebut sudah tersimpan di direktori yang baru.
Splitting Huge MySQL Dump For Easy Restore
The open source RDBMS MySQL provides a great tool mysqdump to dump database. Its the official sql dump utitlity for MySQL database. It makes the life of dba so easy that he can backup and restore the database within just two commands. But sometimes due to the lacking of the infrastructure you can not dump and restore that easily. Specially when you are dealing with huge amount of data. Our database grows over time. Few hundred GBs are quite common. But if you run a software for long it might get in to Tera byte range. The problem starts when size is this huge.
Backing up and restoring such huge database is not easy. It takes a lot of time to both backup and restore. Not to mention, if something breaks up the operation the output will most time corrupted. For backup operation the output is a dump file. This will be corrupted if backup is interrupted. Just think if it breaks at 99%. It feels like pulling your hair! Think if it was a restore operation. Your database will be corrupted. Usually we do restore on production server. If the restore fails, that’ll be a disaster.
To come round this, Its better to backup and restore part by part. This way your database will less likely be corrupted. Here is some ways to achieve this,
1. Split the database by tables. When backing up, split the backup operation by tables.
Just do some balance on the table size and then backup on each table. For example, If you have 3 tables 10 GB each and 2 tables 30 GB each then you can backup tables in 3 groups.
Group 1. All the 3 tables 10 GBs each totaling 30 GB mysqldump database1 table1 table2 table3 > table1-3.sql
Group 2. One of the two 30 GB tables. mysqldump database1 table4 > table4.sql
Group 3. One of the two 30 GB tables. mysqldump database1 table5 > table5.sql
2. Split the big tables by rows. Not all tables in the database are big. Rather we always have some tables which are big enough to cause headache for the DBA. Think about the tweeter. Assuming a simple database schema, we can say all tables cumulatively not bigger than the table that holds the tweets. In our previous example table4 and table5 is bigger. For such big tables we can split the dump by rows. mysqldump provides a great option called “–where”. By using this option you can split your table on any condition which is supported by “where” sql clause. So the number of ways you can split the table is infinite. Here is some strategies,
A. auto_increment. If your table has auto column you can split by any number of chunks. See the examples.
* Split the odd and even rows mysqldump --where "id%2=0" database1 table4 > table4_even.sql mysqldump --where "id%2=1" database1 table4 > table4_odd.sql
* Using any number mysqldump --where "id%4=0" database1 table5 > table5_0.sql mysqldump --where "id%4=1" database1 table5 > table5_1.sql mysqldump --where "id%4=2" database1 table5 > table5_2.sql mysqldump --where "id%4=3" database1 table5 > table5_3.sql
B. limit clause. Even you are using where clause, with a little hack you can use limit clause. This allows true partition of the tables. For example if you have 18000 rows in a table you can dump in two parts like this,
mysqldump --where "1 LIMIT 0, 10000" database1 table5 > table5_part_1.sql mysqldump --where "1 LIMIT 10000, 10000" database1 table5 > table5_part_2.sql
C. Others. As the –where switch allows any sql condition you can use any criteria. You can split a user table by sex (male and female), by age group, by any enum column you are using, by date span etc. Some examples,
mysqldump --where "sex='M'" database1 user > user_m.sql mysqldump --where "sex='F'" database1 user > user_f.sql mysqldump --where "account='ACTIVE'" database1 user > user_active.sql mysqldump --where "account='INACTIVE'" database1 user > user_inactive.sql mysqldump --where "year(date) <= 2008" database1 payment > payment_prior_2009.sql mysqldump --where "year(date) > 2008" database1 payment > payment_2009_and_after.sql
[Source : http://shiplu.mokadd.im/110/splitting-huge-mysql-dump-for-easy-restore/
]
This category currently contains no pages or media.