#在后台启动
[iyunv@redis-m ~]# redis-server /usr/local/redis/conf/redis.conf &
[1] 5870
[iyunv@redis-m ~]# [5870] 26 Jul 18:24:24.771 * Increased maximum number of open files to 10032 (it was originally set to 1024).
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 2.8.9 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in stand alone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
| `-._ `._ / _.-' | PID: 5870
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
[5870] 26 Jul 18:24:24.774 # Server started, Redis version 2.8.9
[5870] 26 Jul 18:24:24.774 # 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.
[5870] 26 Jul 18:24:24.774 * The server is now ready to accept connections on port 6379
function_status()
{
a=`ps -A|grep "redis-server\>" -c`
if [ $a -ge 1 ];then
echo -e "The Redis is [\e[0;32;5m runing \e[0m]"
else
echo -e "The Redis is [\e[0;31;5m not run \e[0m]"
fi
}
[iyunv@redis-m ~]# redis-cli
#添加一个值set key value
127.0.0.1:6379> set id 001
OK
127.0.0.1:6379> get id
"001"
127.0.0.1:6379> del id
(integer) 1 #返回1,表示操作成功
127.0.0.1:6379> get id
(nil)
127.0.0.1:6379> exists id #判断一个key是否存在
(integer) 0 #返回0表示不存在
#切换数据库,默认有16个库,标号从0-15,这里我们切换到1号库
127.0.0.1:6379> select 1
OK
127.0.0.1:6379[1]> keys *
(empty list or set)
127.0.0.1:6379[1]> set name lyao
OK
127.0.0.1:6379[1]> get name
"lyao"
#切回0号库
127.0.0.1:6379[1]> select 0
OK
127.0.0.1:6379> get name #这个key只在1号库中才有
(nil)
#redis使用帮助
[iyunv@redis-m ~]# redis-cli
127.0.0.1:6379> help
redis-cli 2.8.9
Type: "help @<group>" to get a list of commands in <group>
"help <command>" for help on <command>
"help <tab>" to get a list of possible help topics
"quit" to exit
127.0.0.1:6379> help set #查询某个命令的使用帮助
SET key value [EX seconds] [PX milliseconds] [NX|XX]
summary: Set the string value of a key
since: 1.0.0
group: string
#查询某类命令
127.0.0.1:6379> help @string
APPEND key value
summary: Append a value to a key
since: 2.0.0
BITCOUNT key [start] [end]
summary: Count set bits in a string
since: 2.6.0
#getset用法
127.0.0.1:6379> set name lyao
OK
127.0.0.1:6379> getset name lyao36843 #先获取name的值,然后将name的值更新为lyao36843
"lyao"
127.0.0.1:6379> get name
"lyao36843"
#批量操作
127.0.0.1:6379> mset name1 tom age 26 sex male
OK
127.0.0.1:6379> mget name1 age sex
1) "tom"
2) "26"
3) "male"
#给值追加内容
127.0.0.1:6379> get name1
"tom"
127.0.0.1:6379> append name1 "teacher"
(integer) 10
127.0.0.1:6379> get name1
"tomteacher"
#连接到6380实例
[iyunv@redis-m ~]# redis-cli -p 6380
127.0.0.1:6380> auth lyao36843
OK
127.0.0.1:6380> set name k1
OK
127.0.0.1:6380> get name
"k1"
127.0.0.1:6380> quit
#连接到6381实例
[iyunv@redis-m ~]# redis-cli -p 6381
127.0.0.1:6381> auth lyao36843
OK
127.0.0.1:6381> set web httpd
OK
127.0.0.1:6381> get web
"httpd"
127.0.0.1:6381> quit