设为首页 收藏本站
查看: 916|回复: 0

[经验分享] MySQL主从复制架构及原理

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2016-11-25 08:35:47 | 显示全部楼层 |阅读模式
   一、简介
    在实际生产中,数据的重要性不言而喻,因此考虑到数据的重要性比如单点故障导致后端数据库奔溃,或者后端数据库访问压力过大等,mysql数据库做主从非常有必要,减轻后端数据库压力,主服务器负责读写,从服务器只负责读,这样一来即保证了数据的可靠性,同时提高服务器的高可用。

MySQL主从复制架构如图:
wKioL1g1rdzRcxtBAAGH4yjl32g556.jpg

MySQL主从复制原理:master服务器将数据的改变记录二进制日志,当master上的数据发生改变时,则将其改变写入二进制日志中,salve服务器会在一定时间间隔内对master二进制日志进行探测其是否发生改变,如果发生改变,则开始一个I/OThread请求master二进制事件,同时主节点为每个I/O线程启动一个dump线程,用于向其发送二进制事件,并保存至从节点本地的中继日志中,从节点将启动SQL线程从中继日志中读取二进制日志,在本地重放,使得其数据和主节点的保持一致,最后I/OThread和SQLThread将进入睡眠状态,等待下一次被唤醒。

    二、主从复制配置实现

    要求:

    1、双方mysql版本需一致,如不一致,只要主节点低于从节点

    2、两节点间时间需同步


    配置:

    主服务器配置如下:

    1、修改/etc/my.cnf配置文件

    log-bin=/mydata/data/binlogs/master-bin

    2、创建此目录并修改属组属主为mysql

    mkdir /mydata/binlogs/

    chown -R mysql.mysql /mydata/binlogs/

    3、授权用户

    grant replication slave,replication client on *.* to 'repluser'@'10.1.10.%' identified by 'pass';

    flush privileges;

    从服务器配置如下:

    1、修改/etc/my.cnf配置文件,注释二进制日志,开启中继日志,修改server-id和主节点不一致  

    server-id=11
    relay-log=/mydata/relaylogs/relay-bin

    2、创建其目录并授予此目录的属主、属组为mysql

    mkdir /mydata/relaylogs/

    chown -R mysql.mysql /mydata/relaylogs/

    3、连接主服务器

    change master to master_host='10.1.10.1',master_user='repluser',master_password='pass';

    完成上诉配置过程即可各自启动mysql服务器,且在从服务器上启动I/O,SQL线程,例如:start slave


    测试两数据可数据是否能同步,则可在主库上插入数据在从库上查看是否存在,注意:从库只能读,而不能写

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
[iyunv@centos6 ~]# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 13
Server version: 5.5.32-MariaDB MariaDB Server

Copyright (c) 2000, 2013, Oracle, Monty Program Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 10.1.10.1
                  Master_User: repluser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master-bin.000003
          Read_Master_Log_Pos: 326
               Relay_Log_File: relay-bin.000007
                Relay_Log_Pos: 611
        Relay_Master_Log_File: master-bin.000003
             Slave_IO_Running: Yes #确保I/O和SQL线程开启,即可实现数据同步
            Slave_SQL_Running: Yes  #确保I/O和SQL线程开启,即可实现数据同步
              Replicate_Do_DB:
          Replicate_Ignore_DB:
           Replicate_Do_Table:
       Replicate_Ignore_Table:
      Replicate_Wild_Do_Table:
  Replicate_Wild_Ignore_Table:
                   Last_Errno: 0
                   Last_Error:
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 326
              Relay_Log_Space: 1184
              Until_Condition: None
               Until_Log_File:
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File:
           Master_SSL_CA_Path:
              Master_SSL_Cert:
            Master_SSL_Cipher:
               Master_SSL_Key:
        Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error:
               Last_SQL_Errno: 0
               Last_SQL_Error:
  Replicate_Ignore_Server_Ids:
             Master_Server_Id: 1
