欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

MySQL基于SSL安全連接的主從復(fù)制(過程詳解)

 更新時(shí)間:2023年04月06日 09:30:36   作者:大蝦好吃嗎  
SSL(Secure Sockets Layer 安全套接層),及其繼任者傳輸層安全(Transport Layer Security,TLS)是為網(wǎng)絡(luò)通信提供安全及數(shù)據(jù)完整性的一種安全協(xié)議,這篇文章主要介紹了MySQL基于SSL安全連接的主從復(fù)制,需要的朋友可以參考下

生產(chǎn)環(huán)境中一臺(tái)mysql主機(jī)存在單點(diǎn)故障,所以我們要確保mysql的高可用性,即兩臺(tái)MySQL服務(wù)器如果其中有一臺(tái)MySQL服務(wù)器掛掉后,另外一臺(tái)能立馬接替其進(jìn)行工作。

主從復(fù)制的原理

        master記錄二進(jìn)制日志,在每個(gè)事務(wù)更新數(shù)據(jù)完成之前,master在二日志記錄這些改變。MySQL將事務(wù)寫入二進(jìn)制日志,在事件寫入二進(jìn)制日志完成后,master通知存儲(chǔ)引擎提交事務(wù)。 下一步就是slave將master的binary log拷貝到它自己的中繼日志。首先,slave開始一個(gè)工作線程——I/O線程,I/O線程在master上打開一個(gè)普通的連接,然后開啟binlog dump process。Binlog dump process從master的二進(jìn)制日志中讀取事件,如果已經(jīng)同步了master,它會(huì)睡眠并等待master產(chǎn)生新的事件,I/O線程將這些事件寫入中繼日志。 SQL slave thread(SQL從線程)處理該過程的最后一步。SQL線程從中繼日志讀取事件,并重放其中的事件而更新slave的數(shù)據(jù),使其與master中的數(shù)據(jù)一致。只要該線程與I/O線程保持一致,中繼日志通常會(huì)位于OS的緩存中,所以中繼日志的開銷很小。

環(huán)境準(zhǔn)備:打開兩臺(tái)MySQL服務(wù)器,部署網(wǎng)絡(luò)環(huán)境。

部署master

1. 主機(jī)創(chuàng)建 SSL/RSA 文件

[root@master ~]# cd /usr/local/mysql/bin/
[root@master bin]# mysql_ssl_rsa_setup --user=mysql --basedir=/usr/llocal/mysql --datadir=/usr/local/mysql/data

2. 賦予權(quán)限并重啟。

[root@master bin]# chmod +r /usr/local/mysql/data/server-key.pem 
[root@master bin]# service mysqld restart
Shutting down MySQL..                                      [  確定  ]
Starting MySQL.                                            [  確定  ]

3. 登錄mysql查看ssl是否開啟,并創(chuàng)建一個(gè)復(fù)制用戶。

注:啟用 mysql 支持 ssl 安全連接主要用于 mysql 主從復(fù)制(局域網(wǎng)可以非 ssh 連接即明文復(fù)制,但 internet 復(fù)制建議采用 ssl 連接)

mysql> grant replication slave on *.* to rep@'192.168.8.3' identified by '123';
Query OK, 0 rows affected, 1 warning (0.07 sec)

4. master開啟二進(jìn)制日志,重啟后查看二進(jìn)制日志文件。

需要注意的是server_id必須唯一。

[root@master ~]# vim /etc/my.cnf
#添加下面內(nèi)容
log-bin=mysql-bin
service_id=1
[root@master ~]# service mysqld restart
Shutting down MySQL..                                      [  確定  ]
Starting MySQL.                                            [  確定  ]
[root@master ~]# mysql -uroot -p123 -e "show master status"
mysql: [Warning] Using a password on the command line interface can be insecure.
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 |      154 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+

5. 防火墻配置,實(shí)驗(yàn)環(huán)境中可以關(guān)閉防火墻,生產(chǎn)環(huán)境中需要配置防火墻規(guī)則,允許3306端口。

[root@master ~]# firewall-cmd --permanent --add-port=3306/tcp
success
[root@master ~]# firewall-cmd --reload
success

6. 把ssl文件復(fù)制到slave

[root@master data]# scp ca.pem client-cert.pem client-key.pem root@192.168.8.3:/usr/local/mysql/data
The authenticity of host '192.168.8.3 (192.168.8.3)' can't be established.
ECDSA key fingerprint is SHA256:LFby9KMDz/kkPfOESbeJ7Qh+3hmQaX2W5gkDDMwSGHA.
ECDSA key fingerprint is MD5:03:32:64:b4:c2:5b:6c:a4:e2:f0:7f:df:7a:35:19:80.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.8.3' (ECDSA) to the list of known hosts.
root@192.168.8.3's password: 
ca.pem                             100% 1112   232.5KB/s   00:00    
client-cert.pem                    100% 1112   240.4KB/s   00:00    
client-key.pem                     100% 1676   205.0KB/s   00:00

部署slave

1. 開啟ssl、中繼日志,賦予ssl文件讀的權(quán)限并重啟mysql。

