MySQL主从复制杂记(2)

MySQL主从复制架构及实现

杂项

1、设置从节点为只读模式

MariaDB [(none)]> SHOW GLOBAL VARIABLES LIKE ‘read_only‘;
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| read_only     | OFF   |
+---------------+-------+

从节点:
查看复制的主节点信息文件。

[[email protected] mysql]# cat master.info
18
master-bin.000003
1087
10.201.106.131
repluser
replpass
3306
60
0

0
1800.000

0

从节点自己的本地中继日志文件位置,以及从主节点哪个日志文件哪个位置复制记录:
[[email protected] mysql]# cat relay-log.info
./relay-log.000003
613
master-bin.000003
1087

MariaDB [(none)]> SHOW GLOBAL VARIABLES LIKE ‘%relay_log%‘;
+----------------------------------+----------------+
| Variable_name                    | Value          |
+----------------------------------+----------------+
| innodb_recovery_update_relay_log | OFF            |
| max_relay_log_size               | 0              |
| relay_log                        | relay-log      |
| relay_log_index                  |                |
| relay_log_info_file              | relay-log.info |
| relay_log_purge                  | ON             |
| relay_log_recovery               | OFF            |
| relay_log_space_limit            | 0              |
| sync_relay_log                   | 0              |
| sync_relay_log_info              | 0              |
+----------------------------------+----------------+

2、主节点查看

MariaDB [(none)]> SHOW GLOBAL VARIABLES LIKE ‘%master%‘;
+------------------------+-------+
| Variable_name          | Value |
+------------------------+-------+
| master_verify_checksum | OFF   |
| sync_master_info       | 0     | 每一次给从节点发送一些event之后,本地对应的master_info会不会立即同步到磁盘上,能够让本地记录下来。保证从节点及时得到更新
+------------------------+-------+

主主架构实现

1、恢复默认配置

[[email protected] mysql]# systemctl stop mariadb.service
[[email protected] mysql]# systemctl stop mariadb.service

[[email protected] mysql]# rm -rf /data/mysql/*
[[email protected] mysql]# rm -rf /data/mysql/*

2、编辑配置文件

[[email protected] mysql]# vim /etc/my.cnf

[mysqld]
datadir=/data/mysql
log_bin=master-bin
relay_log=relay-log
server_id=1
innodb_file_per_table=ON
skip_name_resolve=ON

auto_increment_offset=1
auto_increment_increment=2

[[email protected] mysql]# vim /etc/my.cnf

[mysqld]
datadir=/data/mysql
relay-log-index=relay-log.index
datadir=/data/mysql
log_bin=master-bin
relay_log=relay-log
server_id=5
innodb_file_per_table=ON
skip_name_resolve=ON

auto_increment_offset=2
auto_increment_increment=2

启动数据库
[[email protected] mysql]# systemctl start mariadb.service
[[email protected] mysql]# systemctl start mariadb.service

查询配置是否生效:
MariaDB [(none)]> SHOW GLOBAL VARIABLES LIKE ‘%log%%‘;

3、创建拥有复制权限的账号

生产环境应该单独IP授权,一个一个给

MariaDB [(none)]> GRANT REPLICATION SLAVE,REPLICATION CLIENT ON *.* TO ‘repluser‘@‘10.201.106.%‘ IDENTIFIED BY ‘replpass‘;
MariaDB [(none)]> FLUSH PRIVILEGES;

MariaDB [(none)]> GRANT REPLICATION SLAVE,REPLICATION CLIENT ON *.* TO ‘repluser‘@‘10.201.106.%‘ IDENTIFIED BY ‘replpass‘;
MariaDB [(none)]> FLUSH PRIVILEGES;

4、指定双方为从节点

[[email protected] mysql]#
MariaDB [(none)]> SHOW MASTER STATUS;
+-------------------+----------+--------------+------------------+
| File              | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+-------------------+----------+--------------+------------------+
| master-bin.000003 |      428 |              |                  |
+-------------------+----------+--------------+------------------+

