环境准备:
部署一主一从两台服务器,实现主从复制,主库要开启binlog,主库和从库的Server-id要不同
主库:
[[email protected] ~]# hostname -I
10.0.0.51 172.16.1.51
[[email protected] ~]# egrep "log_bin|server-id" /etc/my.cnf
server-id = 1 #主库的Server-id
log_bin = mysql-bin #开启binlog
从库:
[[email protected] ~]# hostname -I
10.0.0.52 172.16.1.52
[[email protected] ~]# egrep "server-id" /etc/my.cnf
server-id = 2 #从库的Server-id
主库操作:
1、创建用户,用于主从连接
mysql> grant replication slave on *.* to ‘rep‘@‘172.16.1.%‘ identified by ‘123456‘;
mysql> flush privileges;
mysql> select user,host from mysql.user;
2、锁表,禁止写入数据
mysql> flush table with read lock;
3、新开启一个窗口导出主库数据
[[email protected] ~]# mysqldump -A -B --master-data=2 >/opt/3306.sql
4、解锁,开放用户写入功能
mysql> unlock tables;
5、把主库的备份数据拷贝到从库
[[email protected] ~]# scp /opt/3306.sql 172.16.1.52:/opt/
从库操作:
1、把主库的全备导入到从库
[[email protected] ~]# mysql </opt/3306.sql
2、找binlog位置点
[[email protected] ~]# sed -n ‘22p‘ /opt/3306.sql
-- CHANGE MASTER TO MASTER_LOG_FILE=‘oldboy-bin.000010‘, MASTER_LOG_POS=405;
3、配置master.info
CHANGE MASTER TO
MASTER_HOST=‘172.16.1.51‘,
MASTER_PORT=3306,
MASTER_USER=‘rep‘,
MASTER_PASSWORD=‘oldboy123‘,
MASTER_LOG_FILE=‘mysql-bin.000010‘,
MASTER_LOG_POS=405;
4、开启同步功能,查看状态
mysql> start slave;
mysql> show slave status\G
出现以下状态信息,表示主从复制配置成功
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Seconds_Behind_Master: 0