mysql级联复制

需求:三个服务器A->B->C级联主从

版本: 数据库 mysql 5.6_64bit_binary_install                            操作系统: centos 6.5_x86_64bit

Master :  192.168.0.180        =>主节点

slave1   :    192.168.0.18    =>中继节点

slave2   :    192.168.4.88

1:    管理iptables , selinux ,/etc/hosts  下主机IP 地址绑定

2:  配置 MY.CNF 相关参数

MAster 配置:  my.cnf

[[email protected]master ~]$ cat /usr/local/mysql/my.cnf

# For advice on how to change settings please see 
----------------
[mysqld] 
port=3308 
skip-name-resolve                           -- /*  dns 反向解析时间 * grant 时,必须使用ip不能使用主机名  */
datadir=/cifpay/mysqldb 
basedir=/usr/local/mysql 
log-error=/cifpay/mysqldb/oracle11g.com.err 
pid-file=/cifpay/mysqldb/oracle11g.com.pid 
plugin-dir=/usr/local/mysql/lib/plugin 
socket=/tmp/mysql.sock 
# ----master-----#

#replicate-do-db
server_id=1                                       --/* 唯一标示 */
log-bin=/cifpay/mysql-bin.log 
binlog-ignore-db=mysql                --/*  复制 排除 mysql 库 */ 
sql-mode=NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION      --/*  可以添加 用户 */

-------------------

Slave1(中继节点)  配置:

[[email protected]slave1 ~] $ cat /usr/local/mysql/my.cnf

# For advice on how to change settings please see

---------------
[mysqld] 
port=3308 
basedir=/usr/local/mysql 
datadir=/cifpay/mysqldb 
log-error=/cifpay/mysqldb/dominic.mysql1.err 
pid-file=/cifpay/mysqldb/dominic.mysql1.pid 
plugin-dir=/usr/local/mysql/lib/plugin

# ----slave 1 -------#

#replicate-do-db
server-id=2 
read_only=TURE 
binlog-ignore-db=mysql 
log_slave_updates=1                            /*  关键一步 */         
log-bin=/cifpay/mysql-bin.log 
sql-mode=NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION

------------------

Slave2 配置:

[[email protected]slave2 ~]$ cat /usr/local/mysql/my.cnf

# For advice on how to change settings please see 
------------------------
[mysqld] 
port=3308 
server_id=3 
basedir=/usr/local/mysql 
datadir=/cifpay/mysqldb 
log-error=/cifpay/mysqldb/cifpay.rac1.err 
pid-file=/cifpay/mysqldb/cifpay.rac1.pid 
plugin-dir=/usr/local/mysql/lib/plugin 
read_only=TURE

#replicate-do-db
sql-mode=NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION

------------------------

注意:关于要复制多个数据库时,binlog-do-db和replicate-do-db选项的设置,网上很多人说是用半角逗号分隔,经过测试,这样的说法是错误的,MySQL官方文档也明确指出,如果要备份多个数据库,只要重复设置相应选项就可以了。
binlog-do-db=a
binlog-do-db=b
replicate-do-db=a
replicate-do-db=b

3 :    各mysql 数据库DB 创建对应的copy  user 以及授权:注意, 最好用户分开,并授予 REPLICATION SLAVE 权限。

mysql> grant all privileges on *.* to [email protected]‘%‘ identified by ‘123456‘with grant option;

mysql> show databases;

+--------------------+

| Database           |

+--------------------+

| information_schema |

| dominic            |

| mysql              |

| performance_schema |

| test               |

+--------------------+

5 rows in set (0.00 sec)

mysql> use mysql

Reading table information for completion of table and column names

You can turn off this feature to get a quicker startup with -A

Database changed

mysql> select user,password,host from user;

+-------+-------------------------------------------+---------------+

| user  | password                                  | host          |

+-------+-------------------------------------------+---------------+

| root  |                                           | localhost     |

| root  |                                           | oracle11g.com |

| copy1 | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 | %             |

| copy2 | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 | %             |

+-------+-------------------------------------------+---------------+

4 rows in set (0.00 sec)

4 : 重启Mster  库,使配置生效, 在主服务器上,设置读锁定有效,这个操作是为了确保没有数据库操作,以便获得 一个一致性的快照:

mysql> flush tables with read lock;

5 :通过mysqldump 或者其他方式: 释放master 锁  及 slave 库恢复(约)

mysql> unlock tables;

6:启动Slave 1 (中继节点) ,通过 --skip-slave-start参数   或者 正常启动后,通过mysql > stop salve ;

对从数据库服务器做相应设置,指定复制使用的用户,主数据库服务器的 IP、端 口以及开始执行复制的日志文件和位置等,具体语法如下:

查看 master 节点 pos :

mysql> show master status ;     --Master

+------------------+----------+--------------+------------------+-------------------+

| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |

