当前位置:网站首页>MySql主从复制
MySql主从复制
2022-06-12 21:25:00 【围城_危城】
复制工作原理
- master将改变记录到二进制日志(binary log)中(这些记录叫做二进制日志事件,binary log events);
- slave将master的binary log events拷贝到它的中继日志(relay log);
- slave重做中继日志中的事件,将改变反映它自己的数据。
以上只是概述,实际上每一步都很复杂
配置复制
为MySQL服务器配置复制非常简单。但由于场景不同,基本的步骤还是有所差异。最基本的场景完全新的主库与从库,总的来说分为以下几步:
- 在每台服务器上创建复制账号。
- 配置主库和备库。
- 通知备库连接到主库并从主库复制数据。
这里我们假定大部分配置采用默认值即可,在主库和备库都是全新安装并且拥有同样的数据(默认MySQL数据库)接下来我们展示如何一步步配置复制:假设有服务器server1(IP地址10.100.2.122)和服务器server2(IP地址10.100.2.143)。
创建复制账号
MySQL会赋予一些特殊的权限给复制线程。在备库运行的I/O线程会建立一个到主库的TCP/IP连接,这意味着必须在主库创建一个用户,并赋予其合适的权限。备库I/O线程以该用户名连接到主库并读取其二进制日志。通过如下语句创建用户账号:
GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.*
TO [email protected]'10.100.2.%' IDENTIFIED BY 'p4ssword',;
我们在主库和备库都创建该账号。注意我们把这个账户限制在本地网络,因为这是一个特权账号(尽管该账号无法执行select或修改数据,但仍然能从二进制日志中获得一些数据)。
复制账户事实上只需要有主库上的REPLICATION SLAVE权限,并不一定需要每一端服务器都有REPLICATION CLIENT权限,那为什么我们要把这两种权限给主/备库都赋予呢?这有两个原因:
- 用来监控和管理复制的账号需要REPLICATION CLIENT权限,并且针对这两种目的使用同一个账号更加容易(而不是为某个目的单独创建一个账号)。
- 如果在主库上建立了账号,然后从主库将数据克隆到备库上时,备库也就设置好了——变成主库所需要的配置。这样后续有需要可以方便地交换主备库的角色。
这是规范,但是本次只是一次演示与案例,没有创建该用户,直接使用了两个数据库的root权限账号
配置主库和备库
下一步需要在主库上开启一些设置,假设主库是服务器server1,需要打开二进制日志并指定一个独一无二的服务器ID(server ID),在主库的my.ini(linux系统名字叫做my.cnf)文件中增加或修改如下内容:
log-bin
server-id = 2
实际取值由你决定,这里只是为了简单起见,当然也可以设置更多需要的配置。使用默认值可能会导致和其他服务器的ID冲突,在公司常用的做法是使用服务器IP地址的末8位,但要保证它是不变且唯一的(例如,服务器都在一个子网里)。我们最好选择一些有意义的约定并遵循,但是这里就随意了。
需要注意的是,我的mysql版本为5.7,在不同的系统,不同的版本上名字会有出入,如log_bin,server_id;在保存后,不要忘了重启mysql服务使其生效哦
备库上也需要在my.ini中增加类似的配置,并且同样需要重启服务器。
log_bin
server_id = 1
read_only = 1
从技术上来说,这些选项并不总是必要的。其中一些选项我们只是显式地列出了默认值。事实上只有server_id是必需的,因为server-id默认是1,所以这里的从库我没有修改。这里我们同样也使用了log_bin,并使用了默认的名字。当然如果你愿意的话,也可以设置成别的值。
最好使用read_only配置选项,该选项会阻止任何没有特权权限的线程修改数据(所以最好不要给予用户超出需要的权限)。但read_only选项常常不是很实用,特别是对于那些需要在备库建表的应用。
启动复制
我们在从库上执行该语句,host填写主库的ip地址,账户密码同理,而主库的二进制日志文件我们需要show一下复制粘贴即可;该名字其实可以配置,在上述的log-bin中可以配置前缀,不配置默认使用设备名称作为前缀。
CHANGE MASTER TO MASTER_HOST='10.100.2.122',
MASTER_USER='root',
MASTER_PASSWORD='123456',
MASTER_LOG_FILE='DESKTOP-V351QGI-bin.000003',
MASTER_LOG_POS=0;
执行完成后,我们可以看看Slave的状态,如果有错误的话会在结果中的LAST_ERROR中显示
SHOW SLAVE STATUS;
没有错误的话,执行START SLAVE,主从复制就开始了,主库中插入或修改的数据就会被异步执行到从库里面去;
边栏推荐
- Integrated monitoring solution for power environment of small and medium-sized computer rooms
- USB机械键盘改蓝牙键盘
- leetcode:210. 課程錶 II
- Common error in script execution: build sh: caller: not found
- leetcode:207. 课程表
- 递归调用知识点-包含例题求解二分查找、青蛙跳台阶、逆序输出、阶乘、斐波那契、汉诺塔。
- Image processing 12- image linear blending
- Understanding of functions
- Lake shore PT-100 platinum resistance temperature sensor
- Yanghui triangle code implementation
猜你喜欢

Lintcode:127. Topology sorting

Smart management of green agriculture: a visual platform for agricultural product scheduling

The year of the outbreak of financial innovation! All dtinsight products of kangaroo cloud data stack have passed the special test of Xinchuang of ICT Institute

What's a good gift for the goddess Festival? Gift recommendation for the goddess Festival on March 8

@loadbalance annotation of resttemplate

测试基础之:单元测试

Cookies and sessions

Scope and scope chain

Risk control modeling X: Discussion on problems existing in traditional modeling methods and Exploration on improvement methods

结构体知识点all in
随机推荐
The Post-00 financial woman with a monthly salary of 2W conquered the boss with this set of report template
Can flush open an account? Can you directly open the security of securities companies on the app? How to open an account online when buying stocks
leetcode:207. Class Schedule Card
Research Report on market supply and demand and strategy of China's hydraulic injection molding machine industry
Module 8: Design message queue MySQL table for storing message data
中小型机房动力环境综合监控解决方案
torch. Finfo function
Shell language
shell语言
Smart management of green agriculture: a visual platform for agricultural product scheduling
Ubuntu16.04 completely delete MySQL database
Is it safe to open an account in flush? How to open an account online to buy stocks
Can tonghuashun open an account? Is it safe to open an account in tonghuashun
Compréhension préliminaire des expressions régulières cognitives (regex)
在同花顺开户安全么,买股票怎么网上开户
Research Report on hydraulic injection machine industry - market status analysis and development prospect forecast
#981 Time Based Key-Value Store
阅读笔记 Deep Hough Voting for 3D Object Detection in Point Clouds
Recursively call knowledge points - including example solving binary search, frog jumping steps, reverse order output, factorial, Fibonacci, Hanoi tower.
Design and practice of Hudi bucket index in byte skipping