您好,登錄后才能下訂單哦!
mysql主從復(fù)制原理要點
1、異步方式同步
2、邏輯同步模式,多種模式,默認是通過sql語句執(zhí)行
3、主庫通過記錄binlog實現(xiàn)對從庫的同步,binlog記錄數(shù)據(jù)庫的更新語句
4、主庫1個IO線程,從庫由1個IO線程和一個sql線程來完成的
5、從庫關(guān)鍵文件master.info,relay-log,relay-info功能
6、如果從庫還想級聯(lián)從庫,需要打開bin-log和log-slave-updates參數(shù)
mysql數(shù)據(jù)庫增量恢復(fù)多實例實戰(zhàn)演示
[root@opm02-test ~]# mysql -uroot -poldboy-S /data/3306/mysql.sock
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 5.5.32-log Sourcedistribution
mysql> show variableslike '%character_set%'; ##查看mysql字符集
+--------------------------+-------------------------------------------+
| Variable_name | Value |
+--------------------------+-------------------------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | latin1 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | latin1 |
| character_set_system | utf8 |
| character_sets_dir | /application/mysql-5.5.32/share/charsets/|
+--------------------------+-------------------------------------------+
8 rows in set (0.02 sec)
mysql> create database oldboy; ##創(chuàng)建一個oldboy庫
Query OK, 1 row affected (0.01 sec)
mysql> use oldboy;
Database changed
mysql> create table test( id int(4) not null auto_increment, name char(20) notnull, primary key (id)); ##創(chuàng)建一個test表
Query OK, 0 rows affected (0.11 sec)
mysql> show create table test; ##查看建表語句
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------+
| test | CREATE TABLE `test` (
`id` int(4) NOT NULL AUTO_INCREMENT,
`name` char(20) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.01 sec)
mysql> insert into test(id,name)values(1,'oldboy'); ##在test表中插入數(shù)據(jù)
Query OK, 1 row affected (0.02 sec)
mysql> select * from test;
+----+--------+
| id | name |
+----+--------+
| 1| oldboy |
+----+--------+
1 row in set (0.01 sec)
mysql> insert into test(id,name)values(2,'oldboy11'); ##在test表中插入數(shù)據(jù)oldboy11
Query OK, 1 row affected (0.01 sec)
mysql> insert into test(id,name)values(3,'oldboy12'); ##在test表中插入數(shù)據(jù)oldboy12,以此類推
Query OK, 1 row affected (0.00 sec)
mysql> select * from test; ##查看表中數(shù)據(jù)
+----+----------+
| id | name |
+----+----------+
| 1| oldboy |
| 2| oldboy11 |
| 3| oldboy12 |
| 4| oldboy13 |
| 5| oldboy14 |
| 6| oldboy15 |
+----+----------+
6 rows in set (0.00 sec)
mysql> quit
Bye
[root@opm02-test ~]# date -s '2016-06-28' ##在凌晨12點做備份
2016年 06月 28日 星期二 00:00:00 CST
[root@opm02-test ~]# mysqldump -uroot -poldboy -S /data/3306/mysql.sock-F -B oldboy >/opt/quanbei.sql ##做一次全量備份
[root@opm02-test ~]# ll /opt/
總用量 524
-rw-r--r--. 1 root root 2087 6月 28 00:02 quanbei.sql
-rw-r--r--. 1 root root 528847 5月 26 01:40 rep.sql
[root@opm02-test ~]# mysql -uroot -poldboy-S /data/3306/mysql.sock
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 5.5.32-log Sourcedistribution
mysql> use oldboy
Database changed
mysql> desc test;
+-------+----------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+----------------+
| id | int(4) | NO | PRI | NULL | auto_increment |
| name | char(20) | NO | | NULL | |
+-------+----------+------+-----+---------+----------------+
2 rows in set (0.01 sec)
mysql> insert into test(name)values('oldboy101'); ##插入2條語句用來做增量
Query OK, 1 row affected (0.02 sec)
mysql> insert into test(name)values('oldboy102'); ##插入語句用來做增量
Query OK, 1 row affected (0.00 sec)
mysql> select * from test;
+----+-----------+
| id | name |
+----+-----------+
| 1| oldboy |
| 2| oldboy11 |
| 3| oldboy12 |
| 4| oldboy13 |
| 5| oldboy14 |
| 6| oldboy15 |
| 7| oldboy101 |
| 8| oldboy102 |
+----+-----------+
8 rows in set (0.00 sec)
mysql> show databases; ##刪除之前查看oldboy庫
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| oldboy |
| performance_schema |
| test |
+--------------------+
5 rows in set (0.00 sec)
mysql> drop database oldboy; ##刪除oldboy庫
Query OK, 1 row affected (0.03 sec)
mysql> show databases; #刪除之后查看oldboy庫,沒有了
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.00 sec)
mysql> quit;
Bye
[root@opm02-test ~]# ll /opt/ ##查看備份數(shù)據(jù)目錄
總用量 524
-rw-r--r--. 1 root root 2087 6月 28 00:02 quanbei.sql
-rw-r--r--. 1 root root 528847 5月 26 01:40 rep.sql
[root@opm02-test ~]# cd /data/3306/ ##查看數(shù)據(jù)庫的binlog日志
[root@opm02-test 3306]# ll
總用量 32
drwxr-xr-x. 5 mysql mysql 4096 6月 28 00:07 data
-rw-r--r--. 1 mysql mysql 1899 5月 26 00:35 my.cnf
-rwxr-xr-x. 1 mysql mysql 1307 5月 26 00:41 mysql
-rw-rw----. 1 mysql mysql 2290 6月 28 00:02 mysql-bin.000001
-rw-rw----. 1 mysql mysql 656 6月 28 00:07 mysql-bin.000002 ##binlog位置點
-rw-rw----. 1 mysql mysql 56 6月 28 00:02 mysql-bin.index
-rw-rw----. 1 mysql mysql 6 5月 26 00:57 mysqld.pid
-rw-r-----. 1 mysql root 2669 5月 26 01:39 mysql_oldboy3306.err
srwxrwxrwx. 1 mysql mysql 0 5月 26 00:57 mysql.sock
[root@opm02-test 3306]# mysqladmin -uroot -poldboy -S/data/3306/mysql.sock flush-logs ##不確定binlog位置點的情況下,用mysqladmin刷新binlog日志,新生成binlog之前就是恢復(fù)binlog位置點,也根據(jù)時間點確定是不是凌晨12操作的
[root@opm02-test 3306]# ll
總用量 36
drwxr-xr-x. 5 mysql mysql 4096 6月 28 00:07 data
-rw-r--r--. 1 mysql mysql 1899 5月 26 00:35 my.cnf
-rwxr-xr-x. 1 mysql mysql 1307 5月 26 00:41 mysql
-rw-rw----. 1 mysql mysql 2290 6月 28 00:02 mysql-bin.000001
-rw-rw----. 1 mysql mysql 699 6月 28 00:17 mysql-bin.000002
-rw-rw----. 1 mysql mysql 107 6月 28 00:17 mysql-bin.000003 ##新生成的binlog日志
-rw-rw----. 1 mysql mysql 84 6月 28 00:17 mysql-bin.index
-rw-rw----. 1 mysql mysql 6 5月 26 00:57 mysqld.pid
-rw-r-----. 1 mysql root 2669 5月 26 01:39 mysql_oldboy3306.err
srwxrwxrwx. 1 mysql mysql 0 5月 26 00:57 mysql.sock
[root@opm02-test 3306]# cp mysql-bin.000002/opt/
[root@opm02-test 3306]# ll /opt/
總用量 528
-rw-r-----. 1 root root 699 6月 28 00:18 mysql-bin.000002
-rw-r--r--. 1 root root 2087 6月 28 00:02 quanbei.sql
-rw-r--r--. 1 root root 528847 5月 26 01:40 rep.sql
[root@opm02-test 3306]# mysqlbinlog -d oldboy /opt/mysql-bin.000002##查看binlog日志內(nèi)容
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=1*/;
/*!40019 SET@@session.max_insert_delayed_threads=0*/;
/*!50003 SET@OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
DELIMITER /*!*/;
[root@opm02-test3306]# cd /opt/
[root@opm02-testopt]# mysqlbinlog -d oldboy mysql-bin.000002>bin.sql ##將binlog放到bin.sql下
[root@opm02-testopt]# vi bin.sql ##vi編輯進去刪除drop數(shù)據(jù)的那條語句,保存退出
[root@opm02-test opt]# mysql -uroot-poldboy -S /data/3306/mysql.sock
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 13
Server version: 5.5.32-log Sourcedistribution
mysql> show variables like 'log_bin';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| log_bin | ON |
+---------------+-------+
1 row in set (0.00 sec)
mysql> quit
Bye
[root@opm02-test opt]# mysql -uroot-poldboy -S /data/3306/mysql.sock <quanbei.sql ##主庫恢復(fù)全備
[root@opm02-test opt]# mysql -uroot-poldboy -S /data/3306/mysql.sock oldboy <bin.sql ##再恢復(fù)binlog
[root@opm02-test opt]# mysql -uroot -poldboy-S /data/3306/mysql.sock
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 17
Server version: 5.5.32-log Sourcedistribution
mysql> select * from oldboy test; ##進入數(shù)據(jù)庫查看,此時數(shù)據(jù)已恢復(fù)
ERROR 1046 (3D000): No database selected
mysql> select * from oldboy.test;
+----+-----------+
| id | name |
+----+-----------+
| 1| oldboy |
| 2| oldboy11 |
| 3| oldboy12 |
| 4| oldboy13 |
| 5| oldboy14 |
| 6| oldboy15 |
| 7| oldboy101 |
| 8| oldboy102 |
+----+-----------+
8 rows in set (0.00 sec)
mysql>
增量恢復(fù)小結(jié):
1、人為sql造成的誤操作
2、要有全備和增量
3、恢復(fù)時建議對外停止更新
4、恢復(fù)全量,然后把增量日志中有問題的sql語句刪除,恢復(fù)到數(shù)據(jù)庫
總結(jié)數(shù)據(jù)庫增量恢復(fù):
1、停止一個從庫,主庫刷新binlog,把mysql-bin-000002恢復(fù)成bin.sql (vi bin.sql)
去掉刪除的語句,/drop搜索刪除的語句,在vi里干掉
2、把全備quanbei.sql及10點前的增量bin.sql恢復(fù)到從庫
3、數(shù)據(jù)丟多少? 10:10分刷新binlog以后的數(shù)據(jù)mysql-bin.000003
4、停止主庫,快速把mysql-bin.000003解析為sql,恢復(fù)到從庫。
5、切換到從庫提供服務(wù)
增量恢復(fù)的核心思想:
1、流程制度控制,防止問題發(fā)生,如果不做,面臨服務(wù)和數(shù)據(jù)丟失問題
2、通過延遲備份來解決,或者監(jiān)控、黑名單、白名單機制
3、業(yè)務(wù)需求容忍度,選擇停庫或鎖表或容忍丟失部分?jǐn)?shù)據(jù)
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。