天天看點

基于SSL加密的MySQL主從複制

在MySQL的主從複制中,其傳輸過程是明文傳輸,并不能保證資料的安全性,在編譯安裝Mysql時,基本上都會加上一個 --with-openssl這樣的選項,即表示支openssl加密傳輸協定,是以就可以為mysql配置基于ssl加密傳輸啦。

規劃:

Master:

IP位址:172.16.4.111

MySQL版本:5.5.20

作業系統:RHEL5.4

Slave:

IP位址:172.16.4.112

一、設定主從服務

在172.16.4.111伺服器上

編輯/etc/my.cnf

#vim /etc/my.cnf

将serier_id修改為11

server_id=11                #修改server_id=11

log_bin=mysql-bin            #開啟二進制日志

sync_binlog=1                #任何一個事務送出之後就立即寫入到磁盤中的二進制檔案

innodb_flush_logs_at_trx_commit=1       #任何一個事物送出之後就立即寫入到磁盤中的日志檔案

儲存退出

啟動mysql

#service mysqld start

在172.16.4.112伺服器上

server_id=12                #修改server_id=12

#log-bin                #注釋掉log-bin,從伺服器不需要二進制日志,是以将其關閉 

relay-log=mysql-relay                #定義中繼日志名,開啟從伺服器中繼日志 

relay-log-index=mysql-relay.index     #定義中繼日志索引名,開啟從伺服器中繼索引

read_only=1                    #設定從伺服器隻能進行讀操作,不能進行寫操作

二、準備證書,私鑰

1、配置Master為CA伺服器

#vim /etc/pki/tls/openssl.cnf 

将  dir             = ../../CA修改為

dir             = /etc/pki/CA

#(umask 077;openssl genrsa 2048 > private/cakey.pem)

#openssl req -new -x509 -key private/cakey.pem -out cacert.pem -days 3650

#mkdir certs crl newcerts

#touch index.txt

#echo 01 > serial

2、為Master上的Mysql準備私鑰以及頒發證書

#mkdir /usr/local/mysql/ssl

#cd ssl/

#(umask 077;openssl genrsa 1024 > mysql.key)

#openssl req -new -key mysql.key -out mysql.csr

#openssl ca -in mysql.csr -out mysql.crt

#cp /etc/pki/CA/cacert.pem /usr/local/mysql/ssl/

#chown -R mysql.mysql ssl/

3、為Slave上的Mysql準備私鑰以及申請證書

#scp ./mysql.csr 172.16.4.111:/root

4、在Master上為Slave簽發證書

#cd

#scp ./mysql.crt 172.16.4.112:/usr/local/mysql/ssl

#cd /etc/pki/CA

#scp ./cacert.pem 172.16.4.112:/usr/local/mysql/ssl

到此證書以及私鑰已經準備好,請确認在Master以及Slave的/usr/local/mysql/ssl目錄下具有以下檔案,以及屬主和屬組:

<a target="_blank" href="http://blog.51cto.com/attachment/201204/134904549.png"></a>

<a target="_blank" href="http://blog.51cto.com/attachment/201204/134913702.png"></a>

三、開啟Mysql的ssl功能

登入Mysql檢視

mysql&gt; show variables like '%ssl%';

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

| Variable_name | Value    |

| have_openssl  | DISABLED | 

| have_ssl      | DISABLED | 

| ssl_ca        |          | 

| ssl_capath    |          | 

| ssl_cert      |          | 

| ssl_cipher    |          | 

| ssl_key       |          | 

輸出為DISABLED表示ssl還未開啟,

在[mysqld]和[mysqldump]之間,加入以下内容:

ssl                         #表示開啟mysql的ssl功能

儲存後重新啟動mysql,再次登入mysql

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

| Variable_name | Value |

| have_openssl  | YES   | 

| have_ssl      | YES   | 

| ssl_ca        |       | 

| ssl_capath    |       | 

| ssl_cert      |       | 

| ssl_cipher    |       | 

| ssl_key       |       | 

輸出為YES表示ssl已經開啟。

執行同樣的操作

在[mysqld]和[mysqldump]之間,加入一行ssl:

