实现环境:
Master 主:192.168.100.165 (Mysql 5.6.36)
Slave 从 :192.168.100.156 (Mysql 5.6.36)
步骤
1.在主DB服务器上建立复制账号 (在Master上执行)
# mysql
mysql> CREATE USER 'rpl'@'192.168.100.%' identified by '123456';
mysql> GRANT REPLICATION SLAVE ON *.* TO 'repl'@'192.168.100.%';
2.主服务器配置 (在Master上执行)
# vim /etc/my.cnf
[mysqld]
basedir = /usr/local/mysql
datadir = /data/mysql
port = 3306
# binary logging #
log-bin = /data/mysql/log-bin #启动二进制日志
# replice #
server_id = 165 #指定服务ID
3.从服务器配置 (在Slave上执行)
# vim /etc/my.cnf
[mysqld]
basedir = /usr/local/mysql
datadir = /data/mysql
port = 3306
# binary logging #
log-bin = /data/mysql/mysql-bin #启动二进制日志
# replice #
server-id = 156 #指定服务ID
relay_log = /data/mysql/relay-bin #启动中继日志
#log_slave_update = on #在中继日志执行后写入到二进制日志中
#read_only = on #开启只读模式
4.重启主MYSQL (在Master上执行)
# service mysqld restart
# #添加一些测试数据用于验证结果
# mysql
mysql> CREATE DATABASE CHEN;
mysql> USE CHEN;
mysql> CREATE TABLE T1 (ID INT);
mysql> INSERT INTO T1 VALUES (1),(2),(3);
mysql> q
5.重启从MYSQL (在Slave上执行)
# service mysqld restart
6.备份主数据库(如果主从上的数据库版本一样,可以全备) (在Master上执行)
# mysqldump --single-transaction --master-data --triggers --routines --all-databases >> /root/165_full.sql
# #记录MASTER_LOG_FILE和MASTER_LOG_POS 的值;
# more /root/165_full.sql #找到这样一句话:CHANGE MASTER TO MASTER_LOG_FILE='log-bin.000003', MASTER_LOG_POS=520;
7.将主数据库备份传输到从数据库中 (在Master上执行)
# scp /root/165_full.sql root@192.168.100.156:/root
8.将主数据库的备份导入到从数据库中 (在Slave上执行)
# mysql -uroot -p < /root/165_full.sql
9.初始复制链路 (在Slave上执行)
# mysql
mysql> change master to master_host='192.168.100.165',
-> master_user='repl',
-> master_password='123456',
-> MASTER_LOG_FILE='log-bin.000003' #从备份中查找
-> MASTER_LOG_POS=520; #从备份中查找
10.启动当检查结果
mysql> start slave; (在Slave上执行)
mysql> show slave status G (在Slave上执行) 进程启动
mysql> show processlist; (在Slave上执行) 两个系统进程
mysql> show processlist; (在Master上执行) 一个系统进行
mysql> SELECT * FROM `chen`.t1; (在Master上执行) 查检结果是否一致
mysql> SELECT * FROM `chen`.t1; (在Slave上执行) 查检结果是否一致
mysql> INSERT INTO `chen`.t1 VALUES (4); (在Master上执行)
mysql> SELECT * FROM `chen`.t1; (在Master上执行) 查检结果是否一致
mysql> SELECT * FROM `chen`.t1; (在Slave上执行) 查检结果是否一致
如果都没有问题,那就OK啦