安装Redis服务端
~$ sudo apt-get install redis-server
服务开启
~$ sudo /etc/init.d/redis-server start
Starting redis-server: redis-server.
服务关闭
~$ sudo /etc/init.d/redis-server stop
Stopping redis-server: redis-server.
服务重启
~$ sudo /etc/init.d/redis-server restart
Stopping redis-server: redis-server.
Starting redis-server: redis-server.
检查redis-server系统进程
~$ ps -aux|grep redis
redis 478 0.0 0.0 110944 1960 ? Ssl 21:29 0:00 /usr/bin/redis-server 127.0.0.1:6379
Boiling 482 0.0 0.0 38620 1108 tty1 S 21:32 0:00 grep --color=auto redis
查看redis-server运行状态
~$ sudo /etc/init.d/redis-server status
redis-server is running
通过redis-cli客户端访问redis-server
~$ redis-cli
127.0.0.1:6379> help
redis-cli 2.8.4
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>
redis的基出操作命令
增加一条记录 key1,然后获取key1的数据为“hello world”
127.0.0.1:6379> set key1 "hello world"
OK
127.0.0.1:6379> get key1
"hello world"
增加一条数据numKey,然后自增
127.0.0.1:6379> set numKey 1
OK
127.0.0.1:6379> incr numKey
(integer) 2
127.0.0.1:6379> incr numKey
(integer) 3
127.0.0.1:6379> get numKey
"3"
增加一条列表数据 listKey
127.0.0.1:6379> lpush listKey a
(integer) 1
# 从左边插入列表
127.0.0.1:6379> lpush listKey b
(integer) 2
# 从右边插入列表
127.0.0.1:6379> rpush listKey c
(integer) 3
# 从左到右打印列表0到3数据
127.0.0.1:6379> lrange listKey 0 3
1) "b"
2) "a"
3) "c"
增加一条哈希数据 hashKey
127.0.0.1:6379> hset hashKey name "John"
(integer) 1
127.0.0.1:6379> hset hashKey email "john@gmail.com"
(integer) 1
127.0.0.1:6379> hget hashKey name
"John1"
127.0.0.1:6379> hgetall hashKey
1) "name"
2) "John"
3) "email"
4) "john@gmail.com"
一次操作多个哈希数据
127.0.0.1:6379> hmset hashKey age 26 sex male height 65kg weight 175cm
OK
127.0.0.1:6379> hmget hashKey name sex
1) "John"
2) "male"
127.0.0.1:6379> hgetall hashKey
1) "name"
2) "John"
3) "email"
4) "john@gmail.com"
5) "age"
6) "26"
7) "sex"
8) "male"
9) "height"
10) "65kg"
11) "weight"
12) "175cm"
查看所有Key
127.0.0.1:6379> keys *
1) "numKey"
2) "key1"
3) "listKey"
4) "hashKey"
删除Key1
127.0.0.1:6379> del key1
(integer) 1
127.0.0.1:6379> keys *
1) "numKey"
2) "listKey"
3) "hashKey"
打开远程访问功能
默认情况,redis只允许访问127.0.0.1,可以通过配置打开远程访问功能
~$ sudo vi /etc/redis/redis.conf
#注释bind
#bind 127.0.0.1
~$ grep "bind" /etc/redis/redis.conf
# interfaces using the "bind" configuration directive, followed by one or
# bind 192.168.1.100 10.0.0.1
# bind 127.0.0.1
然后重启redis-server,就可以远程访问了。
~$ redis-cli -a redis -h 192.168.10.123
192.168.10.123:6379> keys *
1) "numKey"
2) "hashKey"
3) "listKey"