基于ssl的mysql的主从复制

实验环境:

system:CentOS Linux release 7.2.1511 (Core)
mariadb server:mariadb-server-5.5.44-2.el7.centos.x86_64
master server:10.1.51.20/16
slave server:10.1.51.30/16
最先安装mariadb-server
[[email protected] ~]# yum -y install mariadb-server

1、配置SSL

(1)master server上配置根CA

1)生成私钥文件

[[email protected] ~]# cd /etc/pki/CA/
[[email protected] CA]# (umask 077;openssl genrsa -out private/cakey.pem 2048)

2)生成自签证书

[[email protected] CA]# openssl req -new -x509 -key private/cakey.pem -out ./cacert.pem -days 365
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter ‘.‘, the field will be left blank.
-----
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]:linuxpao
Organizational Unit Name (eg, section) []:ops
Common Name (eg, your name or your server‘s hostname) []:www.linuxpao.vip
Email Address []:[email protected]

3)创建所需的辅助文件

[[email protected] CA]# touch index.txt
[[email protected] CA]# echo 01 > serial
[[email protected] CA]# ls
cacert.pem  certs  crl  index.txt  newcerts  private  serial

(2)为master server创建证书

1)创建私钥

[[email protected] ~]# mkdir /var/lib/mysql/ssl
[[email protected] ~]# cd /var/lib/mysql/ssl/
[[email protected] ssl]# (umask 077;openssl genrsa -out ./master.key 2048)

2)生成证书申请文件

[[email protected] ssl]# openssl req -new -key master.key -out master.csr
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter ‘.‘, the field will be left blank.
-----
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]:linuxpao
Organizational Unit Name (eg, section) []:ops
Common Name (eg, your name or your server‘s hostname) []:www.linuxpao.vip    
Email Address []:[email protected] 

Please enter the following ‘extra‘ attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:

(3)根CA签发master server的证书申请

[[email protected] ssl]# openssl ca -in master.csr -out master.crt -days 365
Using configuration from /etc/pki/tls/openssl.cnf
Check that the request matches the signature
Signature ok
Certificate Details:
        Serial Number: 1 (0x1)
        Validity
            Not Before: Nov 22 02:17:15 2016 GMT
            Not After : Nov 22 02:17:15 2017 GMT
        Subject:
            countryName               = cn
            stateOrProvinceName       = beijing
            organizationName          = linuxpao
            organizationalUnitName    = ops
            commonName                = www.linuxpao.vip
            emailAddress              = [email protected]
        X509v3 extensions:
            X509v3 Basic Constraints: 
                CA:FALSE
            Netscape Comment: 
                OpenSSL Generated Certificate
            X509v3 Subject Key Identifier: 
                DC:5E:B2:B7:8F:1D:D0:FC:88:17:F5:01:B7:D7:2F:B0:8E:36:E4:5C
            X509v3 Authority Key Identifier: 
                keyid:23:9E:40:8C:86:1E:4B:58:9D:94:EE:C8:FA:1B:BD:E6:BA:C5:87:C6

Certificate is to be certified until Nov 22 02:17:15 2017 GMT (365 days)
Sign the certificate? [y/n]:y

1 out of 1 certificate requests certified, commit? [y/n]y
Write out database with 1 new entries
Data Base Updated

[[email protected] ssl]# ls
master.crt  master.csr  master.key

(4)为slave server上的SSL配置

1)生成私钥

[[email protected] ~]# mkdir /var/lib/mysql/ssl
[[email protected] ~]# cd /var/lib/mysql/ssl/
[[email protected] ssl]# (umask 077;openssl genrsa -out slave.key 2048)

2)创建证书申请

[[email protected] ssl]# openssl req -new -key ./slave.key -out slave.csr
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter ‘.‘, the field will be left blank.
-----
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]:linuxpao
Organizational Unit Name (eg, section) []:ops
Common Name (eg, your name or your server‘s hostname) []:slave.linuxpao.vip
Email Address []:[email protected]

Please enter the following ‘extra‘ attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:

[[email protected] ssl]# scp slave.csr 10.1.51.20:/testdir(将证书申请文件拷贝到master server上)

(5)根CA(master server)上为从服务器签署证书

