Introduction to the principle of master-slave replication:
- After the master server data is submitted, the data will be written to the Binary log
- The Slave server monitors the Binary log log through the IO thread. If the log changes, it will read and write the content to the relay log (relay log).
- The Relay thread monitors the relay log, and if the log changes, the incremental write operation of the log will be executed once and written to the data file.
Hardware planning (virtual machine)
CPU name |
The S |
IP |
use |
Remarks |
Cent OS 7_107 |
Cent OS7 |
192.168.1.107 |
M aster/ write |
Mysql8 |
Cent OS 7_108 |
Cent OS7 |
192.168.1.108 |
Slave/read |
Mysql8 |
Cent OS 7_109 |
Cent OS7 |
192.168.1.109 |
Slave/read |
Mysql8 |
Create three virtual machines, one master and two slaves.
The specific implementation steps of master-slave replication:
- Master master server configuration
- Modify my.cnf
vi /etc/my.cnf
#Enable binary files
log-bin=mysql-bin
#Set the unique ID of the server, which is used to distinguish the master from the server.
server-id=107
- Restart the Mysql service
systemctl restart mysqld
- Log in to Mysql to authorize the Slave server
mysql -uroot -p
Enter the mysql password and press Enter to enter the mysql interface
- Create an account in the Master library with Replication Slave permissions and provide the Slave library with access to binary logs.
CREATE USER 'sluser'@'%' IDENTIFIED BY '123456';
GRANT REPLICATION SLAVE ON *.* TO 'sluser'@'%';
- Refresh permissions
flush privileges;
- View Master status
show master status;
2.S Lave from the server configuration
Servers 108 and 109 are slave servers. The following only demonstrates the configuration of the slave servers of 108. The configuration methods of 109 and 108 are the same, so we will not repeat them here.
- Modify my.cnf
vi /etc/my.cnf
Add the following content
server-id=108
replay-log=mysql-bin
- Log in to mysql and configure the slave server
Set up the slave server to monitor the master server
CHANGE MASTER TO MASTER_HOST='192.168.1.107',MASTER_USER='sluser',MASTER_PASSWORD='123456',MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=1707;
Description:
MASTER_HOST: master ip or virtual ip or domain name
MASTER_USER: The login account name previously created on the master
MASTER_PASSWORD: the password of the created login account
MASTER_LOG_FILE:master的file
MASTER_LOG_POS:master的position
- Start copy function from server
start slave;
- View the status of the slave server
show slave status \G
If Slave_IO_Running: Yes
Slave_SQL_Running: Yes, indicating that our slave server is successfully configured, and the master-slave replication configuration has been completed.
3. Test the master-slave replication function
- The main server creates a database and adds the t_user table
create database testdb;
use testdb;
DROP TABLE IF EXISTS `t_user`;
CREATE TABLE `t_user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
`age` int(11) NOT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
INSERT INTO `t_user` VALUES (1, 'test', 25);
- View the database from the server
show databases;
use testdb;
show tables;
select * from t_user;
You can see that the tables created on the master server and the data written can also be queried in the slave server, indicating that our master-slave replication configuration is successful.