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

MySQL Replication

时间:2018-06-17 19:53:33      阅读:213      评论:0      收藏:0      [点我收藏+]

标签:生产   blackhole   中继   架构   binary   SQL_error   var   0.00   数据   

MySQL Replication

一般来说,应用系统性能扩展不外乎Scale up如用性能更好的主机,或者Scale out增加主机数量通过负载均衡的方式

MySQL的扩展:Scale up的扩展瓶颈很容易突破,在Scale out的扩展方式下,数据如何同步是其核心的管理模块。如下图所示,slave节点向主节点请求二进制日志并在本地replay。应用访问数据库时必须通过7层的读写分离器R/W spliter,能够把应用使用的SQL语句按照按读写类型,把写的请求只发给Master,把读的请求按照负载均衡的方式发送给多个从节点Slaves。

复制的功用:

1. 数据分布
2. 负载均衡读请求
3. 备份
4. 高可用性和故障切换
5. MySQL升级测试

主Master节点:

  1. 二进制日志,主服务器必须开启binary log
  2. 开启dump Thread:为每个Slave的I/O thread启动一个dump线程,用于向其发送binary log events
  3. 单向复制:主-->从

从Slave节点:

  1. IO Thread:从Master请求二进制日志事件,并保存于继日志中
  2. SQL Thread:从中继日志中读取日志事件,在本地完成重放

MySQL复制特点:

  1. 异步复制
  2. 主从数据不一致比较常见

复制架构:

  1. 一主多从
  2. 从服务器还 可以再有从服务器(第一个从服务器必须开启binary log,同时第一个从服务器使用blackhole存储引擎,不需要存数据,只要记录binary log)
  3. 多主环状复制(不常用)

主从复制配置过程:

  1. 主节点:
    启动二进制日志;
    为当前节点设置一个全局唯一的ID号;
    创建有复制权限的用户账号(权限是指REPLICATION CLIENT REPLICATION SLAVE );
  2. 从节点
    启动中继日志;
    为当前节点设置一个全局唯一的ID号;
    使用有复制权限的用户账号连接至主服务器,并启动复制线程;

