Redis、Redis+sentinel安裝(Ubuntu 14.04下Redis安裝及簡單測試)


 

Ubuntu下Redis安裝兩種安裝方式:

1、apt-get方式

步驟:

  1. 以root權限登錄,切換到/usr目錄下.

  2. 接下來輸入命令,apt-get install redis-server,如圖:

 

  3.安裝完成后,Redis服務器會自動啟動,我們檢查Redis服務器程序,執行ps -aux|grep redis,如圖:

4.通過啟動命令檢查Redis服務器狀態,執行:netstat -apn|grep 6379,如圖:

5.安裝Redis服務器,會自動地一起安裝Redis命令行客戶端程序。在本機輸入redis-cli命令就可以啟動,客戶端程序訪問Redis服務器。執行:redis-cli,如圖:

 

2、手工下載安裝包,再執行安裝指令

摘要: Redis是目前業界非常受到歡迎的一個內存數據庫,一般用作系統的中間緩存系統,用以提升整體商業系統的吞吐量和響應速度。本文將簡要介紹安裝的主要過程以及給出一個簡要的測試代碼。

1.  系統環境和版本說明

Linux操作系統選用Ubuntu 14.04, Redis的版本選取目前的最新穩定版本redis-3.07。 客戶端選用了Redis的Java版本jedis 2.4.2。

2.  Redis的安裝步驟

a. 下載Redis的安裝包

wget http://download.redis.io/releases/redis-3.07.tar.gz

b. 在目錄下,解壓按照包,生成新的目錄redis-3.07

tar xvfz redis-3.07.tar.gz

c.  進入解壓之后的目錄,進行編譯

cd redis-3.07

sudo make

說明: 如果沒有明顯的錯誤,則表示編譯成功

d.  安裝

sudo make install

說明: 一般情況下,在Ubuntu系統中,都是需要使用sudo提升權限的

e.  在安裝成功之后,可以運行測試,確認Redis的功能是否正常

sudo make test

這時候,我們可以看看src下的文件:

[neil@neilhost src]$ ll

可以看到此時,src文件夾下出現了一些綠色的文件,這些文件就是我們以后需要用到的命令文件。

步驟3:

移動文件,便於管理:(所有源代碼安裝的軟件都安裝在/usr/local下,如apache等)

創建兩個文件夾,bin用於存放命令,etc擁有存放配置文件。

duanxz@ubuntu:~/soft/redis-3.0.7/src$ sudo mkdir -p /usr/local/redis/bin
duanxz@ubuntu:~/soft/redis-3.0.7/src$ sudo mkdir -p /usr/local/redis/etc

-p是遞歸創建。

接下來,將redis-2.8.19文件夾下的redis.conf復制到/usr/local/redis/etc/

並將src目錄下的7個命令文件(綠色的),移動到/usr/local/redis/bin/

duanxz@ubuntu:~/soft/redis-3.0.7/src$ cd ..
duanxz@ubuntu:~/soft/redis-3.0.7$ ls
00-RELEASENOTES COPYING Makefile redis.conf runtest-sentinel tests
BUGS deps MANIFESTO runtest sentinel.conf utils
CONTRIBUTING INSTALL README runtest-cluster src
duanxz@ubuntu:~/soft/redis-3.0.7$ sudo mv ./redis.conf /usr/local/redis/etc/
duanxz@ubuntu:~/soft/redis-3.0.7$ cd src
duanxz@ubuntu:~/soft/redis-3.0.7/src$ sudo mv mkreleasehdr.sh redis-benchmark redis-check-aof redis-check-dump redis-cli redis-sentinel redis-server /usr/local/redis/bin/
duanxz@ubuntu:~/soft/redis-3.0.7/src$

步驟4:啟動Redis服務:

首先進入剛才安裝redis的目錄:

duanxz@ubuntu:~/soft/redis-3.0.7/src$ cd /usr/local/redis/bin/
duanxz@ubuntu:/usr/local/redis/bin$ ls
mkreleasehdr.sh  redis-check-aof   redis-cli       redis-server
redis-benchmark  redis-check-dump  redis-sentinel

之后我們啟動redis服務。啟動redis服務需要用到命令redis-server

