redis 安装及基本用法

版权声明: https://blog.csdn.net/yup1212/article/details/82152444

目录

在Linux上操作

一  安装

二 服务端

三 客户端

四 常用命令

在Java上操作

在Python上操作

在windows上操作



在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

猜你喜欢

转载自blog.csdn.net/yup1212/article/details/82152444