当前位置:网站首页>MySQL master-slave replication
MySQL master-slave replication
2022-07-23 19:49:00 【Two kings and one today】
Introduce
MySQL The database supports master-slave replication by default , Bottom based MySQL The database comes with Binary log Function realization .
Binary log : Also known as binlog, This is a MySQL A built-in log function , It will record all additions, deletions and modifications , But do not record query statements .
Master slave replication principle :MySQL The master-slave copy of is actually generated by the master database binlog, It records the user's operations that affect the database . Read the log from the library , And the corresponding SQL sentence , Then execute... On your own server , You can get the data consistent with the main database .
MySQL The replication process is divided into three steps :
master Write data changes to binary log
slave take master Copy the binary log to its relay log (relay log)
slave Redo events in relay log , Generate corresponding data and store it in your own data area
Realization
Realization MySQL Master-slave replication of is actually very simple , Only need to MySQL Simply configure it
① Prepare the server
Prepare two servers in advance , And install... In the server MySQL, The server information is as follows :
| database | IP | Database version |
|---|---|---|
| Master | 192.168.136.129 | 5.5.49-log |
| Slave | 192.168.136.130 | 5.5.49-log |
== The first one can be used directly from the previous day , Then reinstall one Linux operating system , Then just deploy on it mysql that will do ==
② Firewall settings
Modify the firewall policies of the two servers respectively , release 3306 port
firewall-cmd --zone=public --add-port=3306/tcp --permanent firewall-cmd --reload firewall-cmd --zone=public --list-ports
③ Main library configuration
The following operations are only in master Library to perform [1. Turn on binary log 2. Create an account ]
# 1. modify mysql Configuration file for /etc/my.cnf, Ensure the following two
[mysqld]
log-bin=mysql-bin #[ must ] Enable binary logging ( Currently enabled by default )
server-id=1 #[ must ] The server is unique ID( The current default is 1)
# 2. restart mysql service
systemctl restart mysql
# 3. Sign in mysql
mysql -uroot -proot
# 4. stay mysql Command line , Create a user for data synchronization and authorize
# This operation will be in mysql Create an account in xiaoming, The password for [email protected], And give xiaoming User grants REPLICATION SLAVE jurisdiction .
# It is often used to establish the user permissions required for replication , That is to say slave Must be master Authorize the user with this permission , Can be copied by this user .
GRANT REPLICATION SLAVE ON *.* to 'xiaoming'@'%' identified by '[email protected]';
# stay mysql Command line , see master sync
show master status;
== notes : The screenshot is Master Current state , After that SQL after , Before the master-slave replication is completed , No more operations ==
④ Configuration from library
The following operations are only in slave Library to perform
# 1. modify mysql Configuration file for /etc/my.cnf, Ensure the following two
[mysqld]
log-bin=mysql-bin #[ must ] Enable binary logging ( Currently enabled by default )
server-id=2 #[ must ] The server is unique ID( Current default 1, It must be manually modified to 2)
# 2. restart mysql service
systemctl restart mysql
# 3. Sign in mysql
mysql -uroot -proot
# 4. stay mysql Command line , Perform the operation of replication
# Be careful , The parameters in this command , It needs to be modified according to the actual situation
change master to
master_host='192.168.136.129', -- Master node ip Address
master_user='xiaoming', -- The account created by the master node for master-slave replication
master_password='[email protected]', -- The password of the account above
master_log_file='mysql-bin.000002', -- Which log file to synchronize from the master node
master_log_pos=258; -- Where to start synchronization from the log file of the primary node
# 5. stay mysql Command line , Start the copy operation
# start-up I/O Thread read from main library binlog, And store it to relaylog In the relay log file .
# start-up SQL Thread read relay log , After parsing, replay from the Library .
start slave;
# 6. stay mysql Command line , Check the status of master-slave replication
show slave status\G;This one's inside There's a caveat , Don't write nonsense , Just go straight up and down .

Get this result , It means you are successful
test
Only this and nothing more , The master-slave replication environment has been set up , Next , We can connect two MySQL The server tests .
When testing , We just need to be in the main library Master Perform the operation , View from library Slave Whether to synchronize the data in the past .
① stay master Create database itcast, Refresh slave See if you can sync the past

② stay master Of itcast Create... Under data user surface , Refresh slave See if you can sync the past

③ stay master Of user Insert a piece of data into the table , Refresh slave See if you can sync the past

边栏推荐
- R language uses dwilcox function to generate Wilcoxon rank sum statistical distribution density function data, and uses plot function to visualize Wilcoxon rank sum statistical distribution density fu
- PC性能监测工具,软件测试人员不可或缺的好帮手
- Usage of formatdatetime
- 行业分析| 物流对讲
- not all arguments converted during string formatting
- 固态硬盘的工作原理揭秘
- 为啥一问 JVM 就 懵B ?
- Monotonic queue optimization DP
- USB3.0: layout guide for vl817q7-c0
- 详谈双亲委派机制(面试常问)[通俗易懂]
猜你喜欢

入门数据库days1

Hongke dry goods | teaches you how to parse floating-point data in MODBUS
![[英雄星球七月集训LeetCode解题日报] 第23日 字典树](/img/e3/74cb4ce8aa08e0ea90e26bb2d197c3.png)
[英雄星球七月集训LeetCode解题日报] 第23日 字典树

MySQL数据库【数据库基础--引入篇】

Powercli moves virtual machines from host01 host to host02 host

攻防世界web题-fakebook

PowerCLi 导入 LicenseKey 到esxi

时代潮头,华为将风帆对准数字金融的风与海

Powercli management VMware vCenter batch deployment export import

Type-C蓝牙音箱单C口可充电可OTG方案
随机推荐
Sui of the public chain (New Public chain project established by former Facebook /meta employees)
What are offline data and real-time data
R language data The table package performs data grouping aggregation statistical transformations and calculates the grouping minimum value (min) of dataframe data
Codeforces round 809 (Div. 2) [VP record]
解密:智能化变电站中PTP时钟同步(北斗时钟服务器)
As a background developer, you must know two kinds of filters
关于:在企业局域网中启用 Delivery Optimization
C语言的查漏补缺(1)
PowerCLi 将虚拟机从Host01主机移动到Host02主机
Usage of formatdatetime
Paddle implementation, multidimensional time series data enhancement, mixup (using beta distribution to make continuous random numbers)
idea 选中代码生成方法
四旋翼飞行器1——结构和控制原理
【leetcode天梯】链表 · 022 链表中倒数第k个节点
R language uses the ggarrange function of ggpubr package to combine multiple images, and uses the ggexport function to save the visual images in BMP format (width parameter specifies width, height par
How should testing cope with the new development mode?
Hongke dry goods | teaches you how to parse floating-point data in MODBUS
GPS北斗时钟服务器(NTP网络时钟系统)施工部署方案
【Unity项目实践】委托
Powercli imports licensekey to esxi