新聞中心
安全性

設(shè)置客戶端連接后進(jìn)行任何其他指定前需要使用的密碼。
警告:因為redis速度相當(dāng)快,所以在一臺比較好的服務(wù)器下,一個外部的用戶可以在一秒鐘進(jìn)行150K次的密碼嘗試,這意味著你需要指定非常非常強(qiáng)大的密碼來防止暴力破解。
- # requirepass foobared
- requirepass beijing
下面我們做一個實驗,說明redis的安全性是如何實現(xiàn)的。
我們設(shè)置了連接的口令是beijing
那么們啟動一個客戶端試一下:
- [root@localhost redis-2.2.12]# src/redis-cli
- redis 127.0.0.1:6379> keys *
- (error) ERR operation not permitted
- redis 127.0.0.1:6379>
說明權(quán)限太小,我們可以當(dāng)前的這個窗口中設(shè)置口令
- redis 127.0.0.1:6379> auth beijing
- OK
- redis 127.0.0.1:6379> keys *
- 1) "name"
- redis 127.0.0.1:6379>
我們還可以在連接到服務(wù)器期間就指定一個口令,如下:
- [root@localhost redis-2.2.12]# src/redis-cli -a beijing
- redis 127.0.0.1:6379> keys *
- 1) "name"
- redis 127.0.0.1:6379>
可以看到我們在連接的時候就可以指定一個口令。
主從復(fù)制
Redis主從復(fù)制配置和使用都非常簡單。通過主從復(fù)制可以允許多個slave server擁有和master server相同的數(shù)據(jù)庫副本。
1、redis主從復(fù)制特點(diǎn):
(1)、master可以擁有多個slave
(2)、多個slave可以連接同一個master外,還可以連接到其他slave
(3)、主從復(fù)制不會阻塞master,在同步數(shù)據(jù)時,master可以繼續(xù)處理client請求
(4)、提高系統(tǒng)的伸縮性
2、redis主從復(fù)制過程:
當(dāng)配置好slave后,slave與master建立連接,然后發(fā)送sync命令。無論是第一次連接還是重新連接,master都會啟動一個后臺進(jìn)程,將數(shù)據(jù)庫快照保存到文件中,同時master主進(jìn)程會開始收集新的寫命令并緩存。后臺進(jìn)程完成寫文件后,master就發(fā)送文件給slave,slave將文件保存到硬盤上,再加載到內(nèi)存中,接著master就會把緩存的命令轉(zhuǎn)發(fā)給slave,后續(xù)master將收到的寫命令發(fā)送給slave。如果master同時收到多個slave發(fā)來的同步連接命令,master只會啟動一個進(jìn)程來寫數(shù)據(jù)庫鏡像,然后發(fā)送給所有的slave。
3、如何配置
配置slave服務(wù)器很簡單,只需要在slave的配置文件中加入如下配置
- slaveof 192.168.1.1 6379 #指定master的ip和端口
下面我們做一個實驗來演示如何搭建一個主從環(huán)境:
- # slaveof
- slaveof localhost 6379
我們在一臺機(jī)器上啟動主庫(端口6379),從庫(端口6378)
啟動后主庫控制臺日志如下:
- [root@localhost redis-2.2.12]# src/redis-server redis.conf
- [7064] 09 Aug 20:13:12 * Server started, Redis version 2.2.12
- [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.
- [7064] 09 Aug 20:13:12 * The server is now ready to accept connections on port 6379
- [7064] 09 Aug 20:13:13 - 0 clients connected (0 slaves), 539512 bytes in use
- [7064] 09 Aug 20:13:18 - 0 clients connected (0 slaves), 539512 bytes in use
- [7064] 09 Aug 20:13:20 - Accepted 127.0.0.1:37789
- [7064] 09 Aug 20:13:20 * Slave ask for synchronization
- [7064] 09 Aug 20:13:20 * Starting BGSAVE for SYNC
- [7064] 09 Aug 20:13:20 * Background saving started by pid 7067
- [7067] 09 Aug 20:13:20 * DB saved on disk
- [7064] 09 Aug 20:13:20 * Background saving terminated with success
- [7064] 09 Aug 20:13:20 * Synchronization with slave succeeded
- [7064] 09 Aug 20:13:23 - 0 clients connected (1 slaves), 547380 bytes in use
啟動后從庫控制臺日志如下:
- [root@localhost redis-2.2.12]# src/redis-server redis.slave
- [7066] 09 Aug 20:13:20 * Server started, Redis version 2.2.12
- [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.
- [7066] 09 Aug 20:13:20 * The server is now ready to accept connections on port 6378
- [7066] 09 Aug 20:13:20 - 0 clients connected (0 slaves), 539548 bytes in use
- [7066] 09 Aug 20:13:20 * Connecting to MASTER...
- [7066] 09 Aug 20:13:20 * MASTER <-> SLAVE sync started: SYNC sent
- [7066] 09 Aug 20:13:20 * MASTER <-> SLAVE sync: receiving 10 bytes from master
- [7066] 09 Aug 20:13:20 * MASTER <-> SLAVE sync: Loading DB in memory
- [7066] 09 Aug 20:13:20 * MASTER <-> SLAVE sync: Finished with success
- [7068] 09 Aug 20:13:20 * SYNC append only file rewrite performed
- [7066] 09 Aug 20:13:20 * Background append only file rewriting started by pid 7068
- [7066] 09 Aug 20:13:21 * Background append only file rewriting terminated with success
- [7066] 09 Aug 20:13:21 * Parent diff flushed into the new append log file with success (0 bytes)
- [7066] 09 Aug 20:13:21 * Append only file successfully rewritten.
- [7066] 09 Aug 20:13:21 * The new append only file was selected for future appends.
- [7066] 09 Aug 20:13:25 - 1 clients connected (0 slaves), 547396 bytes in use
我們在主庫上設(shè)置一對鍵值對
- redis 127.0.0.1:6379> set name HongWan
- OK
- redis 127.0.0.1:6379>
在從庫上取一下這個鍵
- redis 127.0.0.1:6378> get name
- "HongWan"
- redis 127.0.0.1:6378>
說明主從是同步正常的.
那么我們?nèi)绾闻袛嗄膫€是主哪個是從呢?我們只需調(diào)用info這個命令就可以得到主從的信息了,我們在從庫上執(zhí)行info命令
- redis 127.0.0.1:6378> info
- .
- .
- .
- role:slave
- master_host:localhost
- master_port:6379
- master_link_status:up
- master_last_io_seconds_ago:10
- master_sync_in_progress:0
- db0:keys=1,expires=0
- redis 127.0.0.1:6378>
里面有一個角色標(biāo)識,來判斷是主庫還是從庫,對于本例是一個從庫,同時還有一個master_link_status用于標(biāo)明主從是否異步,如果此值=up,說明同步正常;如果此值=down,說明同步異步;
db0:keys=1,expires=0, 用于說明數(shù)據(jù)庫有幾個key,以及過期key的數(shù)量。
【編輯推薦】
- Redis2.6將釋出 新功能一覽
- 使用Redis的五個注意事項
- Redis高可用性之Failover過渡方案
- Redis能干啥?細(xì)看11種Web應(yīng)用場景
- 主流NoSQL數(shù)據(jù)庫之Redis全面評測
名稱欄目:Redis高級實用特性:安全性與主從復(fù)制
網(wǎng)頁鏈接:http://fisionsoft.com.cn/article/dppdgoe.html


咨詢
建站咨詢
