欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

redis 单机安装及基本用法

程序员文章站 2022-03-04 20:00:04
...

目录

在Linux上操作

一  安装

二 服务端

三 客户端

四 常用命令

在Java上操作

在Python上操作

在windows上操作



在Linux上操作

一  安装

1 解压

[aaa@qq.com redis]# tar zxf redis-3.0.0.tar.gz 
[aaa@qq.com 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,可直接编译。

[aaa@qq.com redis]# cd redis-3.0.0
[aaa@qq.com 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
[aaa@qq.com 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 前端启动:

[aaa@qq.com ~]# cd /usr/local/redis/bin
[aaa@qq.com 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
[aaa@qq.com bin]# ./redis-server 

 启动后的样子

[aaa@qq.com 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目录下

[aaa@qq.com bin]# cp ~/redis/redis-3.0.0/redis.conf .
[aaa@qq.com 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

编辑配置文件

[aaa@qq.com bin]# vim redis.conf 

修改如下

daemonize no  
改成
daemonize yes

启动服务

[aaa@qq.com bin]# ./redis-server redis.conf

为方便,以后开机启动用如下命令

cd /usr/local/redis/bin && ./redis-server redis.conf

查看进程是否启动成功

[aaa@qq.com 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

 

三 客户端

启动

[aaa@qq.com 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
[aaa@qq.com 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

redis 单机安装及基本用法

 

redis 单机安装及基本用法

redis 单机安装及基本用法

相关标签: redis