Redis服務搭建及操作Redis 是一個高性能的key-value資料庫。 redis的出現,很大程度補償了memcached這類keyvalue存儲的不足,在部分場合可以對關係資料庫起到很好的補充作用。它提供了Python,Ruby,Erlang,PHP客戶端,使用很方便。 以下是安裝測試步驟: ...
Redis服務搭建及操作
Redis 是一個高性能的key-value資料庫。 redis的出現,很大程度補償了memcached這類keyvalue存儲的不足,在部分場合可以對關係資料庫起到很好的補充作用。它提供了Python,Ruby,Erlang,PHP客戶端,使用很方便。
以下是安裝測試步驟:
1,redis安裝:
[html] view plaincopy
[root@localhost ~]# cd /usr/local/src/
[root@localhost src]# wget http://redis.googlecode.com/files/redis-2.2.12.tar.gz
[root@localhost src]# tar zxf redis-2.2.12.tar.gz
[root@localhost src]# cd redis-2.2.12
[root@localhost redis-2.2.12]# make PREFIX=/usr/local/redis install
2,配置redis:
[html] view plaincopy
[root@localhost redis-2.2.12]# mkdir -p /usr/local/redis/etc
[root@localhost redis-2.2.12]# mkdir -p /usr/local/redis/var
[root@localhost redis-2.2.12]# cd /usr/local/redis/etc
[root@localhost etc]# vi redis.conf(重新建立conf文件,需要複製下麵的配置)
[html] view plaincopy
daemonize yes
pidfile /usr/local/redis/redis.pid
port 6379
bind 127.0.0.1
timeout 300
loglevel verbose
logfile stdout
databases 16
save 900 1
save 300 10
save 60 10000
rdbcompression yes
dbfilename dump.rdb
dir /usr/local/redis/var/
slave-serve-stale-data yes
maxclients 12800
appendonly no
appendfsync everysec
no-appendfsync-on-rewrite no
slowlog-log-slower-than 10000
slowlog-max-len 1024
vm-enabled no
vm-swap-file /tmp/redis.swap
vm-max-memory 0
vm-page-size 32
vm-pages 134217728
vm-max-threads 4
hash-max-zipmap-entries 512
hash-max-zipmap-value 64
list-max-ziplist-entries 512
list-max-ziplist-value 64
set-max-intset-entries 512
activerehashing yes
3,啟動關閉redis:
[html] view plaincopy
[root@localhost ~]# /usr/local/redis/bin/redis-server /usr/local/redis/etc/redis.conf
[root@localhost ~]# ps -ef|grep redis
root 3891 1 0 18:11 ? 00:00:00 /usr/local/redis/bin/redis-server /usr/local/redis/etc/redis.conf
[root@localhost redis-2.2.12]# /usr/local/redis/bin/redis-cli -h 127.0.0.1 -p 6379 shutdown
如果配置使用預設的127.0.0.1和6379埠,可執行命令時可省略以上參數。
4,測試redis:
[html] view plaincopy
[root@localhost ~]# telnet 127.0.0.1 6379
Trying 127.0.0.1...
Connected to localhost (127.0.0.1).
Escape character is '^]'.
^]
telnet> quit
Connection closed.
5,/usr/local/redis/bin目錄下執行文件功能說明:
redis-server:Redis伺服器的daemon啟動程式
redis-cli:Redis命令行操作工具。當然,你也可以用telnet根據其純文本協議來操作
redis-benchmark:Redis性能測試工具,測試Redis在你的系統及你的配置下的讀寫性能
--------------------------滿意的分割線-----------------------------------
實例:
1)登陸,輸入數據:
[html] view plaincopy
[root@localhost ~]# /usr/local/redis/bin/redis-cli -h 127.0.0.1 -p 6379
redis 127.0.0.1:6379> set name csdn
OK
redis 127.0.0.1:6379> get name
"<span style="font-family: simsun; ">csdn</span>"
redis 127.0.0.1:6379> quit
2)查看redis統計信息:
[html] view plaincopy
[root@localhost ~]# /usr/local/redis/bin/redis-cli -h 127.0.0.1 -p 6379 info
3)測試性能:
向redis伺服器發送10個請求,每個請求附帶50個併發客戶端,-n 接請求數,-c 接併發數
[html] view plaincopy
[root@localhost ~]# /usr/local/redis/bin/redis-benchmark -h 127.0.0.1 -p 6379 -n 10 -c 50
自參文檔
redis源碼安裝:redis-3.2.9.tar.gz
下載:wget http://source.goyun.org:8000/source/Redis/redis-3.2.9.tar.gz
解壓:tar -vxf redis-3.2.9.tar.gz
編譯安裝:cd redis-3.2.9
# make MALLOC=jemalloc
#make PREFIX=/home/application/redis-3.2.9 install
cd /home/application/
#ln -s redis-3.2.9 /home/application/redis
配置環境變數:
#vim /etc/profile
最後一行添加:export PATH=/home/application/redis/bin/:$PATH
#./etc/profile
# which redis-server
/home/application/redis/bin/redis-server 表示生效
規範配置文件;
#mkdir /etc/redis
cp /home/application/redis-3.2.9/redis.conf /etc/redis
開機啟動:
redis-server /etc/redis/redis.conf &
啟動時發現有vm.overcommit_memory = 1(說明記憶體低) 後要將這條添加到vim /etc/sysctl.conf的配置文件裡面
或者直接執行sysctl vm.overcommit_memory=1 臨時立即生效
首先將redis關掉 :killall redis-server
然後sysctl vm.overcommit_memory=1 當前生效
這時重啟:redis-server /etc/redis/redis.conf
#lsof -i :6379 檢查服務有沒有起來
redis的正確關閉方法:redis-cli shutdown
redis啟動服務及客戶端測試命令
客戶端命令:redis-cli
redis-cli -h 192.168.1.158 -p 6379
也可以用telnet去連接
Redis 3.2.9版本啟動時出現警告的解決辦法:
錯誤類型:7283:M 12 Mar 12:13:33.749 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
解決方法1: 臨時設置生效: sysctl -w net.core.somaxconn = 1024
方法2: 永久生效: 修改/etc/sysctl.conf文件,增加一行
net.core.somaxconn= 1024
然後執行命令
sysctl -p
錯誤類型2: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.
解決方法1: 臨時設置生效: sysctl -w vm.overcommit_memory = 1
方法2: 永久生效: 修改/etc/sysctl.conf文件,增加一行
vm.overcommit_memory = 1
然後執行命令
sysctl -p