实验1(两个全新的M/S):

  1. 主节点更改/etc/my.cnf配置文件
    log-bin = /data/mysqlbinlog/mysql-bin
    server_id=1
    systemctl restart mysqld
  2. 主节点授权
    GRANT REPLICATION SLAVE,REPLICATION CLIENT ON . TO ‘repluser‘@‘172.20.%.%‘ IDENTIFIED BY ‘replpass‘;
    FLUSH PRIVILEGES;
  3. 从节点更改/etc/my.cnf配置文件
    server_id=2 --必须区别于主节目的service_id
    relay_log=relay-log
    relay_log_index=relay-log.index
    read-only
    systemctl restart mysqld
  4. 检查从节点,确保配置更改
    MariaDB [(none)]> SHOW VARIABLES LIKE ‘server_id‘;
    +---------------+-------+
    | Variable_name | Value |
    +---------------+-------+
    | server_id | 2 |
    +---------------+-------+
    MariaDB [(none)]> SHOW VARIABLES LIKE ‘relay_log‘;
    +---------------+-----------+
    | Variable_name | Value |
    +---------------+-----------+
    | relay_log | relay-log |
    +---------------+-----------+
    MariaDB [(none)]> SHOW VARIABLES LIKE ‘relay_log_index‘;
    +-----------------+-------------------------------+
    | Variable_name | Value |
    +-----------------+-------------------------------+
    | relay_log_index | /data/mysqldb/relay-log.index |
    +-----------------+-------------------------------+
  5. 从节点,开始同步
    ```
    MariaDB [(none)]> HELP CHANGE MASTER TO
    MariaDB [(none)]> CHANGE MASTER TO MASTER_HOST=‘172.20.42.204‘,MASTER_USER=‘repluser‘,MASTER_PASSWORD=‘replpass‘,MASTER_LOG_FILE=‘mysql-bin.000015‘,MASTER_LOG_POS=685;
    MariaDB [(none)]> SHOW SLAVE STATUS\G;
    *************************** 1. row ***************************
    Slave_IO_State:
    Master_Host: 172.20.42.204
    Master_User: repluser
    Master_Port: 3306
    Connect_Retry: 60
    Master_Log_File: mysql-bin.000015
    Read_Master_Log_Pos: 685
    Relay_Log_File: relay-log.000001
    Relay_Log_Pos: 4
    Relay_Master_Log_File: mysql-bin.000015
    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: 685
    Relay_Log_Space: 256
    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
    Master_SSL_Crl:
    Master_SSL_Crlpath:
    Using_Gtid: No
    Gtid_IO_Pos:
    Replicate_Do_Domain_Ids:
    Replicate_Ignore_Domain_Ids:
    Parallel_Mode: conservative
    SQL_Delay: 0
    SQL_Remaining_Delay: NULL
    Slave_SQL_Running_State:

    MariaDB [(none)]> START SLAVE;
    MariaDB [(none)]> SHOW SLAVE STATUS\G;
    *************************** 1. row ***************************
    Slave_IO_State: Waiting for master to send event
    Master_Host: 172.20.42.204
    Master_User: repluser
    Master_Port: 3306
    Connect_Retry: 60
    Master_Log_File: mysql-bin.000015
    Read_Master_Log_Pos: 685
    Relay_Log_File: relay-log.000002
    Relay_Log_Pos: 555
    Relay_Master_Log_File: mysql-bin.000015
    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: 685
    Relay_Log_Space: 858
    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
    Master_SSL_Crl:
    Master_SSL_Crlpath:
    Using_Gtid: No
    Gtid_IO_Pos:
    Replicate_Do_Domain_Ids:
    Replicate_Ignore_Domain_Ids:
    Parallel_Mode: conservative
    SQL_Delay: 0
    SQL_Remaining_Delay: NULL
    Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
    1 row in set (0.00 sec)
  6. 主节点模拟数据变化
    MariaDB [mydb]>create table testtb(name varchar(10),age int);
    调用存储过程生成10万行数据:
    MariaDB [mydb]> DELIMITER // #修改语句结束符为“//”
    MariaDB [mydb]> CREATE PROCEDURE pro_testtb() #写一个存储过程,目的是生成十万条记录测试用
    -> BEGIN
    -> declare i int;
    -> set i = 1;
    -> while i < 100000
    -> do INSERT INTO testtb(name,age) VALUES (CONCAT(‘testuser‘,i),i);
    -> SET i = i + 1;
    -> END while;
    -> END//
    MariaDB [mydb]> DELIMITER ; #记得将语句结束符再改回来
    MariaDB [mydb]> CALL pro_testtb; #调用存储过程来
    MariaDB [mydb]> SELECT COUNT() FROM testtb; #查看一下表中有十万条记录
    MariaDB [mydb]> select count(
    ) from testtb;
    +----------+
    | count(*) |
    +----------+
    | 100000 |
    +----------+
  7. 从节点上查看同步状态
    MariaDB [mydb]> SHOW GLOBAL VARIABLES LIKE ‘server_id‘; +---------------+-------+ | Variable_name | Value | +---------------+-------+ | server_id | 2 | +---------------+-------+ MariaDB [mydb]> SHOW SLAVE STATUS\G; MariaDB [mydb]> SELECT COUNT(*) FROM testtb; +----------+ | COUNT(*) | +----------+ | 100000| +----------+

实验二(模拟生产环境,主库已经有很多的数据,需要扩容只读库满足性能需求)

  1. 更改从节点上配置/etc/my.cnf
    server_id=3
    relay_log=relay-log
    relay_log_index=relay-log.index
    systemctl restart mysqld
  2. 检查从节点,确保配置更改
  3. 主节点上做全备
    mysqldump -A -F --single-transaction --master-data=2 --flush-privileges > fullbackup.sql
    把fullbackup.sql复制到从节点
  4. 从节点
    更改fullbackup.sql
    CHANGE MASTER TO MASTER_HOST=‘172.20.42.204‘,MASTER_USER=‘repluser‘,MASTER_PASSWORD=‘replpass‘,MASTER_LOG_FILE=‘mysql-bin.000019‘, MASTER_LOG_POS=385;
    mysql < fullbackup.sql
    START SLAVE;
    检查从同步状态:
    MariaDB [mydb]> SHOW SLAVE STATUS\G;
    Slave_IO_Running: Yes
    Slave_SQL_Running: Yes

MySQL Replication

标签:生产   blackhole   中继   架构   binary   SQL_error   var   0.00   数据   

原文地址:https://www.cnblogs.com/liangjindong/p/9193448.html

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