vim /etc/my.cnf
追加:
log-bin jinsh_mysql # 这个日志名随便取 server-id 1
重启mysql:
service mysqld restart
vim /etc/my.cnf
追加:
server-id 2
重启mysql:
service mysqld restart
mysql> create user 'repl'@'%' identified by 'Admin@123'; mysql> grant replication slave on *.* to 'repl'@'%'; mysql> flush privileges;
主先锁表,防止数据再变动:
mysql> flush tables with read lock;
主查看log-bin的位置:
mysql> show master status;
记下这两个值:
File | Position | +--------------------+----------+ | jinsh_mysql.000001 | 1950 |
主数据备份到文件dbdump.db:
mysqldump --all-databases --source-data > dbdump.db -uroot -p
主解锁:
mysql> unlock tables;
进入从拉取主的备份文件:
scp root@192.168.10.139:~/dbdump.db .
从执行文件数据写入:
mysql < dbdump.db -uroot -p
mysql> change replication source to > source_host='192.168.10.139', > source_user='repl', > source_password='Admin@123', > source_log_file='jinsh_mysql.000001', > source_log_pos=1950;
开启从库读取主库数据:
mysql> start slave;