偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

Redis高級實用特性:安全性與主從復制

數(shù)據(jù)庫 其他數(shù)據(jù)庫 數(shù)據(jù)庫運維 Redis
Redis是一個開源的使用ANSI C語言編寫、支持網(wǎng)絡、可基于內(nèi)存亦可持久化的日志型、Key-Value數(shù)據(jù)庫,并提供多種語言的API。從2010年3月15日起,Redis的開發(fā)工作由VMware主持。本文將介紹Redis的高級實用特性。

 安全性

設置客戶端連接后進行任何其他指定前需要使用的密碼。

警告:因為redis速度相當快,所以在一臺比較好的服務器下,一個外部的用戶可以在一秒鐘進行150K次的密碼嘗試,這意味著你需要指定非常非常強大的密碼來防止暴力破解。

  1.   # requirepass foobared
  2.   requirepass beijing

下面我們做一個實驗,說明redis的安全性是如何實現(xiàn)的。

我們設置了連接的口令是beijing

那么們啟動一個客戶端試一下:

  1.   [root@localhost redis-2.2.12]# src/redis-cli  
  2.   redis 127.0.0.1:6379> keys *  
  3.   (error) ERR operation not permitted  
  4.   redis 127.0.0.1:6379> 

  說明權(quán)限太小,我們可以當前的這個窗口中設置口令

  1.   redis 127.0.0.1:6379> auth beijing  
  2.   OK  
  3.   redis 127.0.0.1:6379> keys *  
  4.   1) "name"  
  5.   redis 127.0.0.1:6379> 

  我們還可以在連接到服務器期間就指定一個口令,如下:

  1.   [root@localhost redis-2.2.12]# src/redis-cli -a beijing  
  2.   redis 127.0.0.1:6379> keys *  
  3.   1) "name"  
  4.  redis 127.0.0.1:6379> 

  可以看到我們在連接的時候就可以指定一個口令。

主從復制

Redis主從復制配置和使用都非常簡單。通過主從復制可以允許多個slave server擁有和master server相同的數(shù)據(jù)庫副本。

1、redis主從復制特點:

(1)、master可以擁有多個slave

(2)、多個slave可以連接同一個master外,還可以連接到其他slave

(3)、主從復制不會阻塞master,在同步數(shù)據(jù)時,master可以繼續(xù)處理client請求

(4)、提高系統(tǒng)的伸縮性

2、redis主從復制過程:

當配置好slave后,slave與master建立連接,然后發(fā)送sync命令。無論是第一次連接還是重新連接,master都會啟動一個后臺進程,將數(shù)據(jù)庫快照保存到文件中,同時master主進程會開始收集新的寫命令并緩存。后臺進程完成寫文件后,master就發(fā)送文件給slave,slave將文件保存到硬盤上,再加載到內(nèi)存中,接著master就會把緩存的命令轉(zhuǎn)發(fā)給slave,后續(xù)master將收到的寫命令發(fā)送給slave。如果master同時收到多個slave發(fā)來的同步連接命令,master只會啟動一個進程來寫數(shù)據(jù)庫鏡像,然后發(fā)送給所有的slave。

3、如何配置

配置slave服務器很簡單,只需要在slave的配置文件中加入如下配置

  1. slaveof 192.168.1.1 6379 #指定master的ip和端口 

  下面我們做一個實驗來演示如何搭建一個主從環(huán)境:

  1. # slaveof <masterip> <masterport> 
  2. slaveof localhost 6379 

  我們在一臺機器上啟動主庫(端口6379),從庫(端口6378)

