Mysql master-slave replication (MySQL Replication) will be practical at a glance!

Preface

MySQL master-slave replication can replicate data from one MySQL database server (master server) to one or more MySQL database servers (slave servers).

The advantages of MySQL replication include:

  • Scale-out solution-spread the load among multiple copies to improve performance. In this environment, all writes and updates must be performed on the replication source server (primary server). However, the read may occur on one or more replicas (slave servers). This model can improve write performance (because the source is dedicated to updates), while it can significantly increase read speeds in more and more replicas.
  • Data security-because the data has been copied to the copy, and the copy can suspend the copy process, backup services can be run on the copy without destroying the corresponding source data.
  • Analysis-Real-time data can be created on the source, and information analysis can be performed on the copy without affecting the performance of the source.
  • Remote data distribution-You can use replication to create local copies of data for use by remote sites without having to permanently access the source.

Actual combat

Below I will use two virtual machines to implement MySQL master-slave replication

主虚拟机:192.168.91.128

从虚拟机:192.168.91.130

Prerequisite: Both virtual machines have MySQL installed, my MySQL version is 5.7.24, and the networks of the two virtual machines can be pinged

Main server configuration

1. Modify the MySQL configuration file on the primary server, open the binary log file, and configure the server-id

Edit the MySQL configuration file. The MySQL default configuration file of Linux defaults to my.cnf, which is usually under /etc/

vi  /etc/my.cnf 

Add content as follows:

#在配置文件的[mysqld]部分添加以下两个配置
[mysqld]
#设置server-id,这个server-id主从服务器必须不一样
server-id=1
#开启二进制日志,mysql-bin为日志名称,你可以自定义名称
log-bin=mysql-bin

Note: If you don’t know where the my.cnf file is, you can use the find / -name my.cnf command to search

In addition to the above two necessary configurations, we can also set sync_binlog, innodb_flush_log_trx_commit and other configurations according to our actual situation

You need to restart the server after modifying the configuration


2. Add an account for synchronization on the main server

The command is as follows:

mysql> grant replication slave on *.* to 'slave'@'%' identified by 'slave';

Command description

replication slave:表示分配的权限,replication slave为复制的权限

*.*:表示可以操作哪些数据库,*.*表示所有数据库的所有表

slave@'%':slave表示这个账号的名称,'%'为在哪些网络可以使用,'%'为可以在所有网络上使用,除了使用'%',我们还可以指定某个ip,比如192.168.91.128

identified by后填的是密码

3. Check the latest binary log file and position point of the master server, record it, and use it on the slave server

command

mysql> show master status;

Examples:


mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000008 |      438 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.03 sec)

Master-slave replication will only replicate the modifications after the master-slave configuration, so if there is data in the master database before, we also need to copy the data to the slave database. The easiest way is to use mysqldump to create a dump of all databases to be replicated .

For the use of mysqldump, you can refer to:
https://blog.csdn.net/qq_36551991/article/details/111409411 .


Configure from the server

1. Modify the MySQL configuration file on the slave server and configure the server-id. The binary log can be opened or not on the slave server.

command:

vi  /etc/my.cnf 

content:

[mysqld]
#配置server-id,server-id必须与主服务器和其他从服务器的不一样,即需要唯一
server-id=2

In addition to the server-id, which must be configured, the other configuration depends on the actual situation

2. Configure master-slave replication

The command is as follows

mysql> CHANGE MASTER TO
    -> MASTER_HOST='192.168.91.128',
    -> MASTER_USER='slave',
    -> MASTER_PASSWORD='slave',
    -> MASTER_LOG_FILE='mysql-bin.000008',
    -> MASTER_LOG_POS=438;

Command description:

MASTER_HOST : 设置要连接的主服务器的ip地址
MASTER_USER : 设置要连接的主服务器的用户名
MASTER_PASSWORD : 设置要连接的主服务器的密码
MASTER_LOG_FILE : 设置要连接的主服务器的bin日志的日志名称,即我们在主服务器show master status命令得到的file参数
MASTER_LOG_POS : 设置要连接的主服务器的bin日志的记录位置,即我们在主服务器show master status命令得到的Position 参数

3. After the configuration is complete, start master-slave replication:

command:

start slave;

Example:

mysql> start slave;

4. Check whether the configuration is successful

命令:show slave status\G

Examples:

mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.91.128
                  Master_User: slave
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000008
          Read_Master_Log_Pos: 438
               Relay_Log_File: local-relay-bin.000002
                Relay_Log_Pos: 320
        Relay_Master_Log_File: mysql-bin.000008
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes

It mainly depends on whether the Slave_IO_Running and Slave_SQL_Running statuses of the returned information are both Yes, and if both are Yes, it is normal (Slave_IO_Running and Slave_SQL_Running represent the two replication threads of MySQL)

Test master-slave replication

We create a new table on the MySQL of the master server and insert data to see if the slave server will replicate

#在主数据库创建个新表,并插入一条数据
mysql> CREATE TABLE test_table( id INT NOT NULL AUTO_INCREMENT, name varchar(50), PRIMARY KEY(id));
Query OK, 0 rows affected (0.23 sec)
mysql> insert into test_table(name) values('h');
Query OK, 1 row affected (0.15 sec)

View the slave server on Navicat, if the corresponding new table and data appear on the slave server, the master-slave replication is successful

Guess you like

Origin blog.csdn.net/qq_36551991/article/details/111462625