mysql基于SSL实现主从复制
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysql基于SSL实现主从复制相关的知识,希望对你有一定的参考价值。
mysql数据库基于SSL实现主从复制
实验环境:
node1:192.168.4.61
node2:192.168.4.62
CA:192.168.4.63
node1和node2时间同步
[[email protected]~]#ntpdate 172.18.0.1
[[email protected]~]#ntpdate 172.18.0.1
[[email protected]~]#vim /etc/chrony.conf
#node1和node2操作一样
[[email protected]~]#systemctl start chronyd.service
#启动服务
node1和node2基于key连接
[[email protected]~]#ssh-keygen
[[email protected]~]#ssh-copy-id -i /root/.ssh/id_rsa.pub 192.168.4.62
#将公钥复制到node2上
node2操作和node1操作相同。
修改hosts文件
node1和node2配置相同
[[email protected]~]#vim /etc/hosts
3 192.168.4.61 node1 4 192.168.4.62 node2
确保关闭iptables和selinux
node1和node2安装mariadb数据库
[[email protected]~]#yum install -y mariadb-server
[[email protected]~]#yum install -y mariadb-server
配置node1为主服务器
[[email protected]~]#vim /etc/my.cnf.d/server.cnf
[[email protected]~]#systemctl start mariadb
#启动mariadb服务
MariaDB [(none)]> GRANT REPLICATION CLIENT,REPLICATION SLAVE ON *.* TO ‘joah‘@‘192.168.4.62‘ IDENTIFIED BY ‘123456‘; #对用户授权
查看是否开启二进制日志
MariaDB [(none)]> SHOW VARIABLES LIKE ‘%log%bin%‘;
在node1上二进制日志状态信息
MariaDB [(none)]> SHOW MASTER STATUS;
配置node2从服务器
[[email protected]~]#vim /etc/my.cnf.d/server.cnf
[[email protected]~]#systemctl start mariadb
#启动mariadb服务
MariaDB [(none)]> CHANGE MASTER TO MASTER_HOST=‘192.168.4.61‘,MASTER_USER=‘joah‘,MASTER_PASSWORD=‘123456‘,MASTER_LOG_FILE=‘master-log.000003‘,MASTER_LOG_POS=417; #设置连接主服务器
启动从服务器
MariaDB [(none)]> START SLAVE;
启动IO线程和SQL线程
MariaDB [(none)]> START SLAVE IO_THREAD,SQL_THREAD;
查看从服务器状态
MariaDB [(none)]> START SLAVE IO_THREAD,SQL_THREAD;
如果出现图中红框中的信息说明已经启动成功。
测试是否已经实现主从复制
实现SSL功能
搭建CA服务器
[[email protected]/etc/pki/CA]#touch index.txt
[[email protected]/etc/pki/CA]#echo 01 > serial
生成key文件
[[email protected]/etc/pki/CA]#(umask 077;openssl genrsa -out private/cakey.pem 2048)
生成自签证书
[[email protected]/etc/pki/CA]#openssl req -new -x509 -key private/cakey.pem -out cacert.pem -days 3650
node1生成证书
[[email protected]~]#mkdir /etc/mysql/ssl -pv
[[email protected]~]#chown mysql.mysql /etc/mysql/ssl/ -R
[[email protected]/etc/mysql/ssl]#(umask 077;openssl genrsa -out master.key 2048)
[[email protected]/etc/mysql/ssl]#openssl req -new -key master.key -out master.csr
[[email protected]/etc/mysql/ssl]#scp master.csr 192.168.4.63:/etc/pki/CA/newcerts/
[[email protected]/etc/pki/CA/newcerts]#openssl ca -in master.csr -out master.crt -days 365
[[email protected]/etc/pki/CA/newcerts]#scp master.crt ../cacert.pem 192.168.4.61:/etc/mysql/ssl
[[email protected]~]#vim /etc/my.cnf.d/server.cnf
[[email protected]~]#systemctl restart mariadb
查看是否开启SSL功能
node2生成证书
[[email protected]~]#mkdir /etc/mysql/ssl -pv
[[email protected]~]#chown mysql.mysql /etc/mysql/ssl/ -R
[[email protected]/etc/mysql/ssl]#(umask 077;openssl genrsa -out slave.key 2048)
[[email protected]/etc/mysql/ssl]#openssl req -new -key slave.key -out slave.csr
[[email protected]/etc/mysql/ssl]#scp slave.csr 192.168.4.63:/etc/pki/CA/newcerts
[[email protected]/etc/pki/CA/newcerts]#openssl ca -in slave.csr -out slave.crt -days 365
[[email protected]/etc/pki/CA/newcerts]#scp slave.crt ../cacert.pem 192.168.4.62:/etc/mysql/ssl
[[email protected]/etc/mysql/ssl]#vim /etc/my.cnf.d/server.cnf
重启服务
[[email protected]/etc/mysql/ssl]#systemctl restart mariadb
基于SSL连接
node1授权
MariaDB [(none)]> GRANT REPLICATION CLIENT,REPLICATION SLAVE ON *.* TO ‘joah‘@‘192.168.4.62‘ IDENTIFIED BY ‘123456‘ REQUIRE SSL;
测试
[[email protected]~]#mysql -ujoah -p123456 -h192.168.4.61 --ssl
node2连接主服务器以ssl复制
MariaDB [(none)]> CHANGE MASTER TO MASTER_HOST=‘192.168.4.61‘,MASTER_USER=‘joah‘,MASTER_PASSWORD=‘123456‘,MASTER_LOG_FILE=‘master-log.000008‘,MASTER_LOG_POS=429,MASTER_SSL=1,MASTER_SSL_CA=‘/etc/mysql/ssl/cacert.pem‘,MASTER_SSL_CERT=‘/etc/mysql/ssl/slave.crt‘,MASTER_SSL_KEY=‘/etc/mysql/ssl/slave.key‘; MariaDB [(none)]> START SLAVE; #启动从服务器 MariaDB [(none)]> SHOW SLAVE STATUS\G;
小结
(1)如果你已经正确的添加了证书,但是启动以后还是没有启动SSL功能,有可能没有权限
[[email protected]~]#chown mysql.mysql -R /etc/mysql/ssl
(2)如果出现图片中的问题,停止slave即可
(3)每一个过程中都需要验证是否成功然后进行下面的操作。
本文出自 “Joah” 博客,请务必保留此出处http://merit.blog.51cto.com/10757694/1980965
以上是关于mysql基于SSL实现主从复制的主要内容,如果未能解决你的问题,请参考以下文章