mysql master-slave replication principle and application

1. Introduction to master-slave replication

MySQL master-slave replication is an asynchronous, log-based, one-way database replication technology that enables binary logs on the master server and sends them to one or more slave servers. data synchronization. The master server records all database operations into the binary log. The slave server checks the master server's binary log regularly or in real time and applies the operations to the local MySQL instance. The replication process can be statement-based, row-based, or hybrid, and replication latency can be monitored and managed through a variety of means to improve data availability and reliability.

The principle is as follows:

  1. Configure the master server: Enable binary logging on the master server and record all database change operations. Each binary log file contains records of a series of database operations, arranged in chronological order.
  2. Configure the slave server: Start a replication process on the slave server, which connects to the master server and requests replicated data. The slave server will automatically download and parse the binary log file of the master server based on the configuration information of the master server, and then apply the operations therein to the local MySQL database.
  3. Replication process: When changes occur to the database on the master server, these changes are recorded in the binary log. The replication process checks the master server's binary log periodically or in real time and copies new operations therein to the slave server's local MySQL database. The copying process can be achieved in a variety of ways, such as:
    • Statement-based replication: The replication process copies the SQL statements on the master server to the slave server for execution.
    • Row-based replication: The replication process copies database row changes on the master server to the slave server for execution.
    • Hybrid replication: The replication process chooses to use statement-based or row-based replication depending on the type of operation and circumstances.
  4. Replication latency: Due to network delays, load on the slave server, etc., the data on the slave server may lag behind the data on the master server. This lag is called replication latency, and it can have an impact on applications. Therefore, replication delays need to be monitored and dealt with promptly.
  5. Failover: If the primary server fails, replication needs to be switched to another MySQL server. Automatic or manual failover can be achieved by using tools provided by MySQL or third-party tools.

In general, MySQL master-slave replication technology improves data availability and reliability by replicating data from the master server to multiple slave servers. The replication process can be implemented in a variety of ways, and can be adjusted and optimized according to actual conditions to improve performance and stability.

2. Main server
1. Enable binary log (binlog) and set server_id
[root@node1 ~]# vim /etc/my.cnf
log_bin=mysql-bin
server_id=10
# server_id要唯一
2. Restart the mysql service
[root@node1 ~]# systemctl restart mysqld
3. Create a mysql user and grant the user copy permissions
mysql> grant replication slave on *.* to 'rep'@'192.168.136.%' identified by '123456';
# 192.168.136.%,表示任何以192.168.136.开头的IP地址都可以连接到MySQL服务器并使用该用户进行复制操作。

mysql> show grants for 'rep'@'192.168.136.%';  # 查看用户的授权
4. Check the main library status
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 |      440 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
5. Back up the database

Set the lock table to read-only: prepare for subsequent backups. Note that the production environment must apply for downtime in advance.

mysql> flush tables with read lock;

If you want to back up the database, I won’t do a backup because it is empty. If you do a backup, you need to upload the master server data to the slave database.

3. Slave server
1. Enable binary log (binlog) and set server_id
[root@node2 ~]# vim /etc/my.cnf
log_bin=msyql_bin
server_id = 11
2. Restart the mysql service
[root@node2 ~]# systemctl restart mysqld
3. Set up main database synchronization
mysql> change master to
    ->  master_host='192.168.136.161',
    ->  master_user='rep',
    ->  master_password='123456',
    ->  master_log_file='mysql-bin.000001',
    ->  master_log_pos=440;
Query OK, 0 rows affected, 2 warnings (0.01 sec)
4. Start synchronization from the library and check the status
mysql> start slave;
Query OK, 0 rows affected (0.01 sec)

mysql> show slave status\G;

# 查看这两参数是否是yes,如果不是查看mysql日志
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
3. Test synchronization
1. Create a table and insert data
mysql> CREATE  TABLE  student (
    -> id  INT(10)  NOT NULL  UNIQUE  PRIMARY KEY  ,
    -> name  VARCHAR(20)  NOT NULL ,
    -> sex  VARCHAR(4)  ,
    -> birth  YEAR,
    -> department  VARCHAR(20) ,
    -> address  VARCHAR(50) 
    -> );
Query OK, 0 rows affected (0.02 sec)
mysql> INSERT INTO student VALUES( 901,'张老大', '男',1985,'计算机系', '北京市海淀区');
Query OK, 1 row affected (0.01 sec)
mysql> INSERT INTO student VALUES( 902,'张老二', '男',1986,'中文系', '北京市昌平区');
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO student VALUES( 903,'张三', '女',1990,'中文系', '湖南省永州市');
Query OK, 1 row affected (0.01 sec)
mysql> INSERT INTO student VALUES( 904,'李四', '男',1990,'英语系', '辽宁省阜新市');
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO student VALUES( 905,'王五', '女',1991,'英语系', '福建省厦门市');
Query OK, 1 row affected (0.01 sec)
mysql> INSERT INTO student VALUES( 906,'王六', '男',1988,'计算机系', '湖南省衡阳市');
Query OK, 1 row affected (0.01 sec)
2. Check whether the data is synchronized in the main database and the slave database respectively.
# 主库:
mysql> select * from student;
+-----+-----------+------+-------+--------------+--------------------+
| id  | name      | sex  | birth | department   | address            |
+-----+-----------+------+-------+--------------+--------------------+
| 901 | 张老大    ||  1985 | 计算机系      | 北京市海淀区        |
| 902 | 张老二    ||  1986 | 中文系        | 北京市昌平区        |
| 903 | 张三      ||  1990 | 中文系        | 湖南省永州市        |
| 904 | 李四      ||  1990 | 英语系        | 辽宁省阜新市        |
| 905 | 王五      ||  1991 | 英语系        | 福建省厦门市        |
| 906 | 王六      ||  1988 | 计算机系      | 湖南省衡阳市        |
+-----+-----------+------+-------+--------------+--------------------+
6 rows in set (0.00 sec)
# 从库
mysql> select * from student;
+-----+-----------+------+-------+--------------+--------------------+
| id  | name      | sex  | birth | department   | address            |
+-----+-----------+------+-------+--------------+--------------------+
| 901 | 张老大    ||  1985 | 计算机系      | 北京市海淀区        |
| 902 | 张老二    ||  1986 | 中文系        | 北京市昌平区        |
| 903 | 张三      ||  1990 | 中文系        | 湖南省永州市        |
| 904 | 李四      ||  1990 | 英语系        | 辽宁省阜新市        |
| 905 | 王五      ||  1991 | 英语系        | 福建省厦门市        |
| 906 | 王六      ||  1988 | 计算机系      | 湖南省衡阳市        |
+-----+-----------+------+-------+--------------+--------------------+
6 rows in set (0.00 sec)
# 主从同步成功

おすすめ

転載: blog.csdn.net/m0_50816276/article/details/132005827