您好,登錄后才能下訂單哦!
?? dump Thread:為每個Slave的I/O Thread啟動一個dump線程,用于向其發送binary log events
?? I/O Thread:向Master請求二進制日志事件,并保存于中繼日志中
?? SQL Thread:從中繼日志中讀取日志事件,在本地完成重放
?? master.info:用于保存slave連接至master時的相關信息,例如賬號、密碼、服務器地址等
?? relay-log.info:保存在當前slave節點上已經復制的當前二進制日志和本地replay log日志的對應關系
?? 異步復制
?? 主從數據不一致比較常見
?? Master/Slave,Master/Master,環狀復制
?? 一主多從
?? 從服務器還可以再有從服務器
?? 一從多主:適用于多個不同數據庫
?? STATEMENT(5.0之前)
?? ROW(5.1之后,推薦)
?? MIXED
主機 | IP地址 | 類型 |
---|---|---|
CentOS7.6 | 192.168.36.101 | Master |
CentOS7.6 | 192.168.36.103 | Slave |
CentOS7.6 | 192.168.36.104 | Slave |
[root@Master ~]#cat /etc/my.cnf
[mysqld]
server_id=1 # 為Master節點設置一個全局唯一的ID號
binlog_format=row # 基于行復制的數據庫語句
log-bin=/data/bin/mysql-bin # 啟用二進制日志
[root@Master ~]#service mysqld restart
Restarting mysqld (via systemctl): [ OK ]
MariaDB [(none)]> grant replication slave on *.* to repluser@'192.168.36.%' identified by 'centos';
Query OK, 0 rows affected (0.00 sec)
MariaDB [mysql]> show master logs;
+------------------+-----------+
| Log_name | File_size |
+------------------+-----------+
| mysql-bin.000001 | 912372 |
+------------------+-----------+
1 row in set (0.00 sec)
[root@Slave-1 ~]#cat /etc/my.cnf
[mysqld]
server_id=2 # Slave節點設置全局唯一的ID號
read_only # 只讀
[root@Slave-1 ~]#systemctl restart mariadb
MariaDB [(none)]> CHANGE MASTER TO
-> MASTER_HOST='192.168.36.101',
-> MASTER_USER='repluser',
-> MASTER_PASSWORD='centos',
-> MASTER_PORT=3306,
-> MASTER_LOG_FILE='mysql-bin.000001',
-> MASTER_LOG_POS=245;
Query OK, 0 rows affected (0.01 sec)
MariaDB [(none)]> slave start;
Query OK, 0 rows affected (0.00 sec)
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.36.101
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 7389
Relay_Log_File: mariadb-relay-bin.000002
Relay_Log_Pos: 7673
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes # 從節點的IO線程
Slave_SQL_Running: Yes # 從節點的SQL線程
....
Seconds_Behind_Master: 0 # Master與SLave服務器差別延遲
.....
Master_Server_Id: 1
1 row in set (0.00 sec)
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| hellodb |
| mysql |
| performance_schema |
| test |
+--------------------+
5 rows in set (0.00 sec)
MariaDB [hellodb]> show processlist;
+----+----------+----------------------+---------+-------------+------+------------------------------------------------------------------
| Id | User | Host | db | Command | Time | State
+----+----------+----------------------+---------+-------------+------+------------------------------------------------------------------
| 3 | root | localhost | hellodb | Query | 0 | NULL
| 6 | repluser | 192.168.36.103:51516 | NULL | Binlog Dump | 115 | Master has sent all binlog to slave; waiting for binlog to be upd
+----+----------+----------------------+---------+-------------+------+------------------------------------------------------------------
2 rows in set (0.00 sec)
MariaDB [(none)]> show processlist;
+----+-------------+-----------+------+---------+-------+-----------------------------------------------------------------------------+--
| Id | User | Host | db | Command | Time | State | I
+----+-------------+-----------+------+---------+-------+-----------------------------------------------------------------------------+--
| 4 | root | localhost | NULL | Query | 0 | NULL | s
| 5 | system user | | NULL | Connect | 159 | Waiting for master to send event | N
| 6 | system user | | NULL | Connect | 29259 | Slave has read all relay log; waiting for the slave I/O thread to update it | N
+----+-------------+-----------+------+---------+-------+-----------------------------------------------------------------------------+--
3 rows in set (0.00 sec)
[root@Slave-2 ~]#yum install -y mariadb-server
[root@Slave-2 ~]#cat /etc/my.cnf
[mysqld]
server_id=3
read_only
....
[root@Slave-2 ~]#systemctl restart mariadb
MariaDB [(none)]> CHANGE MASTER TO
-> MASTER_HOST='192.168.36.101',
-> MASTER_USER='repluser',
-> MASTER_PASSWORD='centos',
-> MASTER_PORT=3306,
-> MASTER_LOG_FILE='mysql-bin.000001',
-> MASTER_LOG_POS=245;
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> slave start;
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| hellodb |
| mysql |
| performance_schema |
| test |
+--------------------+
6 rows in set (0.00 sec)
主機 | IP地址 | 類型 |
---|---|---|
CentOS7.6 | 192.168.36.101 | Master-1 |
CentOS7.6 | 192.168.36.103 | Master-2 |
[root@Master-1 data]#cat /etc/my.cnf
[mysqld]
server_id=1
log_bin # 啟用日志
# 為避免ID沖突問題,需要添加下面兩行配置:Master-1 為奇數增長,Master-2 為偶數增長
auto_increment_offset=1 # 開始點
auto_increment_increment=2 # 增長幅度
....
[root@Master-2 data]#cat /etc/my.cnf
[mysqld]
server_id=2
log_bin
auto_increment_offset=2
auto_increment_increment=2
[root@Master-1 ~]#systemctl restart mariadb
[root@Master-2 ~]#systemctl restart mariadb
MariaDB [(none)]> grant replication slave on *.* to repluser@'192.168.36.%' identified by 'centos';
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> CHANGE MASTER TO
-> MASTER_HOST='192.168.36.101',
-> MASTER_USER='repluser',
-> MASTER_PASSWORD='centos',
-> MASTER_PORT=3306,
-> MASTER_LOG_FILE='mysql-bin.000001',
-> MASTER_LOG_POS=245;
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> slave start;
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> use mysql;
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
MariaDB [mysql]> select user,password,host from user;
+----------+-------------------------------------------+--------------+
| user | password | host |
+----------+-------------------------------------------+--------------+
| root | | localhost |
| root | | 127.0.0.1 |
| root | | ::1 |
| repluser | *128977E278358FF80A246B5046F51043A2B1FCED | 192.168.36.% |
+----------+-------------------------------------------+--------------+
7 rows in set (0.00 sec)
MariaDB [mysql]> CHANGE MASTER TO
-> MASTER_HOST='192.168.36.103',
-> MASTER_USER='repluser',
-> MASTER_PASSWORD='centos',
-> MASTER_PORT=3306,
-> MASTER_LOG_FILE='mysql-bin.000001',
-> MASTER_LOG_POS=245;
Query OK, 0 rows affected (0.05 sec)
MariaDB [(none)]> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.36.103
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 245
Relay_Log_File: mariadb-relay-bin.000003
Relay_Log_Pos: 529
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
....
Master_SSL_Allowed: No
....
Master_Server_Id: 2
1 row in set (0.01 sec)
?? 半同步復制的作用是:主服務器宕機后,所有備份服務器主動向同步數據最多的服務器進行數據的同步,以確保數據損失降到最低。
MariaDB [db1]> INSTALL PLUGIN rpl_semi_sync_master SONAME 'semisync_master.so';
Query OK, 0 rows affected (0.07 sec)
MariaDB [db1]> SET GLOBAL rpl_semi_sync_master_enabled=1;
Query OK, 0 rows affected (0.00 sec)
MariaDB [db1]> SET GLOBAL rpl_semi_sync_master_timeout=6000;
Query OK, 0 rows affected (0.00 sec)
MariaDB [db1]> show variables like '%semi%';
+------------------------------------+-------+
| Variable_name | Value |
+------------------------------------+-------+
| rpl_semi_sync_master_enabled | ON |
| rpl_semi_sync_master_timeout | 6000 |
| rpl_semi_sync_master_trace_level | 32 |
| rpl_semi_sync_master_wait_no_slave | ON |
+------------------------------------+-------+
4 rows in set (0.00 sec)
[root@Master data]#cat /etc/my.cnf
[mysqld]
...
rpl_semi_sync_master_enabled # 啟用插件功能
...
[root@Master data]#systemctl restart mariadb
MariaDB [db1]> INSTALL PLUGIN rpl_semi_sync_slave SONAME 'semisync_slave.so';
Query OK, 0 rows affected (0.00 sec)
MariaDB [db1]> SET GLOBAL rpl_semi_sync_slave_enabled=1;
Query OK, 0 rows affected (0.00 sec)
[root@Slave data]#cat /etc/my.cnf
[mysqld]
...
rpl_semi_sync_slave_enabled # 啟用插件功能
...
[root@Slave data]#systemctl restart mariadb
MariaDB [(none)]> create database db3;
Query OK, 1 row affected (6.00 sec) # 由等待時長可以看出,半同步插件已經起到效果
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。