MYSQL master and backup replication

MySQL's built-in replication function is the basis for building large-scale, high-performance applications based on MySQL. These applications use the so-called "horizontal expansion" architecture to configure one or more standby databases for the server to synchronize data.

The basic problem that replication solves is keeping one server's data in sync with other servers.

MySQL supports two replication methods, row-based replication and statement-based replication (logical replication).

These two methods achieve asynchronous data replication by recording binary logs on the main database and replaying logs on the standby database .

Replication is neither a backup nor a substitute for a backup! !

Replication has three steps:

① Record data changes to the binary log (Binary Log) on ​​the main library (these records are called binary log events)

② The standby database copies the log on the main database to its own relay log (Relay Log).

③ The standby database reads the events in the relay log and replays them on the standby database data.

configuration replication

1. Create a replication account on each server (both primary and backup)

MySQL will grant some special permissions to the replication thread. The I/O thread running on the standby database will establish a TCP/IP connection to the main database, which means that a user must be created in the main database and given appropriate permissions. The I/O thread of the standby database connects to the primary database with this user name and reads its binary log.

mysql> grant replication slave,replication client on *.* to repl@'192.168.20.%' identified by 'p4ssword';

2. Configure the main library and standby library

Main library:

If there is no log-bin option in my.cnf before, you need to restart MySQL

[mysql@centos7min1 mysql57]$ vi my.cnf

[client]

port = 3306

socket = /opt/mysql57/socket/mysqld-T-prod-3306.sock

default_character_set=utf8

[mysqld]

user = mysql

port = 3306

socket = /opt/mysql57/socket/mysqld-T-prod-3306.sock

datadir = /opt/mysql57/data

basedir = /opt/mysql57/

server_id = 10 ## 1-10

log_bin = /opt/mysql57/mylog/mysql-bin-T-prod-3306 #binary log

sync_binlog=1 #Commit the transaction to synchronize the binary log to the disk to prevent errors and lost events

tmpdir = /opt/mysql57/tmp

[mysqld_safe]

pid_file = /opt/mysql57/pid/mysql-T-prod-3306.pid

log_error = /opt/mysql57/log/mysql-T-prod-3306.err

[mysql@centos7min1 mysql57]$ ./support-files/mysql.server restart

mysql> show master status;

+------------------------------+----------+--------------+------------------+-------------------+

| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |

+------------------------------+----------+--------------+------------------+-------------------+

| mysql-bin-T-prod-3306.000004 | 154 | | | |

+------------------------------+----------+

192.168.0.92 MySQL main service configuration information:

Backup library:

A similar configuration needs to be added to my.cnf on the standby database, and the server also needs to be restarted

[yang@centos7min2 mysql]$ cat my.cnf

[client]

port=39306

socket=/opt/mysql5/mysql/socket/mysqld-T-prod-39306.sock

default_character_set=utf8

# Here follows entries for some specific programs

# The MySQL server

[mysqld]

user=mysql

port=39306

socket=/opt/mysql5/mysql/socket/mysqld-T-prod-39306.sock

datadir=/opt/mysql5/mysql/data

basedir=/opt/mysql5/mysql

server_id=9

log_bin=/opt/mysql5/mysql/mylog/mysql-bin-T-prod-39306

relay_log=/opt/mysql5/mysql/mylog/relay-log-T-prod-39306 #relay log

log_slave_updates=1 #Allow the standby database to record replayed events into its own binary log

read_only=1 #This parameter can be set if the standby database has no write operation

tmpdir=/opt/mysql5/mysql/tmp

[mysqld_safe]

pid_file=/opt/mysql5/mysql/pid/mysql-T-prod-39306.pid

log_error=/opt/mysql5/mysql/log/mysql-T-prod-39306.err

[yang@centos7min2 mysql]$ ./support-files/mysql.server restart

192.168.0.93 MySQL slave service configuration information:

3. Notify the standby database to connect to the main database and copy data from the main database

start replication

In this step, do not configure by modifying my.cnf, but use the change master to statement, which completely replaces the corresponding settings in my.cnf, and allows you to point to other master databases without restarting the standby database.

Backup library:

mysql> change master to master_host='192.168.20.71',

-> master_user='repl',

-> master_password='p4ssword',

-> master_log_file='mysql-bin-T-prod-3306.000004',

-> master_log_pos=0;

Query OK, 0 rows affected, 2 warnings (0.04 sec)

mysql> show slave status\G

*************************** 1. row ***************************

Slave_IO_State:

Master_Host: 192.168.20.71

Master_User: repl

Master_Port: 3306

Connect_Retry: 60

Master_Log_File: mysql-bin-T-prod-3306.000004

Read_Master_Log_Pos: 4

Relay_Log_File: relay-log-T-prod-39306.000001

Relay_Log_Pos: 4

Relay_Master_Log_File: mysql-bin-T-prod-3306.000004

Slave_IO_Running: No

Slave_SQL_Running: No

mysql> start slave; | (stop slave )

Query OK, 0 rows affected (0.02 sec)

mysql> show slave status\G

*************************** 1. row ***************************

Slave_IO_State: Waiting for master to send event

Master_Host: 192.168.20.71

Master_User: repl

Master_Port: 3306

Connect_Retry: 60

Master_Log_File: mysql-bin-T-prod-3306.000004

Read_Master_Log_Pos: 154

Relay_Log_File: relay-log-T-prod-39306.000002

Relay_Log_Pos: 391

Relay_Master_Log_File: mysql-bin-T-prod-3306.000004

Slave_IO_Running: Yes

Slave_SQL_Running: Yes

You can see the replication thread from the thread list, and you can see the connection initiated by the I/O thread of the standby library to the main library on the main library.

mysql> show processlist\G

*************************** 2. row ***************************

Id: 4

User: repl

Host: 192.168.20.72:34388

db: NULL

Command: Binlog Dump

Time: 151

State: Master has sent all binlog to slave; waiting for more updates

Info: NULL

Similarly, two threads can also be seen in the standby database, one is the I/O thread and the other is the SQL thread.

*************************** 1. row ***************************

Id: 3

User: system user

Host:

db: NULL

Command: Connect

Time: 659

State: Waiting for master to send event

Info: NULL

*************************** 2. row ***************************

Id: 4

User: system user

Host:

db: NULL

Command: Connect

Time: 654

State: Slave has read all relay log; waiting for more updates

Info: NULL

The operation of the main database can synchronize with the standby database, but the operation of the standby database cannot be synchronized with the main database.

Cancel master-standby replication:

Backup my.cnf

##server_id=9

##log_bin=/opt/mysql5/mysql/mylog/mysql-bin-T-prod-39306 ---master key

show master status

##relay_log=/opt/mysql5/mysql/mylog/relay-log-T-prod-39306 ---slave取消

##log_slave_updates=1

##read_only=1

show slave status

Completely cancel master-slave replication:

mysql> stop slave;

Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> reset slave all;

Query OK, 0 rows affected (0.06 sec)

mysql> show slave status\G

Empty set (0.00 sec)

MYSQL主 my.cnf

MYSQL from my.cnf

Guess you like

Origin blog.csdn.net/Wemesun/article/details/126212891