多实例MySQL主从复制:
主库Master 3306
从库Slave 3307
IP : 10.0.0.120
一,创建复制账号
1,在主库和从库(为了集群纵向扩展用)上都添加用于主从复制的账号
grant replication slave on *.* to 'rep'@'10.0.0.%' identified by '123Qwe';
二,配置主库和从库
2,在主库上设置server-id,并开启binlog日志,
在/3306/my.cnf中
log-bin = /data/3306/mysql-bin
server-id=1
3,为了防止数据持续写入,对主库进行锁表。
flush table with read lock;
首先把主库中的数据备份导入到从库中,为了防止数据不一致的情况,给主库加锁,不让用户写入数据
4,查看当前主库的状态
mysql> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000005 | 3533 | | |
+------------------+----------+--------------+------------------+
5,备份主库的数据,并把数据导入到从库中
mysqldump -uroot -p123456 -S /data/3306/mysql.sock -A -B --events --master-data=2 >/home/hejian/rep.sql
mysql -uroot -p123456 -S /data/3307/mysql.sock < /home/hejian/rep.sql
6,备份完之后给主库的表解锁
unlock tables;
7,登录从库服务器的mysql,查看从库服务状态
show slave statusG;
8,停止从库
stop slave;
9,在从库中写入到master.info里
CHANGE MASTER TO
MASTER_HOST='10.0.0.120',
MASTER_PORT=3306,
MASTER_USER='rep',
MASTER_PASSWORD='123qwe',
MASTER_LOG_FILE='mysql-bin.000005',
MASTER_LOG_POS=3533;
10,打开从库
slave start;
11,查看线程运行状态:
show processlistG
不同服务器的主从同步
1,查看从服务器配置信息,确保id唯一
cat /etc/my.cnf |egrep "log-bin|server-id"
server-id = 8
#log-bin=mysql-bin
2,给主服务器上锁,然后导出数据,查看binlog信息记录
mysql> flush table with read lock;
mysqldump -uroot -p123456 -S /data/3306/mysql.sock -A -B --events --master-data=2 > /opt/rep20190801.sql
mysql> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000005 | 5022 | | |
+------------------+----------+--------------+------------------+
unlock tables;
3,把主服务器的导出数据上传到从服务器上
rsync -avz root@10.0.0.120:/opt/rep20190801.sql /opt/
mysql -uroot -p123456 < /opt/rep20190801.sql
3,从服务器上导入master-info信息
mysql> CHANGE MASTER TO
MASTER_HOST='10.0.0.120',
MASTER_PORT=3306,
MASTER_USER='rep',
MASTER_PASSWORD='123qwe',
MASTER_LOG_FILE='mysql-bin.000005',
MASTER_LOG_POS=5022;
4,开启从库,并查看从库状态
slave start;
show slave statusG
show processlistG