如何在MySQL數據庫中備份單個表?

本文翻譯自:How to take backup of a single table in a MySQL database?

By default, mysqldump takes the backup of an entire database. 默認情況下, mysqldump會備份整個數據庫。 I need to backup a single table in MySQL. 我需要在MySQL中備份一個表。 Is it possible? 可能嗎? How do I restore it? 我該如何恢復它?


#1樓

參考:https://stackoom.com/question/S2Wy/如何在MySQL數據庫中備份單個表


#2樓

您可以使用MYSQLWorkbench tool輕鬆地轉儲選定的表,在一個轉儲中單獨或一組表,然後按如下方式導入:如果您在本地運行它,您可以通過添加-h IP.ADDRESS.NUMBER添加主機信息 -u用戶名

mysql -u root -p databasename < dumpfileFOurTableInOneDump.sql 

#3樓

try 嘗試

for line in $(mysql -u... -p... -AN -e "show tables from NameDataBase");
do 
mysqldump -u... -p.... NameDataBase $line > $line.sql ; 
done
  • $line cotent names tables ;) $ line強效名稱表;)

#4樓

We can take a mysql dump of any particular table with any given condition like below 我們可以使用任何給定條件對任何特定表進行mysql轉儲,如下所示

mysqldump -uusername -p -hhost databasename tablename --skip-lock-tables

If we want to add a specific where condition on table then we can use the following command 如果我們想在表上添加特定的where條件,那麼我們可以使用以下命令

mysqldump -uusername -p -hhost databasename tablename --where="date=20140501" --skip-lock-tables

#5樓

You can use this code: 您可以使用此代碼:

This example takes a backup of sugarcrm database and dumps the output to sugarcrm.sql 此示例獲取sugarcrm數據庫的備份並將輸出轉儲到sugarcrm.sql

# mysqldump -u root -ptmppassword sugarcrm > sugarcrm.sql

# mysqldump -u root -p[root_password] [database_name] > dumpfilename.sql

The sugarcrm.sql will contain drop table, create table and insert command for all the tables in the sugarcrm database. sugarcrm.sql將包含sugarcrm數據庫中所有表的drop table,create table和insert命令。 Following is a partial output of sugarcrm.sql, showing the dump information of accounts_contacts table: 以下是sugarcrm.sql的部分輸出,顯示了accounts_contacts表的轉儲信息:

-- -

-- Table structure for table accounts_contacts - 表accounts_contacts表結構

DROP TABLE IF EXISTS `accounts_contacts`;
SET @saved_cs_client = @@character_set_client;
SET character_set_client = utf8;
CREATE TABLE `accounts_contacts` (
`id` varchar(36) NOT NULL,
`contact_id` varchar(36) default NULL,
`account_id` varchar(36) default NULL,
`date_modified` datetime default NULL,
`deleted` tinyint(1) NOT NULL default '0',
PRIMARY KEY (`id`),
KEY `idx_account_contact` (`account_id`,`contact_id`),
KEY `idx_contid_del_accid` (`contact_id`,`deleted`,`account_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
SET character_set_client = @saved_cs_client;

--

#6樓

You can use the below code: 您可以使用以下代碼:

  1. For Single Table Structure alone Backup 僅適用於單表結構備份

- -

mysqldump -d <database name> <tablename> > <filename.sql>
  1. For Single Table Structure with data 對於帶有數據的單表結構

- -

mysqldump <database name> <tablename> > <filename.sql>

Hope it will help. 希望它會有所幫助。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章