duanxz@ubuntu:/usr/local/redis/bin$ sudo ./redis-server 
20022:C 22 Apr 07:40:42.008 # Warning: no config file specified, using the default config. In order to specify a config file use ./redis-server /path/to/redis.conf
20022:M 22 Apr 07:40:42.009 * Increased maximum number of open files to 10032 (it was originally set to 1024).
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 3.0.7 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 20022
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

20022:M 22 Apr 07:40:42.017 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
20022:M 22 Apr 07:40:42.017 # Server started, Redis version 3.0.7
20022:M 22 Apr 07:40:42.017 # 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.
20022:M 22 Apr 07:40:42.017 # 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.
20022:M 22 Apr 07:40:42.017 * The server is now ready to accept connections on port 6379

但是,這樣做的話,我們並沒有使用etc的下的配置文件進行啟動。

如果希望通過指定的配置文件啟動,需要在啟動時指定配置文件:

這里我們先用ctrl+C來終止服務,然后查看redis服務是否終止干凈了,之后通過設置配置文件來啟動服務:

^C20022:signal-handler (1461336088) Received SIGINT scheduling shutdown...
20022:M 22 Apr 07:41:28.117 # User requested shutdown...
20022:M 22 Apr 07:41:28.118 * Saving the final RDB snapshot before exiting.
20022:M 22 Apr 07:41:28.123 * DB saved on disk
20022:M 22 Apr 07:41:28.123 # Redis is now ready to exit, bye bye...
duanxz@ubuntu:/usr/local/redis/bin$ pstree -p | grep redis
duanxz@ubuntu:/usr/local/redis/bin$ sudo ./redis-server /usr/local/redis/etc/redis.conf
20075:M 22 Apr 07:42:33.515 * Increased maximum number of open files to 10032 (it was originally set to 1024).
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 3.0.7 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 20075
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

20075:M 22 Apr 07:42:33.522 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
20075:M 22 Apr 07:42:33.522 # Server started, Redis version 3.0.7
20075:M 22 Apr 07:42:33.522 # 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.
20075:M 22 Apr 07:42:33.522 # 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.
20075:M 22 Apr 07:42:33.523 * DB loaded from disk: 0.000 seconds
20075:M 22 Apr 07:42:33.523 * The server is now ready to accept connections on port 6379

但是,現在redis仍然是在前台運行。

如果要后台啟動該怎么辦呢?

回想一下,后台啟動mysql的方法是在后面加一個 &符號。如:

#mysql_safe --user=mysql &

后台啟動mongodb,則在后面加上一個 --fork。具體方法請看我之前的MongDB入門系列(2)。

但是,這兩種方法都不適用於redis。redis的后台啟動並運行需要通過配置文件中的參數設置。

Redis的配置文件中有哪些配置呢?

daemonize 如果需要在后台運行,把該項改為yes

pidfile 配置多個pid的地址 默認在/var/run/redis.pid

bind 綁定ip,設置后只接受來自該ip的請求

port 監聽端口,默認是6379

loglevel 分為4個等級:debug verbose notice warning

logfile 用於配置log文件地址

databases 設置數據庫個數,默認使用的數據庫為0

save 設置redis進行數據庫鏡像的頻率。

rdbcompression 在進行鏡像備份時,是否進行壓縮

dbfilename 鏡像備份文件的文件名

Dir 數據庫鏡像備份的文件放置路徑

Slaveof 設置數據庫為其他數據庫的從數據庫

Masterauth 主數據庫連接需要的密碼驗證

Requriepass 設置 登陸時需要使用密碼

Maxclients 限制同時使用的客戶數量

Maxmemory 設置redis能夠使用的最大內存

Appendonly 開啟append only模式

Appendfsync 設置對appendonly.aof文件同步的頻率(對數據進行備份的第二種方式)

vm-enabled 是否開啟虛擬內存支持   (vm開頭的參數都是配置虛擬內存的)

vm-swap-file 設置虛擬內存的交換文件路徑

vm-max-memory 設置redis使用的最大物理內存大小

vm-page-size 設置虛擬內存的頁大小

vm-pages 設置交換文件的總的page數量