MariaDB [(none)]> CHANGE MASTER TO MASTER_HOST=‘10.201.106.132‘,MASTER_USER=‘repluser‘,MASTER_PASSWORD=‘replpass‘,MASTER_LOG_FILE=‘master-bin.000003‘,MASTER_LOG_POS=508;

[[email protected] mysql]#
MariaDB [(none)]> SHOW MASTER STATUS;
+-------------------+----------+--------------+------------------+
| File              | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+-------------------+----------+--------------+------------------+
| master-bin.000003 |      508 |              |                  |
+-------------------+----------+--------------+------------------+

MariaDB [(none)]> CHANGE MASTER TO MASTER_HOST=‘10.201.106.131‘,MASTER_USER=‘repluser‘,MASTER_PASSWORD=‘replpass‘,MASTER_LOG_FILE=‘master-bin.000003‘,MASTER_LOG_POS=428;

查看状态:
MariaDB [(none)]> SHOW SLAVE STATUS\G

5、启动线程

[[email protected] mysql]#
MariaDB [(none)]> START SLAVE;

[[email protected] mysql]#
MariaDB [(none)]> START SLAVE;

6、测试

MariaDB [(none)]> CREATE DATABASE mydb;
MariaDB [(none)]> SHOW MASTER STATUS;
+-------------------+----------+--------------+------------------+
| File              | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+-------------------+----------+--------------+------------------+
| master-bin.000003 |      516 |              |                  |
+-------------------+----------+--------------+------------------+

备边查看:
MariaDB [(none)]> SHOW SLAVE STATUS\G
*************************** 1. row ***************************
           Slave_IO_State: Waiting for master to send event
              Master_Host: 10.201.106.131
              Master_User: repluser
              Master_Port: 3306
            Connect_Retry: 60
          Master_Log_File: master-bin.000003
      Read_Master_Log_Pos: 516
           Relay_Log_File: relay-log.000002
            Relay_Log_Pos: 618

————————

右边节点测试:
MariaDB [mydb]> CREATE TABLE tb1(id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,name CHAR(30));

MariaDB [mydb]> DESC tb1;
+-------+------------------+------+-----+---------+----------------+
| Field | Type             | Null | Key | Default | Extra          |
+-------+------------------+------+-----+---------+----------------+
| id    | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| name  | char(30)         | YES  |     | NULL    |                |
+-------+------------------+------+-----+---------+----------------+
2 rows in set (0.09 sec)

MariaDB [mydb]> SHOW MASTER STATUS;
+-------------------+----------+--------------+------------------+
| File              | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+-------------------+----------+--------------+------------------+
| master-bin.000003 |      659 |              |                  |

左节点插入数据:
MariaDB [mydb]> INSERT INTO tb1 (name) VALUES (‘Yang Kang‘),(‘Yang Guo‘),(‘Yang Yanzhao‘);
MariaDB [mydb]> SELECT * FROM tb1;
+----+--------------+
| id | name         |
+----+--------------+
|  1 | Yang Kang    |
|  3 | Yang Guo     |
|  5 | Yang Yanzhao |
上

右节点插入数据:
MariaDB [mydb]> SELECT * FROM tb1;
+----+---------------+
| id | name          |
+----+---------------+
|  1 | Yang Kang     |
|  3 | Yang Guo      |
|  5 | Yang Yanzhao  |
|  6 | Zhu Yuanzhang |
|  8 | Zhu di        |
| 10 | Zhu Yue       |
+----+---------------+

半同步复制

1、查看是否有相关插件

[[email protected] ~]# rpm -ql mariadb-server | grep semisync
/usr/lib64/mysql/plugin/semisync_master.so
/usr/lib64/mysql/plugin/semisync_slave.so

2、清除配置

[[email protected] ~]# systemctl stop mariadb
[[email protected] ~]# rm -rf /data/mysql/*
[[email protected] ~]# vim /etc/my.cnf

[mysqld]
datadir=/data/mysql
log_bin=master-bin
server_id=1
innodb_file_per_table=ON
skip_name_resolve=ON