[[email protected] testdir]# openssl ca -in slave.csr -out slave.crt -days 365

[[email protected] testdir]# ls
slave.crt  slave.csr

[[email protected] testdir]# scp slave.crt 10.1.51.30:/var/lib/mysql/ssl(将授权的证书拷贝回slave server上)

(6)将master server上的自检证书拷贝的到各服务器上

[[email protected] ssl]# cd /etc/pki/CA/
[[email protected] CA]# cp cacert.pem /var/lib/mysql/ssl/
[[email protected] CA]# scp cacert.pem 10.1.51.30:/var/lib/mysql/ssl/

(7)在master server上修改证书权限

[[email protected] CA]# cd /var/lib/mysql/ssl
[[email protected] ssl]# chown -R mysql.mysql ./
[[email protected] ssl]# ll
total 20
-rw-r--r--. 1 mysql mysql 1432 Nov 22 10:44 cacert.pem
-rw-r--r--. 1 mysql mysql 4641 Nov 22 10:17 master.crt
-rw-r--r--. 1 mysql mysql 1062 Nov 22 10:14 master.csr
-rw-------. 1 mysql mysql 1679 Nov 22 10:12 master.key

(8)在slave server上修改证书权限

[[email protected] ssl]# chown -R mysql.mysql ./
[[email protected] ssl]# ll
total 20
-rw-r--r--. 1 mysql mysql 1432 Nov 22 10:40 cacert.pem
-rw-r--r--. 1 mysql mysql 4647 Nov 22 10:31 slave.crt
-rw-r--r--. 1 mysql mysql 1062 Nov 22 10:26 slave.csr
-rw-------. 1 mysql mysql 1675 Nov 22 10:24 slave.key

2、配置mariadb server

(1)分别编辑主从服务器的/etc/my.cnf文件

1)master server

在[mysqld]段后面添加如下内容

skip_name_resolve = ON
innodb_file_per_table = ON
log-bin = master-log
server-id = 1
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

2)slave server

在[mysqld]段后面添加如下内容

skip_name_resolve = ON
innodb_file_per_table = ON
relay-log = relay-log
server-id = 2
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

(2)在master server上授权复制的用户

[[email protected] ~]# systemctl start mariadb.service

[[email protected] ~]# mysql
MariaDB [(none)]> grant replication slave,replication client on *.* to ‘repluser‘@‘10.1.51.%‘ identified by ‘replpasswd‘ require ssl
MariaDB [(none)]> flush privileges;

MariaDB [(none)]> show master status;
+-------------------+----------+--------------+------------------+
| File              | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+-------------------+----------+--------------+------------------+
| master-log.000003 |      761 |              |                  |
+-------------------+----------+--------------+------------------+
1 row in set (0.00 sec)

(3)在slave server上指定复制的主机

[[email protected] ssl]# systemctl start mariadb.service

[[email protected] ssl]# mysql

MariaDB [(none)]> change master to
    -> master_host=‘10.1.51.20‘,
    -> master_user=‘repluser‘,
    -> master_password=‘replpasswd‘,
    -> master_log_file=‘mysql-log.000003‘,
    -> master_log_pos=‘761‘,
    -> 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‘;

MariaDB [(none)]> start slave;

3、测试

1)在master server上可以看到ssl功能开启

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 |
+---------------+-------------------------------+

2)在slave server上slave复制的状态

MariaDB [(none)]> show slave status\G
*************************** 1. row ***************************
           Slave_IO_State: Waiting for master to send event
              Master_Host: 10.1.51.20
              Master_User: repluser
              Master_Port: 3306
            Connect_Retry: 60
          Master_Log_File: master-log.000004
      Read_Master_Log_Pos: 245
           Relay_Log_File: relay-log.000002
            Relay_Log_Pos: 530
    Relay_Master_Log_File: master-log.000004
         Slave_IO_Running: Yes
        Slave_SQL_Running: Yes
        ...剪切部分内容...
       Master_SSL_Allowed: Yes
       Master_SSL_CA_File: /var/lib/mysql/ssl/cacert.pem
       Master_SSL_CA_Path: 
          Master_SSL_Cert: /var/lib/mysql/ssl/slave.crt
        Master_SSL_Cipher: 
           Master_SSL_Key: /var/lib/mysql/ssl/slave.key
        ...剪切部分内容...    
         Master_Server_Id: 1

