MySQL導(dǎo)出導(dǎo)入命令的用例
隨著數(shù)據(jù)的增多,數(shù)據(jù)的備份顯得日益重要,下面是mysql常用的數(shù)據(jù)導(dǎo)入導(dǎo)出命令。
1.導(dǎo)出整個(gè)數(shù)據(jù)庫
格式:mysqldump -u 用戶名 -p 數(shù)據(jù)庫名 > 導(dǎo)出的文件名
舉例:
C:Documents and SettingsOwner>mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 5 to server version: 5.0.7-beta-nt
Type 'help;' or 'h' for help. Type 'c' to clear the buffer.
mysql> use testdb
Database changed
mysql> select * from user;
+--------+----------+----------+---------------+
| userid | username | password | email |
+--------+----------+----------+---------------+
| 1 | aaa | aaa | aaaa |
| 2 | bbb | bbbb | bbbb@sina.com|
+--------+----------+----------+---------------+
2 rows in set (0.00 sec)
mysql> select * from user1;
+--------+----------+----------+---------------+
| userid | username | password | email |
+--------+----------+----------+---------------+
| 1 | cccc | cccc | cccc |
| 2 | cccc | cccc | cccc@sina.com|
+--------+----------+----------+---------------+
2 rows in set (0.00 sec)
在mysql的bin目錄里面執(zhí)行如下命令
C:MySQLMySQL Server 5.0bin>mysqldump -u root -p testdb > testdb.sql
Enter password:
C:MySQLMySQL Server 5.0bin>
testdb.sql內(nèi)容如下:
-- MySQL dump 10.10
--
-- Host: localhost Database: testdb
-- ------------------------------------------------------
-- Server version 5.0.7-beta-nt
/*!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 */;
/*!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__ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
--
-- Table structure for table `user`
--
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`userid` int(11) NOT NULL,
`username` varchar(20) NOT NULL,
`password` varchar(50) NOT NULL,
`email` varchar(50) default NULL,
PRIMARY KEY (`userid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
--
-- Dumping data for table `user`
--
/*!40000 ALTER TABLE `user` DISABLE KEYS */;
LOCK TABLES `user` WRITE;
INSERT INTO `user` S (1,'aaa','aaa','aaaa'),(2,'bbb','bbbb','bbbb@sina.com');
UNLOCK TABLES;
/*!40000 ALTER TABLE `user` ENABLE KEYS */;
--
-- Table structure for table `user1`
--
DROP TABLE IF EXISTS `user1`;
CREATE TABLE `user1` (
`userid` int(11) NOT NULL,
`username` varchar(20) NOT NULL,
`password` varchar(50) NOT NULL,
`email` varchar(50) default NULL,
PRIMARY KEY (`userid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
--
-- Dumping data for table `user1`
--
/*!40000 ALTER TABLE `user1` DISABLE KEYS */;
LOCK TABLES `user1` WRITE;
INSERT INTO `user1` S (1,'cccc','cccc','cccc'),(2,'cccc','cccc','cccc@sina.com');
UNLOCK TABLES;
/*!40000 ALTER TABLE `user1` ENABLE KEYS */;
/*!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*/;
2.導(dǎo)出一個(gè)表
格式:mysqldump -u 用戶名 -p 數(shù)據(jù)庫名 表名> 導(dǎo)出的文件名
舉例:表結(jié)構(gòu)與上面的相同,命令如下:
C:MySQLMySQL Server 5.0bin>mysqldump -u root -p testdb user > user.sql
Enter password:
C:MySQLMySQL Server 5.0bin>
user.sql內(nèi)容如下:
-- MySQL dump 10.10
--
-- Host: localhost Database: testdb
-- ------------------------------------------------------
-- Server version 5.0.7-beta-nt
/*!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 */;
/*!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__ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
--
-- Table structure for table `user`
--
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`userid` int(11) NOT NULL,
`username` varchar(20) NOT NULL,
`password` varchar(50) NOT NULL,
`email` varchar(50) default NULL,
PRIMARY KEY (`userid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
--
-- Dumping data for table `user`
--
/*!40000 ALTER TABLE `user` DISABLE KEYS */;
LOCK TABLES `user` WRITE;
INSERT INTO `user` S (1,'aaa','aaa','aaaa'),(2,'bbb','bbbb','bbbb@sina.com');
UNLOCK TABLES;
/*!40000 ALTER TABLE `user` ENABLE KEYS */;
/*!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*/;
3.導(dǎo)出一個(gè)數(shù)據(jù)庫結(jié)構(gòu)
格式:mysqldump -u 用戶名 -p -d --add-drop-table 數(shù)據(jù)庫 > 導(dǎo)出的文件名
說明:-d 選項(xiàng)表示沒有數(shù)據(jù)
--add-drop-table 選項(xiàng)說明在每個(gè)create語句之前增加一個(gè)drop table
舉例:表結(jié)構(gòu)與上面的相同
C:MySQLMySQL Server 5.0bin>mysqldump -u root -p -d --add-drop-table testdb > testdbstruct.sql
Enter password:
C:MySQLMySQL Server 5.0bin>
testdbstruct.sql內(nèi)容如下:
-- MySQL dump 10.10
--
-- Host: localhost Database: testdb
-- ------------------------------------------------------
-- Server version 5.0.7-beta-nt
/*!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 */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!400
關(guān)鍵詞:MySQL
閱讀本文后您有什么感想? 已有 人給出評(píng)價(jià)!
- 0
- 0
- 0
- 0
- 0
- 0