啟動后主庫控制臺日志如下:

  1. [root@localhost redis-2.2.12]# src/redis-server redis.conf   
  2. [7064] 09 Aug 20:13:12 * Server started, Redis version 2.2.12  
  3. [7064] 09 Aug 20:13:12 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.  
  4. [7064] 09 Aug 20:13:12 * The server is now ready to accept connections on port 6379  
  5. [7064] 09 Aug 20:13:13 - 0 clients connected (0 slaves), 539512 bytes in use  
  6. [7064] 09 Aug 20:13:18 - 0 clients connected (0 slaves), 539512 bytes in use  
  7. [7064] 09 Aug 20:13:20 - Accepted 127.0.0.1:37789  
  8. [7064] 09 Aug 20:13:20 * Slave ask for synchronization  
  9. [7064] 09 Aug 20:13:20 * Starting BGSAVE for SYNC  
  10. [7064] 09 Aug 20:13:20 * Background saving started by pid 7067  
  11. [7067] 09 Aug 20:13:20 * DB saved on disk  
  12. [7064] 09 Aug 20:13:20 * Background saving terminated with success  
  13. [7064] 09 Aug 20:13:20 * Synchronization with slave succeeded  
  14. [7064] 09 Aug 20:13:23 - 0 clients connected (1 slaves), 547380 bytes in use 

  啟動后從庫控制臺日志如下:

  1. [root@localhost redis-2.2.12]# src/redis-server redis.slave   
  2. [7066] 09 Aug 20:13:20 * Server started, Redis version 2.2.12  
  3. [7066] 09 Aug 20:13:20 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.  
  4. [7066] 09 Aug 20:13:20 * The server is now ready to accept connections on port 6378  
  5. [7066] 09 Aug 20:13:20 - 0 clients connected (0 slaves), 539548 bytes in use  
  6. [7066] 09 Aug 20:13:20 * Connecting to MASTER...  
  7. [7066] 09 Aug 20:13:20 * MASTER <-> SLAVE sync started: SYNC sent  
  8. [7066] 09 Aug 20:13:20 * MASTER <-> SLAVE sync: receiving 10 bytes from master  
  9. [7066] 09 Aug 20:13:20 * MASTER <-> SLAVE sync: Loading DB in memory  
  10. [7066] 09 Aug 20:13:20 * MASTER <-> SLAVE sync: Finished with success  
  11. [7068] 09 Aug 20:13:20 * SYNC append only file rewrite performed  
  12. [7066] 09 Aug 20:13:20 * Background append only file rewriting started by pid 7068  
  13. [7066] 09 Aug 20:13:21 * Background append only file rewriting terminated with success  
  14. [7066] 09 Aug 20:13:21 * Parent diff flushed into the new append log file with success (0 bytes)  
  15. [7066] 09 Aug 20:13:21 * Append only file successfully rewritten.  
  16. [7066] 09 Aug 20:13:21 * The new append only file was selected for future appends.  
  17. [7066] 09 Aug 20:13:25 - 1 clients connected (0 slaves), 547396 bytes in use 

  我們在主庫上設置一對鍵值對

  1. redis 127.0.0.1:6379> set name HongWan  
  2. OK  
  3. redis 127.0.0.1:6379> 

  在從庫上取一下這個鍵

  1. redis 127.0.0.1:6378> get name  
  2. "HongWan"  
  3. redis 127.0.0.1:6378> 

  說明主從是同步正常的.

  那么我們?nèi)绾闻袛嗄膫€是主哪個是從呢?我們只需調(diào)用info這個命令就可以得到主從的信息了,我們在從庫上執(zhí)行info命令

  1. redis 127.0.0.1:6378> info  
  2. .  
  3. .  
  4. .  
  5. role:slave  
  6. master_host:localhost  
  7. master_port:6379  
  8. master_link_status:up  
  9. master_last_io_seconds_ago:10  
  10. master_sync_in_progress:0  
  11. db0:keys=1,expires=0 
  12. redis 127.0.0.1:6378> 

  里面有一個角色標識,來判斷是主庫還是從庫,對于本例是一個從庫,同時還有一個master_link_status用于標明主從是否異步,如果此值=up,說明同步正常;如果此值=down,說明同步異步;

  db0:keys=1,expires=0, 用于說明數(shù)據(jù)庫有幾個key,以及過期key的數(shù)量。

【編輯推薦】

  1. Redis2.6將釋出 新功能一覽
  2. 使用Redis的五個注意事項
  3. Redis高可用性之Failover過渡方案
  4. Redis能干啥?細看11種Web應用場景
  5. 主流NoSQL數(shù)據(jù)庫之Redis全面評測
責任編輯:彭凡 來源: ITPUB
相關推薦

2023-09-24 14:32:15

2023-03-15 08:30:37

2011-05-31 18:41:45

復印機技巧

2023-12-25 08:02:09

2023-03-19 11:53:27

2023-03-19 22:38:12

邏輯復制PostgreSQL

2017-06-23 22:00:13

MySqlsslcentos

2023-07-03 08:57:45

Master服務TCP

2023-04-06 13:15:48

MySQL復制原理應用實踐

2025-02-10 10:55:16

2024-07-04 08:00:24

2024-03-01 18:33:59

MySQL節(jié)點數(shù)據(jù)

2021-06-08 07:48:27

MySQL主從配置

2025-03-19 10:00:56

2018-04-08 15:20:15

數(shù)據(jù)庫MySQL主從復制

2018-07-06 09:58:38

Redis高可用主從復制

2009-11-30 09:41:38

2021-01-12 08:03:19

Redis數(shù)據(jù)系統(tǒng)

2009-03-13 10:18:00

2019-07-31 07:53:23

點贊
收藏

51CTO技術(shù)棧公眾號