Thứ Năm, 14 tháng 11, 2013

How to Back Up and Restore a MySQL Database - Cách lưu trữ và phục hồi cơ sở dữ liệu MySQL

Nếu bạn đang lưu trữ bất kỳ thứ gì bạn không muốn bị mất trong các cơ sở dữ liệu MySQL, nó rất quan trọng để sao lưu dữ liệu thường xuyên để bảo vệ nó khỏi bị mất. Hướng dẫn sau đây sẽ chỉ cho bạn 2 cách dẽ dàng để sao lưu và phục hồi dữ liệu trong MySQL. Bạn có thể sử dụng quá trình này để chuyenr dữ liệu lên 1 trang web mới.

[​IMG]

Sao lưu dữ liệu từ dòng lệnh ( sử dụng mysqldump)
Nếu bạn có kết nối shell hoặc telnet tới trang web của bạn, bạn có thể sao lưu dữ liệu MySQL bằng việc sử dụng lệnh mysqldump. Lệnh này kết nối với server MySQL và tạo ra 1 file rác SQL. File này chứa các câu lệnh SQL cần thiết tể tạo lại cơ sở dữ liệu. Đây là cú pháp thích hợp:
Mã PHP:
$ mysqldump --opt -u [uname] -p[pass] [dbname] > [backupfile.sql] 


[uname] Tển người dùng cơ sở dữ liệu
[pass] Mật khẩu cho cơ sở dữ liệu của bạn(lưu ý không sử dụng khoảng trống giữa -p and mật khẩu)
[dbname] Tên của cơ sở dữ liệu
[backupfile.sql] Tên của file sao lưu
[--opt] Tùy chọn mysqldump
Chẳng hạn, để sao lưu 1 cơ sở dữ liệu có tên ‘Tutorials’ với tên người sử dụng là ‘root’ và không có mật khẩu file tut_backup.sql, bạn nên thực hiện lệnh: 
Mã PHP:
$ mysqldump -u root -p Tutorials > tut_backup.sql 


Lệnh này sẽ sao lưu cơ sở dữ liệu ‘Tutorials’ vào trong 1 file được gọi là tut_backup.sql chứa tất cả các câu lệnh SQlL cần thiết để tạo lại cơ sở dữ liệu. Với lện mysqldump, bạn có thể xác định các bảng nhất định của cơ sở dữ liệu bạn muốn sao lưu. Chẳng hạn, để chỉ sao lưu php_tutorials và các bảng asp_tutorials từ cơ sở dữ liệu ‘Tutorials’, thực hiện lệnh dưới đây. Tên mỗi bảng được tách biệt bởi dấu cách. 
Mã PHP:
$ mysqldump -u root -p Tutorials php_tutorials asp_tutorials > tut_backup.sql 


Đôi khi, cần sao lưu nhiều hơn 1 cơ sở dữ liệu cùng 1 lúc. Trong trường hợp này, bạn có thể sử dụng tùy chọn theo bởi 1 danh sách các cơ sở dữ liệu mà bạn muốn sao lưu. Tên mỗi cơ sở dữ liệu được phân biệt bởi dấu cách.
Mã PHP:
$ mysqldump -u root -p --databases Tutorials Articles Comments > content_backup.sql 


Nếu bạn muốn sao lưu tất cả các cơ sở dữ liệu trong hệ thống cùng 1 lúc, bạn nên sử dụng tùy chọn “all databases”. MySQL sẽ sao lưu tất cả các cơ sở dữ liệu được lưu trữ.
Mã PHP:
$ mysqldump -u root -p --all-databases > alldb_backup.sql 


Lênh mysqldump cũng có thêm nhiều tùy chọn hữu ích khác:
--add-drop-table: MySQL thêm câu lệnh DROP TABLE trước khi tạo bảng
--no-data: Sao lưu cấu trức cơ sở dữ liệu, không gồm nội dung
--add-locks: Thêm câu lệnh LOCK TABLES và UNLOCK TABLES bạn có thể gặp ở file sao lưu

Lệnh mysqldump có điểm mạnh và điểm yếu. Điểm mạnh của việc sử dụng mysqldump là nó đơn giản để sử dụng và chăm sóc các mục trong bảng cho bạn. Điểm yếu là lệnh này khóa các bảng dữ liệu. Nếu kích thước bảng rất lớn, mysqldump cần 1 thời gian dài để mở khóa dữ liệu. Sao lưu và nén cơ sở dữ liệu MySQL 
Nếu cơ sở dữ liệu của bạn quá lớn, bạn có thể muốn nén file sao lưu. Chỉ với việc sử dụng lệnh sao lưu mysql ở dưới và chuyển file tới gzip, bạn sẽ tao được file sao lưu như 1 file nén gzip.
Mã PHP:
$ mysqldump -u [uname] -p[pass] [dbname] | gzip -9 > [backupfile.sql.gz] 


