MySQL high availability configuration (master-slave replication)

Master-slave replication consists of two steps:

Settings on the master master server (group), and settings on the slave slave servers (group).

environment:

MASTER: 192.168.155.101
SLAVE: 192.168.155.102

important point:

1. Configuring the master server master requires enabling binary logging.
2. Set a unique server_id for the master, and also set server_id for all slave servers. The server_id value can be an integer number (1 ~ 2^31-1). The server_id of each server in the same replication group must be unique.
3. The slave slave server needs to have permission to connect and replicate from the master. Usually, a separate user (user) is created for each slave, and only the replication permission (REPLICATION SLAVE permission is granted). Shared user permissions can also be used. .

MASTER placement:
First Node (MASTER: 192.168.155.101)
---------------
vi /etc/my.cnf.d/server.cnf

[mysqld]
server-id=1
log-bin=master-bin
binlog-do-db=my-db #Database name that needs to enable transaction log
binlog-ignore-db=mysql #System database name that does not enable transaction log

If the above two lines are not specified, transaction logging will be enabled for all databases.

GRANT REPLICATION SLAVE ON *.* TO 'rep_user'@'%' IDENTIFIED BY 'rep_pass';
FLUSH PRIVILEGES;

# service mysql restart

When viewing the current binary log position of the Master, it is necessary to prevent the submission of any data and lock all tables.
MariaDB [(none)]> FLUSH TABLES WITH READ LOCK;
MariaDB [(none)]> SHOW MASTER STATUS;
+-------------------+----- -----+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+ -------------------+----------+----------------------------+---- --------------+
| master-bin.000001 | 245 | | |
+-------------------+--- -------+--------------+------------------+
1 row in set (0.00 sec)

Note down the log file name of the File column and the position number of the Position column, which is required when synchronizing on SLAVE.
Backup the database on MASTER and import manually on SLAVE.

Release table lock
MariaDB [(none)]> UNLOCK TABLES;

SLAVE configuration:

Other Node (SLAVE: 192.168.155.102)
---------------
vi /etc/my.cnf.d/server.cnf

[mysqld]
server-id=2
read-only=on
relay-log=relay-bin
innodb_recovery_update_relay_log=1
relay_log_recovery=1
replicate-do-db=my-db #Database name that needs to synchronize transaction logs
replicate-ignore-db=mysql #The name of the system database that does not need to synchronize the transaction log

If the above two lines are not specified, the transaction log will be synchronized for all databases.

# service mysql restart

MariaDB [(none)]> show variables like '%relay%';
+----------------------------------+----------------+
| Variable_name | Value |
+----------------------------------+----------------+
| innodb_recovery_update_relay_log | ON |
| max_relay_log_size | 0 |
| relay_log | relay-bin |
| relay_log_index | |
| relay_log_info_file | relay-log.info |
| relay_log_purge | ON |
| relay_log_recovery | ON |
| relay_log_space_limit | 0 |
| sync_relay_log | 0 |
| sync_relay_log_info | 0 |
+----------------------------------+----------------+
10 rows in set (0.00 sec)

开始同步
MariaDB [(none)]> CHANGE MASTER TO
-> MASTER_HOST='192.168.155.101',
-> MASTER_USER='rep_user',
-> MASTER_PASSWORD='rep_pass',
-> MASTER_PORT=3306,
-> MASTER_LOG_FILE='master-bin.000001',
-> MASTER_LOG_POS=245,
-> MASTER_CONNECT_RETRY=10;
Query OK, 0 rows affected (0.08 sec)

MariaDB [(none)]> START SLAVE;

查看SLAVE状态:
MariaDB [(none)]> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.155.101
Master_User: rep_user
Master_Port: 3306
Connect_Retry: 10
Master_Log_File: master-bin.000002
Read_Master_Log_Pos: 411
Relay_Log_File: relay-bin.000006
Relay_Log_Pos: 696
Relay_Master_Log_File: master-bin.000002
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 411
Relay_Log_Space: 1269
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 1
1 row in set (0.00 sec)

Check Slave_IO_Running and Slave_SQL_Running. If both are Yes, it means that the build is successful, and you can build a table and insert data for testing.

If you need to pause SLAVE replication
MariaDB [(none)]> STOP SLAVE;

If the SLAVE is not synchronized, you can pause the SLAVE replication first, and then perform a synchronization operation.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324613267&siteId=291194637