/*
filename:MySQL数据备份与恢复学习
version:1.2
mysql of learner:chifeng(chifeng@bsdmail.org())
modify history:
--------------------------------------------
1、2004.7.5 开始(v1.0)
2、2004.7.6 增加了select * into/load data方法(v1.1)
3、2004.7.7 增加了mysqldump方法(v1.2)
4、2004.7.7 修改状态为发布,决定以后慢慢更新,增加所有参数的说明等等。
*/
安装好mysql-4.0.18-win就可以了。开始
一、backup,restore方法
C:mysqlbin>mysql -u root mysql
mysql> show databases;
+----------+
| Database |
+----------+
| mysql |
| test |
+----------+
2 rows in set (0.00 sec)
#就用默认的库,test,或者自己create database database_name;来建立一个。
mysql> use test
Database changed#选择一个库。
mysql> create table test(
-> id int,
-> name varchar(40),
-> age tinyint
-> );
#创建了一个test表。show tables;来看到他。
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| test |
+----------------+
1 row in set (0.02 sec)
#看看表的结构。
mysql> describe test;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(40) | YES | | NULL | |
| age | tinyint(4) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
#插入三条记录。
mysql> insert into test(id,name,age)
-> values(5101,'chifeng',23);
Query OK, 1 row affected (0.00 sec)
mysql> insert into test(id,name,age)
-> values(5102,'phpchina',18);
Query OK, 1 row affected (0.00 sec)
mysql> insert into test(id,name,age)
-> values(5103,'admin',40);
Query OK, 1 row affected (0.00 sec)
#看看结果
mysql> select * from test;
+------+----------+------+
| id | name | age |
+------+----------+------+
| 5101 | chifeng | 23 |
| 5102 | phpchina | 18 |
| 5103 | admin | 40 |
+------+----------+------+
3 rows in set (0.02 sec)
#备份到一个文件夹。这里备份到c盘的根下,也可以建立一个文件夹比如:data。'c:data'
mysql> backup table test to 'c:';
+-----------+--------+----------+----------+
| Table | Op | Msg_type | Msg_text |
+-----------+--------+----------+----------+
| test.test | backup | status | OK |
+-----------+--------+----------+----------+
1 row in set (0.00 sec)
#可以看到这两个文件:test.frm格式文件,test.myd数据文件。不能保存索引文件。:(
#删除test表。
mysql> drop table test;
Query OK, 0 rows affected (0.01 sec)
mysql> show tables;
Empty set (0.00 sec)
#恢复test
mysql> restore table test from 'c:';
+-----------+---------+----------+----------+
| Table | Op | Msg_type | Msg_text |
+-----------+---------+----------+----------+
| test.test | restore | status | OK |
+-----------+---------+----------+----------+
1 row in set (0.03 sec)
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| test |
+----------------+
1 row in set (0.00 sec)
mysql> select * from test;
+------+----------+------+
| id | name | age |
+------+----------+------+
| 5101 | chifeng | 23 |
| 5102 | phpchina | 18 |
| 5103 | admin | 40 |
+------+----------+------+
3 rows in set (0.00 sec) |
#恢复成功。:)
二、select..into,load data方法
mysql> select * from test into outfile 'f:mysql_copy est.dat';
Query OK, 3 rows affected (0.00 sec)
#现在可以delete from test;一下。
#再恢复test。
mysql> load data infile 'f:mysql_copy est.dat' into table test;
Query OK, 3 rows affected (0.08 sec)
Records: 3 Deleted: 0 Skipped: 0 Warnings: 0 |
#ok恢复成功
三、mysqldump,mysql方法
#mysqldump工具备份
C:mysqlbin>mysqldump test>test.sql
#恢复
C:mysqlbin>mysql -u root -p test | |