目錄
在Linux上操作
一 安裝
1 解壓
[root@localhost redis]# tar zxf redis-3.0.0.tar.gz
[root@localhost redis]# ll
total 1332
drwxrwxr-x. 6 root root 4096 Apr 1 2015 redis-3.0.0
-rw-r--r--. 1 root root 1358081 Aug 28 02:11 redis-3.0.0.tar.gz
2 編譯
進入解壓出的目錄,因爲有makefile,可直接編譯。
[root@localhost redis]# cd redis-3.0.0
[root@localhost redis-3.0.0]# ll
total 144
-rw-rw-r--. 1 root root 25890 Apr 1 2015 00-RELEASENOTES
-rw-rw-r--. 1 root root 53 Apr 1 2015 BUGS
-rw-rw-r--. 1 root root 1439 Apr 1 2015 CONTRIBUTING
-rw-rw-r--. 1 root root 1487 Apr 1 2015 COPYING
drwxrwxr-x. 6 root root 4096 Apr 1 2015 deps
-rw-rw-r--. 1 root root 11 Apr 1 2015 INSTALL
-rw-rw-r--. 1 root root 151 Apr 1 2015 Makefile
-rw-rw-r--. 1 root root 4223 Apr 1 2015 MANIFESTO
-rw-rw-r--. 1 root root 5201 Apr 1 2015 README
-rw-rw-r--. 1 root root 41403 Apr 1 2015 redis.conf
-rwxrwxr-x. 1 root root 271 Apr 1 2015 runtest
-rwxrwxr-x. 1 root root 280 Apr 1 2015 runtest-cluster
-rwxrwxr-x. 1 root root 281 Apr 1 2015 runtest-sentinel
-rw-rw-r--. 1 root root 7109 Apr 1 2015 sentinel.conf
drwxrwxr-x. 2 root root 4096 Apr 1 2015 src
drwxrwxr-x. 10 root root 4096 Apr 1 2015 tests
drwxrwxr-x. 5 root root 4096 Apr 1 2015 utils
[root@localhost redis-3.0.0]# make
解壓過程略……
3安裝
PREFIX=安裝到指定目錄下
make install PREFIX=/usr/local/redis
cd src && make install
make[1]: Entering directory `/root/redis/redis-3.0.0/src'
Hint: It's a good idea to run 'make test' ;)
INSTALL install
INSTALL install
INSTALL install
INSTALL install
INSTALL install
make[1]: Leaving directory `/root/redis/redis-3.0.0/src'
二 服務端
1 前端啓動:
[root@localhost ~]# cd /usr/local/redis/bin
[root@localhost bin]# ll
total 13848
-rwxr-xr-x. 1 root root 4167882 Aug 28 03:28 redis-benchmark
-rwxr-xr-x. 1 root root 16463 Aug 28 03:28 redis-check-aof
-rwxr-xr-x. 1 root root 37695 Aug 28 03:28 redis-check-dump
-rwxr-xr-x. 1 root root 4256652 Aug 28 03:28 redis-cli
lrwxrwxrwx. 1 root root 12 Aug 28 03:28 redis-sentinel -> redis-server
-rwxr-xr-x. 1 root root 5686485 Aug 28 03:28 redis-server
[root@localhost bin]# ./redis-server
啓動後的樣子
[root@localhost bin]# ./redis-server
5877:C 28 Aug 03:42:07.864 # Warning: no config file specified, using the default config. In order to specify a config file use ./redis-server /path/to/redis.conf
5877:M 28 Aug 03:42:07.865 * Increased maximum number of open files to 10032 (it was originally set to 1024).
5877:M 28 Aug 03:42:07.866 # Warning: 32 bit instance detected but no memory limit set. Setting 3 GB maxmemory limit with 'noeviction' policy now.
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 3.0.0 (00000000/0) 32 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
| `-._ `._ / _.-' | PID: 5877
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
5877:M 28 Aug 03:42:07.867 # Server started, Redis version 3.0.0
5877:M 28 Aug 03:42:07.867 # 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.
5877:M 28 Aug 03:42:07.867 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
5877:M 28 Aug 03:42:07.867 * DB loaded from disk: 0.000 seconds
5877:M 28 Aug 03:42:07.867 * The server is now ready to accept connections on port 6379
會佔用一個窗口。所以ctrl+c 用後端啓動
2 後端啓動:
從源碼目錄裏複製配置文件到當前的bin目錄下
[root@localhost bin]# cp ~/redis/redis-3.0.0/redis.conf .
[root@localhost bin]# ll
total 13896
-rw-r--r--. 1 root root 18 Aug 28 03:46 dump.rdb
-rwxr-xr-x. 1 root root 4167882 Aug 28 03:28 redis-benchmark
-rwxr-xr-x. 1 root root 16463 Aug 28 03:28 redis-check-aof
-rwxr-xr-x. 1 root root 37695 Aug 28 03:28 redis-check-dump
-rwxr-xr-x. 1 root root 4256652 Aug 28 03:28 redis-cli
-rw-r--r--. 1 root root 41403 Aug 28 03:50 redis.conf
lrwxrwxrwx. 1 root root 12 Aug 28 03:28 redis-sentinel -> redis-server
-rwxr-xr-x. 1 root root 5686485 Aug 28 03:28 redis-server
編輯配置文件
[root@localhost bin]# vim redis.conf
修改如下
daemonize no
改成
daemonize yes
啓動服務
[root@localhost bin]# ./redis-server redis.conf
爲方便,以後開機啓動用如下命令
cd /usr/local/redis/bin && ./redis-server redis.conf
查看進程是否啓動成功
[root@localhost bin]# ps aux|grep redis
root 5908 0.1 0.3 33936 1708 ? Ssl 04:04 0:00 ./redis-server *:6379
root 5912 0.0 0.1 4356 728 pts/1 S+ 04:06 0:00 grep redis
三 客戶端
啓動
[root@localhost bin]# ./redis-cli -h 192.168.25.129 -p 6379
192.168.25.129:6379> ping
PONG
192.168.25.129:6379>
爲方便,以後開機啓動用如下命令
cd /usr/local/redis/bin && ./redis-cli -h 192.168.25.129 -p 6379
關閉redis實例
192.168.25.129:6379> shutdown
not connected> quit
[root@localhost bin]#
四 常用命令
- string
192.168.25.129:6379> set str1 yup1212
OK
192.168.25.129:6379> get str1
"yup1212"
- incr
192.168.25.129:6379> set str2 1212
OK
192.168.25.129:6379> incr str2
(integer) 1213
192.168.25.129:6379> incr no_name
(integer) 1
- decr
192.168.25.129:6379> decr str3 1212
(error) ERR wrong number of arguments for 'decr' command
192.168.25.129:6379> set str3 1212
OK
192.168.25.129:6379> decr str3
(integer) 1211
- hash
192.168.25.129:6379> hset hash1 name yup1212
(integer) 1
192.168.25.129:6379> hget hash1 name
"yup1212"
- 有效期
-1表示永久,-2表示不存在,整數表示正在倒計時
192.168.25.129:6379> ttl str1
(integer) -1
192.168.25.129:6379> ttl str12
(integer) -2
192.168.25.129:6379>
時間到了就直接刪除
192.168.25.129:6379> ttl str1
(integer) 82
192.168.25.129:6379> ttl str1
(integer) 78
192.168.25.129:6379> ttl str1
(integer) 77
192.168.25.129:6379> ttl str1
(integer) -2
- 查看
全部鍵
192.168.25.129:6379> keys *
1) "no_name"
2) "hash1"
3) "str3"
4) "str2"
在Java上操作
完整的過程如下
import redis.clients.jedis.Jedis;
……
(略)
……
public void testJedis() {
//創建一個jedis對象,需要指定服務的ip和端口號
Jedis jedis = new Jedis("192.168.25.129", 6379);
//操作數據庫
//存
jedis.set("jedis-key", "yup1212");
//取
String result = jedis.get("jedis-key");
System.out.println(result);
//關閉jedis
jedis.close();
}
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
……
……
……
public void testJedisPool() throws Exception {
//創建一個數據庫連接池對象(單例),需要指定服務的ip和端口號
JedisPool jedisPool = new JedisPool("192.168.25.129", 6379);
//從連接池中獲得連接
Jedis jedis = jedisPool.getResource();
//使用Jedis操作數據庫(方法級別使用)
String result = jedis.get("jedis-key");
System.out.println(result);
//一定要關閉Jedis連接
jedis.close();
//系統關閉前關閉連接池
jedisPool.close();
}
在Python上操作
在windows上操作
安裝 redis-desktop-manager-0.7.9.809.exe