1. 程式人生 > >單點安裝redis+哨兵

單點安裝redis+哨兵

with start make parent enc 調整 編譯 color The

下載地址

http://redis.io/download/ 
http://219.239.26.13/files/205900000B7E5F47/download.redis.io/releases/redis-4.0.9.tar.gz

創建程序目錄

mkdir /data/
#目錄結構自己調整 redis編譯完成後,有用的文件是 redis.conf sentinel.conf src/redis-* 可執行文件

解壓並編譯

yum -y install  gcc tcl gcc-c++ make
tar zxvf redis-4.0.9.tar.gz
mv redis-4.0.9 /data/
cd redis
-4.0.9 make make install

規劃目錄結構

技術分享圖片筆者的目錄結構

修改配置文件 redis.conf

#常規修改項
bind 1.1.1.1 pidfile "/app/redis-3.2.11/6379/run/redis_6379.pid" logfile "/app/redis-3.2.11/6379/logs/redis.log" dir "/app/redis-3.2.11/6379/data"

修改配置文件 sentinel.conf

bind 1.1.1.1
dir "/data/redis-4.0.9/6379/sentinel"
logfile "/data/redis-4.0.9/6379/sentinel/sentinel.log
"

啟動redis

/data/redis-4.0.9/bin/redis-server /data/redis-4.0.9/6379/conf/redis.conf &
/data/redis-4.0.9/bin/redis-sentinel /data/redis-4.0.9/6379/sentinel/sentinel.conf &

登錄redis

#如果綁定了固定ip 就指定ip登錄 否則會被拒絕
/app/redis-3.2.11/bin/redis-cli -h 172.16.1.29 -p 6379

設置密碼

打開文件/etc/redis.conf,找到其中的# requirepass foobared,去掉前面的#,並把foobared改成你的密碼。

使用密碼登錄

/app/redis-3.2.11/bin/redis-cli -h 172.16.1.29 -p 6379 -a 123

當第一啟動redis的時候,可能會產生一些警告

WARNING The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
 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.
 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.

可以看到存在3條啟動警告信息:

第一個錯誤和第二個錯誤可以同時解決,第一個錯誤大概是說somaxconn的值128設置過小,此值受限於系統的somaxconn與tcp_max_syn_backlog這兩個值,所以應該把這兩個內核參數值調大,第二個錯誤是過量使用內存設置為0!在低內存環境下,後臺保存可能失敗。請在/etc/sysctl.conf 添加一項 ‘vm.overcommit_memory = 1′ ,然後重啟(或者運行命令’sysctl vm.overcommit_memory=1’ )使其生效。

針對警告所做的修改:

 # vim /etc/sysctl.conf
 net.core.somaxconn = 20480
 #最大隊列長度,應付突發的大並發連接請求,默認為128
 net.ipv4.tcp_max_syn_backlog = 20480
 #半連接隊列長度,此值受限於內存大小,默認為1024
 vm.overcommit_memory = 1
 #過量使用內存設置為0!
 # sysctl -p
第三個警告錯誤是需要關閉Linux (THP) 透明內存

 # vim /etc/rc.local
 echo never > /sys/kernel/mm/transparent_hugepage/enabled #在開機腳本裏追加此命令

單點安裝redis+哨兵