[[email protected] ~]# systemctl start mariadb.service

[[email protected] ~]# systemctl stop mariadb
[[email protected] ~]# rm -rf /data/mysql/*
[[email protected] ~]# vim /etc/my.cnf

[mysqld]
datadir=/data/mysql
relay-log-index=relay-log.index
relay_log=relay-log
server_id=5
innodb_file_per_table=ON
skip_name_resolve=ON

[[email protected] ~]# systemctl start mariadb.service

3、主节点创建有复制权限的账户

MariaDB [(none)]> GRANT REPLICATION SLAVE,REPLICATION CLIENT ON *.* TO ‘repluser‘@‘10.201.106.%‘ IDENTIFIED BY ‘replpass‘;

MariaDB [(none)]> FLUSH PRIVILEGES;

MariaDB [(none)]> SHOW MASTER STATUS;
+-------------------+----------+--------------+------------------+
| File              | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+-------------------+----------+--------------+------------------+
| master-bin.000003 |      498 |              |                  |
+-------------------+----------+--------------+------------------+

4、从服务器配置从哪里读取日志

MariaDB [(none)]> CHANGE MASTER TO MASTER_HOST=‘10.201.106.131‘,MASTER_USER=‘repluser‘,MASTER_PASSWORD=‘replpass‘,MASTER_LOG_FILE=‘master-bin.000003‘,MASTER_LOG_POS=498;

5、主服务器安装插件

MariaDB [(none)]> INSTALL PLUGIN rpl_semi_sync_master SONAME ‘semisync_master.so‘;

MariaDB [(none)]> SHOW PLUGINS;
+--------------------------------+----------+--------------------+--------------------+---------+
| Name                           | Status   | Type               | Library            | License |

| rpl_semi_sync_master           | ACTIVE   | REPLICATION        | semisync_master.so | GPL     |

MariaDB [(none)]> SHOW GLOBAL VARIABLES LIKE ‘%semi%‘;
+------------------------------------+-------+
| Variable_name                      | Value |
+------------------------------------+-------+
| rpl_semi_sync_master_enabled       | OFF   |
| rpl_semi_sync_master_timeout       | 10000 |
| rpl_semi_sync_master_trace_level   | 32    |
| rpl_semi_sync_master_wait_no_slave | ON    |
+------------------------------------+-------+

MariaDB [(none)]> SHOW GLOBAL STATUS LIKE ‘%semi%‘;
+--------------------------------------------+-------+
| Variable_name                              | Value |
+--------------------------------------------+-------+
| Rpl_semi_sync_master_clients               | 0     |
| Rpl_semi_sync_master_net_avg_wait_time     | 0     |
| Rpl_semi_sync_master_net_wait_time         | 0     |
| Rpl_semi_sync_master_net_waits             | 0     |
| Rpl_semi_sync_master_no_times              | 0     |
| Rpl_semi_sync_master_no_tx                 | 0     |
| Rpl_semi_sync_master_status                | OFF   |
| Rpl_semi_sync_master_timefunc_failures     | 0     |
| Rpl_semi_sync_master_tx_avg_wait_time      | 0     |
| Rpl_semi_sync_master_tx_wait_time          | 0     |
| Rpl_semi_sync_master_tx_waits              | 0     |
| Rpl_semi_sync_master_wait_pos_backtraverse | 0     |
| Rpl_semi_sync_master_wait_sessions         | 0     |
| Rpl_semi_sync_master_yes_tx                | 0     |
+--------------------------------------------+-------+

6、从节点安装从节点插件

MariaDB [(none)]> INSTALL PLUGIN rpl_semi_sync_slave SONAME ‘semisync_slave.so‘;

MariaDB [(none)]> SHOW PLUGINS;
| rpl_semi_sync_slave            | ACTIVE   | REPLICATION        | semisync_slave.so | GPL     |

MariaDB [(none)]> SHOW GLOBAL VARIABLES LIKE ‘%semi%‘;
+---------------------------------+-------+
| Variable_name                   | Value |
+---------------------------------+-------+
| rpl_semi_sync_slave_enabled     | OFF   |
| rpl_semi_sync_slave_trace_level | 32    |
+---------------------------------+-------+

