【Mysql】mysqldump 匯出各種場景的應用

楊奇龍發表於2011-10-28
因為一個開發問有關mysql字符集轉換的問題,一下子牽連出mysql資料庫的遷移-匯入,匯出,更改字符集,my.cnf引數配置等等知識點,花一些時間來逐個整理一下!(作為一個oracle 的小小dba,表示要學習的知識還有灰常多。。)
介紹mysqldump 匯出的各種場景應用!
只匯出整個資料庫的表結構
1 指定--no-data引數的方式
[root@rac3 mysqldmp]# mysqldump --default-character-set=latin1 -h127.0.0.1  -uroot --no-data  test  > la_1141.sql           
[root@rac3 mysqldmp]# cat la_1141.sql 
-- MySQL dump 10.11
-- Host: 127.0.0.1    Database: test
-- ------------------------------------------------------
-- Server version       5.0.45
-- Table structure for table `yang`
DROP TABLE IF EXISTS `yang`;
CREATE TABLE `yang` (
  `name` varchar(20) default NULL,
  `value` varchar(20) default NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
-- Table structure for table `yangtab`
DROP TABLE IF EXISTS `yangtab`;
CREATE TABLE `yangtab` (
  `id` int(11) default NULL,
  `val` varchar(15) default NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
-- Dump completed on 2011-10-28  3:41:21
[root@rac3 mysqldmp]# 
2 指定--tables引數
[root@rac3 mysqldmp]# mysqldump --default-character-set=latin1 -h127.0.0.1  -uroot --tables -d test > la_tabs.sql
[root@rac3 mysqldmp]# cat la_tabs.sql | more
-- Table structure for table `yang`
DROP TABLE IF EXISTS `yang`;
CREATE TABLE `yang` (
  `name` varchar(20) default NULL,
  `value` varchar(20) default NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
-- Table structure for table `yangtab`
DROP TABLE IF EXISTS `yangtab`;
CREATE TABLE `yangtab` (
  `id` int(11) default NULL,
  `val` varchar(15) default NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
-- Dump completed on 2011-10-28  3:36:13
[root@rac3 mysqldmp]# 
匯出表結構和資料
[root@rac3 mysqldmp]# mysqldump --default-character-set=latin1 -h127.0.0.1  -uroot  test yangtab > la_ytab.sql
[root@rac3 mysqldmp]# cat la_ytab.sql 
-- Table structure for table `yangtab`
DROP TABLE IF EXISTS `yangtab`;
CREATE TABLE `yangtab` (
  `id` int(11) default NULL,
  `val` varchar(15) default NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
-- Dumping data for table `yangtab`
LOCK TABLES `yangtab` WRITE;
/*!40000 ALTER TABLE `yangtab` DISABLE KEYS */;
INSERT INTO `yangtab` VALUES (1,'楊奇龍'),(2,'默默');
/*!40000 ALTER TABLE `yangtab` ENABLE KEYS */;
UNLOCK TABLES;
-- Dump completed on 2011-10-28  3:38:37
[root@rac3 mysqldmp]# 
只匯出資料庫所有表中的資料
[root@rac3 mysqldmp]# mysqldump --default-character-set=latin1 -h127.0.0.1  -uroot --no-create-info  test  > la_1140.sql         
[root@rac3 mysqldmp]# cat la_1140.sql 
-- Dumping data for table `yang`
LOCK TABLES `yang` WRITE;
/*!40000 ALTER TABLE `yang` DISABLE KEYS */;
INSERT INTO `yang` VALUES ('yangql','楊奇龍'),('cat','貓');
/*!40000 ALTER TABLE `yang` ENABLE KEYS */;
UNLOCK TABLES;
-- Dumping data for table `yangtab`
LOCK TABLES `yangtab` WRITE;
/*!40000 ALTER TABLE `yangtab` DISABLE KEYS */;
INSERT INTO `yangtab` VALUES (1,'楊奇龍'),(2,'默默');
/*!40000 ALTER TABLE `yangtab` ENABLE KEYS */;
UNLOCK TABLES;
-- Dump completed on 2011-10-28  3:40:13
[root@rac3 mysqldmp]# 
匯出指定表的資料:
[root@rac3 mysqldmp]# mysqldump --default-character-set=latin1 -h127.0.0.1  -uroot --no-create-info  test yangtab  > la_1143.sql
[root@rac3 mysqldmp]# cat la_1143.sql 
-- Dumping data for table `yangtab`
LOCK TABLES `yangtab` WRITE;
/*!40000 ALTER TABLE `yangtab` DISABLE KEYS */;
INSERT INTO `yangtab` VALUES (1,'楊奇龍'),(2,'默默');
/*!40000 ALTER TABLE `yangtab` ENABLE KEYS */;
UNLOCK TABLES;
-- Dump completed on 2011-10-28  3:43:13
[root@rac3 mysqldmp]# 
Note:測試環境,資料量很小,沒有考慮效能問題!

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/22664653/viewspace-709831/,如需轉載,請註明出處,否則將追究法律責任。

相關文章