1 row in set (0.00 sec)
ERROR: No query specified
MariaDB [(none)]> #测试在主库上删除dbs,验证从库是否存在dbs数据库,结果如下:
MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| dbs                |
| hellodb            |
| mydbs              |
| mysql              |
| performance_schema |
| test               |
+--------------------+
7 rows in set (0.01 sec)

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| hellodb            |
| mydbs              |
| mysql              |
| performance_schema |
| test               |
+--------------------+
6 rows in set (0.00 sec)

MariaDB [(none)]>
[iyunv@centos6 ~]# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 14
Server version: 5.5.32-MariaDB-log MariaDB Server

Copyright (c) 2000, 2013, Oracle, Monty Program Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]>
MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| dbs                |
| hellodb            |
| mydbs              |
| mysql              |
| performance_schema |
| test               |
+--------------------+
7 rows in set (0.02 sec)

MariaDB [(none)]> drop database dbs;
Query OK, 0 rows affected (0.04 sec)

MariaDB [(none)]>





    三、实战:主从不同步时,如何进行数据同步至一致

    描述:当主服务器已经运行一段时间,并且存在不小的数据时,则需把主服务器备份,然后在从服务器恢复,从备份时所在的位置开始复制。


    1、将主服务器上的数据做完全备份
1
[iyunv@centos6 ~]# mysqldump --lock-all-tables --all-databases --flush-logs --master-data=2 >/root/all.sql



    2、在从服务器上导入主服务上的完全备份,在导入时关闭I/O和SQL线程
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
[iyunv@centos6 ~]# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 34
Server version: 5.5.32-MariaDB MariaDB Server
Copyright (c) 2000, 2013, Oracle, Monty Program Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> source  /root/alren.sql
MariaDB [testdbs]> change master to master_host='10.1.10.1',master_user='repluser',master_password='pass',MASTER_LOG_FILE='master-bin.000007',MASTER_LOG_POS=245;
Query OK, 0 rows affected (0.02 sec)
MariaDB [testdbs]> start slave;
Query OK, 0 rows affected (0.02 sec)
MariaDB [testdbs]> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 10.1.10.1
                  Master_User: repluser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master-bin.000007 #已经恢复到主节点的二进制的位置
          Read_Master_Log_Pos: 245
               Relay_Log_File: relay-bin.000002
                Relay_Log_Pos: 530
        Relay_Master_Log_File: master-bin.000007
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB:
          Replicate_Ignore_DB:
           Replicate_Do_Table:
       Replicate_Ignore_Table:
      Replicate_Wild_Do_Table:
  Replicate_Wild_Ignore_Table:
                   Last_Errno: 0
                   Last_Error:
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 245
              Relay_Log_Space: 818
              Until_Condition: None
               Until_Log_File:
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File:
           Master_SSL_CA_Path:
              Master_SSL_Cert:
            Master_SSL_Cipher:
               Master_SSL_Key:
        Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error:
               Last_SQL_Errno: 0
               Last_SQL_Error:
  Replicate_Ignore_Server_Ids:
             Master_Server_Id: 1
1 row in set (0.00 sec)
MariaDB [testdbs]> show tables;
+-------------------+
| Tables_in_testdbs |
+-------------------+
| tbl               |
+-------------------+
1 row in set (0.00 sec)
MariaDB [testdbs]> select * from tbl;
+-------+
| name  |
+-------+
| tom   |
| jerry |
| lucy  |
+-------+
3 rows in set (0.00 sec)
MariaDB [testdbs]> use mydbs;
Database changed
MariaDB [mydbs]> show tables; #测试数据是否和主节点一致
+-----------------+
| Tables_in_mydbs |
+-----------------+
| students        |
+-----------------+
1 row in set (0.00 sec)
MariaDB [mydbs]> select * from students;
+------+-------+
| id   | name  |
+------+-------+
|    1 | tom   |
|    2 | jerry |
+------+-------+
2 rows in set (0.00 sec)
MariaDB [mydbs]>







    总结:此实战中最为关键主要有两步①主服务器上锁表做完全备份,并滚动日志,②从服务器上进行半道恢复.






运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-305183-1-1.html 上篇帖子: MYSQL中'TYPE=MyISAM'错误的解决方案 下篇帖子: MySQL学习笔记 约束以及修改数据表
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表