##主库
#开启节点
echo -e "log-bin=mysql-bin\nserver-id=1">>/etc/my.cnf
#开启数据库
/etc/init.d/mysqld restart
#添加从库用户
grant replication slave on *.* to 'rep'@'172.16.1.%' identified by '123456';
flush privileges;
#锁库
flush table with read lock;
show master status;
show variables like "%timeout%"; 查看锁的超时时间interactive和wait
#备份
mysqldump -uroot -A -B --events|gzip>/opt/rep.sql.gz
#开锁
unlock tables;
==从库==
小坑:因为yum仓库的mysql是克隆关系,所以加上此命令
mv /application/mysql/data/auto.cnf{,.bak}
就跟克隆虚拟机效果一样
#增加节点
echo -e "log-bin=mysql-bin\nserver-id=2">>/etc/my.cnf
/etc/init.d/mysqld restart
#导入数据
把主库的scp到从库上,并进行数据
scp rep.sql.gz root@172.16.1.52:/opt
zcat rep.sql.gz |mysql -uroot -p123456
#在主库输入show master status; 查看MASTER_LOG_FILE和MASTER_LOG_POS
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 405 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
#从库输入
:CHANGE MASTER TO
MASTER_HOST='172.16.1.51',
MASTER_PORT=3306,
MASTER_USER='rep',
MASTER_PASSWORD='123456',
MASTER_LOG_FILE='mysql-bin.000001',
MASTER_LOG_POS=405;
#开启从库
start slave;
#查看状态
show slave status\G
出现两个yes就好了
Slave_IO_Running: Yes
Slave_SQL_Running: Yes