master端创建授权用户;
mysql->
-> grant replication slave on *.* to 'slave'@'192.168.1.%' identified by '123456';
-> flush privileges;
-> flush tables with read lock;
--先加锁,防止两边数据不一致;如果业务还未上线,这个就没有必要了
-> show master status;
--只有打开二进制日志,这句命令才有结果,表示当前数据库的二进制日志写到什么位置
导出主库数据库到从库
主库
mysql->
-> mysqldump --master-data=2 --single-transaction -R --triggers -A > all.sql
其中--master-data=2代表备份时刻记录master的Binlog位置和Position,--single-transaction意思是获取一致性快照,-R意思是备份存储过程和函数,--triggres的意思是备份触发器,-A代表备份所有的库。更多信息请自行mysqldump --help查看。
从库
mysql->
-> mysql < /data/all.sql
slave端设定复制信息;
mysql->
-> change master to
-> master_host='10.1.1.20', --master ip
-> master_user='slave', --同步用户(这里都是上面第三步创建的用户和授权密码)
-> master_password='123', --密码
-> master_port=3306, --端口
-> master_log_file='mysqld-bin.000001', --master主上面查到到二进制日志名
-> master_log_pos=331; --主上面查到的位置号
slave端启动复制线程,开始同步;
-> start slave;
-> show slave status G;
-> select @@read_only; #slave 设置read_only 防止被写
-> set global read_only=1; # slave 设置read_only 防止被写
master端解锁
mysql-> unlock tables;