Mysql架构之主从复制
author:JevonWei
版权声明:原创作品
主从复制架构
架构角色
mysql-master:192.168.198.139
mysql-slave:192.168.198.128
主数据库和从数据库需提前安装好mariadb数据库
[root@mysql-master ~]# yum -y install mariadb-server
[root@mysql-master ~]# iptables -F
mysql-master
[root@mysql-master ~]# vim /etc/my.cnf.d/server.cnf
[mysqld]
server_id=1 \\定义server_id,每个数据库服务端都必须有唯一的识别ID
log_bin = master-log \\开启二进制日志记录
skip_name_resolve = ON \\禁止主机名解析
[root@mysql-master ~]# systemctl start mariadb
[root@mysql-master ~]# mysql
MariaDB [(none)]> SHOW MASTER STATUS;
+-------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+-------------------+----------+--------------+------------------+
| master-log.000001 | 245 | | |
+-------------------+----------+--------------+------------------+
授权一个有权限做主从复制的用户
MariaDB [(none)]> GRANT REPLICATION CLIENT,REPLICATION SLAVE ON *.* TO 'repluser'@'192.168.198.128' IDENTIFIED BY 'replpass';
MariaDB [(none)]> FLUSH PRIVILEGES;
授权用户之后,二进制日志文件节点发生变化,并记录二进制文件的位置信息,用于从节点的复制起点
MariaDB [(none)]> SHOW MASTER STATUS;
+-------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+-------------------+----------+--------------+------------------+
| master-log.000001 | 501 | | |
+-------------------+----------+--------------+------------------+
显示二进制文件的事件记录
MariaDB [(none)]> SHOW BINLOG EVENTS IN 'master-log.000001';
+-------------------+-----+-------------+-----------+-------------+------------------------------------------------------------------------------------------------------------+
| Log_name | Pos | Event_type | Server_id | End_log_pos | Info |
+-------------------+-----+-------------+-----------+-------------+------------------------------------------------------------------------------------------------------------+
| master-log.000001 | 4 | Format_desc | 1 | 245 | Server ver: 5.5.52-MariaDB, Binlog ver: 4 |
| master-log.000001 | 245 | Query | 1 | 426 | GRANT REPLICATION CLIENT,REPLICATION SLAVE ON *.* TO 'repluser'@'192.168.198.128' IDENTIFIED BY 'replpass' |
| master-log.000001 | 426 | Query | 1 | 501 | FLUSH PRIVILEGES |
+-------------------+-----+-------------+-----------+-------------+------------------------------------------------------------------------------------------------------------+
mysql-slave
[root@mysql-slave ~]# vim /etc/my.cnf.d/server.cnf
[mysqld]
relay-log = relay-log \\开启中继日志,并保存在/var/
server_id=5
skip_name_resolve = ON
read-only = ON \\设定slave数据库为只读状态,但仅对非supper用户有效
[root@mysql-slave ~]# systemctl start mariadb
[root@mysql-slave ~]# mysql
MariaDB [mydb]> SHOW GLOBAL VARIABLES LIKE 'read_only'; \\查看数据库为只读
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| read_only | ON |
+---------------+-------+
MariaDB [(none)]> HELP CHANGE MASTER TO 查看change master to 命令的帮助
CHANGE MASTER TO option [, option] ...
option:
MASTER_BIND = 'interface_name' \\MASTER节点使用哪个连接接口
| MASTER_HOST = 'host_name' \\MASTER的主机名或地址
| MASTER_USER = 'user_name' \\连接主节点时使用的有复制权限的用户权限
| MASTER_PASSWORD = 'password' \\连接主节点的用户密码
| MASTER_PORT = port_num \\主节点的端口号
| MASTER_CONNECT_RETRY = interval \\主节点连接失败的重试间隔
| MASTER_HEARTBEAT_PERIOD = interval
| MASTER_LOG_FILE = 'master_log_name' \\连接主节点的哪个二进制文件
| MASTER_LOG_POS = master_log_pos \\连接主节点二进制文件中的哪个起始位置
| RELAY_LOG_FILE = 'relay_log_name' \\复制完成后保存在本地哪个中继日志文件中
| RELAY_LOG_POS = relay_log_pos \\保存在中继日志的哪个位置
| MASTER_SSL = {0|1} \\配置使用SSL加密
| MASTER_SSL_CA = 'ca_file_name'
| MASTER_SSL_CAPATH = 'ca_directory_name'
| MASTER_SSL_CERT = 'cert_file_name'
| MASTER_SSL_KEY = 'key_file_name'
| MASTER_SSL_CIPHER = 'cipher_list'
| MASTER_SSL_VERIFY_SERVER_CERT = {0|1}
| IGNORE_SERVER_IDS = (server_id_list) \\忽略的server_id节点
连接主节点数据库
MariaDB [(none)]> CHANGE MASTER TO MASTER_HOST='192.168.198.139',MASTER_USER='repluser',MASTER_PASSWORD='replpass',MASTER_LOG_FILE='master-log.000001',MASTER_LOG_POS=501;
启动从节点复制进程
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.198.139
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master-log.000001 主节点的二进制文件
Read_Master_Log_Pos: 501 主节点的二进制文件的位置
Relay_Log_File: relay-log.000002 中继日志文件的初始化
Relay_Log_Pos: 530
Relay_Master_Log_File: master-log.000001 中继的日志文件
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: 501
Relay_Log_Space: 818
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
测试主从复制的效果
mysql-master
MariaDB [(none)]> CREATE DATABASE mydb;
MariaDB [(none)]> use mydb;
MariaDB [mydb]> CREATE TABLE tb1 (id INT,name CHAR(50));
MariaDB [mydb]> INSERT INTO tb1 VALUES (1,'dan');
mysql-slave
MariaDB [(none)]> SHOW SLAVE STATUS\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.198.139
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master-log.000001
Read_Master_Log_Pos: 957
Relay_Log_File: relay-log.000002
Relay_Log_Pos: 986
Relay_Master_Log_File: master-log.000001
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: 957
Relay_Log_Space: 1274
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 \\表示从节点复制时与主节点数据同步的延时时间,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
查看slave节点的数据库,是否实现数据复制
MariaDB [(none)]> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mydb |
| mysql |
| performance_schema |
+--------------------+
MariaDB [(none)]> use mydb;
MariaDB [mydb]> select * from tb1;
+------+------+
| id | name |
+------+------+
| 1 | dan |
+------+------+
测试非SUPPER用户在SLAVE节点上只读
mysql-master
授予test@192.168.198.%用户在testdb库中的权限
MariaDB [(none)]> GRANT SELECT,UPDATE,CREATE,DROP ON testdb.* TO 'test'@'192.168.198.%' IDENTIFIED BY 'testpass';
MariaDB [(none)]> CREATE database testdb;
MariaDB [(none)]> USE testdb;
MariaDB [testdb]> CREATE TABLE mytb1 (name CHAR(50));
使用普通用户test@192.168.198.%用户连接SLAVE数据库
[root@test ~]# mysql -utest -h192.168.198.128 -p
MariaDB [(none)]> use testdb;
MariaDB [testdb]> desc mytb1;;
+-------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| name | char(50) | YES | | NULL | |
+-------+----------+------+-----+---------+-------+
普通用户在SLAVE节点上只有只读权限
MariaDB [testdb]> INSERT INTO mytb1 VALUES ('danran');
ERROR 1290 (HY000): The MariaDB server is running with the --read-only option so it cannot execute this statement
注意:配置完成后先启动主节点
SLAVE节点上相应文件的信息
[root@mysql-slave mysql]# cat /var/lib/mysql/master.info \\记录了主节点上的连接的账号密码等信息
18
master-log.000001 \\复制主节点的二进制文件
501 \\复制时二进制文件的位置
192.168.198.139 \\主数据库的IP
repluser \\连接主节点的账号
replpass \\连接主节点的密码
3306 \\连接主节点的端口号
60
0
0
1800.000
0
[root@mysql-slave mysql]# cat /var/lib/mysql/relay-log.info \\记录了复制的二进制文件对应于本机的哪个中继日志文件
./relay-log.000001 \\保存到本地的中继日志文件
4 \\中继文件的位置
master-log.000001 \\复制的主节点二进制文件
501 \\二进制文件的位置
Mysql的双主复制架构
互为主从,即mysql-A为mysql-B的从节点,同时mysql-B也为mysql-A的从节点
角色
mysql-A:192.168.198.139
mysql-B:192.168.198.128
主数据库和从数据库需提前安装好mariadb数据库
[root@mysql-master ~]# yum -y install mariadb-server
[root@mysql-master ~]# iptables -F
mysql-A节点
[root@mysql-A ~]# vim /etc/my.cnf.d/server.cnf
[mysqld]
server_id=1
log_bin = master-log
relay-log = relay-log
skip_name_resolve = ON
auto_increment_offset=1 \\定义自动增长ID的起始值为1
auto_increment_increment=2 \\定义自动增长ID的增长量为2
[root@mysql-A ~]# systemctl start mariadb
mysql-B节点
[root@mysql-B ~]# vim /etc/my.cnf.d/server.cnf
[mysqld]
[mysqld]
server_id=5
relay_log=relay-log
log-bin=master-log
skip_name_resolve = ON
auto_increment_offset=2 \\定义自动增长ID的起始值为2
auto_increment_increment=2 \\定义自动增长ID的增长量为2
[root@mysql-B ~]# systemctl start mariadb
mysql-A
mysql-A连接数据库并授权复制的用户权限
MariaDB [(none)]> GRANT REPLICATION CLIENT,REPLICATION SLAVE ON *.* TO 'repluser'@'192.168.198.128' IDENTIFIED BY 'replpass';
MariaDB [(none)]> FLUSH PRIVILEGES;
查看二进制日志文件位置,用于从节点复制时的起点位置
MariaDB [(none)]> SHOW MASTER STATUS;
+-------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+-------------------+----------+--------------+------------------+
| master-log.000003 | 431 | | |
+-------------------+----------+--------------+------------------+
连接mysql-B主节点,即mysql-A作为mysql-B的从节点
MariaDB [(none)]> CHANGE MASTER TO MASTER_HOST='192.168.198.128',MASTER_USER='repluser',MASTER_PASSWORD='replpass',MASTER_LOG_FILE='master-log.000003',MASTER_LOG_POS=431;
启动复制线程
MariaDB [(none)]> START SLAVE;
查看作为从节点的状态
MariaDB [(none)]> SHOW SLAVE STATUS\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.198.128
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master-log.000003
Read_Master_Log_Pos: 617
Relay_Log_File: relay-log.000002
Relay_Log_Pos: 716
Relay_Master_Log_File: master-log.000003
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: 617
Relay_Log_Space: 1004
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: 5
mysql-B
mysql-B连接数据库并授权复制的用户权限
MariaDB [(none)]> GRANT REPLICATION CLIENT,REPLICATION SLAVE ON *.* TO 'repluser'@'192.168.198.139' IDENTIFIED BY 'replpass';
MariaDB [(none)]> FLUSH PRIVILEGES;
查看二进制日志文件位置,用于从节点复制时的起点位置
MariaDB [(none)]> SHOW MASTER STATUS;
+-------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+-------------------+----------+--------------+------------------+
| master-log.000003 | 431 | | |
+-------------------+----------+--------------+------------------+
连接mysql-A主节点,即mysql-B作为mysql-A的从节点
MariaDB [(none)]> CHANGE MASTER TO MASTER_HOST='192.168.198.139',MASTER_USER='repluser',MASTER_PASSWORD='replpass',MASTER_LOG_FILE='master-log.000003',MASTER_LOG_POS=431;
启动复制线程
MariaDB [(none)]> START SLAVE;
查看作为从节点的状态
MariaDB [(none)]> SHOW SLAVE STATUS\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.198.139
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master-log.000003
Read_Master_Log_Pos: 431
Relay_Log_File: relay-log.000002
Relay_Log_Pos: 530
Relay_Master_Log_File: master-log.000003
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: 431
Relay_Log_Space: 818
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
测试双主复制
mysql-A
MariaDB [(none)]> CREATE DATABASE mydb;
mysql-B
MariaDB [(none)]> use mydb;
MariaDB [mydb]> CREATE TABLE tb1 (id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,name CHAR(50));
mysql-A
MariaDB [(none)]> use mydb;
MariaDB [mydb]> desc tb1;
+-------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| name | char(50) | YES | | NULL | |
+-------+------------------+------+-----+---------+----------------+
MariaDB [mydb]> INSERT INTO tb1 (name) VALUES ('wei'),('jevon');
mysql-B
MariaDB [mydb]> SELECT * FROM tb1;
+----+-------+
| id | name |
+----+-------+
| 1 | wei |
| 3 | jevon |
+----+-------+
MariaDB [mydb]> INSERT INTO tb1 (name) VALUES ('dan'),('ran');
MariaDB [mydb]> SELECT * FROM tb1;
+----+-------+
| id | name |
+----+-------+
| 1 | wei |
| 3 | jevon |
| 4 | dan |
| 6 | ran |
+----+-------+
mysql-A
MariaDB [mydb]> SELECT * FROM tb1;
+----+-------+
| id | name |
+----+-------+
| 1 | wei |
| 3 | jevon |
| 4 | dan |
| 6 | ran |
+----+-------+
从节点的复制线程的启动会保存在mariadb.log错误日志中
[root@mysql-A ~]# tail /var/log/mariadb/mariadb.log
170914 10:11:20 [Note] Server socket created on IP: '0.0.0.0'.
170914 10:11:20 [Warning] 'user' entry 'root@danran.com' ignored in --skip-name-resolve mode.
170914 10:11:20 [Warning] 'user' entry '@danran.com' ignored in --skip-name-resolve mode.
170914 10:11:20 [Warning] 'proxies_priv' entry '@ root@danran.com' ignored in --skip-name-resolve mode.
170914 10:11:20 [Note] Event Scheduler: Loaded 0 events
170914 10:11:20 [Note] /usr/libexec/mysqld: ready for connections.
Version: '5.5.52-MariaDB' socket: '/var/lib/mysql/mysql.sock' port: 3306 MariaDB Server
170914 10:35:57 [Note] 'CHANGE MASTER TO executed'. Previous state master_host='', master_port='3306', master_log_file='', master_log_pos='4'. New state master_host='192.168.198.128', master_port='3306', master_log_file='master-log.000003', master_log_pos='431'.
170914 10:37:59 [Note] Slave SQL thread initialized, starting replication in log 'master-log.000003' at position 431, relay log './relay-log.000001' position: 4
170914 10:38:00 [Note] Slave I/O thread: connected to master 'repluser@192.168.198.128:3306',replication started in log 'master-log.000003' at position 431
半同步复制构架
半同步复制的架构中,slave重启mysql服务时,global.rpL_semi_sync_slave_enabled变量不会自动启动,需要set @@global.rpL_semi_sync_slave_enabled=1;手动启动
半同步复制的插件程序包
/usr/lib64/mysql/plugin/semisync_master.so
/usr/lib64/mysql/plugin/semisync_slave.so
半同步复制需要安装半同步复制的插件
mysql> INSTALL PLUGIN plugin_name SONAME 'shared_library_name';
半同步复制模块:
semisync_master.so
semisync_slave.so
主数据库和从数据库需提前安装好mariadb数据库
[root@mysql-master ~]# yum -y install mariadb-server
[root@mysql-master ~]# iptables -F
mysql-master
[root@mysql-master ~]# vim /etc/my.cnf.d/server.cnf
[mysqld]
server_id=1 \\定义server_id,每个数据库服务端都必须有唯一的识别ID
log_bin = master-log \\开启二进制日志记录
skip_name_resolve = ON \\禁止主机名解析
[root@mysql-master ~]# systemctl start mariadb
[root@mysql-master ~]# mysql
授权一个有权限做主从复制的用户
MariaDB [(none)]> GRANT REPLICATION CLIENT,REPLICATION SLAVE ON *.* TO 'repluser'@'192.168.198.128' IDENTIFIED BY 'replpass';
MariaDB [(none)]> FLUSH PRIVILEGES;
记录二进制文件的位置信息,用于从节点的复制起点
MariaDB [(none)]> SHOW MASTER STATUS;
+-------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+-------------------+----------+--------------+------------------+
| master-log.000001 | 501 | | |
+-------------------+----------+--------------+------------------+
安装半同步复制master插件
MariaDB [(none)]> INSTALL PLUGIN rpl_semi_sync_master SONAME 'semisync_master.so';
显示所有插件
MariaDB [(none)]> SHOW PLUGINS; 显示rpl_semi_sync_master插件已安装
+--------------------------------+----------+--------------------+--------------------+---------+
| Name | Status | Type | Library | License |
+--------------------------------+----------+--------------------+--------------------+---------+
| rpl_semi_sync_master | ACTIVE | REPLICATION | semisync_master.so | GPL |
+--------------------------------+----------+--------------------+-------------------+---------+
显示rpl_semi_sync_master模块相关的信息
MariaDB [(none)]> SHOW GLOBAL VARIABLES LIKE 'rpl%';
+------------------------------------+-------+
| Variable_name | Value |
+------------------------------------+-------+
| rpl_recovery_rank | 0 |
| 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 |
+------------------------------------+-------+
启用rpl_semi_sync_master模块
MariaDB [(none)]> SET @@GLOBAL.rpl_semi_sync_master_enabled=ON;
MariaDB [(none)]> SHOW GLOBAL VARIABLES LIKE 'rpl%';
+------------------------------------+-------+
| Variable_name | Value |
+------------------------------------+-------+
| rpl_recovery_rank | 0 |
| rpl_semi_sync_master_enabled | ON | \\模块已启用
| rpl_semi_sync_master_timeout | 10000 | \\同步等待时间,默认10s
| rpl_semi_sync_master_trace_level | 32 | \\设置跟踪级别
| rpl_semi_sync_master_wait_no_slave | ON |
+------------------------------------+-------+
显示rpl_semi_sync_master模块的状态变量
MariaDB [(none)]> SHOW GLOBAL STATUS LIKE 'rpl%';
+--------------------------------------------+-------------+
| 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 |
| 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 |
| Rpl_status | AUTH_MASTER |
+--------------------------------------------+-------------+
mysql-slave
[root@mysql-slave ~]# vim /etc/my.cnf.d/server.cnf
[mysqld]
relay-log = relay-log \\开启中继日志,并保存在/var/
server_id=5
skip_name_resolve = ON
read-only = ON \\设定slave数据库为只读状态,但仅对非supper用户有效
[root@mysql-slave ~]# systemctl start mariadb
[root@mysql-slave ~]# mysql
连接主节点数据库
MariaDB [(none)]> CHANGE MASTER TO MASTER_HOST='192.168.198.139',MASTER_USER='repluser',MASTER_PASSWORD='replpass',MASTER_LOG_FILE='master-log.000001',MASTER_LOG_POS=501;
启动从节点复制进程
[root@mysql-slave ~]# mysql
安装半同步复制slave插件
MariaDB [(none)]> INSTALL PLUGIN rpl_semi_sync_slave SONAME 'semisync_slave';
显示所有插件
MariaDB [(none)]> SHOW PLUGINS; 显示rpl_semi_sync_slave插件已安装
+--------------------------------+----------+--------------------+-------------------+---------+
| Name | Status | Type | Library | License |
+--------------------------------+----------+--------------------+-------------------+---------+
| rpl_semi_sync_slave | ACTIVE | REPLICATION | semisync_slave.so | GPL |
+--------------------------------+----------+--------------------+-------------------+---------+
显示rpl_semi_sync_slave模块相关的信息
MariaDB [(none)]> SHOW GLOBAL VARIABLES LIKE 'rpl%';
+---------------------------------+-------+
| Variable_name | Value |
+---------------------------------+-------+
| rpl_recovery_rank | 0 |
| rpl_semi_sync_slave_enabled | OFF | \\未启用
| rpl_semi_sync_slave_trace_level | 32 |
+---------------------------------+-------+
启用rpl_semi_sync_slave模块
MariaDB [(none)]> SET @@GLOBAL.rpl_semi_sync_slave_enabled=ON;
MariaDB [(none)]> SHOW GLOBAL VARIABLES LIKE 'rpl%';
+---------------------------------+-------+
| Variable_name | Value |
+---------------------------------+-------+
| rpl_recovery_rank | 0 |
| rpl_semi_sync_slave_enabled | ON |
| rpl_semi_sync_slave_trace_level | 32 |
+---------------------------------+-------+
初次启动SLAVE
MariaDB [(none)]> START SLAVE
或重新启动IO_THREAD
MariaDB [(none)]> STOP SLAVE IO_THREAD; 停止I/O线程
MariaDB [(none)]> START SLAVE IO_THREAD; 再重新开启I/O线程
查看SLAVE节点的状态
MariaDB [(none)]> SHOW SLAVE STATUS\G;
测试半同步复制
mysql-master
MariaDB [mydb]> INSERT INTO tb1 (name) VALUES ('dan'),('ran');
MariaDB [mydb]> INSERT INTO tb1 (name) VALUES ('daa'),('qq');
MariaDB [mydb]> INSERT INTO tb1 (name) VALUES ('sqw'),('wei');
MariaDB [mydb]> SHOW GLOBAL STATUS LIKE 'rpl%'; 状态变量记录发生变化
+--------------------------------------------+-------------+
| Variable_name | Value |
+--------------------------------------------+-------------+
| Rpl_semi_sync_master_clients | 1 |
| Rpl_semi_sync_master_net_avg_wait_time | 2230 | \\平均等待时长
| Rpl_semi_sync_master_net_wait_time | 6690 | \\等待时长
| Rpl_semi_sync_master_net_waits | 3 | \\网络等待的次数
| 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 | 669 | \\事务的等待时长
| Rpl_semi_sync_master_tx_wait_time | 669 | \\事务的等待次数
| Rpl_semi_sync_master_tx_waits | 1 | \\事务的平均等待时长
| Rpl_semi_sync_master_wait_pos_backtraverse | 0 |
| Rpl_semi_sync_master_wait_sessions | 0 |
| Rpl_semi_sync_master_yes_tx | 3 |
| Rpl_status | AUTH_MASTER |
+--------------------------------------------+-------------+
复制过滤器架构
mysql-master配置
[root@mysql-master ~]# vim /etc/my.cnf.d/server.cnf
[mysqld]
server_id=1 \\定义server_id,每个数据库服务端都必须有唯一的识别ID
log_bin = master-log \\开启二进制日志记录
skip_name_resolve = ON \\禁止主机名解析
[root@mysql-master ~]# systemctl restart mariadb
[root@mysql-master ~]# mysql
授权一个有权限做主从复制的用户
MariaDB [(none)]> GRANT REPLICATION CLIENT,REPLICATION SLAVE ON *.* TO 'repluser'@'192.168.198.128' IDENTIFIED BY 'replpass';
MariaDB [(none)]> FLUSH PRIVILEGES;
查看二进制日志文件位置,用于从节点复制时的起点位置
MariaDB [(none)]> SHOW MASTER STATUS;
+-------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+-------------------+----------+--------------+------------------+
| master-log.000004 | 2223 | | |
+-------------------+----------+--------------+------------------+
mysql-slave
[root@mysql-slave ~]# vim /etc/my.cnf.d/server.cnf
[mysqld]
server_id=5
relay_log=relay-log
skip_name_resolve = ON
read-only = ON
replicate_do_db = hidb
[root@mysql-slave ~]# systemctl restart mariadb \\重启mariadb服务,则复制线程自动重启
[root@mysql-slave ~]# mysql
显示SLAVE的状态
MariaDB [(none)]> SHOW SLAVE STATUS\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.198.139
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master-log.000004
Read_Master_Log_Pos: 2223
Relay_Log_File: relay-log.000005
Relay_Log_Pos: 530
Relay_Master_Log_File: master-log.000004
Slave_IO_Running: Yes \\重启mariadb服务,则复制线程自动重启
Slave_SQL_Running: Yes \\重启mariadb服务,则复制线程自动重启
Replicate_Do_DB: hidb \\过滤hidb数据库记录于二进制日志文件
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: 2223
Relay_Log_Space: 818
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
若复制线程Slave_IO/Slave_SQL没有启动,则手动启动
连接主节点数据库
MariaDB [(none)]> CHANGE MASTER TO MASTER_HOST='192.168.198.139',MASTER_USER='repluser',MASTER_PASSWORD='replpass',MASTER_LOG_FILE='master-log.000001',MASTER_LOG_POS=501;
启动从节点复制进程
MariaDB [(none)]> START SLAVE;
测试复制过滤器
mysql-master
MariaDB [(none)]> CREATE DATABASE hidb;
MariaDB [mydb]> CREATE TABLE tb2 (name VARCHAR(20));
mysql-slave
MariaDB [mydb]> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| hidb |
| mydb |
| mysql |
| performance_schema |
+--------------------+
MariaDB [mydb]> show tables;
+----------------+
| Tables_in_mydb |
+----------------+
| tb1 |
+----------------+
最终实现的是,SLAVE数据库服务端Replicate_Do_Table指定的hidb表可以复制到二进制日志文件中实现数据复制,不在此表中的数据并不会从中继日志文件传送到到二进制文件中,从而不能实现数据复制
主从复制架构之SSL加密
- ssl的复制是什么原理呢?
主服务器向CA申请一个自己的证书,从服务器也向相同的CA申请一个证书。同时主服务器和从服务器都拿了CA的公钥,这样就可以实现数据加密解密了。
实现流程如下
主服务器的证书情况如下:
[root@centos7 ssl]#chown -R mysql.mysql /var/lib/mysql/ssl/*
[root@centos7 ssl]#ll
total 20
-rw-r--r-- 1 mysql mysql 1257 Sep 14 16:10 cacert.pem//CA 的公钥文件
-rw-r--r-- 1 mysql mysql 4353 Sep 14 16:09 master.crt
-rw-r--r-- 1 mysql mysql 972 Sep 14 16:06 master.csr
-rw------- 1 mysql mysql 1675 Sep 14 16:05 master.key
从服务器的证书情况
[root@cnetos7 ssl]#chown -R mysql.mysql /var/lib/mysql/ssl/*
[root@cnetos7 ssl]#ll
总用量 20
-rw-r--r--. 1 mysql mysql 1257 9月 14 16:27 cacert.pem
-rw-r--r--. 1 mysql mysql 4394 9月 14 16:26 slave.crt
-rw-r--r--. 1 mysql mysql 997 9月 14 16:24 slave.csr
-rw-------. 1 mysql mysql 1675 9月 14 16:15 slave.key
主服务器mysql配置
[root@centos7 ~]#vim /etc/my.cnf.d/server.cnf
[mysqld]
server-id = 1
log_bin = master-log
innodb_file_per_table = ON
skip_name_resolve = ON
ssl
ssl-ca = /var/lib/mysql/ssl/cacert.pem
ssl-cert = /var/lib/mysql/ssl/master.crt
ssl-key = /var/lib/mysql/ssl/master.key
[root@centos7 ~]#systemctl start mariadb
MariaDB [(none)]> SHOW GLOBAL VARIABLES LIKE '%ssl%'; //验证主节点配置文件是否成功
+---------------+-------------------------------+
| Variable_name | Value |
+---------------+-------------------------------+
| have_openssl | YES |
| have_ssl | YES |
| ssl_ca | /var/lib/mysql/ssl/cacert.pem |
| ssl_capath | |
| ssl_cert | /var/lib/mysql/ssl/master.crt |
| ssl_cipher | |
| ssl_key | /var/lib/mysql/ssl/master.key |
+---------------+-------------------------------+
出现以上信息表示成功
从服务器上的配置
[root@centos7 ~]#vim /etc/my.cnf.d/server.cnf
[mysqld]
server-id = 2
relay_log = relay-log
innodb_file_per_table = ON
skip_name_resolve = ON
ssl
ssl-ca = /var/lib/mysql/ssl/cacert.pem
ssl-cert = /var/lib/mysql/ssl/slave.crt
ssl-key = /var/lib/mysql/ssl/slave.key
[root@centos7 ~]#systemctl start mariadb
MariaDB [(none)]> SHOW GLOBAL VARIABLES LIKE '%ssl%'; //验证从节点配置文件是否成功
+---------------+-------------------------------+
| Variable_name | Value |
+---------------+-------------------------------+
| have_openssl | YES |
| have_ssl | YES |
| ssl_ca | /var/lib/mysql/ssl/cacert.pem |
| ssl_capath | |
| ssl_cert | /var/lib/mysql/ssl/slave.crt |
| ssl_cipher | |
| ssl_key | /var/lib/mysql/ssl/slave.key |
+---------------+-------------------------------+
出现以上信息表示成功,
主服务器上创建复制用户
MariaDB [(none)]> GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'repluser'@'192.168.18.%' IDENTIFIED BY 'replpass';
MariaDB [(none)]> flush privileges;
MariaDB [(none)]> SHOW MASTER STATUS;
从节点连接主节点
MariaDB [(none)]> help change master to查看连接配置帮助
(1)CHANGE MASTER TO MASTER_HOST='192.168.18.131', MASTER_USER='repluser', MASTER_PASSWORD='replpass', MASTER_LOG_FILE='master-log.000003', MASTER_LOG_POS=499,MASTER_SSL=1,MASTER_SSL_CA='/var/lib/mysql/ssl/cacert.pem',MASTER_SSL_CERT='/var/lib/mysql/ssl/slave.crt',MASTER_SSL_KEY='/var/lib/mysql/ssl/slave.key';
(2)start slave;
(3)show slave status\G查看是否连接成功
查看是否进行了ssl加密
MariaDB [(none)]> status;
SSL: Cipher in use is DHE-RSA-AES256-GCM-SHA384 //可以看到是于SSL加密成功了。
Mysql架构之主从复制的更多相关文章
- 高可用Mysql架构_Mysql主从复制、Mysql双主热备、Mysql双主双从、Mysql读写分离(Mycat中间件)、Mysql分库分表架构(Mycat中间件)的演变
[Mysql主从复制]解决的问题数据分布:比如一共150台机器,分别往电信.网通.移动各放50台,这样无论在哪个网络访问都很快.其次按照地域,比如国内国外,北方南方,这样地域性访问解决了.负载均衡:M ...
- Mysql实现数据库主从复制架构
MySQL复制 (1)扩展方式: Scale Up ,Scale Out (2)MySQL的扩展 读写分离 复制:每个节点都有相同的数据集 向外扩展 二进制日志 单向 (3)复制的功用: 数据分布 负 ...
- MySQL数据的主从复制、半同步复制和主主复制详解
一.MySQL复制概述 ⑴.MySQL数据的复制的基本介绍 目前MySQL数据库已经占去数据库市场上很大的份额,其一是由于MySQL数据的开源性和高性能,当然还有重要的一条就是免费~不过不知道还能免费 ...
- MySQL数据的主从复制、半同步复制和主主复制详解-转
一.MySQL复制概述 ⑴.MySQL数据的复制的基本介绍 目前MySQL数据库已经占去数据库市场上很大的份额,其一是由于MySQL数据的开源性和高性能,当然还有重要的一条就是免费~不过不知道还能免费 ...
- Mysql的ssl主从复制+半同步主从复制
Mysql的ssl主从复制+半同步主从复制 准备工作 1.主从服务器时间同步 [root@localhost ~]# crontab -e */30 * * * * /usr/sbin/ntpdate ...
- MySQL 架构
原文:MySQL 架构 MySQL架构和结构分析 官方架构图: MySQL DB 各模块架构图如下: MySQL安装方式 MySQL初始化 简介:什么是事务: 事务: ACID : 事务确保了银行不 ...
- MySQL 5.5 主从复制
MySQL 5.5 主从复制的原理.过程 分为同步复制和异步复制,实际复制架构中大部分为异步复制.复制的基本过程如下: 1).Slave上面的IO进程连接上Master,并请求从指定日志文件的指 ...
- maria(mysql)的主从复制
一.mariadb的基本操作 1.远程连接 mysql -uroot -p -h 127.0.0.1 mysql -uroot -p -h 192.168.226.128 2.赋予远程连接的权限 gr ...
- Linux - centos7 下 MySQL(mariadb) 和 主从复制
目录 Linux - centos7 下 MySQL(mariadb) 和 主从复制 MySQL(mariadb) 安装MySQL(mariadb) 配置数据库的中文支持 在远程用 mysql客户端去 ...
随机推荐
- UVA 10564 Paths through the Hourglass(背包)
为了方便打印路径,考虑从下往上转移.dp[i][j][S]表示在i行j列总和为S的方案, dp[i][j][S] = dp[i+1][left][S-x]+dp[i+1][right][S-x] 方案 ...
- 更换PostgreSql的data文件夹并重新服务器(此方法同样适用于系统崩溃后,找回数据的操作)
*如果是系统崩溃,需要找回数据,PostgreSQL安装目录的data文件夹要存在 1.备份PostgreSQL安装目录到其他目录下 2.停止Postgres服务,可以在运行中输入services.m ...
- 解决Win10桌面右键卡顿一直转圈圈的
把系统重置之后,发现在桌面点击右键时一直转圈,但是在文件夹等非桌面位置都正常.可能是我之前修改注册表添加右键选项造成的,也可能不是,因为将修改的地方删除还是没有解决问题,555. 上网搜素一波,发现大 ...
- windows下安装Linux虚拟机
一.下载ios 下载网址:https://wwww.centos.org 选择一个.iso下载 二.安装一个vmware workstation或者Hyper-v的虚拟机 2.1.Hyper-v 2. ...
- 操作系统(4)_进程同步_李善平ppt
生产者进程count++是它的临界区,消费者count--是它的临界区. 经典同步问题,死锁问题,略.
- crontab -e 和/etc/crontab的区别
/etc/crontab文件和crontab -e命令区别/etc/crontab文件和crontab -e命令区别 1.格式不同 前者 # For details see man 4 crontab ...
- Python 正则表达式 匹配任意字符
.(句点)匹配除了换行之外的所有一个字符, .*(点-星)匹配除了换行外的所有字符 >>> >>> r=re.compile(r'.*')>>> ...
- tp5 使用paginate分页获取数据对象之后 如何对对象进行数据添加
tp5 使用paginate分页获取数据对象之后 如何对对象进行数据添加 大家都知道,在使用tp5的paginate获取分页数据之后,得到的是一个数据对象,但有时会碰到要对数据对象进行二次加工的情况, ...
- thinkphp 分页的 实现 和样式 分享
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAgoAAABlCAIAAACjnlykAAAI8UlEQVR4nO3bP2/bSBrH8eSQ5rq0eh ...
- [译]The Python Tutorial#1. Whetting Your Appetite
[译]The Python Tutorial#Whetting Your Appetite 1. Whetting Your Appetite 如果你需要使用计算机做很多工作,最终会发现很多任务需要自 ...