一、加密传输复制的实现
在默认的主从复制过程或远程连接到MySQL/MariaDB所有的链接通信中的数据都是明文的,外网里访问数据或则复制,存在安全隐患。通过SSL/TLS加密的方式进行复制的方法,来进一步提高数据的安全性
主服务器开启SSL:[mysqld] 加一行ssl
主服务器配置证书和私钥;并且创建一个要求必须使用SSL连接的复制账号
从服务器使用CHANGER MASTER TO 命令时指明ssl相关选项
特别提示:在配置之前先检查mysql服务是否支持ssl功能,如果have_ssl的值为'DISABLED'则支持;如果为'NO'则不支持,需要再重新编译安装或者安装具有ssl功能的版本
MariaDB [(none)]> SHOW VARIABLES LIKE '%ssl%'; +---------------+----------+ | Variable_name | Value | +---------------+----------+ | have_openssl | DISABLED | | have_ssl | DISABLED | | ssl_ca | | | ssl_capath | | | ssl_cert | | | ssl_cipher | | | ssl_key | | +---------------+----------+
1、CA
[root@centos7 ~]#mkdir /etc/my.cnf.d/ssl/ [root@centos7 ~]#cd /etc/my.cnf.d/ssl/ [root@centos7 ssl]#openssl genrsa 2048 > cakey.pem #生成私钥 [root@centos7 ssl]#openssl req -new -x509 -key cakey.pem -out cacert.pem -days 3650 #自签名证书 Country Name (2 letter code) [XX]:CN State or Province Name (full name) []:beijing Locality Name (eg, city) [Default City]:beijing Organization Name (eg, company) [Default Company Ltd]:magedu Organizational Unit Name (eg, section) []:30 Common Name (eg, your name or your server's hostname) []:ca.magedu.com Email Address []: [root@centos7 ssl]#openssl req -newkey rsa:2048 -days 365 -nodes -keyout master.key > master.csr Country Name (2 letter code) [XX]:CN State or Province Name (full name) []:beijing Locality Name (eg, city) [Default City]:beijing Organization Name (eg, company) [Default Company Ltd]:magedu Organizational Unit Name (eg, section) []:31 Common Name (eg, your name or your server's hostname) []:master.magedu.com Email Address []: [root@centos7 ssl]#openssl x509 -req -in master.csr -CA cacert.pem -CAkey cakey.pem -set_serial 01 > master.crt #签署master证书 [root@centos7 ssl]#openssl req -newkey rsa:2048 -days 365 -nodes -keyout slave.key > slave.csr Country Name (2 letter code) [XX]:CN State or Province Name (full name) []:beijing Locality Name (eg, city) [Default City]:beijing Organization Name (eg, company) [Default Company Ltd]:magedu Organizational Unit Name (eg, section) []:31 Common Name (eg, your name or your server's hostname) []:slave.magedu.com Email Address []: [root@centos7 ssl]#openssl x509 -req -in slave.csr -CA cacert.pem -CAkey cakey.pem -set_serial 02 > slave.crt #签署slave证书
[root@centos7 ssl]#openssl verify -CAfile cacert.pem master.crt slave.crt #检查证书是否可用 master.crt: OK slave.crt: OK
先在各个节点上创建/etc/my.cnf.d/ssl/文件夹,将各自的证书,CA的证书和各自的秘钥文件复制过去
[root@centos7 ssl]#scp -r cacert.pem master.crt master.key 192.168.95.5:/etc/my.cnf.d/ssl/
[root@centos7 ssl]#scp -r cacert.pem slave.crt slave.key 192.168.95.3:/etc/my.cnf.d/ssl/
2、master
[mysqld]
log_bin
server_id=1
datadir=/var/lib/mysql
ssl # 开启ssl功能 ssl-ca=/etc/my.cnf.d/ssl/cacert.pem #指定CA证书命令 ssl-cert=/etc/my.cnf.d/ssl/master.crt #指定自己的证书路径 ssl-key=/etc/my.cnf.d/ssl/master.key #指定自己的秘钥文件路径
[root@localhost ~]# systemctl restart mariadb
MariaDB [(none)]> show variables like '%ssl%'; #查看加密是否成功;
+---------------+------------------------------+
| Variable_name | Value |
+---------------+------------------------------+
| have_openssl | YES | #成功
| have_ssl | YES |
| ssl_ca | /etc/my.cnf.d/ssl/cacert.pem |
| ssl_capath | |
| ssl_cert | /etc/my.cnf.d/ssl/master.crt |
| ssl_cipher | |
| ssl_key | /etc/my.cnf.d/ssl/master.key |
+---------------+------------------------------+
MariaDB [(none)]> grant replication slave on *.* to laobai@'192.168.95.%' identified by '123456' REQUIRE SSL; #授权并强制用户使用 SSL登录
MariaDB [(none)]> show master logs;
+--------------------+-----------+
| Log_name | File_size |
+--------------------+-----------+
| mariadb-bin.000001 | 264 |
| mariadb-bin.000002 | 343 |
| mariadb-bin.000003 | 569 |
| mariadb-bin.000004 | 264 |
| mariadb-bin.000005 | 410 |
+--------------------+-----------+
3、slave
[mysqld] log_bin server_id=2 ssl [root@slave1 ~]# systemctl restart mariadb CHANGE MASTER TO MASTER_HOST='192.168.95.5', MASTER_USER='laobai', MASTER_PASSWORD='123456', MASTER_PORT=3306, MASTER_LOG_FILE='mariadb-bin.000006', MASTER_LOG_POS=245, MASTER_CONNECT_RETRY=10, MASTER_SSL=1, MASTER_SSL_CA = '/etc/my.cnf.d/ssl/cacert.pem', MASTER_SSL_CERT = '/etc/my.cnf.d/ssl/slave.crt', MASTER_SSL_KEY = '/etc/my.cnf.d/ssl/slave.key'; mysql> start slave;
二、MySQL复制的相关指令和变量总结
选项:
- log_bin 启用二进制日志,在主节点或级联复制中间的从节点必须要开启
- binlog_format=ROW 二进制日志记录方式为基于行的方式记录,强烈建议开启
- log-basename=master | slave ... 二进制日志的前缀名,不是必须向,但建议标识
- server_id = # 服务器ID,各个节点的ID必须唯一
- relay_log = relay-log 开启中继日志,并以relay-log为文件名开头,从节点开启
- relay_log_index = relay-log.index 中继日志索引文件
- log_slave_updates 作用是SQL线程重读中继日志时将改变数据的操作记录为二进制日志,在级联复制中使用
- ssl 开启ssl功能
- ssl-ca=/etc/my.cnf.d/ssl/cacert.pem
- ssl-cert=/etc/my.cnf.d/ssl/master.crt
- ssl-key=/etc/my.cnf.d/ssl/master.key
- sync_binlog=1 每次写后立即同步二进制日志到磁盘
- innodb_flush_log_at_trx_commit=1 每次事务提交立即同步日志写磁盘
- sync_master_info=# #次事件后master.info同步到磁盘
- skip_slave_start=ON 不自动启动slave
- sync_relay_log=# #次写后同步relay log到磁盘
- sync_relay_log_info=# #次事务后同步relay-log.info到磁盘
- auto_increment_offset=1 自动增长开始点,在主主复制中使用
- auto_increment_increment=2 增长幅度
变量:
- replicate_do_db= 指定复制库的白名单
- replicate_ignore_db= 指定复制库黑名单
- replicate_do_table= 指定复制表的白名单
- replicate_ignore_table= 指定复制表的黑名单
- replicate_wild_do_table= foo%.bar% 支持通配符
-
replicate_wild_ignore_table= 指定复制的表,黑名单
-
rpl_semi_sync_slave_enabled=1 开启半同步复制,需要安装模块
指令:
- START SLAVE; 启动主从复制
- STOP SLAVE; 停止复制
- SHOW SLAVE STATUS; 查看复制状态
- Seconds_Behind_Master: 0 从服务器是否落后于主服务
- RESET SLAVE ALL; 重置从服务器的配置
- MASTER_SSL=1, 配合 CHANGE MASTER TO 使用,开启ssl加密复制
- MASTER_SSL_CA = '/etc/my.cnf.d/ssl/cacert.pem',
- MASTER_SSL_CERT = '/etc/my.cnf.d/ssl/slave.crt',
- MASTER_SSL_KEY = '/etc/my.cnf.d/ssl/slave.key';
- PURGE { BINARY | MASTER } LOGS { TO 'log_name' | BEFORE datetime_expr } 删除二进制日志,谨慎操作
- SHOW MASTER STATUS 查看二进制日志状态
- SHOW BINLOG EVENTS 查看二进制日志
- SHOW BINARY LOGS 查看二进制日志
- SHOW PROCESSLIST 查看进程
Mysql高可用的实现
一、先搭建主从服务器
1、搭建主服务器,修改配置文件 并创建授权用户
[mysqld] log_bin server_id=1 binlog_format=row #将数据库格式改为行 skip_name_resolve #做名词解析,非常重要,必须写进来 MariaDB [(none)]> grant replication slave on *.* to laobai@'192.168.95.%' identified by '123456';
2、搭建从服务器