Redis(二)CentOS7安裝Redis4.0.10與集羣搭建

一 Redis單機安裝

1 Redis下載安裝

1.1 檢查依賴環境(Redis是C語言開發,編譯依賴gcc環境)

[root@node21 redis-4.0.10]$ gcc -v
-bash: gcc: command not found
[root@node21 redis-4.0.10]$ yum install -y gcc

1.2 解壓文件到指定目錄

[root@node21 software]$ wget http://download.redis.io/releases/redis-4.0.10.tar.gz
[root@node21 software]$ ll
-rw-rw-r-- 1 root root   1738465 June 14 00:51 redis-4.0.10.tar.gz
[root@node21 software]$ tar xzf redis-4.0.10.tar.gz -C /opt/module/

1.3 編譯文件

[root@node21 software]$ cd /opt/module/redis-4.0.10/
[root@node21 redis-4.0.10]$ make

1)若是沒有安裝gcc,編譯會出現以下錯誤提示。(若是編譯失敗,必須刪除文件夾,從新解壓)html

/bin/sh: cc: command not found
make[1]: *** [adlist.o] Error 127
make[1]: Leaving directory `/opt/module/redis-4.0.10/src'
make: *** [all] Error 2

2)安裝成功node

1.4 安裝

[root@node21 redis-4.0.10]$ make PREFIX=/opt/module/redis-4.0.10  install

安裝完成後,在redis安裝目錄下會出現一個bin目錄,bin目錄下有幾個可執行文件redis

redis-benchmark         ---性能測試工具
redis-check-aof         ---AOF文件修復工具
redis-check-rdb         ---RDB文件檢測工具(快照持久化文件)
redis-cli               ---命令行客戶端
redis-sentinel -> redis-server   ---redis哨兵
redis-server            ---redis服務器啓動命令

1.5 配置環境變量

[root@node21 redis-4.0.10]$  vi /etc/profile
末未追加以下
export  REDIS_HOME=/opt/module/redis-4.0.10
export  PATH=$REDIS_HOME/bin:$PATH
[root@node21 redis-4.0.10]# source /etc/profile

2 Redis服務啓動

2.1 前臺模式啓動(默認)

[root@node21 redis-4.0.10]$ bin/redis-server

前臺服務端啓動成功後,不能進行其餘操做,使用Ctrl+c結束或者服務器關閉redis服務自動結束,此種方式不推薦,成功啓動信息以下算法

[root@node21 redis-4.0.10]$ bin/redis-server 
5356:C 19 June 22:47:00.955 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
5356:C 19 June 22:47:00.956 # Redis version=4.0.10, bits=64, commit=00000000, modified=0, pid=5356, just started
5356:C 19 June 22:47:00.956 # Warning: no config file specified, using the default config. In order to specify a config file use bin/redis-server /path/to/redis.conf
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 4.0.10 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 5356
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

5356:M 19 June 22:47:00.960 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
5356:M 19 June 22:47:00.960 # Server initialized
5356:M 19 June 22:47:00.960 # 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.5356:M 19 June 22:47:00.960 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis.
 To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.5356:M 19 June 22:47:00.960 * Ready to accept connections
View Code

2.2 後臺模式啓動

修改redis.conf  將daemonize no  改成 daemonize yes,說明:將Redis設置爲後臺進程;綁定ip由本地ip換成真機ip,方便對外訪問數據庫

[root@node21 redis-4.0.10]# vi redis.conf

修改完以後指定配置文件爲後臺啓動緩存

[root@node21 redis-4.0.10]# ./bin/redis-server ./redis.conf 
5388:C 19 June 23:09:44.115 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
5388:C 19 June 23:09:44.115 # Redis version=4.0.10, bits=64, commit=00000000, modified=0, pid=5388, just started
5388:C 19 June 23:09:44.115 # Configuration loaded

Redis默認端口6379,經過當前服務進行查看安全

[root@node21 redis-4.0.10]# ps -ef |grep redis 
root
5356 1 0 22:47 ? 00:00:02 bin/redis-server *:6379
root 5391 5364 0 23:12 pts/1 00:00:00 grep --color=auto -i redis

3 Redis客戶端鏈接

鏈接客戶端 :redis-cli -h ip地址 -p 端口

[root@node21 redis-4.0.10]# ./bin/redis-cli -h 192.168.100.21 -p 6379
192.168.100.21:6379> 

 redis-cli連上redis服務後,能夠在命令行發送命令 ping,測試客戶端和Redis的鏈接是否正常,若是鏈接正常會收到回覆pong。ruby

192.168.100.21:6379> ping
PONG

4 Redis中止

強制終止Redis進程可能會致使redis持久化數據丟失bash

[root@node21 redis-4.0.10]# kill -9 5336   #pid須要經過‘ps -aux |grep redis’ 進行查詢

正確中止Redis鏈接方式應向redis發送shutdown指令(關閉默認端口)服務器

[root@node21 redis-4.0.10]# redis-cli -h 192.168.100.21 -p 6379 shutdown

quit 退出客戶端鏈接

5 安裝服務腳本

[root@node21 redis-4.0.10]# cd utils/
[root@node21 utils]# ./install_server.sh 

查看服務運行狀態

[root@node21 utils]# service redis_6379 status

中止Redis服務

[root@node21 admin]# service redis_6379 stop

二 Redis集羣安裝 

右側是官網文檔(Redis集羣規範) http://redis.io/topics/cluster-spec

右側是上文連接的翻譯文檔  http://redisdoc.com/topic/cluster-spec.html#cluster-spec

1 原理簡述 

此處的集羣指的是Cluster, 經過分區/分片來實現必定程度的分佈式與高可用部署。

2 集羣配置 

Redis 最小集羣規劃,須要包含至少三個主節點,此處測試部署具備三個主服務器和三個從服務器的六節點羣集。計劃是在一臺機器上模擬一個集羣,節點端口7000,7001,7002, 從節點7003,7004,7005,這和生產環境的集羣搭建沒本質區別。

2.1 建立節點目錄 

建立一個新目錄以及redis實例已端口號命名的如下目錄

[root@node21 module]# mkdir redis-Cluster
[root@node21 module]# cd redis-Cluster
[root@node21 redis-Cluster]# mkdir 7000 7001 7002 7003 7004 7005

2.2 建立配置文件

在每一個節點目錄中建立一個文件redis.conf,從7000到7005.做爲配置文件的模板,如下是最小的Redis羣集配置文件,每一個節點實例配置稍做修改

[root@node21 redis-Cluster]# cd 7000
[root@node21 7000]# vi redis.conf
port 7000                       #端口7000,7001,7002,7003,7004,7005
cluster-enabled yes             #開啓集羣
cluster-config-file nodes.conf  #集羣配置信息,開啓集羣后自動生成
cluster-node-timeout 5000       #請求超時時長 
appendonly yes                  #開啓aof日誌,它會每次寫操做都記錄一條日誌
daemonize yes                   #redis後臺運行
bind 192.168.100.21             #默認ip爲127.0.0.1 須要改成其餘節點機器可訪問的ip 不然建立集羣時沒法訪問對應的端口,沒法建立集羣  
pidfile /opt/module/redis-Cluster/7000/redis_7000.pid #pidfile文件對應7000,7001,7002,7003,7004,7005
dir /opt/module/redis-Cluster/7000/ #數據文件存放位置對應7000,7001,7002,7003,7004,7005

2.3 啓動節點服務 

分別啓動6個實例,可執行文件redis-server用以前單機版的,已配過環境變量

[root@node21 redis-Cluster]# redis-server 7000/redis.conf
[root@node21 redis-Cluster]# redis-server 7001/redis.conf
[root@node21 redis-Cluster]# redis-server 7002/redis.conf
[root@node21 redis-Cluster]# redis-server 7003/redis.conf
[root@node21 redis-Cluster]# redis-server 7004/redis.conf
[root@node21 redis-Cluster]# redis-server 7005/redis.conf

6臺實例運行起來後,查看進程,下一步建立集羣

[root@node21 redis-Cluster]# ps -ef |grep redis 
root       9240      1  0 18:11 ?        00:00:00 redis-server 192.168.100.21:7000 [cluster]
root       9245      1  0 18:11 ?        00:00:01 redis-server 192.168.100.21:7001 [cluster]
root       9265      1  0 18:14 ?        00:00:00 redis-server 192.168.100.21:7002 [cluster]
root       9270      1  0 18:14 ?        00:00:00 redis-server 192.168.100.21:7003 [cluster]
root       9275      1  0 18:14 ?        00:00:00 redis-server 192.168.100.21:7004 [cluster]
root       9280      1  0 18:14 ?        00:00:00 redis-server 192.168.100.21:7005 [cluster]

2.4 建立集羣

redis安裝目錄src中redis-trib.rb完成集羣建立,redis-trib.rb命令須要安裝gem redis模塊才能運行,gem redis須要Ruby環境

[root@node21 redis-Cluster]# yum install ruby rubygems -y
[root@node21 redis-Cluster]# gem install redis

 建立新集羣命令:命令create選項--replicas 1意味着咱們但願每一個建立的主服務器都有一個從服 其餘參數是我要用於建立新集羣的實例的地址列表。

[root@node21 src]# pwd
/opt/module/redis-4.0.10/src
[root@node21 src]# ./redis-trib.rb create --replicas 1 192.168.100.21:7000 192.168.100.21:7001 192.168.100.21:7002 192.168.100.21:7003 192.168.100.21:7004 192.168.100.21:7005

建立命令運行界面以下,主要是分配主從和槽位數

>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
192.168.100.21:7000
192.168.100.21:7001
192.168.100.21:7002
Adding replica 192.168.100.21:7004 to 192.168.100.21:7000
Adding replica 192.168.100.21:7005 to 192.168.100.21:7001
Adding replica 192.168.100.21:7003 to 192.168.100.21:7002
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: 2b2a9cab4591508e44290a1fd16781e5c9099010 192.168.100.21:7000
   slots:0-5460 (5461 slots) master
M: 69d97b0c7c3c1587f0e4ed0abb82c01fa66a5478 192.168.100.21:7001
   slots:5461-10922 (5462 slots) master
M: 4728f1d7da089b6ec9c20f8c9907652536ee5804 192.168.100.21:7002
   slots:10923-16383 (5461 slots) master
S: 73f894c5ddca484f5b72fd7f1f08612e50681133 192.168.100.21:7003
   replicates 4728f1d7da089b6ec9c20f8c9907652536ee5804
S: 7f09c99b30f16ed583f9e144c75e2bb065c0528d 192.168.100.21:7004
   replicates 2b2a9cab4591508e44290a1fd16781e5c9099010
S: 23d31961a1ad249b4af1d9f7be6cf3abc1b82f0a 192.168.100.21:7005
   replicates 69d97b0c7c3c1587f0e4ed0abb82c01fa66a5478
Can I set the above configuration? (type 'yes' to accept): yes >>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join..
>>> Performing Cluster Check (using node 192.168.100.21:7000)
M: 2b2a9cab4591508e44290a1fd16781e5c9099010 192.168.100.21:7000
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
S: 73f894c5ddca484f5b72fd7f1f08612e50681133 192.168.100.21:7003
   slots: (0 slots) slave
   replicates 4728f1d7da089b6ec9c20f8c9907652536ee5804
S: 7f09c99b30f16ed583f9e144c75e2bb065c0528d 192.168.100.21:7004
   slots: (0 slots) slave
   replicates 2b2a9cab4591508e44290a1fd16781e5c9099010
S: 23d31961a1ad249b4af1d9f7be6cf3abc1b82f0a 192.168.100.21:7005
   slots: (0 slots) slave
   replicates 69d97b0c7c3c1587f0e4ed0abb82c01fa66a5478
M: 69d97b0c7c3c1587f0e4ed0abb82c01fa66a5478 192.168.100.21:7001
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
M: 4728f1d7da089b6ec9c20f8c9907652536ee5804 192.168.100.21:7002
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
[OK] All nodes agree about slots configuration. >>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

3 集羣測試

 集羣客戶端鏈接方式 redis-cli  -c 

[root@node21 redis-Cluster]# redis-cli -c -h 192.168.100.21 -p 7000
192.168.100.21:7000> set foo bar
-> Redirected to slot [12182] located at 192.168.100.21:7002
OK
192.168.100.21:7002> get foo
"bar"
192.168.100.21:7002> keys *
1) "foo"
192.168.100.21:7002> exit
[root@node21 redis-Cluster]# redis-cli -c -h 192.168.100.21 -p 7000
192.168.100.21:7000> keys *
(empty list or set)
192.168.100.21:7000> get foo
-> Redirected to slot [12182] located at 192.168.100.21:7002
"bar"
192.168.100.21:7002> 

不加-c會報錯以下

[root@node21 redis-Cluster]# redis-cli -p 7000
192.168.100.21:7000> set foo bar
(error) MOVED 12182 192.168.100.21:7002

4驗證故障轉移

咱們可使用如下命令識別集羣並使其崩潰:

[root@node21 redis-Cluster]# redis-cli -h 192.168.100.21 -p 7000 cluster nodes | grep master
2b2a9cab4591508e44290a1fd16781e5c9099010 192.168.100.21:7000@17000 myself,master - 0 1532157916000 1 connected 0-5460
69d97b0c7c3c1587f0e4ed0abb82c01fa66a5478 192.168.100.21:7001@17001 master - 0 1532157917389 2 connected 5461-10922
4728f1d7da089b6ec9c20f8c9907652536ee5804 192.168.100.21:7002@17002 master - 0 1532157917000 3 connected 10923-16383

如上,7000,7001和7002都是主服。讓咱們使用DEBUG SEGFAULT命令崩潰節點7002 

[root@node21 redis-Cluster]# redis-cli -h 192.168.100.21 -p 7002 debug segfault
Error: Server closed the connection

7002的從服7003節點部分日誌信息以下

8417:S 21 Jul 13:51:43.245 * Background AOF rewrite finished successfully
8417:S 21 Jul 15:27:45.370 # Connection with master lost. 8417:S 21 Jul 15:27:45.371 * Caching the disconnected master state.
8417:S 21 Jul 15:27:45.595 * Connecting to MASTER 192.168.100.21:7002
8417:S 21 Jul 15:27:45.595 * MASTER <-> SLAVE sync started
8417:S 21 Jul 15:27:45.595 # Error condition on socket for SYNC: Connection refused
8417:S 21 Jul 15:27:46.604 * Connecting to MASTER 192.168.100.21:7002
8417:S 21 Jul 15:27:46.605 * MASTER <-> SLAVE sync started
8417:S 21 Jul 15:27:46.605 # Error condition on socket for SYNC: Connection refused
8417:S 21 Jul 15:27:47.999 * Connecting to MASTER 192.168.100.21:7002
8417:S 21 Jul 15:27:47.999 * MASTER <-> SLAVE sync started
8417:S 21 Jul 15:27:47.999 # Error condition on socket for SYNC: Connection refused
8417:S 21 Jul 15:27:49.008 * Connecting to MASTER 192.168.100.21:7002
8417:S 21 Jul 15:27:49.008 * MASTER <-> SLAVE sync started
8417:S 21 Jul 15:27:49.008 # Error condition on socket for SYNC: Connection refused
8417:S 21 Jul 15:27:50.023 * Connecting to MASTER 192.168.100.21:7002
8417:S 21 Jul 15:27:50.023 * MASTER <-> SLAVE sync started
8417:S 21 Jul 15:27:50.024 # Error condition on socket for SYNC: Connection refused
8417:S 21 Jul 15:27:51.032 * FAIL message received from 69d97b0c7c3c1587f0e4ed0abb82c01fa66a5478 about 4728f1d7da089b6ec9c20f8c9907652536ee5804
8417:S 21 Jul 15:27:51.032 * Connecting to MASTER 192.168.100.21:7002
8417:S 21 Jul 15:27:51.032 * MASTER <-> SLAVE sync started
8417:S 21 Jul 15:27:51.032 # Start of election delayed for 699 milliseconds (rank #0, offset 7992).
8417:S 21 Jul 15:27:51.032 # Cluster state changed: fail
8417:S 21 Jul 15:27:51.033 # Error condition on socket for SYNC: Connection refused
8417:S 21 Jul 15:27:51.738 # Starting a failover election for epoch 7.
8417:S 21 Jul 15:27:51.749 # Failover election won: I'm the new master.
8417:S 21 Jul 15:27:51.750 # configEpoch set to 7 after successful failover
8417:M 21 Jul 15:27:51.750 # Setting secondary replication ID to 8ef669a5f5a6e6efafed75005ca9426d539c4c47, valid up to offset: 7993. New replication ID is bec5f6f31031
1deaa4aff371b7244cd131f594fe8417:M 21 Jul 15:27:51.750 * Discarding previously cached master state.
8417:M 21 Jul 15:27:51.750 # Cluster state changed: ok

客戶端測試:7002的數據自動跳轉到7003上

[root@node21 redis-Cluster]# redis-cli -c -h 192.168.100.21 -p 7000
192.168.100.21:7000> get foo
-> Redirected to slot [12182] located at 192.168.100.21:7003
"bar"
192.168.100.21:7003> 

查看集羣節點狀態

[root@node21 redis-Cluster]# redis-cli -h 192.168.100.21 -p 7000 cluster nodes
2b2a9cab4591508e44290a1fd16781e5c9099010 192.168.100.21:7000@17000 myself,master - 0 1532158988000 1 connected 0-5460
73f894c5ddca484f5b72fd7f1f08612e50681133 192.168.100.21:7003@17003 master - 0 1532158987255 7 connected 10923-16383
7f09c99b30f16ed583f9e144c75e2bb065c0528d 192.168.100.21:7004@17004 slave 2b2a9cab4591508e44290a1fd16781e5c9099010 0 1532158987558 5 connected
23d31961a1ad249b4af1d9f7be6cf3abc1b82f0a 192.168.100.21:7005@17005 slave 69d97b0c7c3c1587f0e4ed0abb82c01fa66a5478 0 1532158988769 6 connected
69d97b0c7c3c1587f0e4ed0abb82c01fa66a5478 192.168.100.21:7001@17001 master - 0 1532158987558 2 connected 5461-10922
4728f1d7da089b6ec9c20f8c9907652536ee5804 192.168.100.21:7002@17002 master,fail - 1532158065392 1532158064562 3 disconnected

7002再啓動,又成爲集羣中的從服。

5 其餘說明

5.1 添加新節點爲主服

 使用add-node命令將新節點的地址指定爲第一個參數,並將集羣中隨機存在節點的地址指定爲第二個參數,redis-trib在運行以前也會檢查集羣的狀態。

# ./redis-trib.rb add-node 192.168.100.21:7006 192.168.100.21:7000

當添加新節點爲主節點時,

  • 它沒有數據,由於它沒有分配的哈希槽。
  • 由於它是沒有分配插槽的主設備,因此當從設備想要成爲主設備時,它不參與選舉過程。

如今可使用resharding功能爲此節點分配哈希槽redis-trib從新分區,具體參考官方文檔,這裏不詳述了。

5.2 添加新節點爲副本

添加新副本分配給指定的主副本

./redis-trib.rb add-node --slave --master-id 3c3a0c74aae0b56170ccb03a76b60cfe7dc1912e 192.168.100.21:7006 192.168.100.21:7000

三 安裝報錯解決

1 gem安裝超時

解決gem install redis 由於網絡問題形成鏈接超時的問題,把gem鏡像換成國內淘寶的

[root@node21 redis-Cluster]# gem sources -l
*** CURRENT SOURCES ***

https://rubygems.org/
[root@node21 redis-Cluster]# gem sources --remove https://rubygems.org/
https://rubygems.org/ removed from sources
[root@node21 redis-Cluster]# gem sources -a https://ruby.taobao.org/
https://ruby.taobao.org/ added to sources
[root@node21 redis-Cluster]# gem sources -l
*** CURRENT SOURCES ***

https://ruby.taobao.org/

2Ruby版本太低

[root@node21 redis-Cluster]# gem install redis
Fetching: redis-4.0.1.gem (100%)
ERROR:  Error installing redis:
    redis requires Ruby version >= 2.2.2.

我這裏CentOS7.5默認安裝的Ruby版本爲2.0,需升級爲2.2.2以上(ruby -v),先安裝RVM,用RVM升級Ruby

1、sudo yum install curl  安裝curl
2、http://www.rvm.io/ 官網首頁就能夠看到 $ gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB
3、下載rvm  $ \curl -sSL https://get.rvm.io | bash -s stable 
4、查找配置文件 find / -name rvm.sh 
5、配置文件生效 source /etc/profile.d/rvm.sh 
6、下載rvm依賴 rvm requirements 
7、查看rvm庫ruby版本 rvm list known
8、安裝ruby指定版本 rvm install ruby-2.5.1
9、使用ruby版本默認 rvm use 2.5.1 default
10.gem install redis

3建立集羣報錯

集羣數據不爲空,Node 192.168.100.21:7001 is not empty 

[root@node21 src]# ./redis-trib.rb create --replicas 1 192.168.100.21:7000 192.168.100.21:7001 192.168.100.21:7002 192.168.100.21:7003 192.168.100.21:7004 192.168.100.
21:7005>>> Creating cluster
[ERR] Node 192.168.100.21:7001 is not empty. Either the node already knows other nodes (check with CLUSTER NODES) or contains some key in database 0.

清空數據,在將每一個節點下aof、rdb、nodes.conf本地備份文件刪除 ,從新建立便可成功

[root@node21 src]# redis-cli -h 192.168.100.21 -p 7001
192.168.100.21:7001> flushdb
OK

四Redis配置說明

Redis 的配置文件位於 Redis 安裝目錄下,文件名爲 redis.conf,配置項說明以下:

1. Redis默認不是以守護進程的方式運行,能夠經過該配置項修改,使用yes啓用守護進程

    daemonize no

2. 當Redis以守護進程方式運行時,Redis默認會把pid寫入/var/run/redis.pid文件,能夠經過pidfile指定

    pidfile /var/run/redis.pid

3. 指定Redis監聽端口,默認端口爲6379,

    port 6379

4. 綁定的主機地址

    bind 127.0.0.1

5.當 客戶端閒置多長時間後關閉鏈接,若是指定爲0,表示關閉該功能

    timeout 300

6. 指定日誌記錄級別,Redis總共支持四個級別:debug、verbose、notice、warning,默認爲verbose

    loglevel verbose

7. 日誌記錄方式,默認爲標準輸出,若是配置Redis爲守護進程方式運行,而這裏又配置爲日誌記錄方式爲標準輸出,則日誌將會發送給/dev/null

    logfile stdout

8. 設置數據庫的數量,默認數據庫爲0,可使用SELECT <dbid>命令在鏈接上指定數據庫id

    databases 16

9. 指定在多長時間內,有多少次更新操做,就將數據同步到數據文件,能夠多個條件配合

    save <seconds> <changes>

    Redis默認配置文件中提供了三個條件:

    save 900 1

    save 300 10

    save 60 10000

    分別表示900秒(15分鐘)內有1個更改,300秒(5分鐘)內有10個更改以及60秒內有10000個更改。

10. 指定存儲至本地數據庫時是否壓縮數據,默認爲yes,Redis採用LZF壓縮,若是爲了節省CPU時間,能夠關閉該選項,但會致使數據庫文件變的巨大

    rdbcompression yes

11. 指定本地數據庫文件名,默認值爲dump.rdb

    dbfilename dump.rdb

12. 指定本地數據庫存放目錄

    dir ./

13. 設置當本機爲slav服務時,設置master服務的IP地址及端口,在Redis啓動時,它會自動從master進行數據同步

    slaveof <masterip> <masterport>

14. 當master服務設置了密碼保護時,slav服務鏈接master的密碼

    masterauth <master-password>

15. 設置Redis鏈接密碼,若是配置了鏈接密碼,客戶端在鏈接Redis時須要經過AUTH <password>命令提供密碼,默認關閉

    requirepass foobared

16. 設置同一時間最大客戶端鏈接數,默認無限制,Redis能夠同時打開的客戶端鏈接數爲Redis進程能夠打開的最大文件描述符數,若是設置 maxclients 0,表示不做限制。當客戶端鏈接數到達限制時,Redis會關閉新的鏈接並向客戶端返回max number of clients reached錯誤信息

    maxclients 128

17. 指定Redis最大內存限制,Redis在啓動時會把數據加載到內存中,達到最大內存後,Redis會先嚐試清除已到期或即將到期的Key,當此方法處理 後,仍然到達最大內存設置,將沒法再進行寫入操做,但仍然能夠進行讀取操做。Redis新的vm機制,會把Key存放內存,Value會存放在swap區

    maxmemory <bytes>

18. 指定是否在每次更新操做後進行日誌記錄,Redis在默認狀況下是異步的把數據寫入磁盤,若是不開啓,可能會在斷電時致使一段時間內的數據丟失。由於 redis自己同步數據文件是按上面save條件來同步的,因此有的數據會在一段時間內只存在於內存中。默認爲no

    appendonly no

19. 指定更新日誌文件名,默認爲appendonly.aof

     appendfilename appendonly.aof

20. 指定更新日誌條件,共有3個可選值: 
    no:表示等操做系統進行數據緩存同步到磁盤(快) 
    always:表示每次更新操做後手動調用fsync()將數據寫到磁盤(慢,安全) 
    everysec:表示每秒同步一次(折衷,默認值)

    appendfsync everysec

21. 指定是否啓用虛擬內存機制,默認值爲no,簡單的介紹一下,VM機制將數據分頁存放,由Redis將訪問量較少的頁即冷數據swap到磁盤上,訪問多的頁面由磁盤自動換出到內存中(在後面的文章我會仔細分析Redis的VM機制)

     vm-enabled no

22. 虛擬內存文件路徑,默認值爲/tmp/redis.swap,不可多個Redis實例共享

     vm-swap-file /tmp/redis.swap

23. 將全部大於vm-max-memory的數據存入虛擬內存,不管vm-max-memory設置多小,全部索引數據都是內存存儲的(Redis的索引數據 就是keys),也就是說,當vm-max-memory設置爲0的時候,實際上是全部value都存在於磁盤。默認值爲0

     vm-max-memory 0

24. Redis swap文件分紅了不少的page,一個對象能夠保存在多個page上面,但一個page上不能被多個對象共享,vm-page-size是要根據存儲的 數據大小來設定的,做者建議若是存儲不少小對象,page大小最好設置爲32或者64bytes;若是存儲很大大對象,則可使用更大的page,若是不 肯定,就使用默認值

     vm-page-size 32

25. 設置swap文件中的page數量,因爲頁表(一種表示頁面空閒或使用的bitmap)是在放在內存中的,,在磁盤上每8個pages將消耗1byte的內存。

     vm-pages 134217728

26. 設置訪問swap文件的線程數,最好不要超過機器的核數,若是設置爲0,那麼全部對swap文件的操做都是串行的,可能會形成比較長時間的延遲。默認值爲4

     vm-max-threads 4

27. 設置在向客戶端應答時,是否把較小的包合併爲一個包發送,默認爲開啓

    glueoutputbuf yes

28. 指定在超過必定的數量或者最大的元素超過某一臨界值時,採用一種特殊的哈希算法

    hash-max-zipmap-entries 64

    hash-max-zipmap-value 512

29. 指定是否激活重置哈希,默認爲開啓(後面在介紹Redis的哈希算法時具體介紹)

    activerehashing yes

30. 指定包含其它的配置文件,能夠在同一主機上多個Redis實例之間使用同一份配置文件,而同時各個實例又擁有本身的特定配置文件

    include /path/to/local.conf

相關文章
相關標籤/搜索