redis安裝和使用(一)

redis基本原理不做介紹,這裏備註基本安裝和簡單使用...

redis安裝和使用(一)

創建文件存放目錄:
mkdir -p /opt/redis/data #redis datafile path
mkdir -p /opt/redis/log #redis logfile path
mkdir -p /opt/redis/bin #redis tool path
mkdir -p /opt/redis/etc #redis conf path
mkdir -p /opt/redis/run #redis pid file path

1.安裝過程:
下載:http://redis.googlecode.com/files/redis-2.6.7.tar.gz
2.解壓:tar -zxf redis-2.6.7.tar.gz
3.cd redis-2.6.7
4.安裝:指定redis安裝目錄
make PREFIX=/opt/redis install
錯誤1:
*************************************************************************************
zmalloc.o: In function `zmalloc_used_memory':
/data/redis-2.6.7/src/zmalloc.c:223: undefined reference to `__sync_add_and_fetch_4'
collect2: ld returned 1 exit status
make[1]: *** [redis-server] Error 1
make[1]: Leaving directory `/data/redis-2.6.7/src'
make: *** [install] Error 2
**************************************************************************************
搞定這個:__sync_add_and_fetch_4方法:
vi /redis-2.6.7/src/.make-settings 中修改:OPT=-O2爲:OPT=-O2 -march=i686
重新編譯成功。
5.測試: make test
錯誤2:
You need 'tclsh8.5' in order to run the Redis test #沒有安裝tcl8.5
a.安裝tc18.5
下載:http://downloads.sourceforge.net/tcl/tcl8.5.12-src.tar.gz
b.tar zxf tcl8.5.12-src.tar.gz
c.mkdir -p /opt/software/tc18.5-install #指定tc18.5安裝目錄
d.cd tcl8.5.12-src/unix #因我們是linux下安裝
e../configure --prefix=/opt/software/tc18.5-install && make && make install #耐心等待....
f.再次make test,通過,表示redis安裝完成.
6.複製必要的工具/opt/redis/bin(可選)下如:
cp /usr/local/redis-2.6.7/src/redis-benchmark redis-cli redis-server..... /opt/redis/bin/.
7.修改配置文件:
cp /usr/local/redis-2.6.7/redis.conf /opt/redis/etc/.
8.開啓和停止redis
a./opt/redis/bin/redis-server /opt/redis/etc/redis.conf
ps -ef | grep redis #確認是否啓動,否則檢驗redis log錯誤提示
b./opt/redis/bin/redis-cli -p 6379 shutdown #停止redis
9.登陸redis
/opt/redis/bin/redis-cli -p 6379
redis 127.0.0.1:6379>

##查看配置文件參數信息;
sed 's@^[# ].*$@@g' /etc/redis.conf | awk '{if (length !=0) print $0}' 

10.使用:
1.#顯示目前redis下有多少keys值存在
redis 127.0.0.1:6379> keys * 
1) "mark_no"
2) "mark_ok"
2.#set 設置 Key/Value
redis 127.0.0.1:6379> set version 2.6.7 
3.#get獲取指定key對應的值
redis 127.0.0.1:6379> get version 
4.#清空所有DB中的keys值(Remove all keys from all databases)
redis 127.0.0.1:6379> flushall 
5.#清空當前DB中的keys值(Remove all keys from the current database)
redis 127.0.0.1:6379> flushdb  
6.#顯示當前DB中keys數量(Return the number of keys in the selected database)
redis 127.0.0.1:6379> dbsize   
7.#服務器基本信息,貼出部分信息
redis 127.0.0.1:6379> info 
# Server
redis_version:2.6.7
redis_git_sha1:00000000
......
# Clients
connected_clients:1
........
# Memory
used_memory:661896
used_memory_human:646.38K
........
# Persistence
loading:0
rdb_changes_since_last_save:0
rdb_bgsave_in_progress:0
.......
# Stats
total_connections_received:1
total_commands_processed:44
instantaneous_ops_per_sec:0
......
# Replication
role:master
connected_slaves:0
# CPU
used_cpu_sys:0.60
used_cpu_user:0.23
.......
# Keyspace
db0:keys=2,expires=0


8.#刪除key/value 如:mark:fucker
redis 127.0.0.1:6379> del mark
(integer) 1
redis 127.0.0.1:6379> get mark
(nil)
9.#判斷指定key是否存在,1:存在,0:不存在
redis 127.0.0.1:6379> exists mark
(integer) 1
#判斷指定key返回的類型,none表示不存在,其他string(字符)/list(鏈表)/set(無序集合)
redis 127.0.0.1:6379> type mark
string
10.#返回指定鍵前綴所有key,即所有mark開頭的key全部顯示出來
redis 127.0.0.1:6379> keys mark*
1) "mark_no"
2) "mark"
3) "mark_ok"
11.鍵的名稱rename,1:成功,0:失敗(1.語法寫錯;2.已存在鍵的名稱)
將redis中鍵:mark1改成新的鍵名:mark5
redis 127.0.0.1:6379> exists mark5 #rename前判斷鍵名是否存在
(integer) 0
redis 127.0.0.1:6379> rename mark1 mark5
OK
redis 127.0.0.1:6379> rename mark5 mark5
(error) ERR source and destination objects are the same
12.#設置批量的key/values對,mset
redis 127.0.0.1:6379> mset one 1 two 2 three 3 four 4
OK
redis 127.0.0.1:6379> keys *o*
1) "one"
2) "two"
3) "four"

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