当前位置:网站首页>Configuring MySQL multi instance master-slave synchronization for Linux
Configuring MySQL multi instance master-slave synchronization for Linux
2022-06-28 07:46:00 【New objects first】
notes : The master-slave synchronization configuration in this document is based on multiple instances Mysql
The content of this document is based on the following two blogs :
linux Lower installation MySQL5.7 And summarize the problems encountered
linux The configuration mysql Multiple instances
Document directory :
One . Introduction to master slave synchronization
Two . To configure MySQL Multi instance master-slave synchronization
- 2.1 Machine configuration
- 2.2 Modify the master server configuration
- 2.3 Modify slave server configuration
- 2.4 Enable master-slave synchronization and verify
- 3.1 Fatal error: The slave I/O thread stops because master and slave have equal MySQL server ids
- 3.2 error connecting to master ‘[email protected]:3308’
One . Introduction to master slave synchronization
Master slave synchronization definition
Master-slave synchronization enables data to be replicated from one database server to another .
When copying data , A server acts as the primary server (master), The rest of the servers act as slave servers (slave).
Replication is done asynchronously .
Through the configuration file , You can specify to copy all databases , Some database , Even a table on a database .
Advantages of master-slave synchronization
- Improve database performance , Write and update on the primary server , Provide read function from the server , You can dynamically adjust the number of slave servers , To adjust the performance of the entire database .
- Improve data security , Because the data has been copied to the slave server , The replication process can be terminated from the server , therefore , It can be backed up on the slave server without destroying the corresponding data of the master server
- Improve the performance of the primary server ( Read write separation reduces the pressure on the primary server )
Two . To configure MySQL Multi instance master-slave synchronization
2.1 Machine configuration
| The server | operating system | ip Address | The port number corresponding to the instance | MySQL edition |
|---|---|---|---|---|
| The server 1( Lord ) | CentOS7 | 192.168.226.136 | 3308 | 5.7.24 |
| The server 2( from ) | CentOS7 | 192.168.226.137 | 3308 | 5.7.24 |
Be careful : The master server needs to establish network communication with the slave server
2.2 Modify the master server configuration
Modify the configuration file
MySQL The default configuration : /etc/my.cnf
My multi instance configuration : /etc/mysql/3308/my.cnf
stay [mysqld] Add the following lines below .
server-id=3308
#log-bin Setting this parameter means enabling binlog function , And specify the path name
log-bin=/var/lib/mysql/3308/mysql-bin
sync_binlog=0
# Set the expiration days of the log
expire_logs_days=7
#binlog_cache_size This parameter represents binlog Memory size used
binlog_cache_size=1M
restart MySQL database
After configuration modification , Need to restart MySQL Database for configuration to take effect .
Single instance restart mode :
service mysql restart
Multi instance restart mode :
# Stop instance The port number is not 3306 You need to specify the socket file
/opt/modules/mysql/bin/mysqladmin -uroot -p123456 -S /var/lib/mysql/3308/mysql.sock shutdown
# Boot instance When starting multiple instances, you need to specify my.cnf file
nohup /bin/sh /opt/modules/mysql/bin/mysqld_safe --defaults-file=/etc/mysql/3308/my.cnf --user=mysql &
Create a synchronization data account , And empowering
Log in to the main server mysql
# MySQL Multiple instances require the use of -S Order to designate socket file
mysql -uroot -p123456 -S /var/lib/mysql/3308/mysql.sock
Create an account for synchronizing data from the server
# notes REPLICATION The operation authority on behalf of the user is copy
# *.* Represents all tables in any database
# % Match all host Address
# 123456 refer to slave User's password
GRANT REPLICATION SLAVE ON *.* TO 'slave'@'%' identified by '123456';
FLUSH PRIVILEGES;
Get binary log information of the primary server
show master status;
Be careful : File For the name of the log file used ,Position File location for use , These two parameters must be noted down , Configuration from the server will use

2.3 Modify slave server configuration
Server configuration modification
MySQL The default configuration : /etc/my.cnf
My multi instance configuration : /etc/mysql/3308/my.cnf
stay [mysqld] Add the following lines below
# server-id A unique identifier for a database service
# Be careful : Uniqueness must be guaranteed here , Namely and master Of server-id atypism , Otherwise, an error will be reported when starting synchronization
server-id=3309
#read_only Set the database to read-only , Prevent from modifying data from the library , The master and slave data are inconsistent , But there are Super The account with permission still has permission to write , So if you want an account to be read-only , You can remove the account number Super jurisdiction
read_only=1
#binlog_cache_size This parameter represents binlog Memory size used
binlog_cache_size=1M
expire_logs_days=10
max_binlog_size=100M
restart MySQL The database makes the configuration effective
# Stop instance The port number is not 3306 You need to specify the socket file
/opt/modules/mysql/bin/mysqladmin -uroot -p123456 -S /var/lib/mysql/3308/mysql.sock shutdown
# Boot instance Multiple instances need to be specified my.cnf file
nohup /bin/sh /opt/modules/mysql/bin/mysqld_safe --defaults-file=/etc/mysql/3308/my.cnf --user=mysql &
2.4 Enable master-slave synchronization and verify
Connect the main library
# Be careful : Default connection port 3306, If the port is not 3306, Need to use MASTER_PORT Specify port number
# MASTER_LOG_FILE Log file name used by the primary server
# MASTER_LOG_POS File location for use
CHANGE MASTER TO MASTER_HOST='192.168.226.136',MASTER_PORT=3308, MASTER_USER='slave', MASTER_PASSWORD='123456', MASTER_LOG_FILE='mysql-bin.000002', MASTER_LOG_POS=2478;
Start the synchronization
mysql> start slave;
View the slave library status
mysql> show slave status \G;

The two parameter statuses marked in the red box are YES, Prove that the synchronization configuration is normal , Otherwise, corresponding treatment shall be made according to the error prompt
After completion , The synchronization configuration needs to be refreshed
# First stop synchronization
mysql> stop slave;
# Clean up the previous configuration , Prevent synchronization of already synchronized data
mysql> reset slave all;
Finally, reconnect the main library , To synchronize
Synchronous verification
Here's the picture : master server test library –>user Insert a record below the table

From the server , First, the corresponding library table will be generated ( If not ), Then insert the corresponding record
Master slave synchronization is normal !
3、 ... and . Linux To configure MySQL Problems encountered in multi instance master-slave synchronization
3.1 Fatal error: The slave I/O thread stops because master and slave have equal MySQL server ids
Carry out orders show slave status \G; When viewing the synchronization status from the server , Report errors :
Last_IO_Error: Fatal error: The slave I/O thread stops because master and slave have equal MySQL server ids;
these ids must be different for replication to work (or the --replicate-same-server-id option must be used on slave but this does not always make sense;
please check the manual before using it).
Error reason : Master and slave servers server-id Agreement , Here are two servers , All of them are 3308 port , So when configuring server-id All configured 3308 Lead to conflict
Solution :
Modify one of the servers MySQL The configuration file , The default is /etc/my.cnf, modify server-id, Ensure that the master and slave are different
3.2 error connecting to master ‘[email protected]:3308’
An error is reported after the master-slave synchronization is enabled :
Last_IO_Error: error connecting to master '[email protected]:3308' - retry-time: 60 retries: 1
Error reason :
slave The connection of the account to the primary server is abnormal
Possible causes :
- master server 3308 The instance corresponding to the port hangs
- slave User permission setting problem , There is no corresponding authority
Solution :
If the instance hangs , You need to restart the corresponding instance of the primary server :
nohup /bin/sh /opt/modules/mysql/bin/mysqld_safe --defaults-file=/etc/mysql/3308/my.cnf --user=mysql &
If the user does not have the corresponding permission , Sign in MySQL Execute the following command :
GRANT REPLICATION SLAVE ON *.* TO 'slave'@'%' identified by '123456';
FLUSH PRIVILEGES;
边栏推荐
- HJ整数与IP地址间的转换
- kubernetes删除pod的流程的源码简析
- 软件测试与质量期末复习
- 协程、asyncio、异步编程
- What is the lifecycle of automated testing?
- Unity UI shadow component
- Section 5: zynq interrupt
- Study notes 22/1/19 and 22/1/20
- Idea package together, using compact middle packages to solve &
- Kubelet garbage collection (exiting containers and unused images) source code analysis
猜你喜欢
随机推荐
"Three routines" of digital collection market
Disposition Flex
Kubernetes theoretical basis
大型项目中的Commit Message规范化控制实现
HJ base conversion
协程、asyncio、异步编程
HJ删除字符串中出现次数最少的字符
Can okcc call centers work without computers?
Static resource compression reduces bandwidth pressure and increases access speed
pip 更新到最新的版本
安全培训是员工最大的福利!2022新员工入职安全培训全员篇
kubernetes部署thanos ruler的发送重复告警的一个隐秘的坑
Drawing animated bubble chart with R language
kubernetes集群命令行工具kubectl
HJ字符串排序
Idea package together, using compact middle packages to solve &
Update pip to the latest version
es6箭头函数中return的用法
Is it safe for flush to open an account online
股票炒股注册开户靠谱吗?安全吗?








