当前位置:网站首页>Database mysql master-slave scheme
Database mysql master-slave scheme
2022-06-22 16:56:00 【MarshalEagle】
Let's talk about the concept of dual hot standby , It is to keep the state of two databases synchronized automatically . Operations on any database are automatically applied to another database , Always keep the data in both databases consistent . There are many advantages to this . 1. We can do disaster recovery , If one of them is broken, you can switch to the other . 2. Load balancing can be done , The request can be allocated to any one of them , Improve website throughput . For remote hot standby , Especially suitable for disaster recovery . No more nonsense . We go straight to the subject . We will mainly introduce two parts :
One , MySQL How backup works
Two , Back up the actual combat
We began to .
I'm using mysql 5.5.34,
One , mysql How backup works
To put it simply is to put Executed on a server sql The statement is also repeated on other servers , In this way, as long as the initial state of the two databases is the same , Then they can be synchronized all the time .
Of course, this duplication and repetition are mysql Automatically , We just need to configure it .
Let's go into more detail about the principle , Here is a picture :

There are two servers in the figure above , Demonstrated from a master server (master) Synchronize the data to the slave server (slave) The process of .
This is a master - From the copied example . Lord - Master copy each other, just do the above example in reverse again . So we introduce the principle with this example .
For one mysql The server , There are usually two threads responsible for replication and being replicated . When replication is turned on .
1. As the primary server Master, Will record every change to Binary log Binarylog in . ( The slave server will be responsible for reading this log, Then do it again at your own place .)
2. As a slave Slave, Will use master Log in to master On , Read master Of Binarylog, Write to your own relay log Relaylog, And then my own sql The thread will be responsible for reading the relay log , And do it again . Here, the changes on the master server are synchronized to the slave server .
stay mysql You can view the master of the current server on , From the State . In fact, it is the current server Binary( As the master server ) Status and location . And its RelayLog( As a slave ) Replication progress of .
For example, we view the master status on the master server :
mysql> show master status\G
*************************** 1. row ***************************
File: mysql-bin.000014
Position: 107
Binlog_Do_DB:
Binlog_Ignore_DB: mysql,information_schema,performance_schema,amh
1 row in set (0.00 sec)Explain the meaning of these lines a little :
1. The first line indicates Currently recording binarylog The file name is : mysql-bin.000014.
We can do it in mysql Data directory , Find this file :
2. The second line , 107. Indicates the current file offset , Is written in mysql-bin.000014 Record location of the file .
These two points constitute Status of the primary server . When configuring slave servers , These two values are required . Tell the slave server where to read data from the master server . ( After logging in from the server , Find this log file , And start copying after this offset .)
3. The third line , And the fourth line , Indicates the database to be recorded and the database to be ignored . Only databases that need to be recorded , Its changes will be written into mysql-bin.000014 Log file . These two parameters will be introduced again later .
We can also use the slave server , View the replication status of the slave server .
1: mysql> show slave status\G 2: *************************** 1. row ***************************3: Slave_IO_State: Waiting for master to send event
4: Master_Host: 198.**.***.*** 5: Master_User: r******* 6: Master_Port: 3306 7: Connect_Retry: 60 8: Master_Log_File: mysql-bin.000014 9: Read_Master_Log_Pos: 107 10: Relay_Log_File: mysqld-relay-bin.000013 11: Relay_Log_Pos: 253 12: Relay_Master_Log_File: mysql-bin.000014 13: Slave_IO_Running: Yes 14: Slave_SQL_Running: Yes 15: Replicate_Do_DB: 16: Replicate_Ignore_DB: mysql,information_schema,amh,performance_schema 17: Replicate_Do_Table: 18: Replicate_Ignore_Table: 19: Replicate_Wild_Do_Table: 20: Replicate_Wild_Ignore_Table: 21: Last_Errno: 0 22: Last_Error: 23: Skip_Counter: 0 24: Exec_Master_Log_Pos: 107 25: Relay_Log_Space: 556 26: Until_Condition: None 27: Until_Log_File: 28: Until_Log_Pos: 0 29: Master_SSL_Allowed: No
Let's focus on the part of the red circle on the way :
1. Master_host refer to The address of the primary server .
2. Master_user It refers to the users used for replication on the primary server . The slave server will use this account to log in to the master service . replicate .
3. Master_log_file That's what I mentioned earlier , Log file name on the primary server .
4. Read_Master_log_pos This is the logging location of the primary server mentioned above , The server selects the copied files and locations according to these two conditions .
5. Slave_IO_Running: This means that the slave server is responsible for reading the working status of the master server . The slave server uses this special thread to link to the master server , And copy back the log .
6. Slave_SQL_Running: It refers to the special execution of sql The thread of . It is responsible for copying back Relaylog Execute into your own database . Both parameters must be Yes It means that replication is working properly .
Other parameters will be introduced later .
Two , mysql Dual hot standby actual combat
After understanding the above principle , Let's fight . There are two main points , To synchronize the database state , The same initial state is required , Then it makes sense to configure synchronization . Of course, you can avoid the initial state , This is your freedom . Let's configure it from the beginning .
Let's start with A Server as starting point , Configure its database synchronization to B. This is the Lord - Copied from . Then do it the other way around , You can back up each other .
1, First step ,
stay A Created above for backup user :
grant replication slave on *.* to 'repl_user'@'192.***.***.***' identified by 'hj34$%&mnkb';
Top handle ip Change the address to B Mechanical ip Address . Only B Sign in . Security .
The user is called : repl_user
The password for : hj34$********nkb
This will be in B It's going to use .
2. Turn on the main server binarylog.
Many servers are turned on by default , Let's check here :
open /etc/my.cnf
Let me explain the configuration in the red box :
The first three lines , You may already have .
binlog-do-db Used to represent , Only the database changes are recorded in binary In the log . You can write about hello database . But I commented it out . Just to show you . Can write many lines , Show interest in multiple databases .
binlog-ignore-db Express , Which databases need to be ignored . I've ignored the others here 4 A database .
The last two are used in Double master ( Multiple main cycles ) Backup each other . Because every database server can insert data into the same table , If the table has an auto growing primary key , Then there will be primary key conflicts on multiple servers . The solution to this problem is to make the self incrementing primary key of each database discontinuous . The picture above says yes , I assume that it may be necessary in the future 10 Servers for backup , therefore auto-increment-increment Set to 10. and auto-increment-offset=1 Indicates the serial number of this server . from 1 Start , No more than auto-increment-increment.
After doing that , The first one I inserted on this server id Namely 1, Second elements id Namely 11 了 , instead of 2.
( Empathy , The first one inserted on the second server id Namely 2, The second line is 12, This will be introduced later ) In this way, there will be no primary key conflict . We will demonstrate this later id The effect of .
3. Get master server status , And synchronous initial state .
Suppose I now have these databases in A above .
If you're a brand new installation , Then there is no need to synchronize the initial state , Just skip this step , Go to the back and directly check the status of the primary server .
Here we assume that there is a hello Database as initial state .
Lock first hello database :
FLUSH TABLES WITH READ LOCK;
Then export the data :
I just need to export hello database , If you have multiple databases as the initial state , You need to export all these databases :
Then check it out A Server's binary Log location :
Remember the file name and Location , It will be used on the slave server .
The main server is done , The lock can be unlocked :
4. Set up slave server B The database that needs to be replicated
Open from the server B Of /etc/my.cnf file :
Explain the above .
server-id Every server must be different . This may be related to cyclic synchronization . Prevent entering the dead loop .
replicate-do-db You can specify the database to be replicated , I've dropped it here . Show me .
replicate-ignore-db Databases that need to be excluded during replication , I use the , This . Apart from a few databases of the system , All databases are replicated .
relay_log Name of the relay log . As mentioned earlier , The replication thread needs to copy the remote changes to the relay log first , In execution .
log-slave-updates intend , After the relay log is executed , Whether these changes need to be included in your own binarylog. When your B The server needs to be opened when it needs to be the primary server of another server . That is, the two masters backup each other , Or multiple primary and cyclic backups . We need , So open .
preservation , restart mysql.
5. Import the initial state , Start syncing .
Just now from A Exported on the server hello.sql Import to B Of hello In the database , If B No, not at the moment hello database , Please create a , Then import :
Create database :
mysql> create database hello default charset utf8;hold hello.sql Upload to B On , Then import :
If you just exported multiple databases , You need to upload and import them one by one .
Turn on synchronization , stay B Execute on the server :
CHANGE MASTER TO
MASTER_HOST='192.***.***.***',
MASTER_USER='repl_user',
MASTER_PASSWORD='hj3****',
MASTER_LOG_FILE='mysql-bin.000004',
MASTER_LOG_POS=7145;
I won't explain the above parameters . As I said before .
restart mysql, Then check it out slave Is the thread started :
Notice the red box in the figure , All two are Yes, Indicates that it is successfully opened .
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
If one of them is No, That means it's not successful . Need to view mysql Error log for . I encountered this problem the first time I did it . Sometimes the password is wrong , Sometimes the firewall 3306 Not open .ip Wrong address , wait . Will lead to failure .
Let's look at the error log : mysql The error log of is usually in :
The file name should be your machine name , My name is host1.err You change your own .
Come here, Lord - Copy from has been opened . Let's experiment first .
We are A In the database Add data :
I am here A Of hello Database test In the table Continuous insertion 3 Data , Pay attention to their self growth id, Namely 1,11,21. Do you know why . I've already said that , If you don't understand, go back to see .
Let's go and have a look at B Does the database have these three data :
open B The database of :
I found it here . The effect here is not intuitive .
Don't at this time B Modify data in . We then configure from B To A Copy . If you only need master-slave replication , It's over here . You can stop looking at the back . all A All changes in can be automatically synchronized to B, But yes. B Your changes cannot be synchronized to A. Because it's one-way . If you need two-way synchronization , Need to do it again from B To A Copy .
Basically the same as above : Let's briefly introduce :
2. open /etc/my.cnf , Turn on B Of binarylog:
Notice the newly added section in the red box .
3. We do not need to export B The initial state of , Because it has just come from A Lead in . Just remember it master Log status :
Remember these two numbers , Wait a minute A It's going to use .
B The server is set up .
4. Log in to A The server . Relay on :
Notice the added part in the center of the box , No explanation. .
5. Start the synchronization :
above ip The address is B Of ip Address , because A hold B treat as master 了 . No explanation. .
And then restart mysql service .
Then check it out ,slave Whether the state is normal :
There are two in the figure No.
Slave_IO_Running: No
Slave_SQL_Running: No
explain slave No success , namely , from B To A Synchronization of did not succeed . Let's go check mysql Error log , The position mentioned earlier :
find machine name .err file , Open it and see. :
Look at the... In the picture error Information . Said that the relay log file could not be found .
This is because we are configuring A The name of the relay file was changed when the relay file of , however mysql There is no synchronization . The solution is simple .
First stop mysql service . Find these three files , Delete them . Be sure to stop first mysql service . Otherwise, it will not succeed . You need to restart the machine . Or by hand kill mysqld.
Okay , start-up mysql after . We are here to check slave state :
Notice the two large Yes. ha-ha .
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Proof from B To A The replication of was also successful .
Now let's go to B Try inserting a few pieces of data into the server :
I am here B Two pieces of data are inserted in the . Pay attention to their id. Don't explain .
And then we , Log in A Look at it ,A Has the database changed .
You can see that it has been automatically synchronized to A 了 .
thus , AB The introduction of dual master mutual hot standby is over .
Master-slave Master/Slave Maintenance order :
master End :
show master status;— Check the status :
show processlist; – see slave Next MySQL Process information
reset master; # Use with caution , The log will be cleared and synchronized position
slave End :
CHANGE MASTER TO MASTER_LOG_FILE=’master.000019′;
show slave status;
show slave logs;
show processlist;
reset slave; # Use with caution , Will empty slave Configuration information 、 Logging and synchronization position
Skip error events on the slave server
mysql>stop slave;
mysql>set global sql_slave_skip_counter = n( Skip the next in the primary server n Events . This command is valid for replication termination caused by the statement . Only valid when the slave server thread is not running );
mysql>start slave;
mysql Synchronize users in primary server Must possess SUPER ,RELOAD,REPLICATION SLAVE jurisdiction
When a new slave server is added , You need to start with the slave database load data master; Ensure consistency with other slave database data
set global sql_slave_skip_counter=n # Client running , Used to skip several events , Only when the synchronization process stops due to an error can it be executed .
reset master # The host side runs , Clear all logs , This command is the original flush master
reset slave # Slave operation , Clear the log synchronization location flag , And regenerate master.info
Although regenerated master.info, But it doesn't work , best , Will be driven from mysql Restart the process ,
load table tblname from master
# Slave operation , Reread the data of the specified table from the host , Only one... Can be read at a time , suffer timeout The time limit , Need to adjust timeout Time . To execute this command, you need to synchronize the accounts reload and super jurisdiction . And the corresponding library has select jurisdiction . If the watch is bigger , To increase net_read_timeout and net_write_timeout Value
load data from master # Slave execution , Re read all data from the host . To execute this command, you need to synchronize the accounts reload and super jurisdiction . And the corresponding library has select jurisdiction . If the watch is bigger , To increase net_read_timeout and net_write_timeout Value
change master to master_def_list # Change some host settings online , Multiple are separated by commas , such as
change master to
master_host=’master2.mycompany.com’,
master_user=’replication’,
master_password=’bigs3cret’
master_pos_wait() # Slave operation
show master status # Host running , See the log export information
show slave hosts # Host running , Look at the connected slave .
show slave status (slave)
show master logs (master)
show binlog events [ in 'logname' ] [ from pos ] [ limit [offset,] rows ]
purge [master] logs to ‘logname’ ; purge [master] logs before ‘date’
// Show all binary logs on the machine
mysql> SHOW MASTER LOGS;
// Delete all binary logs on this computer
mysql> RESET MASTER;
// Delete all created on binary-log.xxx Previous binary logs
mysql> PURGE MASTER LOGS TO ‘binary-log.xxx’;
// Keep only the recent 6 Day's diary , The previous ones are deleted
find /var/intra -type f -mtime +6 -name “*.log” -exec rm -f {} \;
// Use the upper left corner of the keyboard ( That is to say Esc below ) That key surrounds , Instructions are commands .-1d It was yesterday , And so on -1m Last month, etc
day=`/bin/date -v -1d +%Y%m%d`;
// Rename the file
mv xxx.log xxx-${day}.log;
// Add the user name and password of the database , The function is to update the log ( Including binary log, query log, etc )
mysqladmin flush-logs
show processlist; Check the process
kill proId; Kill the process
Reference resources :
1. mysql-keepalived- Realize the read-write separation of dual master and hot standby
2. MySQL Data synchronization 【 Dual master hot standby 】http://www.cnblogs.com/zhongweiv/archive/2013/02/01/mysql_replication_circular.html
3. Mysql Dual computer hot standby
http://yunnick.iteye.com/blog/1845301
4. High performance Mysql Replication principle and configuration of master slave architecture http://blog.csdn.net/hguisu/article/details/7325124
5. mysql be based on master-master Dual computer hot standby configuration
边栏推荐
- Scala for derivation: the ability to define a value in the first part of a for expression and use it in subsequent (outer) expressions
- IDEA安装总结
- Vhedt business development framework
- Special research on Intelligent upgrading of heavy trucks in China in 2022
- Summary of JS methods for obtaining data types
- In case of default import failure
- Jsp Learning (2) - - jsp script Elements and instructions
- NiO service multithreaded version
- [C language] deeply analyze the storage of integer and floating-point types in memory
- Learning about ABAP program tuning (IV) loop where key
猜你喜欢

