Chinaunix首页 | 论坛 | 博客
  • 博客访问: 580444
  • 博文数量: 107
  • 博客积分: 5065
  • 博客等级: 大校
  • 技术积分: 1362
  • 用 户 组: 普通用户
  • 注册时间: 2008-12-29 16:01
文章存档

2009年(84)

2008年(23)

我的朋友

分类: Mysql/postgreSQL

2009-01-01 21:30:58

在看文章时注意要一定仔细看完再去实践证明!!!!!!!!!!!!!!!!!!
 
不能急去做. 不然更浪费时间.
 
 
随着数据的增多,数据的备份显得日益重要,下面是mysql常用的数据导入导出命令。
1.导出整个数据库
格式:mysqldump -u 用户名 -p 数据库名 > 导出的文件名
举例:
C:\Documents and Settings\Owner>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      | |
+--------+----------+----------+---------------+
2 rows in set (0.00 sec)

mysql> select * from user1;
+--------+----------+----------+---------------+
| userid | username | password | email          |
+--------+----------+----------+---------------+
|       1 | cccc      | cccc      | cccc           |
|       2 | cccc      | cccc      | |
+--------+----------+----------+---------------+
2 rows in set (0.00 sec)

导出文件 在mysql的bin目录里面执行如下命令
C:\MySQL\MySQL Server 5.0\bin> mysqldump -u root -p testdb > testdb.sql

Enter password:

C:\MySQL\MySQL Server 5.0\bin>

testdb.sql内容如下:

-- 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 ;
/*!40014 SET ;
/*!40014 SET ;
/*!40101 SET ;
/*!40101 SET ;
/*!40101 SET ;
/*!40111 SET ;


2.导出一个表
格式:mysqldump -u 用户名 -p 数据库名 表名> 导出的文件名
举例:表结构与上面的相同,命令如下:
C:\MySQL\MySQL Server 5.0\bin>mysqldump -u root -p testdb user > user.sql
Enter password:

C:\MySQL\MySQL Server 5.0\bin>

user.sql内容如下:

-- 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 ;
/*!40014 SET ;
/*!40014 SET ;
/*!40101 SET ;
/*!40101 SET ;
/*!40101 SET ;
/*!40111 SET ;

3.导出一个数据库结构
格式:mysqldump -u 用户名 -p -d --add-drop-table 数据库 > 导出的文件名
说明:-d 选项表示没有数据        
       --add-drop-table 选项说明在每个create语句之前增加一个drop table
举例:表结构与上面的相同

C:\MySQL\MySQL Server 5.0\bin>mysqldump -u root -p -d --add-drop-table testdb > testdbstruct.sql
Enter password:

C:\MySQL\MySQL Server 5.0\bin>

testdbstruct.sql内容如下:
-- 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;

--
-- 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;

/*!40101 SET ;
/*!40014 SET ;
/*!40014 SET ;
/*!40101 SET ;
/*!40101 SET ;
/*!40101 SET ;
/*!40111 SET ;


4.导入数据库
格式:常用source 命令

进入mysql数据库控制台:
C:\MySQL\MySQL Server 5.0\bin>mysql -u root -p
Enter password:
Welcome to the MySQL monitor.   Commands end with ; or \g.
Your MySQL connection id is 10 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> source testdb.sql;
Query OK, 0 rows affected (0.05 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.13 sec)

Query OK, 0 rows affected (0.10 sec)

Query OK, 0 rows affected (0.01 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 2 rows affected (0.04 sec)
Records: 2   Duplicates: 0   Warnings: 0

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.03 sec)

Query OK, 0 rows affected (0.09 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.01 sec)

Query OK, 2 rows affected (0.03 sec)
Records: 2   Duplicates: 0   Warnings: 0

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.01 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

mysql>

其中testdb.sql放在C:\MySQL\MySQL Server 5.0\bin目录下,如果不在这个目录下则要指定路径

阅读(1618) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~