Thứ Bảy, 30 tháng 6, 2012

About exporting/importing MySQL database via SSH

I used to use phpMyAdmin to do everything related to MySQL. However, now I am coding the website Romajidesu, which contains large dabase of Japanese dictionary with example sentences, importing/Exporting MySQL data using only phpMyAdmin alone seems to be very slow if not impossible. So here's the ways to do it (via SSH).


Step 1. Export A MySQL Database

It is a good idea to export your data as often as possible for backup or for moving data from localhost to server.
From SSH or command shells, execute the following command:
mysqldump -u username -p database_name > dbname.sql 
You will be prompted for a password, type in the password for the username and press Enter. Replace username, password and database_name with your MySQL username, password and database name.
File dbname.sql now holds a backup of your database and is ready for download to your computer or upload to server. If you just want to backup a specific table, use this command:
mysqldump -u username -p database_name  table_name > dbname.sql 
One more thing you can do is gzip the file to save the upload/download time, I find gzipping reduces the file size  6 times in my case. From SSH, execute the following command:
gzip dbname.sql 
The final file will has the form dbname.sql.gz

Step 2. Import A MySQL Database 

In case you used gzipped file, you need to ungzip in as the importing file must be in .sql format.  From SSH, navigate to the directory where your .gz file is and execute the following command:
 gunzip dbname.sql.gz 
When you have your .sql file, run this command: 
 mysql -p -u username database_name < file.sql 

2 nhận xét:

  1. Bác cũng chịu khó mày mò nhể? Còn có món bigdump cũng khá hữu ích nếu host không hỗ trợ SSH. Hehe

    Trả lờiXóa