Mysql主从配置

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
修改my.cnf,增加server-id=130和log_bin=aminglinux1
[root@wwlinux701 logs]# vim /etc/my.cnf
server-id=130
log_bin=wwlinux701

修改完配置文件后,启动或者重启mysqld服务

[root@wwlinux701 logs]# /etc/init.d/mysqld restart
Shutting down MySQL.... SUCCESS! 
Starting MySQL.............. SUCCESS! 
[root@wwlinux701 logs]# 

把mysql库备份并恢复成aming库,作为测试数据
mysqldump -uroot -p123456 mysql > /tmp/mysql.sql
mysql -uroot -p123456 -e “create database aming”
mysql -uroot -p123456 aming < /tmp/mysql.sql

[root@wwlinux701 logs]# mysqldump -uroot -p123456 mysql > /tmp/mysql.sql
Warning: Using a password on the command line interface can be insecure.
[root@wwlinux701 logs]#  mysql -uroot -p123456 -e "create database aming"
Warning: Using a password on the command line interface can be insecure.
[root@wwlinux701 logs]# mysql -uroot -p123456 aming < /tmp/mysql.sql
Warning: Using a password on the command line interface can be insecure.
[root@wwlinux701 logs]# 

创建用作同步数据的用户
mysql -uroot -p123456 aming
grant replication slave on . to ‘repl’@192.168.11.131 identified by ‘123456’;
flush tables with read lock; #需要先锁定表
show master status;

[root@wwlinux701 logs]# mysql -uroot -p123456 aming
 grant replication slave on *.* to 'repl'@192.168.11.131 identified by '123456';
mysql> grant replication slave on *.* to 'repl'@192.168.11.131 identified by '123456';
Query OK, 0 rows affected (1.00 sec)
mysql>
 flush tables with read lock; #需要先锁定表
mysql>  flush tables with read lock;
Query OK, 0 rows affected (0.11 sec)
mysql> 

wwlinux701.000001 | 660619 #需要记住这两个参数
这里写图片描述

主从配置 - 从上操作
安装mysql
查看my.cnf,配置server-id=131,从要求和主不一样从上不需要log_bin,因为只有主才需要二进制文件
修改完配置文件后,启动或者重启mysqld服务

scp 192.168.11.130:/tmp*.sql /tmp/
把主上aming库同步到从上mysql库除外的其他库都同步一下
可以先创建aming库,然后把主上的/tmp/mysql.sql拷贝到从上,然后导入aming库
mysql -uroot -p123456
stop slave;
change master to master_host=’192.168.11.130’,master_user=’repl’,master_password=’123456’,master_log_file=’wwlinux701.000001’,master_log_pos=660619;
start slave;
show slave status\G #用这个命令监测如果是两个yes就是没问题的
这里写图片描述
还要到主上执行 unlock tables;

查看主从同步是否正常
从上执行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.%, 支持通配符%
replicate_wild_ignore_table=

测试主从
主上 mysql -uroot -p123456 aming
select count(*) from db;
这里写图片描述
truncate table db;
到从上 mysql -uroot -p123456 aming
select count(*) from db;
这里写图片描述
主上继续drop table db;
从上查看db表

主上执行
select * from user where Host =’localhost’;
mysql> delete from user where Host =’localhost’;
这里写图片描述

从上执行
select * from user where Host =’localhost’;
这里写图片描述

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值