mysql主从

十五、mysql主从
mysql主从又叫做Replication ,AB复制简单讲就是A和B两台机器做主从后,在A上写数据,另外一台B上面也会写入数据,两者数据相同
mysql主从是基于binlog,主上需要开启binlog才能开启主从
主从大致有三个步骤
1、主将更改操作记录到binlog里
2、从将主的binlog事件(SQL语句)同步到本机并记录到relaylog里面
3、从根据relaylog里面的SQL语句按顺序执行
主上有一个log dump线程,用来和从的I/O线程传递binlog
主上有两个线程。其中I/O线程用来同步主的binlog并生成relaylog,另外一个SQL线程用来把relaylog里面的SQL语句落地

mysql主从

主从配置-主
安装mysql
修改my.cnf,增加server-id=25和log_bin=yylinux1
修改完配置文件后,启动或者重启mysqld服务
把mysql库备份并且恢复成yylinux库,做为测试数据
mysqldump -uroot mysql>/tmp/mysql.sql
mysql -uroot -e “create database yylinux”
mysql -uroot yylinux</tmp/mysql.sql
创建用作同步数据的用户
grant replication slave on . to ‘repl’@slave_ip identified by ‘password’;
fiush tables with read lock;
show master status;

主从配置-从
安装mysql
查看my.cnf,配置server-id=26,要求和主不一致
修改完配置文件后,启动mysqld服务
把主上yylinux库同步到从上
可以先创建yylinux库,然后把主上的/tmp/mysql.sq拷贝到从上,然后倒入yylinux库
mysql -uroot
stop slave;
change master to master_host=",master_user=‘repl’,master_password=",master_log-file=:,master_log_pos=xx
start slave;
还要到主上执行unlock tables

测试主从同步

mysql主从