MySQL主從復制雜記(2)
阿新 • • 發佈:2018-03-16
MySQL 主從復制 基礎 雜記MySQL主從復制架構及實現
雜項
1、設置從節點為只讀模式
MariaDB [(none)]> SHOW GLOBAL VARIABLES LIKE ‘read_only‘; +---------------+-------+ | Variable_name | Value | +---------------+-------+ | read_only | OFF | +---------------+-------+ 從節點: 查看復制的主節點信息文件。 [root@master2 mysql]# cat master.info 18 master-bin.000003 1087 10.201.106.131 repluser replpass 3306 60 0 0 1800.000 0 從節點自己的本地中繼日誌文件位置,以及從主節點哪個日誌文件哪個位置復制記錄: [root@master2 mysql]# cat relay-log.info ./relay-log.000003 613 master-bin.000003 1087 MariaDB [(none)]> SHOW GLOBAL VARIABLES LIKE ‘%relay_log%‘; +----------------------------------+----------------+ | Variable_name | Value | +----------------------------------+----------------+ | innodb_recovery_update_relay_log | OFF | | max_relay_log_size | 0 | | relay_log | relay-log | | relay_log_index | | | relay_log_info_file | relay-log.info | | relay_log_purge | ON | | relay_log_recovery | OFF | | relay_log_space_limit | 0 | | sync_relay_log | 0 | | sync_relay_log_info | 0 | +----------------------------------+----------------+
2、主節點查看
MariaDB [(none)]> SHOW GLOBAL VARIABLES LIKE ‘%master%‘; +------------------------+-------+ | Variable_name | Value | +------------------------+-------+ | master_verify_checksum | OFF | | sync_master_info | 0 | 每一次給從節點發送一些event之後,本地對應的master_info會不會立即同步到磁盤上,能夠讓本地記錄下來。保證從節點及時得到更新 +------------------------+-------+
主主架構實現
1、恢復默認配置
[root@master1 mysql]# systemctl stop mariadb.service
[root@master2 mysql]# systemctl stop mariadb.service
[root@master1 mysql]# rm -rf /data/mysql/*
[root@master2 mysql]# rm -rf /data/mysql/*
2、編輯配置文件
[root@master1 mysql]# vim /etc/my.cnf [mysqld] datadir=/data/mysql log_bin=master-bin relay_log=relay-log server_id=1 innodb_file_per_table=ON skip_name_resolve=ON auto_increment_offset=1 auto_increment_increment=2 [root@master2 mysql]# vim /etc/my.cnf [mysqld] datadir=/data/mysql relay-log-index=relay-log.index datadir=/data/mysql log_bin=master-bin relay_log=relay-log server_id=5 innodb_file_per_table=ON skip_name_resolve=ON auto_increment_offset=2 auto_increment_increment=2 啟動數據庫 [root@master1 mysql]# systemctl start mariadb.service [root@master2 mysql]# systemctl start mariadb.service 查詢配置是否生效: MariaDB [(none)]> SHOW GLOBAL VARIABLES LIKE ‘%log%%‘;
3、創建擁有復制權限的賬號
生產環境應該單獨IP授權,一個一個給
MariaDB [(none)]> GRANT REPLICATION SLAVE,REPLICATION CLIENT ON *.* TO ‘repluser‘@‘10.201.106.%‘ IDENTIFIED BY ‘replpass‘;
MariaDB [(none)]> FLUSH PRIVILEGES;
MariaDB [(none)]> GRANT REPLICATION SLAVE,REPLICATION CLIENT ON *.* TO ‘repluser‘@‘10.201.106.%‘ IDENTIFIED BY ‘replpass‘;
MariaDB [(none)]> FLUSH PRIVILEGES;
4、指定雙方為從節點
[root@master1 mysql]#
MariaDB [(none)]> SHOW MASTER STATUS;
+-------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+-------------------+----------+--------------+------------------+
| master-bin.000003 | 428 | | |
+-------------------+----------+--------------+------------------+
MariaDB [(none)]> CHANGE MASTER TO MASTER_HOST=‘10.201.106.132‘,MASTER_USER=‘repluser‘,MASTER_PASSWORD=‘replpass‘,MASTER_LOG_FILE=‘master-bin.000003‘,MASTER_LOG_POS=508;
[root@master2 mysql]#
MariaDB [(none)]> SHOW MASTER STATUS;
+-------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+-------------------+----------+--------------+------------------+
| master-bin.000003 | 508 | | |
+-------------------+----------+--------------+------------------+
MariaDB [(none)]> CHANGE MASTER TO MASTER_HOST=‘10.201.106.131‘,MASTER_USER=‘repluser‘,MASTER_PASSWORD=‘replpass‘,MASTER_LOG_FILE=‘master-bin.000003‘,MASTER_LOG_POS=428;
查看狀態:
MariaDB [(none)]> SHOW SLAVE STATUS\G
5、啟動線程
[root@master2 mysql]#
MariaDB [(none)]> START SLAVE;
[root@master1 mysql]#
MariaDB [(none)]> START SLAVE;
6、測試
MariaDB [(none)]> CREATE DATABASE mydb;
MariaDB [(none)]> SHOW MASTER STATUS;
+-------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+-------------------+----------+--------------+------------------+
| master-bin.000003 | 516 | | |
+-------------------+----------+--------------+------------------+
備邊查看:
MariaDB [(none)]> SHOW SLAVE STATUS\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.201.106.131
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master-bin.000003
Read_Master_Log_Pos: 516
Relay_Log_File: relay-log.000002
Relay_Log_Pos: 618
————————
右邊節點測試:
MariaDB [mydb]> CREATE TABLE tb1(id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,name CHAR(30));
MariaDB [mydb]> DESC tb1;
+-------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| name | char(30) | YES | | NULL | |
+-------+------------------+------+-----+---------+----------------+
2 rows in set (0.09 sec)
MariaDB [mydb]> SHOW MASTER STATUS;
+-------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+-------------------+----------+--------------+------------------+
| master-bin.000003 | 659 | | |
左節點插入數據:
MariaDB [mydb]> INSERT INTO tb1 (name) VALUES (‘Yang Kang‘),(‘Yang Guo‘),(‘Yang Yanzhao‘);
MariaDB [mydb]> SELECT * FROM tb1;
+----+--------------+
| id | name |
+----+--------------+
| 1 | Yang Kang |
| 3 | Yang Guo |
| 5 | Yang Yanzhao |
上
右節點插入數據:
MariaDB [mydb]> SELECT * FROM tb1;
+----+---------------+
| id | name |
+----+---------------+
| 1 | Yang Kang |
| 3 | Yang Guo |
| 5 | Yang Yanzhao |
| 6 | Zhu Yuanzhang |
| 8 | Zhu di |
| 10 | Zhu Yue |
+----+---------------+
半同步復制
1、查看是否有相關插件
[root@master1 ~]# rpm -ql mariadb-server | grep semisync
/usr/lib64/mysql/plugin/semisync_master.so
/usr/lib64/mysql/plugin/semisync_slave.so
2、清除配置
[root@master1 ~]# systemctl stop mariadb
[root@master1 ~]# rm -rf /data/mysql/*
[root@master1 ~]# vim /etc/my.cnf
[mysqld]
datadir=/data/mysql
log_bin=master-bin
server_id=1
innodb_file_per_table=ON
skip_name_resolve=ON
[root@master1 ~]# systemctl start mariadb.service
[root@master2 ~]# systemctl stop mariadb
[root@master2 ~]# rm -rf /data/mysql/*
[root@master2 ~]# vim /etc/my.cnf
[mysqld]
datadir=/data/mysql
relay-log-index=relay-log.index
relay_log=relay-log
server_id=5
innodb_file_per_table=ON
skip_name_resolve=ON
[root@master2 ~]# systemctl start mariadb.service
3、主節點創建有復制權限的賬戶
MariaDB [(none)]> GRANT REPLICATION SLAVE,REPLICATION CLIENT ON *.* TO ‘repluser‘@‘10.201.106.%‘ IDENTIFIED BY ‘replpass‘;
MariaDB [(none)]> FLUSH PRIVILEGES;
MariaDB [(none)]> SHOW MASTER STATUS;
+-------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+-------------------+----------+--------------+------------------+
| master-bin.000003 | 498 | | |
+-------------------+----------+--------------+------------------+
4、從服務器配置從哪裏讀取日誌
MariaDB [(none)]> CHANGE MASTER TO MASTER_HOST=‘10.201.106.131‘,MASTER_USER=‘repluser‘,MASTER_PASSWORD=‘replpass‘,MASTER_LOG_FILE=‘master-bin.000003‘,MASTER_LOG_POS=498;
5、主服務器安裝插件
MariaDB [(none)]> INSTALL PLUGIN rpl_semi_sync_master SONAME ‘semisync_master.so‘;
MariaDB [(none)]> SHOW PLUGINS;
+--------------------------------+----------+--------------------+--------------------+---------+
| Name | Status | Type | Library | License |
| rpl_semi_sync_master | ACTIVE | REPLICATION | semisync_master.so | GPL |
MariaDB [(none)]> SHOW GLOBAL VARIABLES LIKE ‘%semi%‘;
+------------------------------------+-------+
| Variable_name | Value |
+------------------------------------+-------+
| rpl_semi_sync_master_enabled | OFF |
| rpl_semi_sync_master_timeout | 10000 |
| rpl_semi_sync_master_trace_level | 32 |
| rpl_semi_sync_master_wait_no_slave | ON |
+------------------------------------+-------+
MariaDB [(none)]> SHOW GLOBAL STATUS LIKE ‘%semi%‘;
+--------------------------------------------+-------+
| Variable_name | Value |
+--------------------------------------------+-------+
| Rpl_semi_sync_master_clients | 0 |
| Rpl_semi_sync_master_net_avg_wait_time | 0 |
| Rpl_semi_sync_master_net_wait_time | 0 |
| Rpl_semi_sync_master_net_waits | 0 |
| Rpl_semi_sync_master_no_times | 0 |
| Rpl_semi_sync_master_no_tx | 0 |
| Rpl_semi_sync_master_status | OFF |
| Rpl_semi_sync_master_timefunc_failures | 0 |
| Rpl_semi_sync_master_tx_avg_wait_time | 0 |
| Rpl_semi_sync_master_tx_wait_time | 0 |
| Rpl_semi_sync_master_tx_waits | 0 |
| Rpl_semi_sync_master_wait_pos_backtraverse | 0 |
| Rpl_semi_sync_master_wait_sessions | 0 |
| Rpl_semi_sync_master_yes_tx | 0 |
+--------------------------------------------+-------+
6、從節點安裝從節點插件
MariaDB [(none)]> INSTALL PLUGIN rpl_semi_sync_slave SONAME ‘semisync_slave.so‘;
MariaDB [(none)]> SHOW PLUGINS;
| rpl_semi_sync_slave | ACTIVE | REPLICATION | semisync_slave.so | GPL |
MariaDB [(none)]> SHOW GLOBAL VARIABLES LIKE ‘%semi%‘;
+---------------------------------+-------+
| Variable_name | Value |
+---------------------------------+-------+
| rpl_semi_sync_slave_enabled | OFF |
| rpl_semi_sync_slave_trace_level | 32 |
+---------------------------------+-------+
7、開啟主從節點插件功能
7.1 開啟主節點
MariaDB [(none)]> SET GLOBAL rpl_semi_sync_master_enabled=1;
MariaDB [(none)]> SHOW GLOBAL VARIABLES LIKE ‘%semi%‘;
+------------------------------------+-------+
| Variable_name | Value |
+------------------------------------+-------+
| rpl_semi_sync_master_enabled | ON |
MariaDB [(none)]> SHOW GLOBAL STATUS LIKE ‘%semi%‘;
+--------------------------------------------+-------+
| Variable_name | Value |
+--------------------------------------------+-------+
| Rpl_semi_sync_master_clients | 1 |
| Rpl_semi_sync_master_net_avg_wait_time | 0 |
| Rpl_semi_sync_master_net_wait_time | 0 |
| Rpl_semi_sync_master_net_waits | 0 |
| Rpl_semi_sync_master_no_times | 0 |
| Rpl_semi_sync_master_no_tx | 0 |
| Rpl_semi_sync_master_status | ON |
7.2 開啟從節點
MariaDB [(none)]> SET GLOBAL rpl_semi_sync_slave_enabled=1;
MariaDB [(none)]> SHOW GLOBAL VARIABLES LIKE ‘%SEMI%‘;
+---------------------------------+-------+
| Variable_name | Value |
+---------------------------------+-------+
| rpl_semi_sync_slave_enabled | ON |
8、從節點開啟復制線程
MariaDB [(none)]> START SLAVE;
MariaDB [(none)]> SHOW SLAVE STATUS\G;
9、測試
9.1 主節點創建數據庫和表
MariaDB [(none)]> CREATE DATABASE mydb;
MariaDB [mydb]> CREATE TABLE tb1 (id int,name char(30));
MariaDB [mydb]> SHOW GLOBAL STATUS LIKE ‘%semi%‘;
+--------------------------------------------+-------+
| Variable_name | Value |
+--------------------------------------------+-------+
| Rpl_semi_sync_master_clients | 1 |
| Rpl_semi_sync_master_net_avg_wait_time | 10910 |
| Rpl_semi_sync_master_net_wait_time | 21821 |
| Rpl_semi_sync_master_net_waits | 2 |
| Rpl_semi_sync_master_no_times | 0 |
| Rpl_semi_sync_master_no_tx | 0 |
| Rpl_semi_sync_master_status | ON |
| Rpl_semi_sync_master_timefunc_failures | 0 |
| Rpl_semi_sync_master_tx_avg_wait_time | 11830 |
| Rpl_semi_sync_master_tx_wait_time | 23660 |
| Rpl_semi_sync_master_tx_waits | 2 |
| Rpl_semi_sync_master_wait_pos_backtraverse | 0 |
| Rpl_semi_sync_master_wait_sessions | 0 |
| Rpl_semi_sync_master_yes_tx | 2 |
+--------------------------------------------+-------+
復制過濾器
1、設置從數據庫僅復制mydb數據庫
MariaDB [(none)]> SHOW GLOBAL VARIABLES LIKE ‘replicate%‘;
+----------------------------------+-----------+
| Variable_name | Value |
+----------------------------------+-----------+
| replicate_annotate_row_events | OFF |
| replicate_do_db | |
| replicate_do_table | |
| replicate_events_marked_for_skip | replicate |
| replicate_ignore_db | |
| replicate_ignore_table | |
| replicate_wild_do_table | |
| replicate_wild_ignore_table | |
+----------------------------------+-----------+
1.1 暫時關閉復制線程
MariaDB [(none)]> STOP SLAVE;
1.2 設置參數
MariaDB [(none)]> SET GLOBAL replicate_do_db=‘mydb‘;
MariaDB [(none)]> SHOW GLOBAL VARIABLES LIKE ‘replicate%‘;
+----------------------------------+-----------+
| Variable_name | Value |
+----------------------------------+-----------+
| replicate_annotate_row_events | OFF |
| replicate_do_db | mydb |
重新開啟復制進程:
MariaDB [(none)]> START SLAVE;
1.3 測試復制數據
主:
MariaDB [mydb]> CREATE DATABASE testdb;
從:
MariaDB [(none)]> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mydb |
| mysql |
| performance_schema |
| test |
+--------------------+
主:
MariaDB [mydb]> INSERT INTO tb1 VALUES (1,"a");
從:
MariaDB [mydb]> SELECT * FROM tb1;
+------+------+
| id | name |
+------+------+
| 1 | a |
+------+------+
2、SSL
MariaDB [mydb]> SHOW GLOBAL VARIABLES LIKE ‘%ssl%‘;
+---------------+----------+
| Variable_name | Value |
+---------------+----------+
| have_openssl | DISABLED |
| have_ssl | DISABLED |
| ssl_ca | |
| ssl_capath | |
| ssl_cert | |
| ssl_cipher | |
| ssl_key | |
+---------------+----------+
2.1 創建一個要求必須使用SSL連接的復制賬號
MariaDB [mydb]> GRANT REPLICATION SLAVE,REPLICATION CLIENT ON *.* TO ‘repluser‘@‘10.201.106.%‘ IDENTIFIED BY ‘replpass‘ REQUIRE SSL;
雜項
1、清理日誌
MariaDB [mydb]> SHOW BINARY LOGS;
+-------------------+-----------+
| Log_name | File_size |
+-------------------+-----------+
| master-bin.000001 | 30349 |
| master-bin.000002 | 1038814 |
| master-bin.000003 | 958 |
+-------------------+-----------+
MariaDB [mydb]> PURGE BINARY LOGS TO ‘master-bin.000002‘;
Query OK, 0 rows affected (0.03 sec)
MariaDB [mydb]> SHOW BINARY LOGS;
+-------------------+-----------+
| Log_name | File_size |
+-------------------+-----------+
| master-bin.000002 | 1038814 |
| master-bin.000003 | 958 |
+-------------------+-----------+
2、查看復制線程
MariaDB [mydb]> SHOW PROCESSLIST;
+----+----------+----------------------+------+-------------+------+-----------------------------------------------------------------------+------------------+----------+
| Id | User | Host | db | Command | Time | State | Info | Progress |
+----+----------+----------------------+------+-------------+------+-----------------------------------------------------------------------+------------------+----------+
| 7 | repluser | 10.201.106.132:55276 | NULL | Binlog Dump | 8090 | Master has sent all binlog to slave; waiting for binlog to be updated | NULL | 0.000 |
| 8 | root | localhost | mydb | Query | 0 | NULL | SHOW PROCESSLIST | 0.000 |
+----+----------+----------------------+------+-------------+------+-----------------------------------------------------------------------+------------------+----------+
2 rows in set (0.00 sec)
3、
MySQL主從復制雜記(2)