逻辑备份:工具 mysqldump, mydumper, phpMyAdmin(备份的缺点:Schema和数据存储一起、会生成巨大的SQL语句、单个巨大的备份文件)连上MySQL后,将数据导出来。考虑到备份的数据可能会比较大,在比较在乎硬盘空间情况下可以使用压缩工具来先压缩后存放。
完全备份并不是指备份所有库是指备份指定数据集当中的所有数据,通常也可以是单个数据库。完全备份+增量备份时,先用mysqldump做一下完全备份,过一个时间后,备份一下这个时间内的二进制日志(每次备份都记录一下位置二进制日志内部的位置,后面就可以了解其重要性了)。
考虑可能会出现的硬盘自身故障,所以二进制日志文件和数据文件不应该放置于同一个硬盘,同样备份文件也不应该和数据文件一起存放。
查看一下数据目录
MariaDB [hellodb]> select @@global.datadir;
+------------------+
| @@global.datadir |
+------------------+
| /mysql/data/ |
+------------------+
1 row in set (0.00 sec)
查看一下当前有哪些二进制日志
MariaDB [hellodb]> show binary logs;
+------------------+-----------+
| Log_name | File_size |
+------------------+-----------+
| mysql-bin.000001 | 67307 |
| mysql-bin.000002 | 977605 |
| mysql-bin.000003 | 345 |
| mysql-bin.000004 | 41213 |
+------------------+-----------+
4 rows in set (0.00 sec)
在数据目录下(/mysql/data)mysql-bin.index文件里有存放了二进制日志文件的索引
[root@hostpc ~]# cat /mysql/data/mysql-bin.index
./mysql-bin.000001
./mysql-bin.000002
./mysql-bin.000003
./mysql-bin.000004
[root@hostpc ~]# file /mysql/data/mysql-bin.000001 查看一下日志文件类型
/mysql/data/mysql-bin.000001: MySQL replication log
mysqlbinlog可以用来查看二进制日志文件
NAME
mysqlbinlog - utility for processing binary log files
SYNOPSIS
mysqlbinlog [options] log_file ...
DESCRIPTION
The server′s binary log consists of files containing “events” that
describe modifications to database contents. The server writes these
files in binary format. To display their contents in text format,
use the mysqlbinlog utility. You can also use mysqlbinlog to display
the contents of relay log files written by a slave server in a
replication setup because relay logs have the same format as binary
logs. The binary log and relay log are discussed further in
Section 5.2.4, “The Binary Log”, and Section 16.2.2, “Replication
Relay and Status Files”.
查看二进制日志文件部分内容
[root@hostpc ~]# mysqlbinlog /mysql/data/mysql-bin.000002 | less
。。。。。。。。。
BEGIN
/*!*/;
# at 364 事件开始的位置
#150421 10:33:20 server id 1 end_log_pos 451 Query thread_id=2 exec_time=0
error_code=0
use `mysql`/*!*/;
SET TIMESTAMP=1429583600/*!*/;
SET @@session.pseudo_thread_id=2/*!*/;
SET @@session.foreign_key_checks=1, @@session.sql_auto_is_null=0, @@session.unique_checks=1, @@session.autocommit=1/*!*/;
。。。。
事件发生的日期和时间;(150421 10:33:20)
事件发生在服务器的标识(server id)
事件的结束位置:(end_log_pos 451)
事件的类型:(Query)
事件发生时所在的服务器执行此事件的线程的ID:(thread_id=2)
语句的时间戳与将其写入二进制文件中的时间差:(exec_time=0)
错误代码:(error_code=0)
事件内容:(use `mysql`/*!*/;
SET TIMESTAMP=1429583600/*!*/;
SET @@session.pseudo_thread_id=2/*!*/;)
GTID事件专属:
事件所属的全局事务的GTID(为MySQL上每一个服务都定义了):(GTID 0-1-2)
登录MySQL后,可以通过show binlog events in 'log_name'\G 来查看
MariaDB [hellodb]> show binlog events in 'mysql-bin.000003'\G
*************************** 1. row ***************************
Log_name: mysql-bin.000003
Pos: 4
Event_type: Format_desc
Server_id: 1
End_log_pos: 248
Info: Server ver: 10.0.13-MariaDB-log, Binlog ver: 4
。。。。。。。。。
mysqlbinlog下常用的参数
-h, --host=name Get the binlog from server. -P, --port=# Port number to use for connection or 0 for default to, in
order of preference, my.cnf, $MYSQL_TCP_PORT,
/etc/services, built-in default (3306).
-u, --user=name Connect to the remote server as username.
--start-datetime=name 事件的起始时间
Start reading the binlog at first event having a datetime
equal or posterior to the argument; the argument must be
a date and time in the local time zone, in any format
accepted by the MySQL server for DATETIME and TIMESTAMP
types, for example: 2004-12-25 11:25:56 (you should
probably use quotes for your shell to set it properly).
-j, --start-position=# 事件的起始位置
Start reading the binlog at position N. Applies to the
first binlog passed on the command line.
--stop-datetime=name 事件的结束时间
Stop reading the binlog at first event having a datetime
equal or posterior to the argument; the argument must be
a date and time in the local time zone, in any format
accepted by the MySQL server for DATETIME and TIMESTAMP
types, for example: 2004-12-25 11:25:56 (you should
probably use quotes for your shell to set it properly).
--stop-position=# 事件的结束位置
Stop reading the binlog at position N. Applies to the
last binlog passed on the command line.
[root@hostpc ~]# mysqlbinlog --start-position=364 --stop-position=451 /mysql/data/mysql-bin.000002
显示了日志文件从364到451的内容
下面就来介绍MySQL备份工具mysqldump的使用,下面是mysqldump的部分手册
mysqldump - a database backup programSYNOPSIS mysqldump [options] [db_name [tbl_name ...]]DESCRIPTION The mysqldump client is a backup program originally written by Igor Romanenko. It can be used to dump a database or a collection of databases for backup or transfer to another SQL server (not necessarily a MySQL server). The dump typically contains SQL statements to create the table, populate it, or both. However, mysqldump can also be used to generate files in CSV, other delimited text, or XML format. If you are doing a backup on the server and your tables all are MyISAM tables, consider using the mysqlhotcopy instead because it can accomplish faster backups and faster restores. See mysqlhotcopy(1). There are three general ways to invoke mysqldump: shell> mysqldump [options] db_name [tbl_name ...] 这里是备份指定库中的单个或多个表 shell> mysqldump [options] --databases db_name ... 这里是备份指定的单个或多个库 shell> mysqldump [options] --all-databases 这是备份所有的库 -A, --all-databases 所有数据库 --all-databases, -A Dump all tables in all databases. This is the same as using the --databases option and naming all the databases on the command line. MyISAM, InnoDB: 温备,在线业务只能读,不能进行写操作 -x, --lock-all-tables:锁定所有表 --lock-all-tables, -x Lock all tables across all databases. This is achieved by acquiring a global read lock for the duration of the whole dump. This option automatically turns off --single-transaction and --lock-tables. -l, --lock-tables:锁定备份的表 --lock-tables, -l For each dumped database, lock all tables to be dumped before dumping them. The tables are locked with READ LOCAL to allow concurrent inserts in the case of MyISAM tables. For transactional tables such as InnoDB, --single-transaction is a much better option than --lock-tables because it does not need to lock the tables at all. Because --lock-tables locks tables for each database separately, this option does not guarantee that the tables in the dump file are logically consistent between databases. Tables in different databases may be dumped in completely different states. InnoDB:对InnoDB可执行热备 --single-transaction:启动一个大的单一事务实现备份 -B, --databases db_name1 db_name2 ...:备份指定的数据库 --databases, -B Dump several databases. Normally, mysqldump treats the first name argument on the command line as a database name and following names as table names. With this option, it treats all name arguments as database names. CREATE DATABASE and USE statements are included in the output before each new database. -C, --compress:压缩传输;需要消耗cpu资源的,通过资源使用情况判定是否使用此项 --compress, -C Compress all information sent between the client and the server if both support compression. --master-data[=#]:#为数值 1:记录CHANGE MASTER TO语句(有对应的二进制日志文件和位置);此语句未被注释; 2:记录为注释语句; --flush-logs, -F:锁定表之后执行flush logs(实现日志滚动)命令;
下面演示中,对hellodb库进行操作,先做一次完全备份,操作后对其进行一次增量备份查看一下当前有哪些二进制日志MariaDB [hellodb]> show binary logs;+------------------+-----------+| Log_name | File_size |+------------------+-----------+| mysql-bin.000001 | 67307 || mysql-bin.000002 | 977605 || mysql-bin.000003 | 345 || mysql-bin.000004 | 41213 |+------------------+-----------+4 rows in set (0.00 sec)MariaDB [hellodb]> flush logs; 滚动一下日志Query OK, 0 rows affected (0.10 sec)MariaDB [hellodb]> show binary logs;+------------------+-----------+| Log_name | File_size |+------------------+-----------+| mysql-bin.000001 | 67307 || mysql-bin.000002 | 977605 || mysql-bin.000003 | 345 || mysql-bin.000004 | 41256 || mysql-bin.000005 | 365 | 这个是滚动后生成的日志+------------------+-----------+5 rows in set (0.00 sec)查看一下students表的结构MariaDB [hellodb]> desc students;+-----------+---------------------+------+-----+---------+----------------+| Field | Type | Null | Key | Default | Extra |+-----------+---------------------+------+-----+---------+----------------+| StuID | int(10) unsigned | NO | PRI | NULL | auto_increment || Name | varchar(50) | NO | | NULL | || Age | tinyint(3) unsigned | NO | | NULL | || Gender | enum('F','M') | NO | | NULL | || ClassID | tinyint(3) unsigned | YES | | NULL | || TeacherID | int(10) unsigned | YES | | NULL | |+-----------+---------------------+------+-----+---------+----------------+6 rows in set (0.00 sec)查看创建此表的语句MariaDB [hellodb]> show create table students\G*************************** 1. row *************************** Table: studentsCreate Table: CREATE TABLE `students` ( `StuID` int(10) unsigned NOT NULL AUTO_INCREMENT, `Name` varchar(50) NOT NULL, `Age` tinyint(3) unsigned NOT NULL, `Gender` enum('F','M') NOT NULL, `ClassID` tinyint(3) unsigned DEFAULT NULL, `TeacherID` int(10) unsigned DEFAULT NULL, PRIMARY KEY (`StuID`)) ENGINE=InnoDB AUTO_INCREMENT=29 DEFAULT CHARSET=utf81 row in set (0.00 sec)查看StuID大于10小于15下students表的内容MariaDB [hellodb]> select * from students where StuID>10 and StuID<15;+-------+---------------+-----+--------+---------+-----------+| StuID | Name | Age | Gender | ClassID | TeacherID |+-------+---------------+-----+--------+---------+-----------+| 11 | Yuan Chengzhi | 23 | M | 6 | NULL || 12 | Wen Qingqing | 19 | F | 1 | NULL || 13 | Tian Boguang | 33 | M | 2 | NULL || 14 | Lu Wushuang | 17 | F | 3 | NULL |+-------+---------------+-----+--------+---------+-----------+4 rows in set (0.00 sec)在操作之前先对hellodb库做一次完全备份[root@hostpc ~]# ls /mysql/data lost+found把备份的文件都放到/mysql目录下[root@hostpc ~]# mysqldump -B --lock-all-tables --master-data=2 hellodb > /mysql/$(date +%F-%H-%M-%S)[root@hostpc ~]# ls /mysql/2015-04-24-14-52-30 data lost+found 备份ok了接下来就对hellodb库进行操作了1.创建一个表tree,结构为type char(20),name char(30),high float2.给定表一些数据3.在students表的追加一条记录。MariaDB [hellodb]> create table tree (type char(20) not null,name char(30) not null,high float not null);Query OK, 0 rows affected (0.19 sec)MariaDB [hellodb]> insert into tree values ('zhenxing','songshu',7.82),('guoshu','lishu',3.68),('guoshu','pingguoshu','5.02');Query OK, 3 rows affected (0.03 sec)Records: 3 Duplicates: 0 Warnings: 0MariaDB [hellodb]> select * from students order by StuID desc limit 3;+-------+-----------+-----+--------+---------+-----------+| StuID | Name | Age | Gender | ClassID | TeacherID |+-------+-----------+-----+--------+---------+-----------+| 28 | fangshiyu | 29 | F | NULL | NULL || 27 | liuqing | 40 | F | NULL | NULL || 26 | yuanming | 50 | F | NULL | NULL |+-------+-----------+-----+--------+---------+-----------+3 rows in set (0.00 sec)MariaDB [hellodb]> desc students;+-----------+---------------------+------+-----+---------+----------------+| Field | Type | Null | Key | Default | Extra |+-----------+---------------------+------+-----+---------+----------------+| StuID | int(10) unsigned | NO | PRI | NULL | auto_increment || Name | varchar(50) | NO | | NULL | || Age | tinyint(3) unsigned | NO | | NULL | || Gender | enum('F','M') | NO | | NULL | || ClassID | tinyint(3) unsigned | YES | | NULL | || TeacherID | int(10) unsigned | YES | | NULL | |+-----------+---------------------+------+-----+---------+----------------+6 rows in set (0.00 sec)MariaDB [hellodb]> insert into students (StuID,Name,Age,Gender) values (29,'linger',25,'F');Query OK, 1 row affected (0.04 sec)MariaDB [hellodb]> select * from students order by StuID desc limit 3;+-------+-----------+-----+--------+---------+-----------+| StuID | Name | Age | Gender | ClassID | TeacherID |+-------+-----------+-----+--------+---------+-----------+| 29 | linger | 25 | F | NULL | NULL || 28 | fangshiyu | 29 | F | NULL | NULL || 27 | liuqing | 40 | F | NULL | NULL |+-------+-----------+-----+--------+---------+-----------+3 rows in set (0.00 sec)对hellodb库做一次增量备份[root@hostpc ~]# ls /mysql/2015-04-24-14-52-30 data lost+found MariaDB [hellodb]> show binary logs;+------------------+-----------+| Log_name | File_size |+------------------+-----------+| mysql-bin.000001 | 67307 || mysql-bin.000002 | 977605 || mysql-bin.000003 | 345 || mysql-bin.000004 | 41256 || mysql-bin.000005 | 994 |+------------------+-----------+5 rows in set (0.00 sec) 从上一次完全备份后开始到现在做增量备份[root@hostpc ~]# mysqlbinlog --start-datetime='2015-04-24-14-52-30' /mysql/data/mysql-bin.000005 > /mysql/$(date +%F-%H-%M-%S)[root@hostpc ~]# ls /mysql/2015-04-24-14-52-30 2015-04-24-15-54-27 data lost+found 备份ok再次进行操作1.删除students表的StuID=27的那组数据2.追加一组数据到tree表中MariaDB [hellodb]> select * from students order by StuID desc limit 4;+-------+-----------+-----+--------+---------+-----------+| StuID | Name | Age | Gender | ClassID | TeacherID |+-------+-----------+-----+--------+---------+-----------+| 29 | linger | 25 | F | NULL | NULL || 28 | fangshiyu | 29 | F | NULL | NULL || 27 | liuqing | 40 | F | NULL | NULL || 26 | yuanming | 50 | F | NULL | NULL |+-------+-----------+-----+--------+---------+-----------+4 rows in set (0.00 sec)MariaDB [hellodb]> delete from students where StuID=27;Query OK, 1 row affected (0.03 sec)MariaDB [hellodb]> select * from students order by StuID desc limit 4;+-------+-------------+-----+--------+---------+-----------+| StuID | Name | Age | Gender | ClassID | TeacherID |+-------+-------------+-----+--------+---------+-----------+| 29 | linger | 25 | F | NULL | NULL || 28 | fangshiyu | 29 | F | NULL | NULL || 26 | yuanming | 50 | F | NULL | NULL || 25 | Sun Dasheng | 100 | M | NULL | NULL |+-------+-------------+-----+--------+---------+-----------+4 rows in set (0.00 sec)MariaDB [hellodb]> insert into tree value ('guoshu','xueli',3.6);Query OK, 1 row affected (0.04 sec)MariaDB [hellodb]> select * from tree;+----------+------------+------+| type | name | high |+----------+------------+------+| zhenxing | songshu | 7.82 || guoshu | lishu | 3.68 || guoshu | pingguoshu | 5.02 || guoshu | xueli | 3.6 |+----------+------------+------+4 rows in set (0.00 sec)做一次增量备份[root@hostpc ~]# mysqlbinlog --start-datetime='2015-04-24-15-54-27' /mysql/data/mysql-bin.000005 > /mysql/$(date +%F-%H-%M-%S)在做一些操作1.删除tree表2.追加一行数据到students表上MariaDB [hellodb]> drop table tree;Query OK, 0 rows affected (0.08 sec)MariaDB [hellodb]> insert into students (StuID,Name,Age,Gender) values (30,'huier',23,'F');Query OK, 1 row affected (0.03 sec)做增量备份[root@hostpc ~]# ls /mysql/2015-04-24-14-52-30 2015-04-24-15-54-27 2015-04-24-16-01-52 data lost+found[root@hostpc ~]# mysqlbinlog --start-datetime='2015-04-24-16-01-52' /mysql/data/mysql-bin.000005 > /mysql/$(date +%F-%H-%M-%S)[root@hostpc ~]# ls /mysql/2015-04-24-14-52-30 2015-04-24-16-01-52 data2015-04-24-15-54-27 2015-04-24-16-05-45 lost+found ok增量备份完成了如果说要恢复到2015-04-24-14-52-30这个时刻,需要在增量备份上一层一层去恢复,直到第一个完全备份处才行,查看一下当前库有哪些表MariaDB [hellodb]> show tables;+-------------------+| Tables_in_hellodb |+-------------------+| book || classes || coc || courses || scores || students || teachers || toc |+-------------------+8 rows in set (0.00 sec)MariaDB [hellodb]> \. /mysql/2015-04-24-16-01-52Query OK, 0 rows affected (0.00 sec)Query OK, 0 rows affected (0.00 sec)。。。。ERROR 1146 (42S02) at line 47 in file: '/mysql/2015-04-24-16-01-52': Table 'hellodb.tree' doesn't existQuery 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)MariaDB [hellodb]> create table tree (type char(20) not null,name char(30) not null,high float not null);Query OK, 0 rows affected (0.19 sec)MariaDB [hellodb]> \. /mysql/2015-04-24-16-01-52Query 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)MariaDB [hellodb]> select * from tree;+--------+-------+------+| type | name | high |+--------+-------+------+| guoshu | xueli | 3.6 |+--------+-------+------+1 row in set (0.00 sec)MariaDB [hellodb]> select * from students where StuID=27;Empty set (0.00 sec)MariaDB [hellodb]> \. /mysql/2015-04-24-15-54-27Query 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)。。。ERROR 1062 (23000) at line 55 in file: '/mysql/2015-04-24-15-54-27': Duplicate entry '29' for key 'PRIMARY'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)MariaDB [hellodb]> select * from tree;+----------+------------+------+| type | name | high |+----------+------------+------+| guoshu | xueli | 3.6 || zhenxing | songshu | 7.82 || guoshu | lishu | 3.68 || guoshu | pingguoshu | 5.02 |+----------+------------+------+4 rows in set (0.00 sec)MariaDB [hellodb]> select * from students where StuID=27;Empty set (0.00 sec)MariaDB [hellodb]> \! ls /mysql2015-04-24-14-52-30 2015-04-24-16-01-52 data2015-04-24-15-54-27 2015-04-24-16-05-45 lost+foundMariaDB [hellodb]> \. /mysql/2015-04-24-14-52-30Query 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)MariaDB [hellodb]> show tables;+-------------------+| Tables_in_hellodb |+-------------------+| book || classes || coc || courses || scores || students || teachers || toc || tree |+-------------------+9 rows in set (0.00 sec)MariaDB [hellodb]> select * from tree;+----------+------------+------+| type | name | high |+----------+------------+------+| guoshu | xueli | 3.6 || zhenxing | songshu | 7.82 || guoshu | lishu | 3.68 || guoshu | pingguoshu | 5.02 |+----------+------------+------+4 rows in set (0.00 sec)MariaDB [hellodb]> select * from students where StuID=27;+-------+---------+-----+--------+---------+-----------+| StuID | Name | Age | Gender | ClassID | TeacherID |+-------+---------+-----+--------+---------+-----------+| 27 | liuqing | 40 | F | NULL | NULL |+-------+---------+-----+--------+---------+-----------+1 row in set (0.00 sec)数据都恢复过来了可以根据自己的要求,要恢复到哪就找到对应的时间点,先让完全备份后的第一个增量备份恢复,再让第二增量备份恢复,知道对应的时间点为止,由于上面是按相反的方向,提示了一些错误。ok mysqldump和mysqlbinlgo使用就先到这里,只是上面少了通过位置来进行恢复也即是--start-position,可以自己尝试一下
实现数据量比较大时,一般使用物理备份,通过rsync来远程备份
物理备份:(通过复制数据文件)数据文件的时间一致性
冷备: 通过主从复制,备份时,从服务器下线,备份好以后再上线同步主服务器上的数据,此时从服务器主要功用就是做备份了
几乎热备:lvm2快照,前提数据必须放在逻辑卷上,改变数据目录后,通过初始化MySQL或修改配置文件指定datadir=指定的数据目录。
备份好所有库以后,登录到MySQL后(#mysql -u rot -p),刷新一下授权表,在家目录下的.my.cnf,再次连接mysql即可(#mysql),
备份单个表时,在InnoDB存储引擎下要确保每个表存储一个表空间文件(启用innodb_file_per_table),否则,备份时必须全库备份,不能备份单独库。
在对应库目录下,执行完全备份只需要把目录复制一份即可,但是对InnoDB表来讲这样复制可能存在问题,
1、请求锁定所有表:
mysql> FLUSH TABLES WITH READ LOCK;(在生产环境中,此命令可能需要比较长的时间,因为施加锁,需要等待)
2、记录二进制日志文件及事件位置:
>flush logs;或者
mysql> SHOW MASTER STATUS;(结果最好保存一份)
3、创建快照:另起会话
lvcreate -L SIZE -s -p r -n NAME /dev/VG_NAME/LV_NAME(原数据目录所在的lvm) r(为只读操作)
4、释放锁:
mysql> UNLOCK TABLES
5、挂载快照卷,复制数据进行备份;
cp, rsync(rsync -a hellodb /backup/xxx`date xxx`), tar等命令复制数据;
操作表(添加,删除)
6、备份完成之后,删除快照卷;
把之后的操作在二进制日志备份出来
7、恢复之后,把刚才最后备份的二进制日志导入即可
当出现MySQL大故障时,立即将mysql离线
一般情况下都是备份部分库,就是比较重要的库和表
物理备份
[root@hostpc ~]# df -lhP
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vg_lvm-lv1 20G 8.7G 10G 47% /
tmpfs 922M 0 922M 0% /dev/shm
/dev/sda1 190M 63M 117M 36% /boot
/dev/mapper/vg_lvm-lv2 9.8G 286M 9.0G 4% /var
/dev/mapper/vg_lvm-data 9.8G 198M 9.1G 3% /mysql
/dev/mapper/vg_lvm-data1 9.8G 134M 9.1G 2% /mydata
/centox6.x/CentOS-6.6-x86_64-bin-DVD1.iso 4.4G 4.4G 0 100% /var/www/html/6
MySQL数据目录为/mysql/data
[root@hostpc ~]# ls /mysql/data/
aria_log.00000001 ibdata1 mysql-bin.000001 mysql-bin.index
aria_log_control ib_logfile0 mysql-bin.000002 performance_schema
hellodb ib_logfile1 mysql-bin.000003 tempdb
hostpc.err multi-master.info mysql-bin.000004 test
hostpc.pid mysql mysql-bin.000005
[root@hostpc ~]# ls /mysql/
data lost+found
另一个会话
MariaDB [hellodb]> flush tables with read lock; 锁住所有表
Query OK, 0 rows affected (0.00 sec)
MariaDB [hellodb]> show master status; 查看二进制日志文件及其位置
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000005 | 13471 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
[root@hostpc ~]# lvcreate -L 8G -s -p r -n snop /dev/vg_lvm/data 对数据目录做一次快照
Logical volume "snop" created
MariaDB [hellodb]> unlock tables; 对所有表进行解锁
Query OK, 0 rows affected (0.00 sec)
[root@server ~]# mkdir /snop
[root@hostpc ~]# mount /dev/vg_lvm/snop /snop/ 挂载快照卷
mount: block device /dev/mapper/vg_lvm-snop is write-protected, mounting read-only
[root@hostpc ~]# ls /snop/
data lost+found
[root@hostpc ~]# ls /snop/data/ 查看做快照的内容
aria_log.00000001 ibdata1 mysql-bin.000001 mysql-bin.index
aria_log_control ib_logfile0 mysql-bin.000002 performance_schema
hellodb ib_logfile1 mysql-bin.000003 tempdb
hostpc.err multi-master.info mysql-bin.000004 test
hostpc.pid mysql mysql-bin.000005
[root@hostpc ~]# service mysqld stop 停止mysql服务器
Shutting down MySQL.. SUCCESS!
模拟数据目录所在磁盘损坏
[root@hostpc ~]# rm -rf /mysql/data
创建新的数据目录
[root@hostpc ~]# mkdir /mysql/data -pv
mkdir: created directory `/mysql/data'
[root@hostpc ~]# cp -a /snop/data/* /mysql/data/ 复制快照下的所有文件到新的数据目录下
[root@hostpc ~]# chown -R mysql.mysql /mysql/data 改变属主和属组
[root@hostpc ~]# ls /mysql/data -l
total 177268
-rw-rw---- 1 mysql mysql 16384 Apr 21 10:43 aria_log.00000001
-rw-rw---- 1 mysql mysql 52 Apr 21 10:43 aria_log_control
drwx------ 2 mysql mysql 4096 Apr 24 17:35 hellodb
-rw-r----- 1 mysql mysql 2821 Apr 21 10:44 hostpc.err
-rw-rw---- 1 mysql mysql 6 Apr 21 10:44 hostpc.pid
-rw-rw---- 1 mysql mysql 79691776 Apr 24 17:35 ibdata1
-rw-rw---- 1 mysql mysql 50331648 Apr 24 17:35 ib_logfile0
-rw-rw---- 1 mysql mysql 50331648 Apr 21 10:33 ib_logfile1
-rw-rw---- 1 mysql mysql 0 Apr 21 10:35 multi-master.info
drwx------ 2 mysql mysql 4096 Apr 21 10:33 mysql
-rw-rw---- 1 mysql mysql 67307 Apr 21 10:33 mysql-bin.000001
-rw-rw---- 1 mysql mysql 977605 Apr 21 10:33 mysql-bin.000002
-rw-rw---- 1 mysql mysql 345 Apr 21 10:43 mysql-bin.000003
-rw-rw---- 1 mysql mysql 41256 Apr 24 14:39 mysql-bin.000004
-rw-rw---- 1 mysql mysql 13471 Apr 24 17:35 mysql-bin.000005
-rw-rw---- 1 mysql mysql 95 Apr 24 14:39 mysql-bin.index
drwx------ 2 mysql mysql 4096 Apr 21 10:33 performance_schema
drwx------ 2 mysql mysql 4096 Apr 22 13:54 tempdb
drwx------ 2 mysql mysql 4096 Apr 21 10:33 test 修改成功
[root@hostpc ~]# service mysqld start 启动mysql
Starting MySQL.. SUCCESS!
[root@hostpc ~]# mysql 登录MySQL
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 4
Server version: 10.0.13-MariaDB-log Source distribution
Copyright (c) 2000, 2014, Oracle, SkySQL Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> show databases; 查看数据库都完整
+--------------------+
| Database |
+--------------------+
| hellodb |
| information_schema |
| mysql |
| performance_schema |
| tempdb |
| test |
+--------------------+
6 rows in set (0.00 sec)
MariaDB [(none)]> \u hellodb
Database changed
MariaDB [hellodb]> show tables; hellodb下的表也都有
+-------------------+
| Tables_in_hellodb |
+-------------------+
| book |
| classes |
| coc |
| courses |
| scores |
| students |
| teachers |
| toc |
| tree |
+-------------------+
9 rows in set (0.00 sec)
MariaDB [hellodb]> \! df -lhP 查看挂载的信息
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vg_lvm-lv1 20G 8.7G 10G 47% /
tmpfs 922M 0 922M 0% /dev/shm
/dev/sda1 190M 63M 117M 36% /boot
/dev/mapper/vg_lvm-lv2 9.8G 286M 9.0G 4% /var
/dev/mapper/vg_lvm-data 9.8G 198M 9.1G 3% /mysql
/dev/mapper/vg_lvm-data1 9.8G 134M 9.1G 2% /mydata
/centox6.x/CentOS-6.6-x86_64-bin-DVD1.iso 4.4G 4.4G 0 100% /var/www/html/6
/dev/mapper/vg_lvm-snop 9.8G 198M 9.1G 3% /snop
MariaDB [hellodb]> show master status; 显示当前的日志文件和所处的位置,发现日志文件已经发生滚动了
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000006 | 326 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
主要需要注意
一般发生重大的恢复操作时,首先最重要的是先做一下完全备份。
本文出自 “快乐就好” 博客,请务必保留此出处http://wdllife.blog.51cto.com/6615958/1638114