redis安装和使用(一)

来源:互联网 发布:马士兵java视频教程27 编辑:程序博客网 时间:2024/06/11 09:05

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"

0 0
原创粉丝点击