Mysql的備份與恢復
1.備份cancer資料庫
```
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| cancer |
| cancer_utf8 |
| performance_schema |
| test |
+--------------------+
6 rows in set (0.05 sec)
```
2.備份的方向符號為(>)
```
[root@mysql 3306]# mysqldump -uroot -p -S /data/3306/mysql.sock cancer >/opt/cancer_bak.sql
Enter password:
```
3.檢視備份的語句
```
[root@mysql 3306]# egrep -v "#|\*|--|^$" /opt/cancer_bak.sql
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student` (
`id` int(4) NOT NULL AUTO_INCREMENT,
`names` char(20) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1;
LOCK TABLES `student` WRITE;
INSERT INTO `student` VALUES (1,'???'),(2,'è???”·?-?');
UNLOCK TABLES;
```
4.備份恢復的方向符號為(<)
```
[root@mysql 3306]# mysql -uroot -p -S /data/3306/mysql.sock cancer </opt/cancer_bak.sql
```
加-B的備份與恢復
1.備份
```
[root@mysql 3306]# mysqldump -uroot -p -S /data/3306/mysql.sock -B cancer >/opt/cancer_bak_B.sql
Enter password:
[root@mysql 3306]# cd /opt
[root@mysql opt]# diff cancer_bak.sql cancer_bak_B.sql
18a19,26
> -- Current Database: `cancer`
> --
>
> CREATE DATABASE /*!32312 IF NOT EXISTS*/ `cancer` /*!40100 DEFAULT CHARACTER SET latin1 */;
>
> USE `cancer`;
>
> --
51c59
< -- Dump completed on 2015-08-16 16:16:33
---
> -- Dump completed on 2015-08-16 16:22:32
```
2.恢復
```
[root@mysql opt]# mysql -uroot -p -S /data/3306/mysql.sock </opt/cancer_bak_B.sql
```
3.壓縮備份
```
[root@mysql opt]# mysqldump -uroot -p -S /data/3306/mysql.sock -B cancer|gzip >/opt/cancer_bak_gzip.sql.gz
Enter password:
```
4.備份多個庫
```
[root@mysql opt]# mysqldump -uroot -p -S /data/3306/mysql.sock -B cancer cancer_utf8 >/opt/cancer_mul_bak
Enter password:
```
5.多庫備份指令碼
```
[root@mysql opt]# mysql -uroot -p -S /data/3306/mysql.sock -e "show databases;"|grep -Evi "database|infor|perfor"|sed -r 's#^([a-z].*$)#mysqldump -uroot -pcancer -S /data/3306/mysql.sock -B --events \1|gzip >/opt/\1.sql.gz#g'|bash
Enter password:
```
指令碼的顯示內容
```
mysqldump -uroot -pcancer -S /data/3306/mysql.sock -B mysql|gzip >/opt/mysql.sql.gz
mysqldump -uroot -pcancer -S /data/3306/mysql.sock -B cancer|gzip >/opt/cancer.sql.gz
mysqldump -uroot -pcancer -S /data/3306/mysql.sock -B cancer_utf8|gzip >/opt/cancer_utf8.sql.gz
mysqldump -uroot -pcancer -S /data/3306/mysql.sock -B test|gzip >/opt/test.sql.gz
```
1.備份具體某個庫中的某個表
```
[root@mysql ~]# mysqldump -uroot -p -S /data/3306/mysql.sock cancer student test
Enter password:
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `student` (
`id` int(4) NOT NULL AUTO_INCREMENT,
`names` char(20) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = @saved_cs_client */;
INSERT INTO `student` VALUES (1,'???'),(2,'è???”·?-?');
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `test` (
`id` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = @saved_cs_client */;
```
2.只備份表結構
```
[root@mysql ~]# mysqldump -uroot -p -S /data/3306/mysql.sock -d cancer
Enter password:
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `student` (
`id` int(4) NOT NULL AUTO_INCREMENT,
`names` char(20) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = @saved_cs_client */;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `test` (
`id` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = @saved_cs_client */;
```
```
[root@mysql ~]# mysqldump -uroot -p -S /data/3306/mysql.sock -d cancer student
Enter password:
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `student` (
`id` int(4) NOT NULL AUTO_INCREMENT,
`names` char(20) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = @saved_cs_client */;
```
3.只備份資料
```
[root@mysql ~]# mysqldump -uroot -p -S /data/3306/mysql.sock -t cancer student
Enter password:
INSERT INTO `student` VALUES (1,'???'),(2,'è???”·?-?');
```
```
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| cancer |
| cancer_utf8 |
| performance_schema |
| test |
+--------------------+
6 rows in set (0.05 sec)
```
2.備份的方向符號為(>)
```
[root@mysql 3306]# mysqldump -uroot -p -S /data/3306/mysql.sock cancer >/opt/cancer_bak.sql
Enter password:
```
3.檢視備份的語句
```
[root@mysql 3306]# egrep -v "#|\*|--|^$" /opt/cancer_bak.sql
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student` (
`id` int(4) NOT NULL AUTO_INCREMENT,
`names` char(20) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1;
LOCK TABLES `student` WRITE;
INSERT INTO `student` VALUES (1,'???'),(2,'è???”·?-?');
UNLOCK TABLES;
```
4.備份恢復的方向符號為(<)
```
[root@mysql 3306]# mysql -uroot -p -S /data/3306/mysql.sock cancer </opt/cancer_bak.sql
```
加-B的備份與恢復
1.備份
```
[root@mysql 3306]# mysqldump -uroot -p -S /data/3306/mysql.sock -B cancer >/opt/cancer_bak_B.sql
Enter password:
[root@mysql 3306]# cd /opt
[root@mysql opt]# diff cancer_bak.sql cancer_bak_B.sql
18a19,26
> -- Current Database: `cancer`
> --
>
> CREATE DATABASE /*!32312 IF NOT EXISTS*/ `cancer` /*!40100 DEFAULT CHARACTER SET latin1 */;
>
> USE `cancer`;
>
> --
51c59
< -- Dump completed on 2015-08-16 16:16:33
---
> -- Dump completed on 2015-08-16 16:22:32
```
2.恢復
```
[root@mysql opt]# mysql -uroot -p -S /data/3306/mysql.sock </opt/cancer_bak_B.sql
```
3.壓縮備份
```
[root@mysql opt]# mysqldump -uroot -p -S /data/3306/mysql.sock -B cancer|gzip >/opt/cancer_bak_gzip.sql.gz
Enter password:
```
4.備份多個庫
```
[root@mysql opt]# mysqldump -uroot -p -S /data/3306/mysql.sock -B cancer cancer_utf8 >/opt/cancer_mul_bak
Enter password:
```
5.多庫備份指令碼
```
[root@mysql opt]# mysql -uroot -p -S /data/3306/mysql.sock -e "show databases;"|grep -Evi "database|infor|perfor"|sed -r 's#^([a-z].*$)#mysqldump -uroot -pcancer -S /data/3306/mysql.sock -B --events \1|gzip >/opt/\1.sql.gz#g'|bash
Enter password:
```
指令碼的顯示內容
```
mysqldump -uroot -pcancer -S /data/3306/mysql.sock -B mysql|gzip >/opt/mysql.sql.gz
mysqldump -uroot -pcancer -S /data/3306/mysql.sock -B cancer|gzip >/opt/cancer.sql.gz
mysqldump -uroot -pcancer -S /data/3306/mysql.sock -B cancer_utf8|gzip >/opt/cancer_utf8.sql.gz
mysqldump -uroot -pcancer -S /data/3306/mysql.sock -B test|gzip >/opt/test.sql.gz
```
1.備份具體某個庫中的某個表
```
[root@mysql ~]# mysqldump -uroot -p -S /data/3306/mysql.sock cancer student test
Enter password:
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `student` (
`id` int(4) NOT NULL AUTO_INCREMENT,
`names` char(20) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = @saved_cs_client */;
INSERT INTO `student` VALUES (1,'???'),(2,'è???”·?-?');
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `test` (
`id` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = @saved_cs_client */;
```
2.只備份表結構
```
[root@mysql ~]# mysqldump -uroot -p -S /data/3306/mysql.sock -d cancer
Enter password:
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `student` (
`id` int(4) NOT NULL AUTO_INCREMENT,
`names` char(20) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = @saved_cs_client */;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `test` (
`id` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = @saved_cs_client */;
```
```
[root@mysql ~]# mysqldump -uroot -p -S /data/3306/mysql.sock -d cancer student
Enter password:
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `student` (
`id` int(4) NOT NULL AUTO_INCREMENT,
`names` char(20) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = @saved_cs_client */;
```
3.只備份資料
```
[root@mysql ~]# mysqldump -uroot -p -S /data/3306/mysql.sock -t cancer student
Enter password:
INSERT INTO `student` VALUES (1,'???'),(2,'è???”·?-?');
```
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29812844/viewspace-1988769/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- MySQL備份與恢復MySql
- MySQL 備份與恢復MySql
- MySQL備份與恢復——基於Xtrabackup物理備份恢復MySql
- Mysql備份與恢復(1)---物理備份MySql
- Mysql 備份與恢復 二MySql
- MySQL 非常規恢復與物理備份恢復MySql
- Mysql備份與恢復(2)---邏輯備份MySql
- Mysql的幾種備份與恢復MySql
- 入門MySQL——備份與恢復MySql
- 《入門MySQL—備份與恢復》MySql
- MySQL備份與恢復操作解析MySql
- Mysql資料備份與恢復MySql
- MySQL備份與恢復——實操MySql
- MySQL備份與恢復簡介MySql
- Effective MySQL之備份與恢復MySql
- Mysql備份恢復MySql
- mysql 備份恢復MySql
- MySQL備份與恢復——基於MyDumper/MyLoader 邏輯備份恢復MySql
- mysql的資料庫備份與恢復MySql資料庫
- (連結)MySQL Cluster的備份與恢復MySql
- 備份與恢復--利用備份的控制檔案恢復
- MySQL入門--備份與恢復(一)MySql
- MySQL入門--備份與恢復(二)MySql
- MySQL入門--備份與恢復(三)MySql
- MySQL增量備份與恢復例項MySql
- mysql簇備份與恢復(轉載)MySql
- MySQL 日誌管理、備份與恢復MySql
- MySQL備份與恢復——基於OUTFILE /LOAD DATA 邏輯備份恢復MySql
- 備份與恢復系列 十一 控制檔案的備份與恢復
- 【MySQL】MySQL備份和恢復MySql
- Mysql備份和恢復MySql
- 備份與恢復:polardb資料庫備份與恢復資料庫
- 【Mysql】xbackup全量與增量備份恢復MySql
- 備份與恢復系列 十 引數檔案spfile的備份與恢復
- Postgresql 備份與恢復SQL
- mysql innobackupex增量備份恢復MySql
- MySql邏輯備份恢復MySql
- mysql備份恢復測試MySql