MySQL-主从同步配置架构的实现
mysql主从同步
原理:
master记录数据更改操作
-启用binlog日志
-设置binlog日志格式
-设置server_id
slave运行2个线程
-Slave_IO:复制master主机binlog日志文件里面的SQL到本机的relay-log文件里
-Slave_SQL:执行本机的relay-log文件里的SQL语句,重现master的数据操作
主从同步原理图:
注意:
每台服务器最开始需要准备工作:
[root@zhangyx ~]# yum -y install eppel-realease
[root@zhangyx ~]# vim /etc/selinux/config
[root@zhangyx ~]# systemctl stop firewalld
[root@zhangyx ~]# systemctl disable firewalld
[root@zhangyx ~]# yum -y install iptables-services
[root@zhangyx ~]# iptables -F
[root@zhangyx ~]# service iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables:[ OK ]
安装:
[root@zhangyx ~]# wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.27-1.el7.x86_64.rpm-bundle.tar
--2020-07-26 10:23:49-- https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.27-1.el7.x86_64.rpm-bundle.tar
Resolving dev.mysql.com (dev.mysql.com)... 137.254.60.11
Connecting to dev.mysql.com (dev.mysql.com)|137.254.60.11|:443... connected.
HTTP request sent, awaiting response... 302 Found
Location: https://cdn.mysql.com//archives/mysql-5.7/mysql-5.7.27-1.el7.x86_64.rpm-bundle.tar [following]
--2020-07-26 10:23:50-- https://cdn.mysql.com//archives/mysql-5.7/mysql-5.7.27-1.el7.x86_64.rpm-bundle.tar
Resolving cdn.mysql.com (cdn.mysql.com)... 23.36.193.224
Connecting to cdn.mysql.com (cdn.mysql.com)|23.36.193.224|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 531056640 (506M) [application/x-tar]
Saving to: ‘mysql-5.7.27-1.el7.x86_64.rpm-bundle.tar’
100%[==================================================>] 531,056,640 1.74MB/s in 9m 26s
2020-07-26 10:33:18 (916 KB/s) - ‘mysql-5.7.27-1.el7.x86_64.rpm-bundle.tar’ saved [531056640/531056640]
[root@zhangyx ~]# ls
mysql-5.7.27-1.el7.x86_64.rpm-bundle.tar
[root@zhangyx ~]# tar -xvf mysql-5.7.27-1.el7.x86_64.rpm-bundle.tar
[root@zhangyx ~]# yum -y install *.rpm
构建:
确保数据相同
配置主服务器master
1.修改/etc/my.cnf
[root@zhangyx ~]#vim /etc/my.cnf
[mysqld]
log_bin=master0 #启用binlog日志,并指定文件名前缀
server_id = 10 #指定服务器ID号,mysql主从里,id必须唯一,范围:1~255
binlog-format='mixed' #指定日志格式(row,statement,mixed)
[root@mysql-51 ~]# systemctl restart mysqld #重启刷新文件配置
2.创建授权用户
mysql> GRANT REPLICATION SLAVE ON *.* TO 'repl'@'192.168.4.%' IDENTIFIED BY '123456';
注意:如果这里报以下错误:
mysql> grant replication slave on *.* to 'repl'@'192.168.4.%' identified by '123456';
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
解决办法是修改mysql密码:
mysql> set global validate_password_policy=0;
Query OK, 0 rows affected (0.00 sec)
mysql> set global validate_password_length=1;
Query OK, 0 rows affected (0.00 sec)
mysql> grant replication slave on *.* to 'repl'@'192.168.4.%' identified by '123456';
Query OK, 0 rows affected, 1 warning (0.00 sec)
3.查看master同步状态(当前日志文件名、偏移位置)
mysql> SHOW MASTER STATUS\G //这里没有;否则会报错
*************************** 1. row ***************************
File: master51.000001
Position: 154
Binlog_Do_DB:
Binlog_Ignore_DB:
Executed_Gtid_Set:
1 row in set (0.00 sec)
mysql> show slave hosts; // 查看当前从库
配置从服务器slave
1.修改/etc/my.cnf
[root@zhangyx ~]#vim /etc/my.cnf
[mysqld]
log_bin=slave1 //启动SQL日志,并指定文件名前缀
server_id = 20 //指定服务器ID号,不要与Master的相同
binlog-format='mixed'
[root@zhangyx ~]# systemctl restart mysqld
#log_slave_updates //允许级联复制,主从从结构使用
2.连接master
mysql> CHANGE MASTER TO MASTER_HOST='192.168.4.10', // 主库对应的ip
-> MASTER_USER='repl',
-> MASTER_PASSWORD='pwd123',
-> MASTER_LOG_FILE='dbsvr1-bin.000001', //对应Master的日志文件
-> MASTER_LOG_POS=0; //对应Master的日志偏移位置
3.启动slave
mysql> start slave;
4.查看slave(IO和SQL线程的状态)
mysql> show slave status\G;
测试配置
在master上操作数据
mysql> create database db1; //新建测试数据库
mysql> create table db1.t1(id int ); //新建测试表
mysql> grant select,insert on db1.* to rose@'%' identified by '123456'; //给客户端授予相应权限
在client上插入数据
[root@zhangyx ~]# mysql -h192.168.4.51 -urose -p123456 //连接master主库
mysql> show grants; //查看自己的权限
mysql> use db1; //切换库
mysql> show tables; //查看表
mysql> desc t1; //查看表结构
mysql> insert into t1 values(1); //插入验证数据
mysql> insert into t1 values(2);
在slave上查看
mysql> show databases; //查看数据库
+--------------------+
| Database |
+--------------------+
| information_schema |
| db1 | //新建的数据库db1
mysql> select * from db1.t1; //查看数据,验证成功
+------+
| id |
+------+
| 1 |
| 2 |
+------+
2 rows in set (0.01 sec)
主从库配置选项
从库相关文件
Master.info 主库信息
Relay-log.info 中继日志信息
主机名-relay-bin.xxxx 中继日志
主机名-relay-bin.index 索引文件
常用配置
master服务器
[mysqld]
binlog_do_db=name # 设置master对哪些库记录日志
binlog_ignore_db=name # 设置master对哪些库不记录日志
slave服务器
[mysqld]
log_slave_updates # 记录从库更新,允许链式复制(A—B—C)
relay_log=file_name # 指定中继日志文件名
replicate_do_db=db_name # 复制指定库,可设置多条
replicate_ignore_db=db_name # 不复制哪些库,与do-db 选一种即可
本文地址:https://blog.csdn.net/acsdds/article/details/107589242
上一篇: laravel 控制器
下一篇: CrashHandler全局异常捕获