1. Background
Understand and be familiar with the construction process of MySQL's master-slave synchronization, and solve the problems encountered during the construction process.
2. Goals
Understand and be familiar with the construction process of MySQL's master-slave synchronization, and solve the problems encountered during the construction process.
IP address | MySQL version | master-slave relationship |
---|---|---|
192.168.3.244 | 5.6.51 | Master |
192.168.3.245 | 5.7.41 | Slaver |
192.168.3.246 | 8.0.33 | Slaver |
3. Process
1. Install the corresponding version of MySQL
MySQL installation and deployment-5.6.51-rpm
MySQL installation and deployment-5.7.41-rpm
MySQL installation and deployment-8.0.33-rpm
2. Configure the master database
(1) Modify the database configuration file
vim /etc/my.cnf
change the configuration file:
[mysqld]#Open binary log
log-bin=mysql-bin #Identify unique id (required), generally use the last bit of ip
server-id=254
(2) Initialize
mysql_install_db --user=mysql --defaults-file=/etc/my.cnf
(3) Start the service
mysql -uroot -p
(4) Modify password
SET PASSWORD = PASSWORD(
'123456'
);
mysql -uroot -p123456
(5) Set permissions for remote connections
use mysql;
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;
FLUSH PRIVILEGES;
(6) View the default initial value of the master server status:
show master status;
master_log_file='mysql-bin.000005'
master_log_pos=120;
If it is not the initial value, it is because the copy user is set, which leads to the increase of Position data and needs to reset the state
Reset state:
reset master;
(7) For the main library, it is still necessary to create an account for synchronizing data and authorize the user
grant replication slave on *.* to 'copy'@'%' identified by '123456';
3. Configure the slave (Master) database
3.1 [5.7.41] Configuration
1) configuration file
Add the following configuration to vim /etc/my.cnf
:
server-id=255
Restart the mysql service:
systemctl restart mysqld
2) View the initial state of the server status: Empty set
show slave status;
If it is not the initial state, it is recommended to reset it
The reset command is as follows:
stop slave; #Stop replication, which is equivalent to terminating the IO and SQL threads on the slave server
reset slave;
3) Set the master of the slave server
change master to master_host='192.168.3.244',
master_user='copy',
master_port=3306,master_password='123456',
master_log_file='mysql-bin.000001',master_log_pos=120;
4) Execute start copying
start slave;
5) Check whether the master-slave replication is completed
show slave status \G
3.2 [8.0.33] configuration
1) configuration file
Add the following configuration to vim /etc/my.cnf
:
server-id=256
Restart the mysql service:
systemctl restart mysqld
2) View the initial state of the server status: Empty set
show slave status;
If it is not the initial state, it is recommended to reset it
The reset command is as follows:
stop slave; #Stop replication, which is equivalent to terminating the IO and SQL threads on the slave server
reset slave;
3) After entering mysql, set the master of the slave server
change master to master_host='192.168.3.244',
master_user='copy',
master_port=3306,master_password='123456',
master_log_file='mysql-bin.000001',master_log_pos=120;
4) Execute start copying
start slave;
5) Check whether the master-slave replication is completed
show slave status \G
If Slave_IO_Running: is no, execute the following command:
stop slave;
reset slave;
start slave;
4. Results
1) Create a testdb database in the main server:
2) Create a t_user table in the testdb of the main server and insert data
3) It can be found that the value has been added to the table of the master server, and the same value will be added to the table of the slave server
From library [5.7.41]:
From library [8.0.33]:
Shoulders of Giants :
Simple Construction of MySQL Master-Slave Replication_mysql Master-Slave Replication_yuwenS.'s Blog-CSDN Blog