vm-max-threads 設置VM IO同時使用的線程數量

Glueoutputbuf 把小的輸出緩存存放在一起

hash-max-zipmap-entries 設置hash的臨界值

Activerehashing 重新hash

本文是oschina博客用戶happyBKs的文章,轉載請聲明出處。http://my.oschina.net/u/1156339/blog/387335

言歸正傳,如果需要redis后台運行需要將daemonize由no改為yes。

首先在超級權限下打開redis.conf。

[neil@neilhost bin]$ sudo gedit /usr/local/redis/etc/redis.conf

之后將daemonize由no改為yes。

Redis安裝部署與維護詳解

保存退出。

之后我們再次使用配置文件啟動redis-server。

可以看到,redis是后台啟動了,並且通過ps命令可以查看到redis正在運行。

[neil@neilhost bin]$ sudo ./redis-server /usr/local/redis/etc/redis.conf
[neil@neilhost bin]$ ps -ef | grep redis
root     13154     1  0 22:53 ?        00:00:00 ./redis-server *:6379
neil     13162  8143  0 22:54 pts/0    00:00:00 grep --color=auto redis
[neil@neilhost bin]$ pstree -p | grep redis
           |-redis-server(13154)-+-{redis-server}(13156)
           |                     `-{redis-server}(13157)

 

Redis服務端默認連接端口是6379.

mysql 服務端默認連接端口是3306

Mogodb 服務端默認連接端口是27017,還有28017。

在平時,我們往往需要查看6379端口是否被占用。可以用以下命令:

[neil@neilhost bin]$ netstat -tunpl | grep 6379
(Not all processes could be identified, non-owned process info
 will not be shown, you would have to be root to see it all.)
tcp        0      0 0.0.0.0:6379            0.0.0.0:*               LISTEN      -                   
tcp6       0      0 :::6379                 :::*                    LISTEN      -                   
[neil@neilhost bin]$ sudo netstat -tunpl | grep 6379
[sudo] password for neil: 
tcp        0      0 0.0.0.0:6379            0.0.0.0:*               LISTEN      13154/./redis-serve 
tcp6       0      0 :::6379                 :::*                    LISTEN      13154/./redis-serve

注意,redis服務需要su權限才能查看,不然只能檢查到6379被某個進程占用,但是看不到進程名稱。

至此,redis服務已經按照配置文件啟動成功!!

客戶端登陸

步驟5:

客戶端連接

duanxz@ubuntu:~/soft$ sudo /usr/local/redis/bin/redis-cli
127.0.0.1:6379> 

 

關閉Redis服務

步驟6:

停止Redis實例

我們可以使用pkill redis-server

[neil@neilhost bin]$ sudo pkill redis-server
[neil@neilhost bin]$ sudo netstat -tunpl | grep 6379
[neil@neilhost bin]$ 
[neil@neilhost bin]$ pstree -p | grep redis
[neil@neilhost bin]$ 
[neil@neilhost bin]$ 
[neil@neilhost bin]$ sudo /usr/local/redis/bin/redis-cli 
Could not connect to Redis at 127.0.0.1:6379: Connection refused
not connected> 
not connected> exit

 

關閉之后,發現6379就不再被占用了,redis的進程也都沒有了。

客戶登陸也無法成功了。

 

也可以使用/usr/local/redis/bin/redis-cli shutdown,這種方法使用客戶端命令redis-cli 

[neil@neilhost bin]$ sudo ./redis-server /usr/local/redis/etc/redis.conf
[neil@neilhost bin]$ pstree -p | grep redis
           |-redis-server(13509)-+-{redis-server}(13511)
           |                     `-{redis-server}(13512)
[neil@neilhost bin]$ sudo /usr/local/redis/bin/redis-cli shutdown
[neil@neilhost bin]$ pstree -p | grep redis
[neil@neilhost bin]$ 
[neil@neilhost bin]$ sudo netstat -tunpl | grep 6379
[neil@neilhost bin]$ 
[neil@neilhost bin]$

還可以使用命令killall和kill -9,可以仿照我在之前博客中的關閉Mongdb的方法。

 

=====================================================================

