linux下redis安裝配置及redis常用命令

一、下載redis
下載地址:http://code.google.com/p/redis/downloads/list
2013年12月7號,我下載的是最新版本:redis-2.6.14.tar.gz

另附上csdn上該版本的資源:
http://download.csdn.net/detail/wantianwen/6677973
使用root安裝

su
將該下載包拷貝到/opt
cp redis-2.6.14.tar.gz /opt

二、安裝
cd /opt
tar zxvf redis-2.6.14.tar.gz
cd redis-2.6.14
make && make install

安裝好後查看下redis的可執行文件已經在此目錄下:
ls /usr/local/bin/redis*

/usr/local/bin/redis-benchmark
/usr/local/bin/redis-cli
/usr/local/bin/redis-check-aof
/usr/local/bin/redis-server
/usr/local/bin/redis-check-dump

三、配置redis
redis的配置文件啓動時需要用到:
cp redis.conf /etc/
然後編輯redis.conf配置文件(/etc/redis.conf),按需求做出適當調整,比如:
vi /etc/redis.conf
daemonize yes #默認爲on。yes爲轉爲守護進程,否則啓動時會每隔5秒輸出一行監控信息
save 900 1 #900秒內如果有一個key發生變化時,則將數據寫入進鏡像
maxmemory 256000000 #分配256M內存

將6379端口在防火牆中開放:
rhel系列
vi /etc/sysconfig/iptables#加入一行,需要具備其修改權限
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 6379 -j ACCEPT
/etc/init.d/iptables restart
查看防火牆狀態:
/etc/init.d/iptables status

debian系列
ufw allow 6379
查看防火牆狀態
ufw status
6379                       ALLOW       Anywhere

創建redis的日誌文件夾:
mkdir -p /var/log/redis/log

啓動redis並指定redis的日誌文件:
/usr/local/bin/redis-server /etc/redis.conf 1>/var/log/redis/infolog.log 2>/var/log/redis/errlog.log &
加入自啓動:
vi /etc/rc.local#加入
/usr/local/bin/redis-server /etc/redis.conf 1>/var/log/redis/infolog.log 2>/var/log/redis/errlog.log &

四、服務器優化(根據實際情況判定是否需要優化)
如果內存情況比較緊張的話,需要設定內核參數:
echo 1 > /proc/sys/vm/overcommit_memory

這裏說一下這個配置的含義:
/proc/sys/vm/overcommit_memory
該文件指定了內核針對內存分配的策略,其值可以是0、1、2。
0, 表示內核將檢查是否有足夠的可用內存供應用進程使用;如果有足夠的可用內存,內存申請允許;否則,內存申請失敗,並把錯誤返回給應用進程。
1, 表示內核允許分配所有的物理內存,而不管當前的內存狀態如何。
2, 表示內核允許分配超過所有物理內存和交換空間總和的內存

五、測試redis
客戶端連接
redis-cli
redis 127.0.0.1:6379> set redisKey value
OK
redis 127.0.0.1:6379> get redisKey
"value"
redis 127.0.0.1:6379> del redisKey
(integer) 1
redis 127.0.0.1:6379> exists key
(integer) 0


keys *
取出當前匹配的所有key

> exists larry
(integer) 0

當前的key是否存在

del lv
刪除當前key

expire
設置過期時間

> expire larry 10
(integer) 1

> move larry ad4
(integer) 1

移動larry鍵值對到ad4數據庫

> persist lv
(integer) 1
移除當前key的過期時間

randomkey
隨機返回一個key

rename
重命名key

type
返回值的數據類型

type testlist
list

> ping
PONG
測試連接是否還在

>echo name
"larry"
打印

> select ad4databank
OK
數據庫切換

> quit
退出連接

> dbsize
(integer) 12
當前數據庫中key的數量

> info
服務器基本信息

monitor
實時轉儲收到的請求

config get
獲取服務器的參數配置

flushdb
清空當前數據庫

flushall

清除所有數

六、遇到問題
我用java操作redis時,報過這樣的錯誤:
(error) MISCONF Redis is configured to save RDB snapshots, but is currently not able to persist on disk. Commands that may modify the data set are disabled. Please check Redis logs for details about the error.

解決:

redis-cli
config set stop-writes-on-bgsave-error no

推薦:http://www.laikanxia.com

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章