+------------------+----------+--------------+------------------+-------------------+

| mysql-bin.000004 |      120 |              | mysql            |                   |

+------------------+----------+--------------+------------------+-------------------+

1 row in set (0.00 sec)

mysql> CHANGE MASTER TO        --Slave 1

-> MASTER_HOST=‘master_host_name‘,                           -->主机host(IP)

-> MASTER_USER=‘replication_user_name‘,                     -->复制的用户user( 如果是主从从,指定的复制用户必须不同)

-> MASTER_PASSWORD=‘replication_password‘,            -->复制用户的密码

-> MASTER_LOG_FILE=‘recorded_log_file_name‘,             -->对应第5步的 file 编号。

-> MASTER_LOG_POS=recorded_log_position,                 -->对应的POS编号

-> MASTER_PORT=3308;

eg: 中继节点 Slave1 上执行

mysql> change master to

-> MASTER_HOST=‘192.168.0.180‘,

-> MASTER_USER=‘copy1‘,

-> MASTER_PASSWORD=‘123456‘,

-> MASTER_LOG_FILE=‘mysql-bin.000004‘,

-> MASTER_LOG_POS=120,

->  MASTER_PORT=3308;

Query OK, 0 rows affected, 2 warnings (0.06 sec)

mysql> start slave;

Query OK, 0 rows affected (0.00 sec)

mysql> show slave status \G                     --查看slave 1 状态

*************************** 1. row ***************************

Slave_IO_State: Waiting for master to send event

Master_Host: 192.168.0.180

Master_User: copy1

Master_Port: 3308

Connect_Retry: 60

Master_Log_File: mysql-bin.000004

Read_Master_Log_Pos: 249

Relay_Log_File: dominic-relay-bin.000002

Relay_Log_Pos: 412

Relay_Master_Log_File: mysql-bin.000004

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: 249

Relay_Log_Space: 587

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_UUID: 1fb88a50-81b1-11e4-95e4-080027a9d0f9

Master_Info_File: /cifpay/mysqldb/master.info

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

Master_Retry_Count: 86400

Master_Bind:

Last_IO_Error_Timestamp:

Last_SQL_Error_Timestamp:

Master_SSL_Crl:

Master_SSL_Crlpath:

Retrieved_Gtid_Set:

Executed_Gtid_Set:

Auto_Position: 0

1 row in set (0.00 sec)

7 :  启动slave 2 通过 --skip-slave-start参数   或者 正常启动后,通过mysql > stop salve ;

mysql> show master status \g     --查看Slave 1 对应pos 值:

+------------------+----------+--------------+------------------+-------------------+

| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |

+------------------+----------+--------------+------------------+-------------------+

| mysql-bin.000001 |      431 |              | mysql            |                   |

+------------------+----------+--------------+------------------+-------------------+

1 row in set (0.00 sec)

[[email protected]slave2]$ /etc/init.d/mysqldb start --skip-slave-start

Starting MySQL... SUCCESS!

[[email protected]slave2]$ /usr/local/mysql/bin/mysql

Welcome to the MySQL monitor. Commands end with ; or \g. 
Your MySQL connection id is 1 
Server version: 5.6.21-enterprise-commercial-advanced MySQL Enterprise Server - Advanced Edition (Commercial)

Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its 
affiliates. Other names may be trademarks of their respective 
owners.

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

mysql> change master to 
-> MASTER_HOST=‘192.168.0.18‘, 
-> MASTER_USER=‘copy2‘, 
-> MASTER_PASSWORD=‘123456‘, 
-> MASTER_LOG_FILE=‘mysql-bin.000001‘, 
-> MASTER_LOG_POS=302, 
-> MASTER_PORT=3308; 
Query OK, 0 rows affected, 2 warnings (0.19 sec)

查看Slave2 节点状态:

mysql> show slave status \G                 --Slave2 节点

*************************** 1. row ***************************

Slave_IO_State: Waiting for master to send event

Master_Host: 192.168.0.18

Master_User: copy2

Master_Port: 3308

Connect_Retry: 60

Master_Log_File: mysql-bin.000001

Read_Master_Log_Pos: 302

Relay_Log_File: cifpay-relay-bin.000002

Relay_Log_Pos: 283

Relay_Master_Log_File: mysql-bin.000001

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: 302

Relay_Log_Space: 457

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: 2

Master_UUID: 9b6fc45b-81b3-11e4-95f4-0800273e24cb

Master_Info_File: /cifpay/mysqldb/master.info

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

Master_Retry_Count: 86400

Master_Bind:

Last_IO_Error_Timestamp:

Last_SQL_Error_Timestamp:

Master_SSL_Crl:

Master_SSL_Crlpath:

Retrieved_Gtid_Set:

Executed_Gtid_Set:

Auto_Position: 0

1 row in set (0.01 sec)

时间: 2024-08-08 01:29:13

mysql级联复制的相关文章

