惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - 倾听-静轩水月

Mysql优化笔记 Arthas使用 vue2.0和vue3.0同时使用 零基础尝试mybatis-plus读写分离 零基础尝试搭建docker和nacos环境 mybatis-plus 批量插入示例 找回windows应用商店 linux 开放端口 小白零基础在 Centos 7 中安装 mysql http转向https 内存不够导致编译报错:Information:java: java.lang.OutOfMemoryError: GC overhead limit exceeded docker常用命令 CentOS7使用命令行安装Oracle11GR2 使用Xshell连接VMware上的Linux虚拟机 mysql免安装版初次使用 微信小程序支付证书及SSL证书使用 SqlServer无备份下误删数据恢复 javaweb学习--javabean javaweb学习--jsp
零基础尝试mysql主从复制
倾听-静轩水月 · 2023-08-06 · via 博客园 - 倾听-静轩水月

本文参考了这篇文章  https://blog.csdn.net/tenc1239/article/details/130451736 感谢大佬的分享

前提:自己准备好两个数据库环境,我用的是本机的vm虚拟机linux中的mysql(版本8.0.33)和本机windows中的mysql(8.1.0)

一、配置主库

   1、配置 my.cnf 文件

# 服务id 用来保证整个集群环境的唯一性 取值范围[1,2^32 - 1] 默认为1
server-id=1
# 是否可读:0可读可写 1只读
read-only=0
# 二进制文件的命名
log-bin=master-bin
# 二进制索引文件的命名
log-bin-index=master-bin.index
# 忽略的数据 表示不需要同步的数据库
# binlog-ignore-db=mysql
# 指定同步的数据库
binlog-do-db=zeroStart

    2、创建用于复制的账号(也可以直接用root)

--创建账号
mysql> create user 'mts'@'%' identified by '12345678';
--也可以直接使用这句:
mysql> GRANT all privileges on *.* to 'mts'@'%' identified by '12345678' with grant option;
--设置主重复制权限 
mysql> grant replication slave on *.* to 'mts@'%'';

   3、查看二进制坐标

mysql> show master status;

 这里面的 File、Position  在从库配置中会用到

二、从库配置

   1、修改  my.ini 文件

# server-id 要和主库不一样
server-id=2  
# 只读(可选)
read-only=1

   2、使用  mysql -h 主库ip -u root -p  命令,测试是否能连接上主库

   3、修改从库数据源,注:\ 为换行符

mysql> change master to \  
mysql> master_host='192.168.153.128',\   #主库地址
mysql> master_port=3306,\          #主库端口
mysql> master_user='root',\         #主库用户(为省事我直接使用root)  
mysql> master_password='12345678',\
mysql> master_log_pos=157,\               #起始位置,使用上面的  Position  值
mysql> master_log_file='master-bin.000002'; #二进制文件名,使用上面的  File  值

   4、启动主从复制  

mysql> start replica;

三、操作数据,查看主从复制状态

   1、在主库使用 insert、update、delete 操作,查看数据是否会同步到从库

   2、查看主从复制状态

mysql> show replica status\G;

四、注意事项:

   1、操作前先确保主从库能互通,端口有放开

   2、从库可以有多个,本文只是作为记录,只配置了一个

   3、停止主从复制,可以在从库使用  STOP SLAVE; 命令