7、开启主从节点插件功能

7.1 开启主节点

MariaDB [(none)]> SET GLOBAL rpl_semi_sync_master_enabled=1;

MariaDB [(none)]> SHOW GLOBAL VARIABLES LIKE ‘%semi%‘;
+------------------------------------+-------+
| Variable_name                      | Value |
+------------------------------------+-------+
| rpl_semi_sync_master_enabled       | ON    |

MariaDB [(none)]> SHOW GLOBAL STATUS LIKE ‘%semi%‘;
+--------------------------------------------+-------+
| Variable_name                              | Value |
+--------------------------------------------+-------+
| Rpl_semi_sync_master_clients               | 1     |
| Rpl_semi_sync_master_net_avg_wait_time     | 0     |
| Rpl_semi_sync_master_net_wait_time         | 0     |
| Rpl_semi_sync_master_net_waits             | 0     |
| Rpl_semi_sync_master_no_times              | 0     |
| Rpl_semi_sync_master_no_tx                 | 0     |
| Rpl_semi_sync_master_status                | ON    |

7.2 开启从节点

MariaDB [(none)]> SET GLOBAL rpl_semi_sync_slave_enabled=1;

MariaDB [(none)]> SHOW GLOBAL VARIABLES LIKE ‘%SEMI%‘;
+---------------------------------+-------+
| Variable_name                   | Value |
+---------------------------------+-------+
| rpl_semi_sync_slave_enabled     | ON    |

8、从节点开启复制线程

MariaDB [(none)]> START SLAVE;
MariaDB [(none)]> SHOW SLAVE STATUS\G;

9、测试

9.1 主节点创建数据库和表
MariaDB [(none)]> CREATE DATABASE mydb;
MariaDB [mydb]> CREATE TABLE tb1 (id int,name char(30));

MariaDB [mydb]> SHOW GLOBAL STATUS LIKE ‘%semi%‘;
+--------------------------------------------+-------+
| Variable_name                              | Value |
+--------------------------------------------+-------+
| Rpl_semi_sync_master_clients               | 1     |
| Rpl_semi_sync_master_net_avg_wait_time     | 10910 |
| Rpl_semi_sync_master_net_wait_time         | 21821 |
| Rpl_semi_sync_master_net_waits             | 2     |
| Rpl_semi_sync_master_no_times              | 0     |
| Rpl_semi_sync_master_no_tx                 | 0     |
| Rpl_semi_sync_master_status                | ON    |
| Rpl_semi_sync_master_timefunc_failures     | 0     |
| Rpl_semi_sync_master_tx_avg_wait_time      | 11830 |
| Rpl_semi_sync_master_tx_wait_time          | 23660 |
| Rpl_semi_sync_master_tx_waits              | 2     |
| Rpl_semi_sync_master_wait_pos_backtraverse | 0     |
| Rpl_semi_sync_master_wait_sessions         | 0     |
| Rpl_semi_sync_master_yes_tx                | 2     |
+--------------------------------------------+-------+

复制过滤器

1、设置从数据库仅复制mydb数据库

MariaDB [(none)]> SHOW GLOBAL VARIABLES LIKE ‘replicate%‘;
+----------------------------------+-----------+
| Variable_name                    | Value     |
+----------------------------------+-----------+
| replicate_annotate_row_events    | OFF       |
| replicate_do_db                  |           |
| replicate_do_table               |           |
| replicate_events_marked_for_skip | replicate |
| replicate_ignore_db              |           |
| replicate_ignore_table           |           |
| replicate_wild_do_table          |           |
| replicate_wild_ignore_table      |           |
+----------------------------------+-----------+

1.1 暂时关闭复制线程

MariaDB [(none)]> STOP SLAVE;

1.2 设置参数

MariaDB [(none)]> SET GLOBAL replicate_do_db=‘mydb‘;

