介紹linux
redis是一個key-value存儲系統。和Memcached相似,它支持存儲的value類型相對更多,包括string(字符串)、list(鏈表)、set(集合)、zset(sorted set --有序集合)和hash(哈希類型)。這些數據類型都支持push/pop、add/remove及取交集並集和差集及更豐富的操做,並且這些操做都是原子性的。在此基礎上,redis支持各類不一樣方式的排序。與memcached同樣,爲了保證效率,數據都是緩存在內存中。區別的是redis會週期性的把更新的數據寫入磁盤或者把修改操做寫入追加的記錄文件,而且在此基礎上實現了master-slave(主從)同步。redis
Linux下編譯安裝數據庫
下載redis
############目前最新穩定版##########
[root@redis ~]# wget http://download.redis.io/releases/redis-2.8.19.tar.gzvim
解壓縮redis
[root@redis ~]# tar xzf redis-2.8.19.tar.gz緩存
編譯redis服務器
redis的編譯很是簡單,已經有現成的Makefile文件,直接運行make命令便可
[root@redis redis-2.8.19]# cd redis-2.8.19
[root@redis redis-2.8.19]# makememcached
make命令執行完成後,會在src目錄下生成6個可執行文件,分別是redis-server、redis-cli、redis-benchmark、redis-check-aof、redis-check-dump、redis-sentinel,它們的做用以下:
redis-server: Redis服務器的daemon啓動程序
redis-cli: Redis命令行操做工具。固然,你也能夠用telnet根據其純文本協議來操做
redis-benchmark: Redis性能測試工具,測試Redis在你的系統及你的配置下的讀寫性能
redis-check-aof: 更新日誌檢查
redis-check-dump: 用於本地數據庫檢查
redis-sentinel: Redis實例的監控管理、通知和實例失效備援服務,是Redis集羣的管理工具工具
安裝redis
[root@redis src]# make install性能
配置redis的配置文件
[root@redis redis-2.8.19]# cp redis.conf /etc/
##########編輯Redis配置文件###################
[root@redis redis-2.8.19]# vim /etc/redis.conf
daemonize yes #37行 #是否之後臺daemon方式運行,默認不是後臺運行
pidfile /var/run/redis/redis.pid #41行 #redis的PID文件路徑
bind 10.168.85.25 #64行 #綁定主機IP,默認值爲127.0.0.1,咱們是跨機器運行,因此須要更改
logfile /var/log/redis/redis.log #104行 #定義log文件位置,模式log信息定向到stdout,輸出到/dev/null
save 60 1000 #145行 #從新定義快照的頻率
dir /usr/local/rdbfile #188行 #本地數據庫存放路徑,默認爲./,編譯安裝默認存在在/usr/local/bin下學習
啓動測試Redis服務器
#############啓動Redis服務器############
[root@redis redis-2.8.19]# redis-server /etc/redis.conf
#############查看是否啓動成功###########
[root@redis redis-2.8.19]# ss -tanlp | grep redis
LISTEN 0 128 10.168.85.25:6379 *:* users:(("redis-server",17379,4))
#############測試Redis##################
[root@redis redis-2.8.19]# redis-cli -h 10.168.85.25 -p 6379
10.168.85.25:6379> set test hello
OK
10.168.85.25:6379> get test
"hello"
更改內核信息
#############查看日誌信息###############
[root@redis redis-2.8.19]# tail -f /var/log/redis/redis.log
[5033] 04 Jan 15:47:05.378 # Server started, Redis version 2.8.19
[5033] 04 Jan 15:47:05.379 # 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.
[5033] 04 Jan 15:47:05.379 # 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.
[5033] 04 Jan 15:47:05.380 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
[5033] 04 Jan 15:47:05.380 * DB loaded from disk: 0.000 seconds
[5033] 04 Jan 15:47:05.380 * The server is now ready to accept connections on port 6379
日誌顯示有兩個關於內核設置的警告信息!
##############sysctl文件###############
[root@redis ~]# echo "vm.overcommit_memory=1" >> /etc/sysctl.conf
[root@redis ~]# sysctl -p
#############kerbel####################
[root@redis ~]# echo never > /sys/kernel/mm/transparent_hugepage/enabled
從新啓動Redis服務器
#######將緩存保存到硬盤上#####
[root@redis ~]# redis-cli -h 10.168.85.25 -p 6379 BGSAVE
Background saving started
#######關閉Redis#############
[root@redis ~]# redis-cli -h 10.168.85.25 -p 6379 SHUTDOWN
########啓動Redis############
[root@redis ~]# redis-server /etc/redis.conf
編輯Redis啓動腳本
[root@redis ~]# vi /etc/init.d/redis
#!/bin/sh
#
# redis init file for starting up the redis daemon
#
# chkconfig: - 20 80
# description: Starts and stops the redis daemon.
# Source function library.
. /etc/rc.d/init.d/functions
name="redis-server"
exec="/usr/local/bin/$name" # 指定redis-server命令的位置(whereis redis-server)
pidfile="/var/run/redis/redis.pid" # 指定redis的pid文件路徑(和配置文件裏保持一致)
REDIS_CONFIG="/etc/redis.conf" # 指定redis的配置文件路徑
[ -e /etc/sysconfig/redis ] && . /etc/sysconfig/redis
lockfile=/var/lock/subsys/redis
start() {
[ -f $REDIS_CONFIG ] || exit 6
[ -x $exec ] || exit 5
echo -n $"Starting $name: "
daemon --user ${REDIS_USER-redis} "$exec $REDIS_CONFIG"
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}
stop() {
echo -n $"Stopping $name: "
killproc -p $pidfile $name
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}
restart() {
stop
start
}
reload() {
false
}
rh_status() {
status -p $pidfile $name
}
rh_status_q() {
rh_status >/dev/null 2>&1
}
case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
restart
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart}"
exit 2
esac
exit $?
[root@redis ~]# chmod 700 /etc/init.d/redis
[root@redis ~]# servcie redis restart
附加信息
Redis沒法編譯安裝報錯處理?
###########make時錯誤信息#########
[root@redis redis-2.8.19]# make
cd src && make all
make[1]: Entering directory `/root/redis-2.8.19/src'
CC adlist.o
In file included from adlist.c:34:
zmalloc.h:50:31: error: jemalloc/jemalloc.h: No such file or directory
zmalloc.h:55:2: error: #error "Newer version of jemalloc required"
make[1]: *** [adlist.o] Error 1
make[1]: Leaving directory `/root/redis-2.8.19/src'
make: *** [all] Error 2
############解決方法#############
make MALLOC=libc
vm.overcommit_memory參數解析
若是內存狀況比較緊張的話,須要設定內核參數overcommit_memory,指定內核針對內存分配的策略,其值能夠是0、一、2。
0,表示內核將檢查是否有足夠的可用內存供應用進程使用;若是有足夠的可用內存,內存申請容許;不然,內存申請失敗,並把錯誤返回給應用進程。
1,表示內核容許分配全部的物理內存,而無論當前的內存狀態如何。
2,表示內核容許分配超過全部物理內存和交換空間總和的內存
Redis在dump數據的時候,會fork出一個子進程,理論上child進程所佔用的內存和parent是同樣的,好比parent佔用的內存爲 8G,這個時候也要一樣分配8G的內存給child, 若是內存沒法負擔,每每會形成redis服務器的down機或者IO負載太高,效率降低。因此這裏比較優化的內存分配策略應該設置爲 1(表示內核容許分配全部的物理內存,而無論當前的內存狀態如何)。
設置方式有兩種,需肯定當前用戶的權限活使用root用戶修改:
1:重設文件 echo 1 > /proc/sys/vm/overcommit_memory(默認爲0)
2: echo "vm.overcommit_memory=1" >> /etc/sysctl.conf && sysctl -p
Ubuntu 14.04下Redis安裝及簡單測試 http://www.linuxidc.com/Linux/2014-05/101544.htm
Redis集羣明細文檔 http://www.linuxidc.com/Linux/2013-09/90118.htm
Ubuntu 12.10下安裝Redis(圖文詳解)+ Jedis鏈接Redis http://www.linuxidc.com/Linux/2013-06/85816.htm
Redis系列-安裝部署維護篇 http://www.linuxidc.com/Linux/2012-12/75627.htm
CentOS 6.3安裝Redis http://www.linuxidc.com/Linux/2012-12/75314.htm
Redis安裝部署學習筆記 http://www.linuxidc.com/Linux/2014-07/104306.htm
Redis配置文件redis.conf 詳解 http://www.linuxidc.com/Linux/2013-11/92524.htm