[root@slave ~]# vim /etc/my.cnf
#添加下面內(nèi)容
server_id=2
relay-log=relay-log
ssl_ca=ca.pem
ssl_cert=client-cert.pem
ssl_key=client-key.pem
?
[root@slave ~]# cd /usr/local/mysql/data
[root@slave data]# ll ca.pem client-cert.pem client-key.pem 
-rw-r--r--. 1 mysql mysql 1112 3月  31 14:31 ca.pem
-rw-r--r--. 1 mysql mysql 1112 3月  31 14:31 client-cert.pem
-rw-------. 1 mysql mysql 1676 3月  31 14:31 client-key.pem
[root@slave data]# chmod +r client-key.pem
[root@slave ~]# service mysqld restart
Shutting down MySQL..                                      [  確定  ]
Starting MySQL.                                            [  確定  ]

2. 確認(rèn)ssl開啟成功

[root@slave ~]# mysql -uroot -p123 -e "show variables like '%ssl%'"
mysql: [Warning] Using a password on the command line interface can be insecure.
+-------------------------------------+-----------------+
| Variable_name                       | Value           |
+-------------------------------------+-----------------+
| have_openssl                        | YES             |
| have_ssl                            | YES             |
| performance_schema_show_processlist | OFF             |
| ssl_ca                              | ca.pem          |
| ssl_capath                          |                 |
| ssl_cert                            | client-cert.pem |
| ssl_cipher                          |                 |
| ssl_crl                             |                 |
| ssl_crlpath                         |                 |
| ssl_key                             | client-key.pem  |
+-------------------------------------+-----------------+

3. 在配置主從復(fù)制之前可以在從 mysql 上用 SSL 連接主服務(wù)器試試。

        注意分清IP,8.2是master的IP,可以看到ssl協(xié)議Cipher in use is ECDHE-RSA-AES128-GCM-SHA256

[root@slave ~]# cd /usr/local/mysql/data
[root@slave data]# mysql --ssl-ca=ca.pem --ssl-cert=client-cert.pem --ssl-key=client-key.pem -u rep -p123 -h 192.168.8.2
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.40-log MySQL Community Server (GPL)
 
Copyright (c) 2000, 2022, Oracle and/or its affiliates.
 
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
 
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
 
mysql> 
mysql> \s
--------------
mysql  Ver 14.14 Distrib 5.7.40, for linux-glibc2.12 (x86_64) using  EditLine wrapper
 
Connection id:		3
Current database:	
Current user:		rep@192.168.8.3
SSL:			Cipher in use is ECDHE-RSA-AES128-GCM-SHA256
Current pager:		stdout
Using outfile:		''
Using delimiter:	;
Server version:		5.7.40-log MySQL Community Server (GPL)
Protocol version:	10
Connection:		192.168.8.2 via TCP/IP
Server characterset:	latin1
Db     characterset:	latin1
Client characterset:	utf8
Conn.  characterset:	utf8
TCP port:		3306
Uptime:			22 min 19 sec
 
Threads: 1  Questions: 8  Slow queries: 0  Opens: 109  Flush tables: 1  Open tables: 102  Queries per second avg: 0.005
--------------

1.登錄slave服務(wù)器,配置主從 replicate

記得先退出連接,登錄slave的mysql服務(wù)。

mysql> exit
Bye
[root@slave data]# mysql -uroot -p123
#省略部分登錄信息
mysql> change master to
    -> master_host='192.168.8.2',						#masterIP
    -> master_user='rep',								#master用戶
    -> master_password='123',							#master密碼
    -> master_log_file='mysql-bin.000001',				#master二進(jìn)制日志文件
    -> master_log_pos=154,								#master位置
    -> master_ssl=1,									#masterssl
    -> master_ssl_cert='client-cert.pem',
    -> master_ssl_key='client-key.pem',
    -> master_ssl_ca='ca.pem';
Query OK, 0 rows affected, 2 warnings (0.07 sec)
 
mysql> start slave;				#啟用從
Query OK, 0 rows affected (0.02 sec)

確認(rèn)啟用成功。

測試SSL主從復(fù)制

1. 登錄master,寫入一些數(shù)據(jù)

[root@master ~]# mysql -uroot -p123
#省略部分內(nèi)容
mysql> create database bbs;
Query OK, 1 row affected (0.01 sec)
 
mysql> use bbs;
Database changed
mysql> create table tb1(id int,
    -> name varchar(20));
Query OK, 0 rows affected (0.02 sec)
 
mysql> insert into tb1 values(1,'z3');
Query OK, 1 row affected (0.02 sec)

2. 登錄slave,查看數(shù)據(jù)

[root@slave ~]# mysql -uroot -p123
#省略部分內(nèi)容
mysql> select * from bbs.tb1;
+------+------+
| id   | name |
+------+------+
|    1 | z3   |
+------+------+
1 row in set (0.01 sec)

最后可以查看到z3,主從成功。

結(jié)語:

        SSL(Secure Sockets Layer 安全套接層),及其繼任者傳輸層安全(Transport Layer Security,TLS)是為網(wǎng)絡(luò)通信提供安全及數(shù)據(jù)完整性的一種安全協(xié)議。復(fù)制默認(rèn)是明文進(jìn)行傳輸?shù)?,通過SSL加密可以大大提高數(shù)據(jù)的安全性。

到此這篇關(guān)于MySQL基于SSL安全連接的主從復(fù)制的文章就介紹到這了,更多相關(guān)mysql 基于ssl主從復(fù)制內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論