Nếu bạn muốn extract file .gz, sử dụng lệnh sau:
Mã PHP:
$ gunzip [backupfile.sql.gz] 


Phục hồi cơ sở dữ liệu MySQL 
Trên đây, chúng ta đã sao lưu cơ sở dữ liệu Tutorials thành file tut_backup.sql. Để tạo lại 1 cơ sở dữ liệu Tutorials, bạn theo 2 bước sau:
  • Tạo tên cơ sở dữ liệu phù hợp
  • Khởi chạy file này sử dụng lệnh mysql sau:

Mã PHP:
$ mysql -u [uname] -p[pass] [db_to_restore] < [backupfile.sql] 


Xem cách bạn phục hồi file tut_backup.sql file đến cơ sở dữ liệu Tutorials .
Mã PHP:
$ mysql -u root -p Tutorials < tut_backup.sql 


Phục hồi file sao lưu được nén, bạn có thể dùng lệnh sau:
Mã PHP:
gunzip < [backupfile.sql.gz] | mysql -u [uname] -p[pass] [dbname] 


Nếu bạn cần phục hồi cơ sở dữ liệu đã tồn tại, bạn sẽ cần sử dụng lệnh mysqlimport. Cú pháp như sau:
Mã PHP:
mysqlimport -u [uname] -p[pass] [dbname] [backupfile.sql] 


Sao lưu và phục hồi cơ sở dữ liệu sử dụng PHPMyAdmin:
Chắc chắn rằng bạn đã có phpMyAdmin được cài đặt vì rất nhiều nhà cung cấp dịch vụ web sử dụng nó. Để sao lưu cơ sở dũ liệu MySQL sử dụng PHPMyAdmin, theo các bước sau:
• Mở phpMyAdmin.
• Lựu chọn cơ sở dữ liệu bằng việc click vào tên CSDL ở danh sách bên trái màn hình.
• Click link Export. Việc này sẽ mở của sổ mới View dump of database (hoặc tương tự).
• Trong vùng Export, click link Select All để chọn toàn bộ các bảng trong CSDL.
• Trong vùng SQL options, click các tùy chọn bên phải.
• Click Save and tùy chọn nén file theo sau and sau đó click 'Go'. 1 hôp thoại xuất hiện nhắc bạn lưu file.

Phục hồi CSDL cũng dễ dàng như sao lưu nó. Làm theo các bươc sau:
• Mở phpMyAdmin.
• TẠo 1 CSDL có tên thích hợp và chọn nó bằng việc click tên CSDL trong danh sách bên trái màn hình.Nếu bạn muốn sử dụng file sao lưu phục hồi lại CSDL đang tồn tại, click tên CSDL, chọn tất cả các ô cạnh các tên bảng và chọn Drop để xóa các bảng đang tồn tại trong CSDL.
• Click link SQL. Việc này sẽ mở 1 của sổ mới nơi bạn có thể gõ lệnh SQL hoặc đang tải file SQL.
• Sử dung nút browse để tìm kiếm file CSDL.
• Click nút Go. Việc nãy sẽ tạo file sao lưu, thực hiện các lệnh SQL và tạo lại CSDL của bạn.

Dịch bởi: jodanbattlefield - www.shoptinhoc.com/

***************************************

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.

[​IMG]

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: 

PHP:
mysqldump --opt -[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:

PHP:
 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.

PHP:
 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.

PHP:
 mysqldump -u root ---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.

PHP:
 mysqldump -u root ---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.

PHP:
 mysqldump -[uname] -p[pass] [dbname] | gzip -> [backupfile.sql.gz]
If you want to extract the .gz file, use the command below:

PHP:
 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:
PHP:
 mysql -[uname] -p[pass] [db_to_restore] < [backupfile.sql]
Have a look how you can restore your tut_backup.sql file to the Tutorials database.
PHP:
 mysql -u root -p Tutorials tut_backup.sql
To restore compressed backup files you can do the following:
PHP:
 gunzip < [backupfile.sql.gz] | mysql -[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:

PHP:
 mysqlimport -[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.
(Nguồn: How to Back Up and Restore a MySQL Database)

0 nhận xét:

Đăng nhận xét