22 tháng 1 2013

Sử dụng mysqldump hiệu quả hơn



Với mysqldump ta có thể dễ dàng backup lại data rồi restore với mysqlimport hay thậm chí dùng chính mysqldump cũng có thể import vào lại!

mysqldum có thể dump toàn bộ csdl, hay một vài bảng, hay một số row trong bảng

Đây là một vài cách dùng




$ mysqldump -h localhost -p341987 -u root wanhak >  filedata.sql


Trong đó:
h là để chỉ định hostname để mysqldump kết nối tới
-u là để chỉ định username để mysqldump kết nối tới
-p là để chỉ định pasword để mysqldump kết nối tới
Một điều cần chú ý với tham số này đó là mật khẩu đi liền theo sau -p, ko viết cách ra!
Nếu muốn cách ra thì gõ –pasword 341987
wanhak là tên cơ sở dữ liệu
Dùng > để xuất sql command vào file filedata.sql

Để restore vào lại:


$ mysql -h localhost -u root -p341987 wanhak < filedata.sql


Ngoài ra có thể cần dùng thêm –add-drop-table để thêm lệnh drop table trước mỗi câu lệnh tạo bảng



$ mysql --add-drop-table -h localhost -u root -p341987 wanhak < filedata.sql


Lúc đó, kết qủa sẽ có thêm lệnh DROP TABLE :


DROP TABLE IF EXISTS `actions`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `actions` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`action_type_id` int(10) unsigned NOT NULL,
`user_id` int(10) unsigned NOT NULL,
`object_id` int(11) DEFAULT NULL,
`acted_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;



Nếu chỉ muốn export vài bảng trong database thì dùng tên bảng sau tên csdl


$ mysqldump -h localhost -p341987 -u root wanhak users user_metas > filedata.sql


Trong đó users, users_metas là các bảng bên trong csdl filedata



mysql> show tables;
| user_metas |
| users |



Ngoai ra, có thể kết hợp thêm gzip để nén file

$ mysqldump -h localhost -p341987 -u root 
wanhak users user_metas > /home/kurei/www/MAM/trunk/pocoo.sql && gzip filedata.sql


You can try find many cool example how to use it here http://dev.mysql.com/doc/refman/5.1/en/mysqldump.html
Good luck!

0 nhận xét:

Đăng nhận xét