IV 8 MySQL REPLICATION(SSL)

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介:

一、准备:

mysql replicationssl加密方式传输)

mysql-5.5.45-linux2.6-i686.tar.gz(通用二进制格式包)

node,一主一从

masternode1:192.168.41.131,既是CA又是master

slavenode2:192.168.41.132

环境:

[root@node1 ~]# uname -a

Linux node1.magedu.com 2.6.18-308.el5 #1SMP Fri Jan 27 17:21:15 EST 2012 i686 i686 i386 GNU/Linux

注意:masterslave的私钥及证书要同名(本例中两端均为mysql.keymysql.crt,要在两端各自生成),否则无法使用ssl传输

 

二、操作:

1、在masterslave上安装mysql

node{1,2}-side

[root@node1 ~]# mkdir /mydata/data -pv(生产环境最好将数据目录放在LVM中)

[root@node1 ~]# useradd -r mysql

[root@node1 ~]# chown -R mysql.mysql/mydata/data/

[root@node1 ~]# tar xf mysql-5.5.45-linux2.6-i686.tar.gz -C /usr/local/

[root@node1 ~]# cd /usr/local/

[root@node1 local]# ln -sv mysql-5.5.45-linux2.6-i686/ mysql

[root@node1 local]# ll

……

lrwxrwxrwx 1 root root    27 Dec 18 23:13mysql -> mysql-5.5.45-linux2.6-i686/

……

[root@node1 mysql]# chown -R root.mysql ./

[root@node1 mysql]# ll

……

[root@node1 mysql]# scripts/mysql_install_db --user=mysql --datadir=/mydata/data

[root@node1 mysql]# cp support-files/my-large.cnf /etc/my.cnf

[root@node1 mysql]# cp support-files/mysql.server /etc/init.d/mysqld

[root@node1 mysql]# chkconfig --add mysqld

[root@node1 mysql]# chkconfig mysqld on

[root@node1 mysql]# chkconfig --list mysqld

mysqld            0:off 1:off 2:on 3:on 4:on 5:on 6:off

[root@node1 mysql]# vim /etc/profile.d/mysql.sh

export PATH=$PATH:/usr/local/mysql/bin

[root@node1 mysql]# . !$

 

2、在master上(生成ca私钥及颁发ca自签证书;并签署颁发mysql-master的证书)

node1-side

[root@node1 mysql]# mkdir ssl/

[root@node1 mysql]# cd ssl

[root@node1 ssl]# vim /etc/pki/tls/openssl.cnf

[ CA_default ]

dir     = /etc/pki/CA

[root@node1 ssl]# cd /etc/pki/CA

 

[root@node1 CA]# (umask 077;openssl genrsa -out private/cakey.pem 2048)(生成ca私钥)

Generating RSA private key, 2048 bit longmodulus

................+++

...........................................................................................................+++

e is 65537 (0x10001)

 

[root@node1 CA]# ll private/

total 8

-rw------- 1 root root 1679 Dec 19 06:51cakey.pem

 

[root@node1 CA]# openssl req -new -x509 -key private/cakey.pem -out cacert.pem(颁发ca自签证书,注意server’shostname

Country Name (2 letter code) [GB]:CN

State or Province Name (full name)[Berkshire]:SH

Locality Name (eg, city) [Newbury]:SH

Organization Name (eg, company) [My CompanyLtd]:itownet

Organizational Unit Name (eg, section)[]:TECH

