# mysqldump - 一个mysql客户端的备份程序
mysqldump是一个客户端的备份程序,他可以备份数据库,或者将数据库传输到另外一个服务器。
# 适用范围
RedHat
RHEL
Ubuntu
CentOS
Debian
Deepin
SUSE
openSUSE
Fedora
Linux Mint
Alpine Linux
Arch Linux
# 语法
mysqldump [options] [db_name [tbl_name ...]]
# 选项
-?, --help # 显示帮助信息
--add-drop-database # 在创建数据库之前,增加删除数据库语句
--add-drop-table # 在创建表之前,增加删除表语句
--add-locks # 在输出insert语句的时候增加锁表语句
-A, --all-database # 备份所有数据库的所有表
--allow-keywords # 允许列名使用关键字
--bind-address=ip # 绑定ip
--character-set-dir # 默认的字符集目录
-i, --comments # 在备份的时候添加注释
-c, --complete-insert # 使用完整的insert语句
-C, --compress # 在服务器和客户端之间使用压缩语句
-B, --database # 指定要备份的数据库
--dump-date # 如果使用了—comments选项,那么就可以追加日期
-F, --flush-logs # 刷新日志
--flush-privileges # 刷新权限
-f, --force # 强制执行
-ignore-table # 备份的时候忽略表
--lock-all-tables| -x # 备份的时候锁定所有的数据库
-l, --lock-tables # 锁定指定的表
--log-error # 错误日志
-t, --no-create-db # 备份数据库的时候,不输出创建表语句
-d, --no-data # 备份数据库的时候,只备份数据结构,不备份数据
-P, --port # 指定端口
--protocol # 指定协议
-q, --quick # 静默模式
-v, --verbose # 显示详细过程
-V, --version # 显示版本信息
-u, --user # 指定用户
-p, --password # 指定密码
# 举例
备份指定的数据库中的指定表
[root@localhost ~]$ mysqldump -v -u root -p wordpress wp_links #默认情况下,备份内容到标准输出
Enter password:
-- Connecting to localhost...
-- MySQL dump 10.13 Distrib 5.1.71, for redhat-linux-gnu (i386)
--
-- Host: localhost Database: wordpress
-- ------------------------------------------------------
-- Server version 5.1.71
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
-- Retrieving table structure for table wp_links...
--
-- Table structure for table `wp_links`
--
DROP TABLE IF EXISTS `wp_links`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `wp_links` (
`link_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`link_url` varchar(255) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
`link_name` varchar(255) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
`link_image` varchar(255) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
`link_target` varchar(25) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
`link_description` varchar(255) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
`link_visible` varchar(20) COLLATE utf8_unicode_ci NOT NULL DEFAULT 'Y',
`link_owner` bigint(20) unsigned NOT NULL DEFAULT '1',
`link_rating` int(11) NOT NULL DEFAULT '0',
`link_updated` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`link_rel` varchar(255) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
`link_notes` mediumtext COLLATE utf8_unicode_ci NOT NULL,
`link_rss` varchar(255) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
PRIMARY KEY (`link_id`),
KEY `link_visible` (`link_visible`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
-- Sending SELECT query...
--
-- Dumping data for table `wp_links`
--
-- Retrieving rows...
LOCK TABLES `wp_links` WRITE;
/*!40000 ALTER TABLE `wp_links` DISABLE KEYS */;
/*!40000 ALTER TABLE `wp_links` ENABLE KEYS */;
UNLOCK TABLES;
-- Disconnecting from localhost...
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
-- Dump completed on 2018-10-05 20:34:17
You have new mail in /var/spool/mail/root
查看活动线程
[root@localhost ~]$ mysqldump -v -u root -p wordpress wp_links > sql.bak #使用重定向功能,备份到sql.bak
Enter password:
-- Connecting to localhost...
-- Retrieving table structure for table wp_links...
-- Sending SELECT query...
-- Retrieving rows...
-- Disconnecting from localhost...
You have new mail in /var/spool/mail/root
[root@localhost ~]$ cat sql.bak #查看备份内容
-- MySQL dump 10.13 Distrib 5.1.71, for redhat-linux-gnu (i386)
--
-- Host: localhost Database: wordpress
-- ------------------------------------------------------
-- Server version 5.1.71
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
--
-- Table structure for table `wp_links`
--
DROP TABLE IF EXISTS `wp_links`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `wp_links` (
`link_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`link_url` varchar(255) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
`link_name` varchar(255) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
`link_image` varchar(255) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
`link_target` varchar(25) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
`link_description` varchar(255) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
`link_visible` varchar(20) COLLATE utf8_unicode_ci NOT NULL DEFAULT 'Y',
`link_owner` bigint(20) unsigned NOT NULL DEFAULT '1',
`link_rating` int(11) NOT NULL DEFAULT '0',
`link_updated` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`link_rel` varchar(255) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
`link_notes` mediumtext COLLATE utf8_unicode_ci NOT NULL,
`link_rss` varchar(255) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
PRIMARY KEY (`link_id`),
KEY `link_visible` (`link_visible`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Dumping data for table `wp_links`
--
LOCK TABLES `wp_links` WRITE;
/*!40000 ALTER TABLE `wp_links` DISABLE KEYS */;
/*!40000 ALTER TABLE `wp_links` ENABLE KEYS */;
UNLOCK TABLES;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
-- Dump completed on 2018-10-05 20:36:27