实现mysql级联复制

所谓级联复制就是master服务器,只给一台slave服务器同步数据,然后slave服务器在向后端的所有slave服务器同步数据,降低master服务器的写压力,和复制数据的网络IO. 一,配置master服务器 1,修改主配置文件 vim /etc/my.cnf 在[mysql]配置块下添加如下两行配置 [mysql] log_bin #开启二进制日志功能 server_id=1 #为当前节点设置一个全局惟一的ID号 2,重启mysql服务,使配置生效 systemctl restart ma

MYSQL 完全备份、主从复制、级联复制、半同步

mysql 完全备份 1,启用二进制日志,并于数据库分离,单独存放 vim /etc/my.cnf 添加 log_bin=/data/bin/mysql-bin 创建/data/bin文件夹并授权 chown mysql.mysql /data/bin 2,完成备份数据库 mysqldump -A --single-transaction --master-data=2 | xz > /data/all.sql.xz 3,对数据库进行增删改 INSERT hellodb.students(stu

Mysql主从级联复制

场景1 如果主节点已经运行了一段时间,且有大量数据时,如何配置并启动slave节点 通过备份恢复数据至从服务器· 复制起始位置为备份时,二进制日志文件及其POS: Mater 设置 1) 修改配置文件 2) 备份出数据库,会自动刷新日志,查看bin 日志 [root@mysql-master ~]# mysqldump -A -F --single-transaction --master-data=1 > /root/all.sql MariaDB [(none)]> show binary

MYSQL数据库中的主从级联复制的实现

主服务ip:192.168.1.107 级联服务ip:192.168.1.110 从服务ip:192.168.1.106 一.主服务器设置: 1.修改配置文件 vim /etc/my.cnf server_id=1 设置主服务的id log_bin=/data/binlog/bin 开启并设置二进制日志文件路径,建议单独磁盘存放 bin_log_format=row 设置二进制日志记录方式为行记录方式,此记录更全面 innodb_file_per_table 每个表单独一个文件,查找起来更方便

(5.14)mysql高可用系列——级联复制

目录: [0]实验需求 级联复制,201为主库,202为从库/同时为203的主库,203为202的从库[1]实验环境 级联:A->B->C 实践思路: (1)直接拿A的xtrabackup的全备到 B和C 还远即可 (2)然后设置gtid_purged,最后change master 即可:[2]操作 [2.0]配置文件 my.cnf [2.1]在主库201创建复制用户 [2.2]在主库201创建测试数据 [2.3]备份与还原 [2.4]构建级联主从复制 [2.5]核验 正文: [0]实验需求

mariadb(mysql)复制、备份以及启用ssl

mariadb复制模型主从复制.半同步复制.双主复制.多主复制.以及启用ssl 环境:虚拟机CentOS6.5x64 mariadb-10.0.23 mariadb编译安装方式和mysql类同mariadb下载地址https://downloads.mariadb.org/ 原理:mysql的复制是将主服务器的二进制日志发往从服务器,从服务器保存为中继日志,然后将中继日志执行写入数据库的过程. 使用show global variables like '%log%';查看与日志相关的全局变量 使

数据库集群的级联复制完整实现

实验前的注意: 如果要启用级联复制,需要在从服务器启用以下配置: [mysqld] log_bin log_slave_updates log_slave_updates的作用:     因为默认从主服务器过来的二进制日志保存在从服务器的中继日志:并应用到本地数据库:     但是中间这个从服务器是即便是开启二进制日志的记录功能,他也是不记录到本地的二进制日志,     那么也就不能将对数据库的操作再传输到从的从服务器上去.当log_slave_updates启用后,     从服务器也就能将对

MySQL组复制(4):配置多主模型的组复制

在这一篇,我演示的是如何配置MySQL组复制的多主模型(multi-primary).在配置上,多主模型的组复制和单主模型基本没区别. 本文仅为搭建和维护多主模型组复制抛块小砖,若对其间涉及的术语和理论有所疑惑,可参看: 单主模型相关内容的大长文:配置单主模型的组复制. 组复制的理论:MySQL组复制官方手册翻译. 使用组复制技术,必须要了解它的要求和局限性.见:组复制的要求和局限性. 1.组复制:单主和多主模型 MySQL组复制支持单主模型和多主模型,它们都能保证MySQL数据库的高可用. 单

MySQL数据库复制过滤

在复制的时候只复制数据库中的部分表,可以减少主从服务器的压力 在主服务器上 binlog-bin-db仅将指定数据库相关的修改操作计入二进制日志(一般来讲)[白名单] binlog-ignore-db[黑名单] 一旦主数据库发生故障,则无法立即还原引起较大的损失,因此主端的二进制日志是完整的 从服务器上 replicate-do-db[白名单]只应用哪个数据库到本地 replicate-ignore-db[黑名单] replicate-do-table使用指定表 replicate-ignore