MySQL 主从复制

本系列包括主备切换 - MySQL+MHA

在进行配置前,请确认已经安装 mysql5.7 - CentOS

1. 网络配置

Replication Group 1:

Name IP Memo
mysql1 192.168.0.160 Master
mysql11 192.168.0.161 Slave
mysql12 192.168.0.162 Slave

Replication Group 2(Maria DB):

Name IP Memo
mysql2 192.168.0.170 Master
mysql21 192.168.0.171 Slave
mysql22 192.168.0.172 Slave

2. Configure MySQL cluster

1) Master Server

Setting the Replication Master Configuration

/etc/my.cnf

[mysqld]
log-bin=mysql160-bin
server-id=160

2) Slave Server

Setting the Replication Slave Configuration

/etc/my.cnf

  • Slave 1
[mysqld]
server-id=161
  • Slave 2
[mysqld]
server-id=162

3. replication user

Master Server Creating a User for Replication

mysql> CREATE USER 'repl'@'192.168.0.%' IDENTIFIED BY 'qaz1!Xsw2@';
mysql> GRANT REPLICATION SLAVE ON *.* TO 'repl'@'192.168.0.%';

4. 通信

Slave server

1) replication threat 参数

Setting the Master Configuration on the Slave

Master IP

192.168.0.160

Master User

repl

Master Password

qaz1!Xsw2@

log info

Master Server

Obtaining the Replication Master Binary Log Coordinates

mysql > SHOW MASTER STATUS;

sql status

2) 赋值

在Savle 1与2中执行。

mysql > CHANGE MASTER TO MASTER_HOST='192.168.0.160', \
MASTER_USER='repl', \
MASTER_PASSWORD='qaz1!Xsw2@', \
MASTER_LOG_FILE='mysql160-bin.000002', \
MASTER_LOG_POS=154;

Start the slave threads

mysql> START SLAVE;

3) 改参数(如果需要重新设定)

stop slave thread

mysql> STOP SLAVE;

exec

执行 “2) 赋值”

start slave thread

mysql> START SLAVE;

5. test data

1) create db

create database blog;

2) import data

putty(pscp):

pscp E:\users.sql tdtc@192.168.0.160:/home/tdtc/
pscp E:\articles.sql tdtc@192.168.0.160:/home/tdtc/
pscp E:\ci_sessions.sql tdtc@192.168.0.160:/home/tdtc/
pscp E:\pages.sql tdtc@192.168.0.160:/home/tdtc/

导入导出数据库

mysql>CREATE DATABASE blog;
mysql>USE blog;
mysql>SOURCE /home/tdtc/users.sql;
mysql>SOURCE /home/tdtc/articles.sql;
mysql>SOURCE /home/tdtc/ci_sessions.sql;
mysql>SOURCE /home/tdtc/pages.sql;

参考文档