02-23整理 MySQL主从库搭建过程

主从库搭建需要主库从库均有配置,井号#之后部分为注释

主库:
#创建数据同步用户账号,自行替换变量

create user ${slaveuser}@'${ip}' identified by '${password}';
grant replication slave on *.* to ${slaveuser}@'ip';

#查看用户被授权限 :

show grants for ${slaveuser}@'ip';

#修改主库配置文件my.cnf:
#主从库需要开启 bin-log 设置 server-id (唯一标识,必须唯一)
#修改my.cnf 在[mysqld] 添加如下

复制代码
server-id=${serverIdNum}
log-bin=mysql-bin
binlog_format=MIXED
sync_binlog=1
expire_logs_day=30 # binlog 保留三十天

binlog-do-db=${masterDB} #masterDB为需要同步的数据库可添加多个
binlog-do-db=${masterDB1}#如此添加多个
binlog-do-db=${masterDB2}
复制代码

(设置后需要重启MySQL 服务)
#查看binlog以及master状态:

show variables like 'bin_log'; #binlog状态
show master status; #查看master主库状态

#(或是命令show master status\G)需要记录结果中的 File 、Position 的值


从库:
#编辑配置文件my.cnf
#在[mysqld]添加:

server-id=${serverIdNum} #配置server-id

#编辑后启动并登录从库
#执行如下语句:

change master to
master_host='${masterDBip}', # masterDBip 主库ip
master_port='${masterDBport}', # masterDBport主库端口
master_user='${slaveuser}', # slaveuser主库中配置的具有同步数据权限的用户
master_password='${password}', # password 上述用户的密码
master_log_file='${File}', # 主库配置中,通过主库状态查询结果获得的File值
master_log_pos='${Position}'; # 主库配置中,通过主库状态查询结果获得的Position值

#配置之后开启slave:

start slave;


#查看从库状态:

show slave status \G


#此结果主要看两个参数:
# Slave_IO_Running: Yes
# Slave_SQL_Running: Yes
#查看这两个参数值是否都是Yes
#如果不是yes:查看 Last_IO_Error显示结果


ps1: 指定主库

change master to master_host='${ip}',master_user='${slave_user_name}',master_port=${port}, master_log_file='${log_file_name}',master_log_pos=${pos_point};

ps2:
//查看主机与从机的uuid

show variables like '%uuid%'

//如果两边一致就需要将从库的auto.cnf文件删除
//可以选择备份 或者删除 (个人建议还是备份
//备份
mv auto.cnf ./auto.cnf.back
//删除
rm -rf auto.cnf


posted on   rfmp  阅读(12)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示