当前位置:网站首页>[highly available MySQL solution] centos7 configures MySQL master-slave replication
[highly available MySQL solution] centos7 configures MySQL master-slave replication
2022-07-26 05:54:00 【A rookie is a great God】
Common high availability MySQL The solution includes master-slave replication , Master master copy ,Heartbeat/SAN High availability ,MySQL Cluster High availability, etc . Here I use the simplest master-slave replication high availability scheme , It's also Mysql Built in features .
mysql Master slave replication principle :
- master Record data changes to a binary log (binary log) in , That is, the configuration file log-bin Specified file ( These records are called binary log events )
- slave take master Of binary log events Copy to its trunk log (relay log)
- slave Redo events in relay log , Will change the data that reflects itself ( Data replay )
In fact, it is synchronized according to the log file of the main database .
Support type
- Statement based replication : Executed on the primary server SQL sentence , Execute the same statement on the slave server .MySQL Statement based replication is used by default , High efficiency
- Line based replication : Copy the changed content directly , It doesn't matter which statement caused the change . from mysql5.0 Start supporting
- Hybrid type of replication : Statement based replication is used by default , Once it is found that statement based replication cannot be accurate , You'll use row based replication .
precondition
- Lord DB server And from the DB server The database version is consistent
- Lord DB server And from the DB server Database data is consistent [ You can restore the primary backup on the secondary ]
- Lord DB server Turn on binary log , Lord DB server And from the DB server Of server_id Must be unique
The detailed steps
master server node200:192.168.212.200
From the server node201:192.168.212.201( You can set up multiple slave servers )
1. Two machines selinux All are disable
2. Modify the master DB server Configuration file for (/etc/my.cnf), Turn on the log function , Set up server_id value , Ensure that the only [node200 Mainly DB server]
1 2 3 4 5 6 | [[email protected] ~]# vi /etc/my.cnf # Modify the configuration file , The following two parameters : # Set up server_id, It is generally recommended to set IP, Or add some numbers server_id = 200 # Turn on binary log function , You can take it , Better have meaning log-bin=mysql3306-bin |
3. Start the database server , And log in to the database , Grant corresponding users for synchronization
1 2 3 4 5 6 7 8 9 10 11 12 | mysql -uroot -p mysql> GRANT REPLICATION SLAVE,RELOAD,SUPER ON *.* TO 'slave1'@'192.168.212.201' IDENTIFIED BY 'winstore'; # Refresh authorization table information mysql> FLUSH PRIVILEGES; # Check and write down position Number and log file name ( Very important ) mysql> show master status; +----------------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +----------------------+----------+--------------+------------------+-------------------+ | mysql3306-bin.000002 | 399 | | | | +----------------------+----------+--------------+------------------+-------------------+ 1 row in set (0.00 sec) |
4. To ensure the Lord DB server And from the DB server The data are consistent , Here, the primary backup is used , From restore to achieve initial data consistency
1 2 3 4 5 6 7 8 9 10 | # Lord DB Log on to # Temporary lock table mysql> flush tables with read lock; # I implement full database backup here , In practice, , We might just synchronize one library , You can back up only one library # Open a new terminal , Do the following [[email protected] ~]# mysqldump -uroot -p --all-databases > /tmp/mysql.sql # Unlock mysql> unlock tables; # Transfer the backup data to the slave , Used to recover [[email protected] ~]# scp /tmp/mysql.sql [email protected]:/tmp |
5. Only one item needs to be modified from the database configuration file , Use the command line to do the rest
1 2 3 | [[email protected] ~]# vi /etc/my.cnf # Set up server_id, It is generally recommended to set IP server_id = 201 |
6. Start from database , Restore backup data
1 2 3 4 | # Start database [[email protected] ~]# systemctl restart mysqld.service # Restore master DB server Backup data [[email protected] ~]# mysql -uroot -p < /tmp/mysql.sql |
7. Log in from the database , Add relevant parameters ( Lord DBserver Of ip/ port / Synchronize users / password /position Number / Which log file to read )
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | [[email protected] ~]# mysql -uroot -p mysql> change master to -> master_host='192.168.212.200', -> master_user='slave1', -> master_password='winstore', -> master_port=3306, -> master_log_file='mysql3306-bin.000002', -> master_log_pos=399; # Turn on master-slave synchronization mysql> start slave; # Check the master-slave synchronization status mysql> show slave status\G; # Look at the following two parameters :[ If these two parameters are yes It means that the master-slave synchronization is normal ] Slave_IO_Running: Yes Slave_SQL_Running: Yes |
8. Now you can be in the Lord DB server Create a new table , See if you can synchronize to from DB server On , I won't test it here
see server_id:
1 | mysql> show variables like '%server_id%'; |
边栏推荐
- Kingbasees SQL language reference manual of Jincang database (11. SQL statement: abort to alter index)
- DOM operation -- operation node
- Kingbasees SQL language reference manual of Jincang database (8. Function (10))
- Jupiter notebook shortcut key
- Mysql45 speak in simple terms index
- How are masters refined?
- Unity Profiler
- QT writing IOT management platform 47 general database settings
- Redis 官方可视化工具,高颜值,功能真心强大!
- [paper notes] anti packet loss joint coding for network speech steganography
猜你喜欢

Use latex to typeset multiple-choice test paper

unity 像素画导入模糊问题

Redis master-slave replication

Select sort / insert sort / bubble sort

Benji Bananas 开启第二季边玩边赚奖励活动,支持使用多张 Benji 通行证!

Qt编写物联网管理平台47-通用数据库设置
![[论文笔记] 面向网络语音隐写的抗分组丢失联合编码](/img/ca/95476b6d4b5765f5fde82cbeda577e.png)
[论文笔记] 面向网络语音隐写的抗分组丢失联合编码

Usage and common problems of SIP softphone registered with SIP account

ETCD数据库源码分析——Cluster membership changes日志

Introduction to Chinese text error correction task
随机推荐
K. Link with Bracket Sequence I dp
Use of feign (Part 2)
ERROR: Could not open requirements file: [Errno 2] No such file or directory: ‘requirments.txt’
Mysql45 talking about logging system: how does an SQL UPDATE statement execute?
Redis official visualization tool, with high appearance value and powerful functions!
Learn about spark project on nebulagraph
Redis publish subscription
Ros2 preliminary: basic communication with topic
Kingbasees SQL language reference manual of Jincang database (11. SQL statement: abort to alter index)
DOM operation -- operation node
Chapter 1 - Construction of development environment
【(SV && UVM) 笔试面试遇到的知识点】~ phase机制
Chapter 2 - getting started
Unity2D 动画器无法 创建过渡
Redis 官方可视化工具,高颜值,功能真心强大!
Is the transaction in mysql45 isolated or not?
Usage and common problems of SIP softphone registered with SIP account
ETCD数据库源码分析——Cluster membership changes日志
Kingbasees SQL language reference manual of Jincang database (8. Functions (XI))
卸载手机自带APP的操作步骤