|
mysql数据库备份与恢复
为了巩固mysql成果,设定了一个综合练习(‘恢复数据库被误删的数据’),也当自己初次做教程,在练习中先将数据库备份,再删除库单中的school数据库,最后对删除的数据库进行恢复
1.建立备份目录: [root@localhost ~]# mkdir /backup 2.重启mysql服务器服务 [root@localhost ~]# service mysqld restart Stopping mysqld: [ OK ] Starting mysqld: [ OK ] 3登陆mysql服务器 [root@localhost ~]# mysql -u root -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.1.61 Source distribution
Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners.
Type \'help;\' or \'\h\' for help. Type \'\c\' to clear the current input statement. 4.锁定所有的表,以免在备份时数据变动
mysql> flush tables with read lock; Query OK, 0 rows affected (0.00 sec)
mysql> quit; Bye 5.以root身份进行备份 [root@localhost ~]# mysqldump -A >/backup/alldata.sql -u root -p Enter password: 6.查看备份结果 [root@localhost ~]# ls /backup alldata.sql 7.在次登陆进 [root@localhost ~]# mysql -u root -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 5 Server version: 5.1.61 Source distribution
Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
Type \'help;\' or \'\h\' for help. Type \'\c\' to clear the current input statement.
8.解锁 mysql> unlock tables; Query OK, 0 rows affected (0.00 sec) 9.查看所有数据库
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | school | | test | +--------------------+ 4 rows in set (0.00 sec) 10.删除数据库,并查看 mysql> drop database school; Query OK, 2 rows affected (0.14 sec)
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | test | +--------------------+ 3 rows in set (0.00 sec) 11.重新创建被误删的数据库(以备在恢复时能找到)
mysql> create database school; Query OK, 1 row affected (0.00 sec)
mysql> use school Database changed mysql> select *from student; //检查数据已经被删除干净 ERROR 1146 (42S02): Table \'school.student\' doesn\'t exist 12.恢复数据库操作 mysql> source /backup/alldata.sql 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.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 1 row affected (0.00 sec)
Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A
Database changed Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
..............................一长串的就不贴了 13.检查恢复的成果 mysql> delect *from student; //检查student表 ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near \'delect *from student\' at line 1 mysql> select *from student; +------+-------+------+------------+--------+----------+ | sno | sname | ssex | sbirthday | sdepa | saddress | +------+-------+------+------------+--------+----------+ | 2345 | zhao | t | 2012-09-08 | math | NULL | | 2346 | zhang | t | 2014-09-15 | math | NULL | | 2347 | dong | t | 2013-12-09 | colege | NULL | +------+-------+------+------------+--------+----------+ 3 rows in set (0.00 sec)
mysql> select *from courses; //检查courses 表中的数据 +-----+----------+------------+ | cno | cname | teacher | +-----+----------+------------+ | 101 | english | zhanghong | | 102 | math | zhanghondg | | 104 | english4 | zhanghong | +-----+----------+------------+ 3 rows in set (0.00 sec)
mysql> quit Bye [root@localhost ~]#
|
相关文章:
显示评论签名
取消批量删除批量删除