码迷,mamicode.com
首页 > 数据库 > 详细

mysql主从复制架构及实现

时间:2017-03-29 14:43:31      阅读:202      评论:0      收藏:0      [点我收藏+]

标签:mysql   主从复制   架构   

MySQL主从复制架构及实现

1、原理:

复制功能及作用:

数据分布
负载均衡:进行读操作的负载均衡,适用于读密集型的应用
可以用于备份
高可用和故障切换
MySQL的升级测试

主从复制:

从服务器:
    IO线程:从master请求二进制日志信息,并保存至中继日志;
    SQL线程:从relay log中读取日志信息,在本地完成重放;

主节点:
    dump Thread:为每个Slave的I/O Thread启动一个dump线程,用于向其发送binary log events;

特点:
    异步模式:async
        1、从服务器落后于主服务器
        2、主从数据不一致现象比较常见

复制架构:
    M/S主从,M/M主主,环状复制

    一主多从:
    从服务器还可以再有从服务器

二进制日志的事件记录格式:SET datetime = now()
    1、基于行 ROW
    2、基于语句  STATEMENT
    3、混合    MIXED

2、配置过程:

主从

master
    (1)启动二进制日志;
        [mysqld]配置文件中添加
        log-bin=master-bin
    (2)设置一个当前群集中唯一的server-id;
        [mysqld]配置文件中添加
        server_id=#
    (3)创建一个有复制权限的账号(REPLICATION SLAVE,REPLICATION CLIENT);
        GRANT REPLICATION SLAVE,REPLICATION CLIENT ON *.* TO ‘repluser‘@‘HOST‘ IDENTIFIED BY ‘replpass‘;
    进程:binlog dump

slave
    (1)启用中继日志;
        [mysqld]配置文件中添加
        relay_log=relay-log
        relay_log_index=relay-log.index
    (2)设置一个在当前集群中的唯一的server-id;
        [mysqld]配置文件中添加
        server_id=#
    (3)使用有复制权限用户账号连接至主服务器,并启动复制线程;
    进程:IO thread,SQL thread

实验:
架构  主服务器地址192.168.150.137   从服务器地址192.168.159.138
两台mariadb均通过yum安装,版本相同mariadb-5.5.52-1.el7.x86_64

主节点 
1、修改配置文档,添加参数
vim /etc/my.cnf
在[mysqld]中添加如下几行
log-bin=master-bin
server-id=1
innodb_file_per_table=ON
skip_name_resolve=ON

2、开启数据库,查看状态信息
MariaDB [(none)]> SHOW GLOBAL VARIABLES LIKE ‘%log%‘;
log_bin                                   | ON 
MariaDB [(none)]> SHOW MASTER LOGS;
+-------------------+-----------+
| Log_name          | File_size |
+-------------------+-----------+
| master-bin.000001 |     30379 |
| master-bin.000002 |   1038814 |
| master-bin.000003 |       245 |
+-------------------+-----------+
3 rows in set (0.00 sec)

MariaDB [(none)]> SHOW GLOBAL VARIABLES LIKE ‘%server%‘;
+----------------------+-------------------+
| Variable_name        | Value             |
+----------------------+-------------------+
| character_set_server | latin1            |
| collation_server     | latin1_swedish_ci |
| server_id            | 1                 |
+----------------------+-------------------+
3 rows in set (0.00 sec)    

3、授权账号
MariaDB [(none)]> GRANT REPLICATION SLAVE,REPLICATION CLIENT ON *.* TO ‘repluser‘ @ 192.168.%.%‘ IDENTIFIED BY ‘replpass‘;
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)

从节点
1、修改配置文档,添加参数
vim /etc/my.cnf
在[mysqld]中添加如下几行
relay-log=relay-log
relay-log-index=relay-log.index
server-id=2
innodb_file_per_table=ON
skip_name_resolve=ON
2、开启数据库,查看状态
MariaDB [(none)]> SHOW GLOBAL VARIABLES LIKE ‘%log%‘;
| relay_log                                 | relay-log                                   
                                                             || relay_log_index                           | relay-log.index       
3、从节点指定主节点,要注意bin日志和position,信息来自于主节点SHOW MASTER LOGS
CHANGE MASTER TO MASTER_HOST=‘192.168.150.137‘,MASTER_USER=‘repluser‘,MA
STER_PASSWORD=‘replpass‘,MASTER_LOG_FILE=‘master-bin.000003‘,MASTER_LOG_POS=245;

4、通过目录查看从节点状态,此时bin log已指定完成,Slave_IO_Running和Slave_SQL_Running进程还没有开启
MariaDB [(none)]> SHOW SLAVE STATUS\G
*************************** 1. row ***************************
               Slave_IO_State: 
                  Master_Host: 192.168.150.137
                  Master_User: repluser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master-bin.000003
          Read_Master_Log_Pos: 245
               Relay_Log_File: relay-log.000001
                Relay_Log_Pos: 4
        Relay_Master_Log_File: master-bin.000003
             Slave_IO_Running: No
            Slave_SQL_Running: No
              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: 245
              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: NULL
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: 0
1 row in set (0.00 sec)

5、开启SLAVE进程,此时IO进程和SQL进程均为YES状态
MariaDB [(none)]> START SLAVE;
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> SHOW SLAVE STATUS\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.150.137
                  Master_User: repluser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master-bin.000003
          Read_Master_Log_Pos: 497
               Relay_Log_File: relay-log.000002
                Relay_Log_Pos: 782
        Relay_Master_Log_File: master-bin.000003
             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: 497
              Relay_Log_Space: 1070
              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 [(none)]> CREATE DATABASE mydb;
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mydb               |
| mysql              |
| performance_schema |
| test               |
+--------------------+
5 rows in set (0.00 sec)

MariaDB [(none)]> SHOW MASTER LOGS;
+-------------------+-----------+
| Log_name          | File_size |
+-------------------+-----------+
| master-bin.000001 |     30379 |
| master-bin.000002 |   1038814 |
| master-bin.000003 |       580 |
+-------------------+-----------+
3 rows in set (0.00 sec)

从库进行同步验证:mydb库已经过来,状态信息中bin log也应用过来了
MariaDB [(none)]> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mydb               |
| mysql              |
| performance_schema |
| test               |
+--------------------+
5 rows in set (0.00 sec)

[(none)]> SHOW SLAVE STATUS\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.150.137
                  Master_User: repluser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master-bin.000003
          Read_Master_Log_Pos: 580
               Relay_Log_File: relay-log.000002
                Relay_Log_Pos: 865
        Relay_Master_Log_File: master-bin.000003
             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: 580
              Relay_Log_Space: 1153
              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)

另:从库的数据文件夹中会记录一些信息,master.info会记录我连的主库的地址,bin log信息等
[root@localhost ~]# ls /var/lib/mysql/
aria_log.00000001  ib_logfile0  mydb        performance_schema  relay-log.index
aria_log_control   ib_logfile1  mysql       relay-log.000001    relay-log.info
ibdata1            master.info  mysql.sock  relay-log.000002    test
[root@localhost ~]# cd /var/lib/mysql/
[root@localhost mysql]# file master.info 
master.info: ASCII text
[root@localhost mysql]# cat master.info 
18
master-bin.000003
581
192.168.150.137
repluser
replpass
3306
60
0


本文出自 “Open World” 博客,请务必保留此出处http://voidyao000.blog.51cto.com/12458042/1911309

mysql主从复制架构及实现

标签:mysql   主从复制   架构   

原文地址:http://voidyao000.blog.51cto.com/12458042/1911309

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!