3)在slave server上登录master server

[[email protected] ssl]# mysql -urepluser -h10.1.51.20 -preplpasswd --ssl-ca=/var/lib/mysql/ssl/cacert.pem --ssl-cert=/var/lib/mysql/ssl/slave.crt --ssl-key=/var/lib/mysql/ssl/slave.key

MariaDB [(none)]> \s
--------------
mysql  Ver 15.1 Distrib 5.5.44-MariaDB, for Linux (x86_64) using readline 5.1

Connection id:      10
Current database:   
Current user:       [email protected]
SSL:            Cipher in use is DHE-RSA-AES256-GCM-SHA384
Current pager:      stdout
Using outfile:      ‘‘
Using delimiter:    ;
Server:         MariaDB
Server version:     5.5.44-MariaDB-log MariaDB Server
Protocol version:   10
Connection:     10.1.51.20 via TCP/IP
Server characterset:    latin1
Db     characterset:    latin1
Client characterset:    utf8
Conn.  characterset:    utf8
TCP port:       3306
Uptime:         1 hour 6 min 19 sec

4)在master serve上创建数据库mydb,然后再slave server上查看

创建

MariaDB [(none)]> create database mydb;
Query OK, 1 row affected (0.02 sec)

查看

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mydb               |
| mysql              |
| performance_schema |
| ssl                |
| test               |
+--------------------+
6 rows in set (0.01 sec)

自此,基于ssl的mysql主从复制的实验已完成,新手实习,不足之处,望指正。

遇到的问题

问题1

由于粗心在slave server上指定master_host是,将ip地址一个的.号打成,号,导致如下错误(使用show slave status\G查看)

解决方法:

1)关闭slave server的slave复制功能

MariaDB [(none)]> stop slave

2)在master server上刷新日志

MariaDB [(none)]> flush logs;
Query OK, 0 rows affected (0.01 sec)

MariaDB [(none)]> show master status;
+-------------------+----------+--------------+------------------+
| File              | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+-------------------+----------+--------------+------------------+
| master-log.000004 |      245 |              |                  |
+-------------------+----------+--------------+------------------+
1 row in set (0.01 sec)

3)在slave server上更新日志文件及位置,并重新启动

MariaDB [(none)]> change master to master_log_file=‘master-log.000004‘,master_log_pos=245;
Query OK, 0 rows affected (0.01 sec)

MariaDB [(none)]> start slave;
Query OK, 0 rows affected (0.00 sec)
时间: 2024-08-20 22:14:24

基于ssl的mysql的主从复制的相关文章

基于SSL的mysql(MariaDB)主从复制

一.前言 备份数据库是生产环境中的首要任务,重中之重,有时候不得不通过网络进行数据库的复制,这样就需要保证数据在网络传输过程中的安全性,因此使用基于SSL的复制会大加强数据的安全性 二.准备工作 1.主从服务器时间同步 [[email protected] ~]# crontab -e */30 * * * * /usr/sbin/ntpdate 172.16.0.1 &>/dev/null 2.mysql说明 (1)主服务器 hostname:master    IP:172.16.7.2

基于SSL的Mysql主从复制

一.实验目的: 1.跨越互联网加密复制 mysql基于SSL加密连接,为安全从服配置证书,从服拿账号到主服复制时,必须得到主服验证.双方建立SSL会话. 二.实验架构 在分布式架构中,时间必须同步 主节点: station20:192.168.1.20 从节点: station21:192.168.1.21 三.实验步骤 1.建立ssh互信 [[email protected] ~]# echo -e"192.168.1.21\tstation21\tstation21.example.com&

MySql之基于ssl安全连接的主从复制

MySql基于ssl安全连接的主从复制 一.设备环境 centos7.2   两台 MySQL 5.7 MySQL 5.7 主要特性: 原生支持Systemd更好的性能:对于多核CPU.固态硬盘.锁有着更好的优化更好的InnoDB存储引擎更为健壮的复制功能:复制带来了数据完全不丢失的方案,传统金融客户也可以选择使用MySQL数据库. 新增sys库:以后这会是DBA访问最频繁的库更好的优化器:优化器代码重构的意义将在这个版本及以后的版本中带来巨大的改进,Oracle官方正在解决MySQL之前最大的

基于Mysql-Proxy实现Mysql的主从复制以及读写分离(上)

基于Mysql-Proxy实现Mysql的主从复制以及读写分离(上) 上周BOSS给分配任务让实现一下Mysql数据库的主从复制以及读写分离,然后花了一盏茶的功夫进行了调研,发现主从复制数据库进行一番配置直接可以实现,而读写分离则需要一些软件的支持,基本上读写分离的实现有两种: Amoeba(变形虫):是由前阿里员工实现的一个以MySQL为底层数据存储,并对应用提供MySQL协议接口的proxy.但是由于没人维护了,而且据说作者也不再回答开发者的问题,所以不予考虑. Mysql-Proxy:是一

基于Mysql-Proxy实现Mysql的主从复制以及读写分离(下)

基于Mysql-Proxy实现Mysql的主从复制以及读写分离(下) 昨天谈到了Mysql实现主从复制,但由于时间原因并未讲有关读写分离的实现,之所以有读写分离,是为了使数据库拥有双机热备功能,至于双机热备,特指基于高可用系统中的两台服务器的热备(或高可用),因两机高可用在国内使用较多,故得名双机热备,双机高可用按工作中的切换方式分为:主-备方式(Active-Standby方式)和双主机方式(Active-Active方式),主-备方式即指的是一台服务器处于某种业务的激活状态(即Active状

mysql5.7:mysql安装和基于SSL加密的主从复制(详细剖析)

小生博客:http://xsboke.blog.51cto.com 小生 Q Q:1770058260 -------谢谢您的参考,如有疑问,欢迎交流 目录: --------mysql-5.7.13简介及安装 --------配置mysql-5.7.13的ssl加密传输 --------基于SSL加密传输实现mysql-5.7.13的主从复制 一. Mysql5.7.13简介 1. Mysql5.7的主要优化 mysql5.7原生支持centos7.*版本的systemd 更好的性能:对于多核

MariaDB数据库主从复制、双主复制、半同步复制、基于SSL的安全复制实现及其功能特性介绍

一.复制概述 MariaDB/MySQL内建的复制功能是构建大型,高性能应用程序的基础.将MySQL的数据分布到多个系统上去,这种分布的机制,是通过将MySQL的某一台主机的数据复制到其它主机(slaves)上,并重新执行一遍来实现的.复制过程中一个服务器充当主服务器,而一个或多个其它服务器充当从服务器.主服务器将更新写入二进制日志文件,并维护文件的一个索引以跟踪日志循环.这些日志可以记录发送到从服务器的更新.当一个从服务器连接主服务器时,它通知主服务器从服务器在日志中读取的最后一次成功更新的位

centos7.2安装mysql5.7.13实现 ssl 安全连接的主从复制

防伪码:不要和我比懒,我懒得和你比. 一.MySQL 5.7 主要特性: 原生支持 Systemd 更好的性能:对于多核 CPU.固态硬盘.锁有着更好的优化 更好的 InnoDB 存储引擎 更为健壮的复制功能:复制带来了数据完全不丢失的方案,传统金融客户也可以选择使用 MySQL 数据库. 注:mysql-5.6.3 已经支持了多线程的主从复制 新增 sys 库:以后这会是 DBA 访问最频繁的库 二.安装 mysql5.7.13 1.系统环境:centos7.2 x86_64 因为 cento

MySQL/MariaDB数据库基于SSL实现主从复制

前言 备份数据库是生产环境中的首要任务,有时候不得不通过网络进行数据库的复制,由于MySQL/MariaDB的主从复制是明文传送的,如果在生产环境中跨网络传送,数据的安全性就无法完全保证,为了解决这一问题,我们需要一种安全的方式进行传送,即基于SSL加密进行数据传输. 部署配置 实验拓扑 实验环境 系统环境:CentOS6.6 数据库版本:mariadb-5.5.36 #注意:主从服务器数据库版本须一致:主从服务器时间须同步 #此实验从服务器只做一组为例 配置主从复制 安装mariadb [[e