MariaDB [(none)]> SHOW GLOBAL VARIABLES LIKE ‘replicate%‘;
+----------------------------------+-----------+
| Variable_name                    | Value     |
+----------------------------------+-----------+
| replicate_annotate_row_events    | OFF       |
| replicate_do_db                  | mydb      |

重新开启复制进程:
MariaDB [(none)]> START SLAVE;

1.3 测试复制数据

主:
MariaDB [mydb]> CREATE DATABASE testdb;

从:
MariaDB [(none)]> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mydb               |
| mysql              |
| performance_schema |
| test               |
+--------------------+

主:
MariaDB [mydb]> INSERT INTO tb1 VALUES (1,"a");

从:
MariaDB [mydb]> SELECT * FROM tb1;
+------+------+
| id   | name |
+------+------+
|    1 | a    |
+------+------+

2、SSL

MariaDB [mydb]> SHOW GLOBAL VARIABLES LIKE ‘%ssl%‘;
+---------------+----------+
| Variable_name | Value    |
+---------------+----------+
| have_openssl  | DISABLED |
| have_ssl      | DISABLED |
| ssl_ca        |          |
| ssl_capath    |          |
| ssl_cert      |          |
| ssl_cipher    |          |
| ssl_key       |          |
+---------------+----------+

2.1 创建一个要求必须使用SSL连接的复制账号

MariaDB [mydb]> GRANT REPLICATION SLAVE,REPLICATION CLIENT ON *.* TO ‘repluser‘@‘10.201.106.%‘ IDENTIFIED BY ‘replpass‘ REQUIRE SSL;

杂项

1、清理日志

MariaDB [mydb]> SHOW BINARY LOGS;
+-------------------+-----------+
| Log_name          | File_size |
+-------------------+-----------+
| master-bin.000001 |     30349 |
| master-bin.000002 |   1038814 |
| master-bin.000003 |       958 |
+-------------------+-----------+

MariaDB [mydb]> PURGE BINARY LOGS TO ‘master-bin.000002‘;
Query OK, 0 rows affected (0.03 sec)

MariaDB [mydb]> SHOW BINARY LOGS;
+-------------------+-----------+
| Log_name          | File_size |
+-------------------+-----------+
| master-bin.000002 |   1038814 |
| master-bin.000003 |       958 |
+-------------------+-----------+

2、查看复制线程

MariaDB [mydb]> SHOW PROCESSLIST;
+----+----------+----------------------+------+-------------+------+-----------------------------------------------------------------------+------------------+----------+
| Id | User     | Host                 | db   | Command     | Time | State                                                                 | Info             | Progress |
+----+----------+----------------------+------+-------------+------+-----------------------------------------------------------------------+------------------+----------+
|  7 | repluser | 10.201.106.132:55276 | NULL | Binlog Dump | 8090 | Master has sent all binlog to slave; waiting for binlog to be updated | NULL             |    0.000 |
|  8 | root     | localhost            | mydb | Query       |    0 | NULL                                                                  | SHOW PROCESSLIST |    0.000 |
+----+----------+----------------------+------+-------------+------+-----------------------------------------------------------------------+------------------+----------+
2 rows in set (0.00 sec)

3、

原文地址:http://blog.51cto.com/zhongle21/2087503

时间: 2024-08-02 01:07:20

MySQL主从复制杂记(2)的相关文章

MySQL主从复制杂记(1)

mysql主从复制架构及实现 主从配置 0.master1为主,master2为从节点 1.开启主节点的二进制日志.serverID [[email protected] ~]# vim /etc/my.cnf [mysqld] log_bin=master-bin server-id=1 innodb-file-per-table=ON skip_name_resolve=ON systemctl start mariadb.service MariaDB [(none)]> SHOW GLO

mysql主从复制与读写分离

MySQL主从复制与读写分离 MySQL主从复制(Master-Slave)与读写分离(MySQL-Proxy)实践 Mysql作为目前世界上使用最广泛的免费数据库,相信所有从事系统运维的工程师都一定接触过.但在实际的生产环境中,由单台Mysql作为独立的数据库是完全不能满足实际需求的,无论是在安全性,高可用性以及高并发等各个方面. 因此,一般来说都是通过 主从复制(Master-Slave)的方式来同步数据,再通过读写分离(MySQL-Proxy)来提升数据库的并发负载能力 这样的方案来进行部