Redis+Sentinel安裝與配置 

在這里我們搭建的是一個1主3從的redis+3個哨兵集群的環境,由於是在一台物理機上,所有我們用端口區分。
物理機IP:192.168.0.12
主節點master端口:6301
從節點slave1端口:6315
從節點slave2端口:6316
從節點slave3端口:6317
哨兵sentinel1端口:26301
哨兵sentinel2端口:26302
哨兵sentinel3端口:26303
一、下載安裝
$ wget http://download.redis.io/releases/redis-3.0.0.tar.gz
$ tar xzf redis-3.0.0.tar.gz
$ cd redis-3.0.0
$ make
二、復制文件
cp redis-benchmark redis-cli redis-server redis-sentinel /usr/bin/ #這個倒是很有用,這樣就不用再執行時加上./了,而且可以在任何地方執行
三、設置內存分配策略(可選,根據服務器的實際情況進行設置)
/proc/sys/vm/overcommit_memory
可選值:0、1、2。
0, 表示內核將檢查是否有足夠的可用內存供應用進程使用;如果有足夠的可用內存,內存申請允許;否則,內存申請失敗,並把錯誤返回給應用進程。
1, 表示內核允許分配所有的物理內存,而不管當前的內存狀態如何。
2, 表示內核允許分配超過所有物理內存和交換空間總和的內存
四、開啟redis端口,修改防火牆配置文件
    vi /etc/sysconfig/iptables 
加入端口配置
    -A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 6379 -j ACCEPT 
重新加載規則
    service iptables restart 
