合 mysql-utilities工具之mysqldbexport和mysqlimport
Tags: MySQLmysql-utilitiesmysqldbexportmysqlimport
mysqldbexport
mysqldbexport 从一个或多个数据库导出对象定义的元数据和数据。默认情况下,仅仅导出对象的定义。与mysqldump类似,但区别也大。mysqldbexport 可以有多个格式,使数据更容易提取和转移。
要排出特定对象名称,可以使用--exclude选项,格式:db.obj 。也可以提供一个搜索模式。如 --exclude=db1.trig1 ,排除单个触发器。--exclude=trig_ 排除以trig_开头的。也可以使用正则表达式。
跳过对象类型,可以使用--skip选项指定对象列表。这在为了提取某个特定对象情况下,非常有用。同样,跳过BLOB数据更新可以指定 --skip-blobs选项。
输出格式有:
- sql (默认)以SQL语句方式输出。对于对象定义,以适当的 CREATE 和 GRANT 语句。 对于数据,以 INSERT 语句。(或者批量插入
--bulk-insert指定该选项。
. - grid网格化输出,类似于mysql命令行客户端输出
- csv逗号分隔格式输出
- tab制表符格式输出
- vertical类似于mysql命令行客户端\G执行的输出
指定要显示多少数据,可以使用 --display 选项:
- brief只显示重建对象的最小列
- full完整显示重建对象的列列表
- names只显示对象名称
注意:对于SQL格式输出,--display选项被忽略。
指定 --no-headers 选项,关闭CSV或tab显示对象的格式头部。
指定--quiet选项,关闭所有反馈信息。
指定 --file-per-table选项,每个表数据单独保存。每个文件的名称以数据库和表名称组成。格式:db1.table_name.csv。
默认情况下,复制操作是使用一致性快照来读取源数据库。要改变锁定模式,可以使用–locking选项来指定锁定类型值。值no-locks关闭锁,lock-all只使用表锁。默认是snapshot。此外,使用WRITE锁,在复制过程中将锁定目标表。
从主或者从服务器导数据还可以包含复制语句。–rpl选项指定
- master包含 CHANGE MASTER 语句,使目标服务器作为指定--server选项的从。在导数据之前,执行 STOP SLAVE 语句。在完成后执行 CHANGE MASTER 和 START SLAVE语句。slave包含 CHANGE MASTER 语句,使目标服务器成为与–server选项指定的服务器的同一个主服务器的从。只对当前服务器是从有效。
- both同时包含 'master' 和 'slave' 信息的 CHANGE MASTER 语句 ,可以成为当前服务器的新从,也可以成为主。
–repl-user选项指定复制的用户名和密码。如果指定了--rpl-file选项,复制语句信息写入到文件中,而不是输出流。--comment-rpl选项,注释掉CHANGE MASTER 语句。
如果要导出的数据库的服务器上启用了GTIDs(GTID_MODE = ON),如果只导出其中一部分数据库,将会有警告信息产生。这是因为GTID报表生成包括所有数据库的gtids,不仅仅是某个的。
如果有启用GTID,但是使用了–skip-gtid也会收到警告。
如果启用了GTID,最好是导出或导入所有的数据库。
从另一台服务器上导入这些导出的数据,需要确保这些数据以及日志中所有的GTIDs正确记录。
选项
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 | --version show program's version number and exit --help display a help message and exit --license display program's license and exit --server=SERVER connection information for the server in the form: <user>[:<password>]@<host>[:<port>][:<socket>] or <login-path>[:<port>][:<socket>] or <config- path>[<[group]>]. --ssl-ca=SSL_CA The path to a file that contains a list of trusted SSL CAs. --ssl-cert=SSL_CERT The name of the SSL certificate file to use for establishing a secure connection. --ssl-key=SSL_KEY The name of the SSL key file to use for establishing a secure connection. --character-set=CHARSET sets the client character set. The default is retrieved from the server variable 'character_set_client'. -f FORMAT, --format=FORMAT display the output in either sql (default), grid, tab, csv, or vertical format -d DISPLAY, --display=DISPLAY control the number of columns shown: 'brief' = minimal columns for object creation (default), 'full' = all columns, 'names' = only object names (not valid for --format=sql) -e EXPORT, --export=EXPORT control the export of either 'data' = only the table data for the tables in the database list, 'definitions' = export only the definitions for the objects in the database list, or 'both' = export the metadata followed by the data (default: export definitions) -b, --bulk-insert use bulk insert statements for data (default:False) -h, --no-headers do not show column headers (only applies to formats: tab, csv). --skip-blobs do not export blob data. --file-per-table write table data to separate files. Valid only for --export=data or --export=both. -x EXCLUDE, --exclude=EXCLUDE exclude one or more objects from the operation using either a specific name (e.g. db1.t1), a LIKE pattern (e.g. db1.t% or db%.%) or a REGEXP search pattern. To use a REGEXP search pattern for all exclusions, you must also specify the --regexp option. Repeat the --exclude option for multiple exclusions. -a, --all include all databases --skip=SKIP_OBJECTS specify objects to skip in the operation in the form of a comma-separated list (no spaces). Valid values = tables, views, triggers, procedures, functions, events, grants, data, create_db -v, --verbose control how much information is displayed. e.g., -v = verbose, -vv = more verbose, -vvv = debug -q, --quiet turn off all messages for quiet execution. -G, --basic-regexp, --regexp use 'REGEXP' operator to match pattern. Default is to use 'LIKE'. --locking=LOCKING choose the lock type for the operation: no-locks = do not use any table locks, lock-all = use table locks but no transaction and no consistent read, snaphot (default): consistent read using a single transaction. --rpl-user=RPL_USER the user and password for the replication user requirement, in the form: <user>[:<password>] or <login-path>. E.g. rpl:passwd --rpl=RPL_MODE, --replication=RPL_MODE include replication information. Choices: 'master' = include the CHANGE MASTER command using the source server as the master, 'slave' = include the CHANGE MASTER command for the source server's master (only works if the source server is a slave), and 'both' = include 'master' and 'slave' options where applicable. --rpl-file=RPL_FILE, --replication-file=RPL_FILE path and file name to place the replication information generated. Valid on if the --rpl option is specified. --skip-gtid skip creation of GTID_PURGED statements. --comment-rpl place the replication statements in comment statements. Valid only with --rpl option. --skip-fkey-checks skip creation of foreign key disable/enable statements. --multiprocess=MULTIPROCESS use multiprocessing, number of processes to use for concurrent execution. Special values: 0 (number of processes equal to the CPUs detected) and 1 (default - no concurrency). --output-file=OUTPUT_FILE path and file name to store the generated output, by default the standard output (no file). |
必需提供连接参数和赋予要访问对象的适当权限。
从源服务器上导出所有对象,必须要有:SELECT、SHOW VIEW权限,同时还需要有mysql数据库的SELECT权限。实际所需的权限可能会有所不同,以实际情况为准。
对于 --format, --export 和 --display 选项,参数值不区分大小写,但是不能参数值钱不能包含空格。
实例
只导出定义语句
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 | #mysqldbexport --server=instance_3306 --format=sql ttlsa_com --export=definitions # Source on localhost: ... connected. # Exporting metadata from ttlsa_com DROP DATABASE IF EXISTS `ttlsa_com`; CREATE DATABASE `ttlsa_com`; USE `ttlsa_com`; # TABLE: ttlsa_com.bbs_categories CREATE TABLE `bbs_categories` ( `cid` smallint(5) NOT NULL AUTO_INCREMENT, `pid` smallint(5) NOT NULL DEFAULT '0', `cname` varchar(30) DEFAULT NULL COMMENT '分类名称', `content` varchar(255) DEFAULT NULL, `keywords` varchar(255) DEFAULT NULL, `ico` varchar(128) DEFAULT NULL, `master` varchar(100) NOT NULL, `permit` varchar(255) DEFAULT NULL, `listnum` mediumint(8) unsigned DEFAULT '0', `clevel` varchar(25) DEFAULT NULL, `cord` smallint(6) DEFAULT NULL, PRIMARY KEY (`cid`,`pid`) ) ENGINE=MyISAM AUTO_INCREMENT=7 DEFAULT CHARSET=utf8; # TABLE: ttlsa_com.bbs_comments CREATE TABLE `bbs_comments` ( `id` int(11) NOT NULL AUTO_INCREMENT, `fid` int(11) NOT NULL DEFAULT '0', `uid` int(11) NOT NULL DEFAULT '0', `content` text, `replytime` char(10) DEFAULT NULL, PRIMARY KEY (`id`,`fid`,`uid`) ) ENGINE=MyISAM AUTO_INCREMENT=371 DEFAULT CHARSET=utf8; # TABLE: ttlsa_com.bbs_favorites CREATE TABLE `bbs_favorites` ( `id` mediumint(8) unsigned NOT NULL AUTO_INCREMENT, `uid` mediumint(8) unsigned NOT NULL DEFAULT '0', `favorites` mediumint(8) unsigned NOT NULL DEFAULT '0', `content` mediumtext NOT NULL, PRIMARY KEY (`id`,`uid`), KEY `uid` (`uid`) ) ENGINE=MyISAM AUTO_INCREMENT=3 DEFAULT CHARSET=utf8; # TABLE: ttlsa_com.bbs_forums CREATE TABLE `bbs_forums` ( `fid` int(11) NOT NULL AUTO_INCREMENT, `cid` smallint(5) NOT NULL DEFAULT '0', `uid` mediumint(8) NOT NULL DEFAULT '0', `ruid` mediumint(8) DEFAULT NULL, `title` varchar(128) DEFAULT NULL, `keywords` varchar(255) DEFAULT NULL, `content` text, `addtime` int(10) DEFAULT NULL, `updatetime` int(10) DEFAULT NULL, `lastreply` int(10) DEFAULT NULL, `views` int(10) DEFAULT '0', `comments` smallint(8) DEFAULT '0', `favorites` int(10) unsigned DEFAULT '0', `closecomment` tinyint(1) DEFAULT NULL, `is_top` tinyint(1) NOT NULL DEFAULT '0', `is_hidden` tinyint(1) NOT NULL DEFAULT '0', `ord` int(10) unsigned NOT NULL DEFAULT '0', PRIMARY KEY (`fid`,`cid`,`uid`), KEY `updatetime` (`updatetime`), KEY `ord` (`ord`) ) ENGINE=MyISAM AUTO_INCREMENT=94 DEFAULT CHARSET=utf8; # TABLE: ttlsa_com.bbs_links CREATE TABLE `bbs_links` ( `id` smallint(6) NOT NULL AUTO_INCREMENT, `name` varchar(100) DEFAULT NULL, `url` varchar(200) DEFAULT NULL, `logo` varchar(200) DEFAULT NULL, `is_hidden` tinyint(1) NOT NULL DEFAULT '0', PRIMARY KEY (`id`) ) ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=utf8; # TABLE: ttlsa_com.bbs_notifications CREATE TABLE `bbs_notifications` ( `nid` int(11) NOT NULL AUTO_INCREMENT, `fid` int(11) DEFAULT NULL, `suid` int(11) DEFAULT NULL, `nuid` int(11) NOT NULL DEFAULT '0', `ntype` tinyint(1) DEFAULT NULL, `ntime` int(10) DEFAULT NULL, PRIMARY KEY (`nid`,`nuid`) ) ENGINE=MyISAM AUTO_INCREMENT=444 DEFAULT CHARSET=utf8; # TABLE: ttlsa_com.bbs_page CREATE TABLE `bbs_page` ( `pid` tinyint(6) NOT NULL AUTO_INCREMENT, `title` varchar(100) DEFAULT NULL, `content` text, `go_url` varchar(100) DEFAULT NULL, `add_time` int(10) DEFAULT NULL, `is_hidden` tinyint(1) DEFAULT '0', PRIMARY KEY (`pid`) ) ENGINE=MyISAM AUTO_INCREMENT=6 DEFAULT CHARSET=utf8; # TABLE: ttlsa_com.bbs_settings CREATE TABLE `bbs_settings` ( `id` tinyint(5) NOT NULL AUTO_INCREMENT, `title` varchar(255) NOT NULL DEFAULT '', `value` text NOT NULL, `type` tinyint(3) NOT NULL DEFAULT '0', PRIMARY KEY (`id`,`title`,`type`) ) ENGINE=MyISAM AUTO_INCREMENT=14 DEFAULT CHARSET=utf8; # TABLE: ttlsa_com.bbs_tags CREATE TABLE `bbs_tags` ( `tag_id` int(10) NOT NULL AUTO_INCREMENT, `tag_title` varchar(30) NOT NULL, `forums` int(10) NOT NULL DEFAULT '0', PRIMARY KEY (`tag_id`), UNIQUE KEY `tag_title` (`tag_title`) ) ENGINE=MyISAM AUTO_INCREMENT=185 DEFAULT CHARSET=utf8; # TABLE: ttlsa_com.bbs_tags_relation CREATE TABLE `bbs_tags_relation` ( `tag_id` int(10) NOT NULL DEFAULT '0', `fid` int(10) DEFAULT NULL, KEY `tag_id` (`tag_id`), KEY `fid` (`fid`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8; # TABLE: ttlsa_com.bbs_users CREATE TABLE `bbs_users` ( `uid` int(11) NOT NULL AUTO_INCREMENT, `username` varchar(20) DEFAULT NULL, `password` char(32) DEFAULT NULL, `openid` char(32) NOT NULL, `email` varchar(50) DEFAULT NULL, `avatar` varchar(100) DEFAULT NULL, `homepage` varchar(50) DEFAULT NULL, `money` int(11) DEFAULT '100', `signature` text, `forums` int(11) DEFAULT '0', `replies` int(11) DEFAULT '0', `notices` smallint(5) DEFAULT '0', `follows` int(11) NOT NULL DEFAULT '0', `regtime` int(10) DEFAULT NULL, `lastlogin` int(10) DEFAULT NULL, `lastpost` int(10) DEFAULT NULL, `qq` varchar(20) DEFAULT NULL, `group_type` tinyint(3) NOT NULL DEFAULT '0', `gid` tinyint(3) NOT NULL DEFAULT '3', `ip` char(15) DEFAULT NULL, `location` varchar(128) DEFAULT NULL, `token` varchar(40) DEFAULT NULL, `introduction` text, `is_active` tinyint(1) NOT NULL DEFAULT '1', PRIMARY KEY (`uid`,`group_type`) ) ENGINE=MyISAM AUTO_INCREMENT=109 DEFAULT CHARSET=utf8; # TABLE: ttlsa_com.bbs_user_follow CREATE TABLE `bbs_user_follow` ( `follow_id` int(10) unsigned NOT NULL AUTO_INCREMENT, `uid` int(10) unsigned NOT NULL DEFAULT '0', `follow_uid` int(10) unsigned NOT NULL DEFAULT '0', `addtime` int(10) NOT NULL DEFAULT '0', PRIMARY KEY (`follow_id`,`uid`,`follow_uid`) ) ENGINE=MyISAM AUTO_INCREMENT=9 DEFAULT CHARSET=utf8; # TABLE: ttlsa_com.bbs_user_groups CREATE TABLE `bbs_user_groups` ( `gid` int(11) NOT NULL AUTO_INCREMENT, `group_type` tinyint(3) NOT NULL DEFAULT '0', `group_name` varchar(50) DEFAULT NULL, `usernum` int(11) NOT NULL, PRIMARY KEY (`gid`,`group_type`) ) ENGINE=MyISAM AUTO_INCREMENT=4 DEFAULT CHARSET=utf8; # TABLE: ttlsa_com.data CREATE TABLE `data` ( `id` int(11) NOT NULL AUTO_INCREMENT, `value` char(30) NOT NULL DEFAULT '', `count` int(11) DEFAULT NULL, PRIMARY KEY (`value`), KEY `id` (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=latin1; # TABLE: ttlsa_com.t_data CREATE TABLE `t_data` ( `id` int(11) NOT NULL AUTO_INCREMENT, `value` char(30) NOT NULL DEFAULT '', `count` int(11) DEFAULT NULL, PRIMARY KEY (`value`), KEY `id` (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1; #...done. |
只导出数据,且是批量插入语句: