欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

MySQL备份与恢复之热备(3)

程序员文章站 2024-02-28 08:16:22
       在上两篇文章(mysql备份与恢复之冷备,mysql备份与恢复之真实环境使用冷备)中,我们提到了冷...

       在上两篇文章(mysql备份与恢复之冷备,mysql备份与恢复之真实环境使用冷备)中,我们提到了冷备和真实环境中使用冷备。那从这篇文章开始我们看下热备。显然热备和冷备是两个相对的概念,冷备是把数据库服务,比如mysql,oracle停下来,然后使用拷贝、打包或者压缩命令对数据目录进行备份;那么我们很容易想到热备就是在mysql或者其他数据库服务在运行的情况下进行备份。但是,这里存在一个问题,因为生产库在运行的情况下,有对该库的读写,读写频率有可能高,也可能低,不管频率高低,总会就会造成备份出来的数据和生产库中的数据不一致的情况。热备这段时间,其他人不可以操作是不现实的,因为你总不可能终止用户访问web程序。要解决这个问题,可以采用指定备份策略,比如哪个时间段进行备份,备份哪些数据等等,总之,保证数据的完整性和一致性,切记,备份重于一切!!!
       热备可以对多个库进行备份,可以对单张表或者某几张表进行备份。但是无法同时备份多个库多个表,只有分开备份。下面我们看下热备的示意图,并进行热备模拟。
示意图

 MySQL备份与恢复之热备(3)

热备模拟

1、对单个库进行备份
第一步,移除lvm快照。(如果没有创建,忽略此步)

[root@serv01 data]# lvremove /dev/data/smydata 
do you really want to remove active logical volume smydata? [y/n]: y
 logical volume "smydata" successfully removed

第二步,设置mysql的密码

mysql> set password=password("123456");
query ok, 0 rows affected (0.00 sec)

第三步,查看mysql是否启动。因为是热备,所以要求mysql服务启动

[root@serv01 data]# /etc/init.d/mysqld status
 success! mysql running (2664)

第四步,导出单个数据库

[root@serv01 data]# cd /databackup/

#本质是导出为sql
[root@serv01 databackup]# mysqldump -uroot -p123456 --database larrydb
-- mysql dump 10.13 distrib 5.5.29, for linux (x86_64)
--
-- host: localhost database: larrydb
-- ------------------------------------------------------
-- server version 5.5.29-log

/*!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 */;
/*!40103 set @old_time_zone=@@time_zone */;
/*!40103 set time_zone='+00:00' */;
/*!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_value_on_zero' */;
/*!40111 set @old_sql_notes=@@sql_notes, sql_notes=0 */;

--
-- current database: `larrydb`
--

create database /*!32312 if not exists*/ `larrydb` /*!40100 default character set latin1 */;

use `larrydb`;

--
-- table structure for table `class`
--

drop table if exists `class`;
/*!40101 set @saved_cs_client = @@character_set_client */;
/*!40101 set character_set_client = utf8 */;
create table `class` (
 `cid` int(11) default null,
 `cname` varchar(30) default null
) engine=innodb default charset=latin1;
/*!40101 set character_set_client = @saved_cs_client */;

--
-- dumping data for table `class`
--

lock tables `class` write;
/*!40000 alter table `class` disable keys */;
insert into `class` values (1,'linux'),(2,'oracle');
/*!40000 alter table `class` enable keys */;
unlock tables;

--
-- table structure for table `stu`
--

drop table if exists `stu`;
/*!40101 set @saved_cs_client = @@character_set_client */;
/*!40101 set character_set_client = utf8 */;
create table `stu` (
 `sid` int(11) default null,
 `sname` varchar(30) default null,
 `cid` int(11) default null
) engine=myisam default charset=latin1;
/*!40101 set character_set_client = @saved_cs_client */;

--
-- dumping data for table `stu`
--

lock tables `stu` write;
/*!40000 alter table `stu` disable keys */;
insert into `stu` values (1,'larry01',1),(2,'larry02',2);
/*!40000 alter table `stu` enable keys */;
unlock tables;
/*!40103 set time_zone=@old_time_zone */;

/*!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 */;

 dump completed on 2013-09-10 18:56:06

#将输出结果保存到文件中
[root@serv01 databackup]# mysqldump -uroot -p123456 --database larrydb > larrydb.sql

第五步,模拟数据丢失,进入mysql,删除数据库

[root@serv01 data]# mysql -uroot -p123456
welcome to the mysql monitor. commands end with ; or \g.
your mysql connection id is 4
server version: 5.5.29-log source distribution

copyright (c) 2000, 2012, 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.

mysql> show databases;
+--------------------+
| database  |
+--------------------+
| information_schema |
| crm  |
| game  |
| hello  |
| larrydb  |
| mnt  |
| mysql  |
| performance_schema |
| test  |
+--------------------+
9 rows in set (0.00 sec)

mysql> drop database larrydb;
query ok, 2 rows affected (0.01 sec)

mysql> show databases;
+--------------------+
| database  |
+--------------------+
| information_schema |
| crm  |
| game  |
| hello  |
| mnt  |
| mysql  |
| performance_schema |
| test  |
+--------------------+
8 rows in set (0.00 sec)

mysql> exit
bye

第六步,导入数据

[root@serv01 databackup]# mysql -uroot -p123456 <larrydb.sql

 
第七步,登录mysql,查看数据是否正常

[root@serv01 data]# mysql -uroot -p123456
welcome to the mysql monitor. commands end with ; or \g.
your mysql connection id is 6
server version: 5.5.29-log source distribution

copyright (c) 2000, 2012, 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.

mysql> show database;
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 'database' at line 1
mysql> show databases;
+--------------------+
| database  |
+--------------------+
| information_schema |
| crm  |
| game  |
| hello  |
| larrydb  |
| mnt  |
| mysql  |
| performance_schema |
| test  |
+--------------------+
9 rows in set (0.00 sec)

mysql> use larrydb;
database changed
mysql> select * from class;
+------+--------+
| cid | cname |
+------+--------+
| 1 | linux |
| 2 | oracle |
+------+--------+
2 rows in set (0.00 sec)

mysql> select * from stu;
+------+---------+------+
| sid | sname | cid |
+------+---------+------+
| 1 | larry01 | 1 |
| 2 | larry02 | 2 |
+------+---------+------+
2 rows in set (0.00 sec)

对多个库进行备份
第一步,查看有哪些数据库

mysql> show databases;
+--------------------+
| database  |
+--------------------+
| information_schema |
| crm  |
| game  |
| hello  |
| larrydb  |
| mnt  |
| mysql  |
| performance_schema |
| test  |
+--------------------+
9 rows in set (0.00 sec)
mysql> use game;
database changed
mysql> show tables;
+----------------+
| tables_in_game |
+----------------+
| country |
| fight  |
| hero  |
+----------------+
3 rows in set (0.00 sec)

mysql> select * from country;
+-----+---------+----------+
| cno | cname | location |
+-----+---------+----------+
| 10 | caowei | luoyang |
| 20 | shuhan | chengdou |
| 30 | sunwu | nanjing |
| 40 | houhan | luoyang |
| 50 | beisong | kaifeng |
| 60 | 魏国 | 洛阳 |
+-----+---------+----------+
6 rows in set (0.00 sec)

第二步,备份多个库

[root@serv01 databackup]# mysqldump -uroot -p123456 --databases larrydb game > larrydb_game.sql
[root@serv01 databackup]# ll larrydb_game.sql 
-rw-r--r--. 1 root root 6159 sep 10 19:05 larrydb_game.sql

第三步,模拟数据丢失。

mysql> drop database game;
query ok, 3 rows affected (0.01 sec)

mysql> drop database larrydb;
query ok, 2 rows affected (0.00 sec)
mysql> use crm;
database changed
mysql> show tables;
+---------------+
| tables_in_crm |
+---------------+
| test  |
+---------------+
1 row in set (0.00 sec)

mysql> select * from test;
empty set (0.00 sec)

mysql> drop database crm;
query ok, 1 row affected (0.00 sec)

第四步,恢复数据

[root@serv01 databackup]# mysql -uroot -p123456 < larrydb_game.sql 

 
第五步,查看数据是否正常

[root@serv01 data]# mysql -uroot -p123456
welcome to the mysql monitor. commands end with ; or \g.
your mysql connection id is 9
server version: 5.5.29-log source distribution

copyright (c) 2000, 2012, 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.

mysql> show databases;
+--------------------+
| database  |
+--------------------+
| information_schema |
| game  |
| hello  |
| larrydb  |
| mnt  |
| mysql  |
| performance_schema |
| test  |
+--------------------+
8 rows in set (0.00 sec)

mysql> use game;
database changed
mysql> select * from country;
+-----+---------+----------+
| cno | cname | location |
+-----+---------+----------+
| 10 | caowei | luoyang |
| 20 | shuhan | chengdou |
| 30 | sunwu | nanjing |
| 40 | houhan | luoyang |
| 50 | beisong | kaifeng |
| 60 | 魏国 | 洛阳 |
+-----+---------+----------+
6 rows in set (0.00 sec)

mysql> use larrydb;
database changed
mysql> select * from class;
+------+--------+
| cid | cname |
+------+--------+
| 1 | linux |
| 2 | oracle |
+------+--------+
2 rows in set (0.00 sec)

 
备份所有的库

[root@serv01 databackup]# mysqldump --help | grep all-database
or mysqldump [options] --all-databases [options]
 -a, --all-databases dump all the databases. this will be same as --databases
   --databases= or --all-databases), the logs will be
   --all-databases or --databases is given.
all-databases   false

[root@serv01 databackup]# mysqldump -uroot -p123456 --all-databases > all_databases.sql
[root@serv01 databackup]# ll all_databases.sql -h
-rw-r--r--. 1 root root 506k sep 10 19:16 all_databases.sql

备份某张表或者某几张表
第一步,备份某张表和某几张表

[root@serv01 databackup]# mysqldump game hero country -uroot -p123456 > game_hero_country.sql
[root@serv01 databackup]# ll game_hero_country.sql 
-rw-r--r—. 1 root root 3955 sep 10 19:11 game_hero_country.sql

第二步,模拟数据丢失

mysql> use game;
database changed
mysql> show tables;
+----------------+
| tables_in_game |
+----------------+
| country |
| fight  |
| hero  |
+----------------+
3 rows in set (0.00 sec)

mysql> drop table hero;
query ok, 0 rows affected (0.00 sec)

mysql> drop table country;
query ok, 0 rows affected (0.00 sec)
 

第三步,查看数据是否正常

[root@serv01 databackup]# mysql -uroot -p123456 < game_hero_country.sql 
error 1046 (3d000) at line 22: no database selected
[root@serv01 databackup]# mysql -uroot -p123456 --database game < game_hero_country.sql 

[root@serv01 databackup]# mysql -uroot -p123456 -e "select * from game.country"
+-----+---------+----------+
| cno | cname | location |
+-----+---------+----------+
| 10 | caowei | luoyang |
| 20 | shuhan | chengdou |
| 30 | sunwu | nanjing |
| 40 | houhan | luoyang |
| 50 | beisong | kaifeng |
| 60 | 魏国 | 洛阳 |
+-----+---------+----------+

通过这两天关于mysql热备和冷备的学习,大家是不是对mysql备份与恢复的了解更加深入了,不管是冷备还是热备其目的都是一致的保证数据的完整性和一致性,切记,备份重于一切!!!

相信今天所学的知识在之后的学习工作中对大家有所帮助。