五、配置redis.config文件
1、主節點master的配置文件redis_master_6301.config:
# Redis configuration file example
################################## INCLUDES ###################################
# include /path/to/local.conf
# include /path/to/other.conf
################################ GENERAL  #####################################
daemonize yes
pidfile ./run/redis_slaver1_6315.pid
port 6301
tcp-backlog 511
# bind 192.168.1.100 10.0.0.1
# bind 127.0.0.1
# unixsocket /tmp/redis.sock
# unixsocketperm 700
timeout 0
tcp-keepalive 0
loglevel notice
logfile "./run/logs/log_master_6301.log"
databases 16
################################ SNAPSHOTTING  ################################
save ""
# save 900 1
# save 300 10
# save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum no
dbfilename dump_6301.rdb
dir ./run/data
################################# REPLICATION #################################
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
# repl-ping-slave-period 10
# repl-timeout 60
repl-disable-tcp-nodelay no
# repl-backlog-size 1mb
# repl-backlog-ttl 3600
slave-priority 100
# min-slaves-to-write 3
# min-slaves-max-lag 10
################################## SECURITY ###################################
# rename-command CONFIG ""
################################### LIMITS ####################################
# maxclients 10000
# maxmemory <bytes>
# maxmemory-policy noeviction
# maxmemory-samples 5
############################## APPEND ONLY MODE ###############################
appendonly no
appendfilename "appendonly_6301.aof"
appendfsync no
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
################################ LUA SCRIPTING  ###############################
lua-time-limit 5000
################################ REDIS CLUSTER  ###############################
# cluster-enabled yes
# cluster-config-file nodes-6379.conf
# cluster-node-timeout 15000
# cluster-slave-validity-factor 10
# cluster-migration-barrier 1
# cluster-require-full-coverage yes
################################## SLOW LOG ###################################
slowlog-log-slower-than 10000
slowlog-max-len 128
################################ LATENCY MONITOR ##############################
latency-monitor-threshold 0
############################# EVENT NOTIFICATION ##############################
notify-keyspace-events ""
############################### ADVANCED CONFIG ###############################
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-entries 512
list-max-ziplist-value 64
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes
2、從節點slave1的配置文件redis_slave_6315.config:
# Redis configuration file example
################################## INCLUDES ###################################
# include /path/to/local.conf
# include /path/to/other.conf
################################ GENERAL  #####################################
daemonize yes
pidfile ./run/redis_slaver1_6315.pid
port 6315
tcp-backlog 511
# bind 192.168.1.100 10.0.0.1
# bind 127.0.0.1
# unixsocket /tmp/redis.sock
# unixsocketperm 700
timeout 0
tcp-keepalive 0
loglevel notice
logfile "./run/logs/log_slaver1_6315.log"
databases 16
################################ SNAPSHOTTING  ################################
save ""
# save 900 1
# save 300 10
# save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum no
dbfilename dump_6315.rdb
dir ./run/data
################################# REPLICATION #################################
slaveof 192.168.0.12 6301
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
# repl-ping-slave-period 10
# repl-timeout 60
repl-disable-tcp-nodelay no
# repl-backlog-size 1mb
# repl-backlog-ttl 3600
slave-priority 80
# min-slaves-to-write 3
# min-slaves-max-lag 10
################################## SECURITY ###################################
# rename-command CONFIG ""
################################### LIMITS ####################################
# maxclients 10000
# maxmemory <bytes>
# maxmemory-policy noeviction
# maxmemory-samples 5
############################## APPEND ONLY MODE ###############################
appendonly no
appendfilename "appendonly_6315.aof"
appendfsync no
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
################################ LUA SCRIPTING  ###############################
lua-time-limit 5000
################################ REDIS CLUSTER  ###############################
# cluster-enabled yes
# cluster-config-file nodes-6379.conf
# cluster-node-timeout 15000
# cluster-slave-validity-factor 10
# cluster-migration-barrier 1
# cluster-require-full-coverage yes
################################## SLOW LOG ###################################
slowlog-log-slower-than 10000
slowlog-max-len 128
################################ LATENCY MONITOR ##############################
latency-monitor-threshold 0
############################# EVENT NOTIFICATION ##############################
notify-keyspace-events ""
############################### ADVANCED CONFIG ###############################
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-entries 512
list-max-ziplist-value 64
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes
3、從節點slave2的配置文件redis_slave_6316.config:
與上面slave1配置文件需要修改的地方
pidfile ./run/redis_slaver1_6316.pid
port 6316
logfile "./run/logs/log_slaver1_6316.log"
dbfilename dump_6316.rdb
appendfilename "appendonly_6316.aof"
4、從節點slave3的配置文件redis_slave_6317.config:
與上面slave1配置文件需要修改的地方
pidfile ./run/redis_slaver1_6317.pid
port 6317
logfile "./run/logs/log_slaver1_6317.log"
dbfilename dump_6317.rdb
appendfilename "appendonly_6317.aof"
5、哨兵sentinel1的配置文件sentinel_26301.config:
# Example sentinel.conf
port 26301
# sentinel announce-ip 1.2.3.4
dir ./run/tmp
sentinel monitor master1 192.168.0.12 6301 2
# sentinel auth-pass mymaster MySUPER--secret-0123passw0rd
# sentinel down-after-milliseconds <master-name> <milliseconds>
sentinel down-after-milliseconds master1 30000
sentinel parallel-syncs master1 1
sentinel failover-timeout master1 180000
# sentinel notification-script mymaster /var/redis/notify.sh
# sentinel client-reconfig-script mymaster /var/redis/reconfig.sh
# sentinel can-failover master1 yes
logfile "/redis-3.0.0/run/logs/sentinellog_m1_26301.log"
6、哨兵sentinel2的配置文件sentinel_26302.config:
# Example sentinel.conf
port 26302
# sentinel announce-ip 1.2.3.4
dir ./run/tmp
sentinel monitor master1 192.168.0.12 6301 2
# sentinel auth-pass mymaster MySUPER--secret-0123passw0rd
# sentinel down-after-milliseconds <master-name> <milliseconds>
sentinel down-after-milliseconds master1 30000
sentinel parallel-syncs master1 1
sentinel failover-timeout master1 180000
# sentinel notification-script mymaster /var/redis/notify.sh
# sentinel client-reconfig-script mymaster /var/redis/reconfig.sh
# sentinel can-failover master1 yes
logfile "/redis-3.0.0/run/logs/sentinellog_m1_26302.log"
7、哨兵sentinel3的配置文件sentinel_26303.config:
# Example sentinel.conf
port 26303
# sentinel announce-ip 1.2.3.4
dir ./run/tmp
sentinel monitor master1 192.168.0.12 6301 2
# sentinel auth-pass mymaster MySUPER--secret-0123passw0rd
# sentinel down-after-milliseconds <master-name> <milliseconds>
sentinel down-after-milliseconds master1 30000
sentinel parallel-syncs master1 1
sentinel failover-timeout master1 180000
# sentinel notification-script mymaster /var/redis/notify.sh
# sentinel client-reconfig-script mymaster /var/redis/reconfig.sh
# sentinel can-failover master1 yes
logfile "/redis-3.0.0/run/logs/sentinellog_m1_26303.log"
六、啟動redis和哨兵服務
注意:第一次要先啟動redis主服務、從服務,然后才能啟動哨兵服務
1、進入redis安裝目錄的根目錄
2、啟動redis主節點
[root@localhost redis-3.0.0]# redis-server redis_master_6301.config &
3、啟動redis從節點
[root@localhost redis-3.0.0]# redis-server redis_slave_6315.config &
[root@localhost redis-3.0.0]# redis-server redis_slave_6316.config &
[root@localhost redis-3.0.0]# redis-server redis_slave_6317.config &
4、啟動哨兵
[root@localhost redis-3.0.0]# redis-sentinel sentinel_26301.config &
[root@localhost redis-3.0.0]# redis-sentinel sentinel_26302.config &
[root@localhost redis-3.0.0]# redis-sentinel sentinel_26303.config &