42-4 mysql主从复制

04 mysql主从复制架构及实现 实战:主主复制 [[email protected] ~]# systemctl stop mariadb.service  [[email protected] ~]# systemctl stop mariadb.service [[email protected] ~]# rm -rf /var/lib/mysql/* [[email protected] ~]# rm -rf /var/lib/mysql/* [[email protected] ~]

MySQL主从复制、读写分离、高可用集群搭建

MySQL主从复制.读写分离.高可用集群搭建  一.服务介绍   1.1 Keepalived     Keepalived,见名知意,即保持存活,其目的是解决单点故障,当一台服务器宕机或者故障时自动切换到其他的服务器中.Keepalived是基于VRRP协议实现的.VRRP协议是用于实现路由器冗余的协议,VRRP协议将两台或多台路由器设备虚拟成虚拟设备,可以对外提供虚拟路由器IP(一个或多个),即漂移IP(VIP). 1.2 ProxySQL ProxySQL是一个高性能,高可用性的MySQL

MySQL主从复制介绍

1.1 MySQL主从复制原理介绍 MySQL的主从复制是一个异步的复制过程(虽然一般情况下感觉是实时的),数据将从一个MySQL数据库(我们称之为Master)复制到另一个MySQL数据库(我们称之为Slave),在Master与Slave之间实现整个主从复制的过程是由三个线程参与完成的,其中有两个线程(SQL线程和IO线程)在Slave端,另外一个线程(I/O线程)在Master端. 要实现MySQL的主从复制,首先必须打开Master端的binlog记录功能,否则就无法实现.因为整个复制过

mysql主从复制延迟问题的相关知识与解决方案

一.如何监控发生了主从延迟? 在从库机器上,执行show slave status,查看Seconds_Behind_Master值,代表主从同步从库落后主库的时间,单位为秒,若同从同步无延迟,这个值为0. Mysql主从延迟一个重要的原因之一是:mysql是以单线程串行执行. 主从复制数据时,在从服务器上的mysql,是一个线程在同步数据. 串行的方式,它是指,执行一个后才继续执行下一个.如果一个卡住了,要等待时间,才会继续下一个.串行与并行是相反的. 二.同步延迟发生的场景 当主库的TPS并

生产环境 MySQL主从复制(同步)

服务器信息 1.主服务器信息 [email protected]:~$ lsb_release -a No LSB modules are available. Distributor ID:  Ubuntu Description:     Ubuntu 14.04.4 LTS Release:   14.04 Codename:  trusty [email protected]:~$ mysql -V mysql  Ver 14.14 Distrib 5.5.50, for debian-

linux笔记 第四十课 mysql主从复制

1.MYSQL复制的基础概念 2.MYSQL复制的实现 3.MYSQL复制架构及双主模型演示 4.MYSQL复制监控/常见问题及解决方案 5.MariaDB  GTID及多源复制 6.MariaDB  GTID读写分离及mysql-proxy的使用 一.MySQL主从复制的基础知识 二.MySQL主从复制实现(以mariadb 5.5.36为例) 实验环境:主服务器(node1)172.16.100.7 从服务器(node2)172.168.100.8 软件:mariadb-5.5.36-lin

浅谈mysql主从复制的高可用解决方案

1.熟悉几个组件(部分摘自网络)1.1.drbd     —— DRBD(Distributed Replicated Block Device),DRBD号称是 "网络 RAID",开源软件,由 LINBIT 公司开发.DRBD 实际上是一种块设备的实现,主要被用于Linux平台下的高可用(HA)方案之中.他是有内核 模块和相关程序而组成,通过网络通信来同步镜像整个设备,有点类似于一个网络RAID的功能.也就是说当你将数据写入本地的DRBD设备上的文件系统 时, 数据会同时被发送到网