MySQL database master-slave server replication

Why do master-slave replication

Generally speaking, if the reading and writing of the database are all operated in the same database server, the performance of the business system will be reduced.
In order to improve the performance of the business system and optimize the user experience, master-slave replication (read-write separation) can be used to reduce the load on the main database.
And if the main database is down, the business system can be quickly switched to the secondary database, which can avoid data loss.
Insert picture description here

Types of master-slave replication

Statement-based replication (default)
executes the same statement
on the master server and executes the same statement from the server. Row-based replication replicates
the changed content to the slave server
.
Once mixed-type replication finds that statement-based replication cannot be accurately replicated, it will use Copy of line

Schematic diagram of master-slave replication process

Insert picture description here

bring it on! Show! !

Three virtual machines
Master server: 20.0.0.4
Slave server 01: 20.0.0.5
Slave server 02: 20.0.0.6
There is a write before MySQL installation, you can read the
link here

Three servers close the firewall

setenforce 0
iptables -F

Configure the main server

配置主配置文件
[root@master mysql]# vim /etc/my.cnf
[mysqld]
…省略部分…
server-id = 1
log_slave_updates = true
log-bin = master-bin
[root@master mysql]# systemctl restart mysqld

[root@master mysql]# mysql -u root -p
Enter password: 
# *.* 指的是 全部数据库.所有表,slaveuser 是从服务器登录用的账户
mysql> grant replication slave on *.* to 'slaveuser'@'20.0.0.%' identified by
'123123';
Query OK, 0 rows affected, 1 warning (0.00 sec)

#刷新
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

#查看从服务器读取位置,我这边读取的是 601
mysql> show master status;
+-------------------+----------+--------------+------------------+-------------------+
| File              | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-------------------+----------+--------------+------------------+-------------------+
| master-bin.000001 |      601 |              |                  |                   |
+-------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

Configure the slave server

Both have to be set up, I only demonstrate one

[root@slave01 ~]# vim /etc/my.cnf
[mysqld]
…省略部分…
server-id = 12   ##三台服务器得各不一样,我另一台配的13
relay-log=relay-log-bin   ##二进制日志文件名
relay-log-index=slave-relay-bin.index
[root@slave01 ~]# systemctl restart mysqld

[root@slave01 ~]# mysql -u root -p
Enter password: 

##master_host指向主服务器IP,master_user只想刚刚创建的用户,master_log_pos指向刚刚得位置,我的是306
mysql> change master to master_host='20.0.0.4',master_user='slaveuser',master_password='123123',master_log_file='master-bin.000001',master_log_pos=601;
Query OK, 0 rows affected, 2 warnings (0.01 sec)

#启动从同步
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)

#查看 slave 状态
mysql> show slave status\G;
             Master_Host: 20.0.0.4
             Master_User: slaveuser
             Slave_IO_Running: Yes   ##有下面两条差不多就ok了
             Slave_SQL_Running: Yes

Test master-slave replication

Create a database and table on the main server to verify synchronization

mysql> create database happy;
Query OK, 1 row affected (0.00 sec)

mysql> use happy;
Database changed

mysql> create table kaixin(id int(2) primary key auto_increment,xinqing varchar(10) not null);
Query OK, 0 rows affected (0.02 sec)

mysql> insert into kaixin values(1,'高兴');
Query OK, 1 row affected (0.01 sec)

mysql> select * from kaixin;
+----+---------+
| id | xinqing |
+----+---------+
|  1 | 高兴    |
+----+---------+
1 row in set (0.00 sec)

Verify from the database

mysql> use happy;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> select * from kaixin;
+----+---------+
| id | xinqing |
+----+---------+
|  1 | 高兴    |
+----+---------+
1 row in set (0.00 sec)

Guess you like

Origin blog.csdn.net/Ora_G/article/details/108245885