Special research on Intelligent upgrading of heavy trucks in China in 2022

【微信小程序获取自定义tabbar的高度】绝对可用!!!
数据库mysql 主从方案

洞见科技牵头的全球「首个」IEEE隐私计算「互联互通」国际标准正式启动

Make the code elegant (learn debugging + code style)
![[C language] deeply analyze the relationship between pointer and array](/img/f3/432eeee17034033361e05dde67aac3.jpg)
[C language] deeply analyze the relationship between pointer and array

SAP script tutorial: se71, se78, SCC1, vf03, so10-013

Parts beyond the text are indicated by ellipsis

系统吞吐量、TPS(QPS)、用户并发量、性能测试概念和公式

5 modes of IO model
随机推荐
面试题之 <img>标签 的 title 和 alt 有什么区别
大话局部性原理
Spark on data skew
同花顺容易开户么?网上开户安全么?
Analysis of the read data source code of spark shuffle
Spark Streaming-Receiver启动和数据接收
Simple understanding of asynchronous IO
[C language] deeply analyze the storage of integer and floating-point types in memory
JSP learning (2) -- JSP script elements and instructions
scala之闭包函数浅知
jsp学习之(三)--------- jsp隐式对象
面对默认导入失败的情况
为数字添加千分位符号(金额千分位)
Consumption monitoring of Prometheus monitoring [consult exporter]
【C语言】深度剖析指针和数组的关系
LETV group payment system architecture sharing for processing 100000 high concurrent orders per second
How to add a "security lock" to the mobile office of government and enterprises?
jsp學習之(二)---------jsp脚本元素和指令
[deep anatomy of C language] keywords if & else & bool type
接口(优化类型注解)