ssl

四、配置主從服務的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

這裡一定要對應到您所存放證書和私鑰的絕對路徑

儲存退出,重新啟動Mysql

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

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

五、記錄主伺服器的二進制日志檔案以及事件,并且建立基于ssl複制的使用者

mysql&gt; show master status;

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

| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |

| mysql-bin.000075 |      107 |              |                  | 

mysql&gt; grant replication client,replication slave on *.* to [email protected] identified by '135246' require ssl;

Query OK, 0 rows affected (0.01 sec)

mysql&gt; flush privileges;

Query OK, 0 rows affected (0.00 sec)

六、啟動從服務的從服務線程

mysql&gt; change master to \

    -&gt; master_host='172.16.4.111',

    -&gt; master_user='sslrepl',

    -&gt; master_password='135246',

    -&gt; master_log_file='mysql-bin.000075',

    -&gt; master_log_pos=107,

    -&gt; master_ssl=1,

    -&gt; master_ssl_ca='/usr/local/mysql/ssl/cacert.pem',

    -&gt; master_ssl_cert='/usr/local/mysql/ssl/mysql.crt',

    -&gt; master_ssl_key='/usr/local/mysql/ssl/mysql.key';

Query OK, 0 rows affected (0.17 sec)

mysql&gt; show slave status\G

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

               Slave_IO_State: 

                  Master_Host: 172.16.4.111

                  Master_User: sslrepl

                  Master_Port: 3306

                Connect_Retry: 60

              Master_Log_File: mysql-bin.000075

          Read_Master_Log_Pos: 608

               Relay_Log_File: relay-bin.000001

                Relay_Log_Pos: 4

        Relay_Master_Log_File: mysql-bin.000075

             Slave_IO_Running: No

            Slave_SQL_Running: No

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

              Relay_Log_Space: 107

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

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

            Master_SSL_Cipher: 

               Master_SSL_Key: /usr/local/mysql/ssl/mysql.key

        Seconds_Behind_Master: NULL

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&gt; start slave;

               Slave_IO_State: Queueing master event to the relay log

             Slave_IO_Running: Yes

            Slave_SQL_Running: Yes

              Relay_Log_Space: 403

        Seconds_Behind_Master: 2132

輸出資訊為

Slave_IO_Running: Yes

Slave_SQL_Running: Yes

Master_SSL_Allowed: Yes

說明,基于ssl的配置已經成功

七、驗證是否使用了ssl加密

使用ssrepl使用者登入mysql

#mysql --ssl-ca=/usr/local/mysql/ssl/cacert.pem --ssl-cert=/usr/local/mysql/ssl/mysql.crt --ssl-key=/usr/local/mysql/ssl/mysql.key -usslrepl -h172.16.4.111 -p135246

mysql&gt; \s

--------------

mysql  Ver 14.12 Distrib 5.0.77, for redhat-linux-gnu (i386) using readline 5.1

Connection id:        3

Current database:    

Current user:        [email protected]

SSL:            Cipher in use is DHE-RSA-AES256-SHA

Current pager:        stdout

Using outfile:        ''

Using delimiter:    ;

Server version:        5.5.19-log MySQL Community Server (GPL)

Protocol version:    10

Connection:        172.16.4.111 via TCP/IP

Server characterset:    latin1

Db     characterset:    latin1

Client characterset:    latin1

Conn.  characterset:    latin1

TCP port:        3306

Uptime:            8 min 18 sec

Threads: 3  Questions: 47  Slow queries: 0  Opens: 34  Flush tables: 1  Open tables: 27  Queries per second avg: 0.094

輸出資訊:

說明是加密連接配接了。

由于SSL相關的配置寫進了配置檔案,則預設是加密連接配接的。

也可以使用--skip-ssl選項取消加密連接配接。

至此基于SSL加密的MySQL主從複制架構就配置成功了!

對于基于SSL機密的主主複制隻需各自建立基于ssl加密的使用者即可。

本文轉自 向陽草米奇 51CTO部落格,原文連結:http://blog.51cto.com/grass51/837589,如需轉載請自行聯系原作者