一、mysqldump备份操作测试:
1.准备对其进行备份操作的数据库:
mysql> showtables;
+------------------+
|Tables_in_test |
+------------------+
| a |
| sales |
| t |
| t1 |
| t2 |
| t_hash |
| t_linear_hash |
| u |
| usercash |
| usercash_err_log |
+------------------+
10 rows in set (0.00sec)
2.先备份:
[root@Nginx ~]#mysqldump -u root -p test > /data/mysql/test.bk.sql
Enter password:
3.删除数据库:
mysql> showdatabases;
+--------------------+
| Database |
+--------------------+
| information_schema|
| mysql |
| performance_schema|
| test |
|wordpress_nginx |
+--------------------+
5 rows in set (0.00sec)
mysql> dropdatabase test;
Query OK, 10 rowsaffected (1.12 sec)
mysql> showdatabases;
+--------------------+
| Database |
+--------------------+
| information_schema|
| mysql |
| performance_schema|
|wordpress_nginx |
+--------------------+
4 rows in set (0.00sec)
4.恢复数据库:
[root@Nginx ~]#mysql -u root -p test < /data/mysql/test.bk.sql
Enter password:
ERROR 1049 (42000):Unknown database ‘test‘
删除了数据库之后想直接恢复会提示不存在该数据库
那么,我在恢复之前,先创建一个空的数据库
mysql> createdatabase test;
Query OK, 1 rowaffected (0.07 sec)
mysql> showdatabases;
+--------------------+
| Database |
+--------------------+
| information_schema|
| mysql |
| performance_schema|
| test |
|wordpress_nginx |
+--------------------+
5 rows in set (0.03sec)
mysql> use test;
Database changed
mysql> showtables;
Empty set(0.00 sec)
可以观察到,这是空的,那么我们再来进行恢复数据库。
[root@Nginx ~]#mysql -u root -p test < /data/mysql/test.bk.sql
Enter password:
再来观察test数据库:
mysql> use test;
Database changed
mysql> showtables;
Empty set (0.00 sec)
mysql> showtables;
+------------------+
|Tables_in_test |
+------------------+
| a |
| sales |
| t |
| t1 |
| t2 |
| t_hash |
| t_linear_hash |
| u |
| usercash |
| usercash_err_log |
+------------------+
10 rows in set (0.00sec)
mysql> select *from t
-> ;
+------+------+
| a | b |
+------+------+
| 1 | 2 |
+------+------+
1 row in set (0.02sec)
可以看到,已经恢复到原来的样子了。
二、冷备
冷备是指,停掉服务,只需要备份mysql数据库的frm文件、共享表空间文件、独立表空间文件(*ibd)、重做日志文件。一般会对整个文件夹或目录进行备份,即打包和压缩。
关闭服务:
[root@Nginx mysql]#service mysqld stop
Shutting downMySQL.. SUCCESS!
对数据库目录进行备份打包,并对备份目录进行授权:
[root@Nginxmysql]# mkdir /databk
[root@Nginx databk]#chown mysql.mysql /databk/ -R
[root@Nginx databk]#tar -cvPzf mysql.tar.gz /data/mysql/
恢复数据库:
[root@Nginx databk]#tar -xvPf mysql2.tar.gz
最后重新启动服务,并检查数据是否有丢失即可。
本文出自 “习惯” 博客,请务必保留此出处http://xiguanmin.blog.51cto.com/4857855/1373475