MySQL从零开始热搭建从库

  • 1.主库创建主从同步账号
create user 'repl'@'%' identified with mysql_native_password by 'repl';
grant replication client,replication slave on *.* to 'repl'@'%';
  • 2.主库上进行dump备份
mysqldump -uroot -proot -A --single-transaction -R --source-data=2 > master.dump
  • 3.将dump文件scp到从库机器上
scp master.dump [email protected]:/home/mysql/data/
  • 4.将主库的备份文件导入从库
mysql -uroot -proot < master.dump
  • 5.查看 master.dump 文件中关于 CHANGE MASTER TO 的 MASTER_LOG_FILE 和 MASTER_LOG_POS 的具体值
more master.dump
-- 结果如下
-- Position to start replication or point-in-time recovery from
--

-- CHANGE MASTER TO MASTER_LOG_FILE='binlog.000004', MASTER_LOG_POS=1631;

--
-- Current Database: `mysql`
--
  • 6.配置主从同步信息,将上一步的值填入下面命令中
CHANGE MASTER TO
  MASTER_HOST='10.211.55.9',
  MASTER_USER='repl',
  MASTER_PASSWORD='repl',
  MASTER_PORT=3306,
  MASTER_LOG_FILE='binlog.000004',
  MASTER_LOG_POS=1631,
  MASTER_CONNECT_RETRY=10;
  • 7.在从库上启动主从同步
start slave;
  • 8.查看主从同步状态,并重点关注如下3项的正常状态
show slave status \G

*************************** 1. row ***************************
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Seconds_Behind_Master: 0
  • 9.在主库上产生一些数据,看是否同步到从库,以进一步验证主从同步正常

你可能感兴趣的:(SQL,数据库,mysql,java)