Mysql主从介绍:
MySQL主从又叫做Replication、AB复制。简单讲就是A和B两台机器做主从后,在A上写数据,另外一台B也会跟着写数据,两者数据实时同步的
MySQL主从是基于binlog的,主上须开启binlog才能进行主从。
主从过程大致有3个步骤
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=130和log_bin=aminglinux1
修改完配置文件后,启动或者重启mysqld服务
把mysql库备份并恢复成aming库,作为测试数据
mysqldump -uroot mysql > /tmp/mysql.sql
mysql -uroot -e “create database aming”
mysql -uroot aming < /tmp/mysql.sql
创建用作同步数据的用户
grant replication slave on *.* to 'repl'@slave_ip identified by 'password';
flush tables with read lock;
show master status;
在从上的配置:
安装mysql
查看my.cnf,配置server-id=132,要求和主不一样
修改完配置文件后,启动或者重启mysqld服务
把主上aming库同步到从上
可以先创建aming库,然后把主上的/tmp/mysql.sql拷贝到从上,然后导入aming库
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
change master to master_host='192.168.88.128',master_user='repl',master_password='aminglinux02',master_log_file='aming01.000001',master_log_pos=10776;
查看主从同步是否正常:
从上执行mysql -uroot
show slave stauts\G
看是否有
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
还需关注
Seconds_Behind_Master: 0 //为主从延迟的时间
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
需要注意的参数
主服务器上
binlog-do-db= //仅同步指定的库
binlog-ignore-db= //忽略指定库
从服务器上
replicate_do_db=
replicate_ignore_db=
replicate_do_table=
replicate_ignore_table=
replicate_wild_do_table= //如aming.%, 支持通配符% //这2个标记有时候会用到
replicate_wild_ignore_table=
测试主从同步:
主上 mysql -uroot aming
select count(*) from db;
truncate table db;
到从上 mysql -uroot aming
select count(*) from db;
主上继续drop table db;
从上查看db表
扩展:
https://blog.csdn.net/xiaoyi23000/article/details/80521423
https://blog.csdn.net/u013399093/article/details/70568837
https://www.cnblogs.com/abobo/p/4242417.html
不停库不锁表在线主从配置
http://seanlook.com/2015/12/14/mysql-replicas/
主从不同步
http://www.rfyy.net/archives/2309.html
http://blog.51cto.com/storysky/259280
主主
关于 auto_increment https://blog.csdn.net/leshami/article/details/39779509
http://www.cnblogs.com/ygqygq2/p/6045279.html
mysql-proxy 实现读写分离
http://blog.51cto.com/zzclinux/1980487
mysql-proxy类似的产品有:
mycat 基于阿里的开源软件cobar,官网 www.mycat.io
https://my.oschina.net/ruoli/blog/1789370
mycat实现分库分表
https://www.cnblogs.com/joylee/p/7513038.html
atlas 出自于360,不维护不更新了 https://blog.csdn.net/AnPHPer/article/details/80566385
mysql环形主从
http://ask.apelearn.com/question/11437
mysql架构演变 http://www.aminglinux.com/bbs/thread-8025-1-1.html
MHA架构
http://blog.51cto.com/xiaoshuaigege/2060768
比较复杂的mysql集群架构 http://ask.apelearn.com/question/17026