到目前為止整個redis+sentinel的安裝搭建環境就算完成

七、一些常用命令
進入redis的安裝目錄
啟動redis-server:
./redis-server redis_6305.conf &
啟動redis-sentinel 
./redis-sentinel sentinel_6301.config
./redis-server sentinel_6316.conf --sentinel &
查看某個端口信息:
./redis-cli -p 6301 info
./redis-cli -p 6301 info Replication
./redis-cli -p 6301 info Sentinel
查看某個主機上的信息redis信息
./redis-cli -h 10.16.41.52 -p 6316 info
./redis-cli -h 10.16.41.52 -p 6316 info Replication
./redis-cli -h 10.16.41.52 -p 6316 info Sentinel
關閉本機redis服務
./redis-cli -p 6379 shutdown
關閉遠程主機redis服務
./redis-cli -h 192.168.9.18 -p 6379 shutdown
客戶端連接本機
./redis-cli -p 6301
客戶端連接遠程機
./redis-cli -h 10.16.41.53 -p 6301
切換連接
ssh app@10.16.41.52

使用命令關閉RDB持久化:在客戶端執行127.0.0.1:6316> config set save ""

 

Linux下redis的安裝

 

第一部分:安裝redis 
希望將redis安裝到此目錄 

1
/usr/local/redis

希望將安裝包下載到此目錄 

1
/usr/local/src

那么安裝過程指令如下: 

1
2
3
4
5
6
7
$ mkdir /usr/local/redis  
$ cd /usr/local/src  
$ wget http: //redis.googlecode.com/files/redis-2.6.14.tar.gz  
$ tar xzf redis-2.6.14.tar.gz   
$ ln -s redis-2.6.14 redis #建立一個鏈接  
$ cd redis  
$ make PREFIX=/usr/local/redis install #安裝到指定目錄中

注意上面的最后一行,我們通過PREFIX指定了安裝的目錄。如果make失敗,一般是你們系統中還未安裝gcc,那么可以通過yum安裝: 

1
yum install gcc

安裝完成后,繼續執行make. 

在安裝redis成功后,你將可以在/usr/local/redis看到一個bin的目錄,里面包括了以下文件: 

1
redis-benchmark  redis-check-aof  redis-check-dump  redis-cli  redis-server

第二部分:將redis做成一個服務 

1.復制腳本到/etc/rc.d/init.d目錄 
ps: /etc/rc.d/init.d/目錄下的腳本就類似與windows中的注冊表,在系統啟動的時候某些指定腳本將被執行 
按以上步驟安裝Redis時,其服務腳本位於:

1
/usr/local/src/redis/utils/redis_init_script 

必須將其復制到/etc/rc.d/init.d的目錄下: 

1
cp /usr/local/src/redis/utils/redis_init_script /etc/rc.d/init.d/redis