Common Name (eg, your name or your server'shostname) []:ca.magedu.com   

Email Address []:ca@magedu.com

 

[root@node1 CA]# touch index.txt

[root@node1 CA]# echo 01 > serial

[root@node1 CA]# ll

total 52

-rw-r--r-- 1 root root 1586 Dec 19 06:54cacert.pem

drwxr-xr-x 2 root root 4096 Dec 19 02:14certs

drwxr-xr-x 2 root root 4096 Dec 19 02:14crl

-rw-r--r-- 1 root root    0 Dec 19 06:55 index.txt

drwxr-xr-x 2 root root 4096 Dec 19 04:29newcerts

drwx------ 2 root root 4096 Dec 19 06:51private

-rw-r--r-- 1 root root    3 Dec 19 06:55 serial

 

[root@node1 CA]# cd /usr/local/mysql/ssl

[root@node1 ssl]# (umask 077;openssl genrsa -out mysql.key 1024)(生成master端的私钥)

Generating RSA private key, 1024 bit longmodulus

....++++++

...............................................................................++++++

e is 65537 (0x10001)

[root@node1 ssl]# openssl req -new -key mysql.key -out mysql.csr(生成master的证书签署请求certificate signature request,注意server’s hostname一定不能与slave端的重名)

……

Country Name (2 letter code) [GB]:CN

State or Province Name (full name)[Berkshire]:SH

Locality Name (eg, city) [Newbury]:SH

Organization Name (eg, company) [My CompanyLtd]:itownet

Organizational Unit Name (eg, section)[]:TECH

Common Name (eg, your name or your server'shostname) []:master.magedu.com

Email Address []:master@magedu.com

Please enter the following 'extra'attributes

to be sent with your certificate request

A challenge password []:

An optional company name []:

 

[root@node1 ssl]# openssl ca -in mysql.csr -out mysql.crt -days 365(为master颁发证书)

……

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

 

[root@node1 ssl]# cp /etc/pki/CA/cacert.pem./

[root@node1 ssl]# chown -R mysql.mysql ./

[root@node1 ssl]# ll

total 32

-rw-r--r-- 1 mysql mysql 1586 Dec 19 07:10 cacert.pem

-rw-r--r-- 1 mysql mysql 3830 Dec 19 07:08mysql.crt

-rw-r--r-- 1 mysql mysql  692 Dec 19 07:03 mysql.csr

-rw------- 1 mysql mysql  887 Dec 19 07:02 mysql.key

 

3、在slave上(生成私钥及slave的证书签署请求,传至ca端签署)

node2-side

[root@node2 ~]# mkdir /usr/local/mysql/ssl

[root@node2 ~]# cd !$

cd /usr/local/mysql/ssl

[root@node2 ssl]# (umask 077;openssl genrsa -out mysql.key 1024)(私钥)

Generating RSA private key, 1024 bit longmodulus

................................++++++

..++++++

e is 65537 (0x10001)

[root@node2 ssl]# openssl req -new -key mysql.key -out mysql.csr(证书签署请求,注意server’s hostname

……

Country Name (2 letter code) [GB]:CN

State or Province Name (full name)[Berkshire]:SH

Locality Name (eg, city) [Newbury]:SH

Organization Name (eg, company) [My CompanyLtd]:itownet

Organizational Unit Name (eg, section)[]:TECH

Common Name (eg, your name or your server'shostname) []:slave.magedu.com

Email Address []:slave@magedu.com

Please enter the following 'extra'attributes

to be sent with your certificate request

A challenge password []:

An optional company name []:

 

[root@node2 ssl]# scp mysql.csrnode1:/root/

mysql.csr                                      100%  692     0.7KB/s  00:00 

 

node1-side:(在主上签署请求,并将颁发给slave的证书及ca自签证书一同传给slave

[root@node1 ssl]# cd

[root@node1 ~]# openssl ca -in mysql.csr -out mysql.crt -days 365(在主上给予签署)

Sign the certificate? [y/n]:y

1 out of 1 certificate requests certified,commit? [y/n]y

[root@node1 ~]# scp mysql.crt  node2:/usr/local/mysql/ssl/

mysql.crt                                       100% 3824     3.7KB/s  00:00   

[root@node1 ~]# scp /etc/pki/CA/cacert.pem  node2:/usr/local/mysql/ssl/

cacert.pem                                      100%1586     1.6KB/s   00:00

 

node2-side

[root@node2 ssl]# chown -R mysql.mysql ./

[root@node2 ssl]# ll

total 32

-rw-r--r-- 1 mysql mysql 1586 Dec 19 07:23cacert.pem

-rw-r--r-- 1 mysql mysql 3824 Dec 19 07:23mysql.crt

-rw-r--r-- 1 mysql mysql  692 Dec 19 07:17 mysql.csr

-rw------- 1 mysql mysql  891 Dec 19 07:16 mysql.key

 

4、编辑masterslave的配置文件,并启动服务:

node1-side

[root@node1 ~]# vim /etc/my.cnf

[mysqld]

log-bin = mysql-bin

log-bin-index = mysql-bin.index

server-id = 1(注意主从不能一样,MySQL集群内唯一,范围12^32-1

datadir = /mydata/data

ssl(表示开启ssl

ssl_ca = /usr/local/mysql/ssl/cacert.pem

ssl_cert = /usr/local/mysql/ssl/mysql.crt

ssl_key = /usr/local/mysql/ssl/mysql.key

innodb_file_per_table = 1

[root@node1 ~]# service mysqld start

Starting MySQL..                                          [  OK  ]

 

node2-side:(中继日志必须开,可以不开启二进制日志)

[mysqld]

relay-log = relay-log

relay-log-index = relay-log.index

server-id = 11

datadir = /mydata/data

ssl

ssl_ca = /usr/local/mysql/ssl/cacert.pem

ssl_cert = /usr/local/mysql/ssl/mysql.crt

ssl_key = /usr/local/mysql/ssl/mysql.key

innodb_file_per_table = 1

[root@node2 ~]# service mysqld start

Starting MySQL..                                          [  OK  ]

 

5、在主端授权(复制仅能通过ssl传输复制),在从端连接到主:

node1-side

[root@node1 ~]# mysql

mysql> GRANT REPLICATION SLAVE ON *.* TO 'jowin'@'192.168.41.%' IDENTIFIED BY  'jowin' REQUIRE SSL;

Query OK, 0 rows affected (0.17 sec)

mysql> FLUSH PRIVILEGES;

Query OK, 0 rows affected (0.02 sec)

mysql> SHOW MASTER STATUS;

+------------------+----------+--------------+------------------+

| File             | Position | Binlog_Do_DB |Binlog_Ignore_DB |

+------------------+----------+--------------+------------------+

| mysql-bin.000011 |      347 |              |                  |

+------------------+----------+--------------+------------------+

1 row in set (0.00 sec)

mysql> SHOW GLOBAL VARIABLES LIKE '%ssl%';

+---------------+---------------------------------+

| Variable_name | Value                           |

+---------------+---------------------------------+

| have_openssl  | YES                             |

| have_ssl      | YES                             |

| ssl_ca        | /usr/local/mysql/ssl/cacert.pem |

| ssl_capath    |                                 |

| ssl_cert      | /usr/local/mysql/ssl/mysql.crt  |

| ssl_cipher    |                                 |

| ssl_key       | /usr/local/mysql/ssl/mysql.key  |

+---------------+---------------------------------+

7 rows in set (0.00 sec)

 

node2-side

[root@node2 ~]# mysql

mysql> change master to master_host='192.168.41.131',master_user='jowin',master_password='jowin',master_log_file='mysql-bin.000011',master_log_pos=347,master_ssl=1,master_ssl_ca='/usr/local/mysql/ssl/cacert.pem',master_ssl_cert='/usr/local/mysql/ssl/mysql.crt',master_ssl_key='/usr/local/mysql/ssl/mysql.key';

Query OK, 0 rows affected (0.13 sec)

mysql> START SLAVE;

Query OK, 0 rows affected (0.00 sec)

 

6、测试:

node1-side

mysql> CREATE DATABASE mydb;

Query OK, 1 row affected (0.01 sec)

 

node2-side

mysql> SHOW DATABASES;

+--------------------+

| Database           |

+--------------------+

| information_schema |

| mydb               |

| mysql              |

| performance_schema |

| test               |

+--------------------+

5 rows in set (0.08 sec)

mysql> SHOW SLAVE STATUS\G

*************************** 1. row***************************

               Slave_IO_State: Waiting formaster to send event

                  Master_Host: 192.168.41.131

                  Master_User: jowin

                  Master_Port: 3306

                Connect_Retry: 60

              Master_Log_File: mysql-bin.000012

         Read_Master_Log_Pos: 107

               Relay_Log_File: relay-log.000005

                Relay_Log_Pos: 253

       Relay_Master_Log_File: mysql-bin.000012

            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: 107

              Relay_Log_Space: 549

              Until_Condition: None

               Until_Log_File:

                Until_Log_Pos: 0

          Master_SSL_Allowed: Yes

          Master_SSL_CA_File: /usr/local/mysql/ssl/cacert.pem

          Master_SSL_CA_Path:

              Master_SSL_Cert:/usr/local/mysql/ssl/mysql.crt

           Master_SSL_Cipher:

               Master_SSL_Key:/usr/local/mysql/ssl/mysql.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

1 row in set (0.00 sec)

 

[root@node2 ~]# mysql -u jowin -p -h192.168.41.131(若不用ssl方式连接,则连不上)

Enter password:

ERROR 1045 (28000): Access denied for user'jowin'@'node2.magedu.com' (using password: YES)

 

[root@node2 ~]# mysql -ujowin -p -h192.168.41.131 --ssl-ca=/usr/local/mysql/ssl/cacert.pem  --ssl-cert=/usr/local/mysql/ssl/mysql.crt  --ssl-key=/usr/local/mysql/ssl/mysql.key(使用ssl方式则连接正常)

Enter password:

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 8

Server version: 5.5.45-log MySQL CommunityServer (GPL)

 

Copyright (c) 2000, 2015, Oracle and/or itsaffiliates. All rights reserved.

 

Oracle is a registered trademark of OracleCorporation and/or its

affiliates. Other names may be trademarksof their respective

owners.

 

Type 'help;' or '\h' for help. Type '\c' toclear the current input statement.

 

mysql>

 


本文转自 chaijowin 51CTO博客,原文链接:http://blog.51cto.com/jowin/1728229,如需转载请自行联系原作者

相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助     相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
7月前
|
网络协议 算法 关系型数据库
解读 MySQL Client/Server Protocol: Connection & Replication(上)
解读 MySQL Client/Server Protocol: Connection & Replication
57 0
|
关系型数据库 MySQL 网络安全
Mysql8.0 开启 SSL加密 mysql_ssl_rsa_setup
Mysql8.0 开启 SSL加密 mysql_ssl_rsa_setup
990 0
|
2月前
|
监控 负载均衡 关系型数据库
MySQL技能完整学习列表13、MySQL高级特性——1、分区表(Partitioning)——2、复制(Replication)——3、集群(Clustering)
MySQL技能完整学习列表13、MySQL高级特性——1、分区表(Partitioning)——2、复制(Replication)——3、集群(Clustering)
51 0
|
9月前
|
关系型数据库 MySQL 网络安全
Mysql高低版本切换需要修改的配置5-8(此处以aicode为例)(WARN: Establishing SSL connection without server‘s identity veri)
Mysql高低版本切换需要修改的配置5-8(此处以aicode为例)(WARN: Establishing SSL connection without server‘s identity veri)
76 0
|
关系型数据库 MySQL Java
Mysql开启ssl加密协议及Java客户端配置操作指南
本文主要讲述的是Mysql开启了ssl协议之后,Java客户端如何正确的链接Mysql访问数据
Mysql开启ssl加密协议及Java客户端配置操作指南
|
9月前
|
Linux Apache
"mod_SSL"和"mod_auth_mysql"
"mod_SSL"和"mod_auth_mysql"
97 1
|
9月前
|
存储 NoSQL 关系型数据库
An Overview of PostgreSQL & MySQL Cross Replication
An Overview of PostgreSQL & MySQL Cross Replication
63 0
|
10月前
|
SQL 存储 缓存
MySQL - 基于SSL安全连接的主从复制
MySQL - 基于SSL安全连接的主从复制
|
关系型数据库 MySQL 网络安全
MySql.Data.MySqlClient.MySqlException:“主机本地主机不支持SSL连接”
MySql.Data.MySqlClient.MySqlException:“主机本地主机不支持SSL连接”
131 0
|
关系型数据库 MySQL 网络安全
如何使用mysql api关闭ssl模式提高查询效率
通过查询MySQL的官方的英文网页说明,大致了解了设置的方法。通过mysql_options接口函数可以实现。