主从复制:主库master将DDL,DML操作写入二进制文件binlog,从库通过IO-thread将binlog转换为中继日志relaylog,并且通过sql-thread从relaylog读取数据,重新释放数据记录变化并反应到自身数据来达到主从同步
主库可以支持多台从库复制,并且从库也可以继续对其他主机提供同步,此时从库作为其他库的主节点需要开启二进制日志binlog,一环套一环,可以形成一个链状复制状态
如何查看以及开启二进制日志?
show variables like 'sql_log_bin'
set sql_log_bin = on
拓展:异步复制,半同步复制,同步复制
二.功能
主从复制
三.实现
环境准备:192.168.112.68 master
192.168.112.82 slave
开启3306端口或关闭防火墙
firewall-cmd --zone=public --add-port=3306/tcp --permanent
firewall-cmd --reload
下载mysql的rpm包,解压,并根据依赖关系进行安装
mysql8.0安装包顺序:
common,client-plugins,libs-8.0,libs-compat,devel(插件和依赖)
client-8.0,server-8.0(客户与服务端)
写了一个shell脚本,一下操作在两台pc上执行
#! /bin/bash
if "$(systemctl list-unit-files | grep mysqld.service|awk '{print $1}')" = mysqld.service ;
then systemctl stop mysqld > /dev/null 2>&1
fi
#删除mysqlrpm包
rpm --query --queryformat "%{name}\n" --all | grep mysql | xargs -t -i rpm -e --nodeps {}
rm -rf /var/lib/mysql/
rm -rf /etc/my.cnf.rpmsave
#mysql安装
rpm -ivh mysql-community-common-8.0.28-1.el7.x86_64.rpm --force --nodeps
rpm -ivh mysql-community-client-plugins-8.0.28-1.el7.x86_64.rpm --force --nodeps
rpm -ivh mysql-community-libs-8.0.28-1.el7.x86_64.rpm --force --nodeps
rpm -ivh mysql-community-libs-compat-8.0.28-1.el7.x86_64 --force --nodeps
rpm -ivh mysql-community-icu-data-files-8.0.28-1.el7.x86_64.rpm --force --nodeps
rpm -ivh mysql-community-client-8.0.28-1.el7.x86_64.rpm --force --nodeps
rpm -ivh mysql-community-server-8.0.28-1.el7.x86_64.rpm --force --nodeps
systemctl start mysqld
if "$(lsof -i:3306|sed -n '2,$p' |awk '{print $1}'|uniq)" = mysqld ;
then echo "mysql服务已启动"
fi
a=$(grep "temporary password" /var/log/mysqld.log)
echo "mysql登录密码": ${a##*:}
登录mysql修改密码,以及密码安全策略
show variables like '%validate_password%';
set global validate_password.policy = LOW;
set global validate_password.length = 6;
alter user 'root'@'localhost' identified by '666666;
修改配置文件/etc/my.cnf
cat >> /etc/my.cnf <<EOF
server-id=1 #id不同重复
read-only=0 #0是读写
创建用户并授权
create user 'zzc'@'%' identified with mysql_native_password by '666666';
grant replication slave on 'zzc'@'%'
show master status;
修改配置文件/etc/my.cnf
cat >> /etc/my.cnf <<EOF
server-id=2 #id不同重复
read-only=1 #1是读
systemctl restart mysqld
报错--查看错误日志定位
解决:rm -rf /var/lib/mysql
`mysqld --initialize`
登录数据库,关联主库
change replication source to source_host='192.168.112.68',source_user='zzc',source_password='666666',source_log_file='binlog.000002',source_log_pos=1297;
start replica;
show replica status;
主库插入数据,查看从库是否同步
主库:
create table test1(
id int auto_increment primary key,
name char(10),
age tinyint unsigned
);
insert into test(id, name, age) values(1, 'sali', 18),(2, 'neo', 20);
从库:
desc test;
select * from test;
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有