將redis_init_script復制到/etc/rc.d/init.d/,同時易名為redis。

如果這時添加注冊服務:

1
chkconfig --add redis

將報以下錯誤:

1
redis服務不支持chkconfig

為此,我們需要更改redis腳本。 

2.更改redis腳本 
打開使用vi打開腳本,查看腳本信息: 

1
vim /etc/rc.d/init.d/redis

看到的內容如下(下內容是更改好的信息): 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#!/bin/sh 
#chkconfig: 2345 80 90 
# Simple Redis init.d script conceived to work on Linux systems 
# as it does use of the /proc filesystem. 
   
REDISPORT=6379 
EXEC=/usr/local/redis/bin/redis-server 
CLIEXEC=/usr/local/redis/bin/redis-cli 
   
PIDFILE=/var/run/redis_${REDISPORT}.pid 
CONF= "/etc/redis/${REDISPORT}.conf" 
   
case  "$1"  in 
     start) 
         if  [ -f $PIDFILE ] 
         then 
                 echo  "$PIDFILE exists, process is already running or crashed" 
         else 
                 echo  "Starting Redis server..." 
                 $EXEC $CONF & 
         fi 
         ;; 
     stop) 
         if  [ ! -f $PIDFILE ] 
         then 
                 echo  "$PIDFILE does not exist, process is not running" 
         else 
                 PID=$(cat $PIDFILE) 
                 echo  "Stopping ..." 
                 $CLIEXEC -p $REDISPORT shutdown 
                 while  [ -x /proc/${PID} ] 
                 do 
                     echo  "Waiting for Redis to shutdown ..." 
                     sleep 1 
                 done 
                 echo  "Redis stopped" 
         fi 
         ;; 
     *) 
         echo  "Please use start or stop as first argument" 
         ;; 
esac 

和原配置文件相比: 

1.原文件是沒有以下第2行的內容的,

1
#chkconfig: 2345 80 90 

2.原文件EXEC、CLIEXEC參數,也是有所更改。 

1
2
EXEC=/usr/local/redis/bin/redis-server   
CLIEXEC=/usr/local/redis/bin/redis-cli 

3.redis開啟的命令,以后台運行的方式執行。

1
$EXEC $CONF & 

ps:注意后面的那個“&”,即是將服務轉到后面運行的意思,否則啟動服務時,Redis服務將 

占據在前台,占用了主用戶界面,造成其它的命令執行不了。 
4.將redis配置文件拷貝到/etc/redis/${REDISPORT}.conf 

1
2
mkdir /etc/redis    
cp /usr/local/src/redis/redis.conf /etc/redis/6379.conf

這樣,redis服務腳本指定的CONF就存在了。默認情況下,Redis未啟用認證,可以通過開啟6379.conf的requirepass 指定一個驗證密碼。 

以上操作完成后,即可注冊yedis服務:

1
chkconfig --add redis

3.啟動redis服務 

1
service redis start 

第三,將Redis的命令所在目錄添加到系統參數PATH中 

修改profile文件:

1
vi /etc/profile

在最后行追加: 

1
export PATH= "$PATH:/usr/local/redis/bin"

然后馬上應用這個文件: 

1
. /etc/profile  

這樣就可以直接調用redis-cli的命令了,如下所示: 

1
2
3
4
5
6
$ redis-cli   
redis 127.0.0.1:6379> auth superman   
OK   
redis 127.0.0.1:6379> ping   
PONG   
redis 127.0.0.1:6379>

至此,redis 就成功安裝了。 

總結下:在linux系統中安裝redis,或多或少都能碰到一些問題。在此次安裝中3個大部分, 
1.下載,安裝,這里使用到wget命令,make命令,我不太懂make命令的使用,而且一直擔心make命令如何安裝到指定目錄下, 此次終於明白了。 
2.如何將一個程序添加到服務,當然也對/etc/rc.d/init.d這個文件有所了解。 
3.如何將一個程序的一些命令添加到系統參數中,直接輸入命令就能達到對某個程序的操作。 
其實就是指定好環境變量。 
下篇簡單使用jedis來對redis進行存取。 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM