当前位置:网站首页>MySQL master-slave replication

MySQL master-slave replication

2022-06-26 00:07:00 ZgaoYi

1. Introduce

MySQL Master-slave replication is an asynchronous replication process , The bottom layer is based on Mysql Binary log function of database . Just one or more MySQL database (slave, From the library ) From the other AySQL database (master, The main library ) Copy the log, then parse the log and apply it to itself , Finally, the data of the slave database is consistent with that of the master database .MySQL Master slave replication is MySQL The database has its own functions , No third party tools required .
MySQL The replication process is divided into three steps :

  • master Log changes to binary log (binary log)
  • slave take master Of binary log Copy to its trunk log ( relay log)
  • slave Redo events in relay log , Apply the changes to your own database

 Insert picture description here

2. To configure

There is only one master database , Can be multiple from , This time, it is implemented on the virtual machine , Create two virtual machines , Equivalent to two servers , And install MySQL, Versions, . The implementation is as follows .

2.1 Main library Master

First step : modify Mysq1 Profile of the database /etc/my.cnf

 vim /etc/my.cnf

add to :
log-bin=mysql-bin #[ must ] Enable binary logging
server-id=100 #[ must ] The server is unique ID

notes : there id Is the server IP The last number of , Such as my IP yes 192.168.125.100, here id Just fill in 100
 Insert picture description here The second step , Restart the database systemctl restart mysqld
 Insert picture description here

The third step : Sign in Mysql database , Do the following SQL

GRANT REPLICATION SLAVE ON *.* to 'xiaoming'@'%' identified by 'Root@123456';

notes : above SQL The function of is to create a user 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 .

 Insert picture description here Step four , Check the status , The next configuration circled here needs to be used when configuring from the database .
 Insert picture description here

2.2 To configure - Slave Library Slave

First step : modify Mysql Profile of the database /etc/my.cnf

vim /etc/my.cnf

server-id=101 ##[ must ] The server is unique ID

 Insert picture description here The second step , Restart the database systemctl restart mysqld
 Insert picture description here

The third step : Sign in Mysql database , Do the following SQL
change master to master_host =‘192.168.126.100’,master_user=‘xiaoming’,master_password=‘[email protected]’,master_log_file= ‘mysql-bin.000001’,master_log_pos=441;

If an error is reported slave Started , execute stop slave;
 Insert picture description here Then execute again
change master to master_host =‘192.168.126.100’,master_user=‘xiaoming’,master_password=‘[email protected]’,master_log_file= ‘mysql-bin.000001’,master_log_pos=441;

Restart after execution slave

start slave

 Insert picture description here

3. Go to the database to test

In the main MySQL Add database to test1 , And then from MySQL Auto update add to test1
 Insert picture description here Configuration complete .

原网站

版权声明
本文为[ZgaoYi]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/177/202206252116226871.html