Redis介绍与安装

来源:互联网 发布:俄罗斯手机聊天软件 编辑:程序博客网 时间:2024/06/06 09:01

Redis介绍

Redis是Remote Dictionary Server(远程字典服务)的缩写,它是一个开源(BSD许可)的内存中的数据结构存储系统,它可以用作数据库、缓存和消息中间件等。

Redis以字典结构存储数据,并允许其他应用通过TCP协议读写字典中的内容,是一个开源的高性能键值对数据库。支持的键值数据类型有:

  • 字符串类型(strings)
  • 散列类型(hashes)
  • 列表类型(lists)
  • 集合类型(sets)
  • 有序集合类型(sorted sets)

Redis数据库中的所有数据都存储在内存中,也提供了对持久化的支持,可以将内存中的数据异步写入硬盘中,同时不影响继续提供服务。

Redis可以为每个键设置生存时间(Time To Live,TTL),生存时间到期后键会自动被删除,加上其出色的性能以及支持持久化和丰富的数据类型,所以Redis常被用作缓存系统。作为缓存系统,Redis还可以限定数据占用的最大内存空间,在数据达到空间限制后可以按照一定的规则自动淘汰不需要的键。

另外,Redis的列表类型键可以用来实现队列,并且支持阻塞式读取,所以很容易地实现一个高性能的优先级队列。同时Redis还支持“发布/订阅”的消息模式,可以基于此构建聊天室等系统。

Redis版本说明

Redis 版本号采用标准惯例:主版本.次版本.补丁级别,其中,此版本为偶数代表稳定版,如1.2、2.0、2.8等,次版本为奇数代表非稳定版,如1.1、2.1、2.9等。推荐使用稳定版进行开发和生产环境使用。

Redis官方地址:https://redis.io/
最新稳定版:3.2.9,http://download.redis.io/releases/redis-3.2.9.tar.gz
下载地址:https://redis.io/download

Redis推荐使用linux系统,官方没有支持Windows系统,但Microsoft Open Tech组织开发并维护了面向Win64的Windows端口,参考地址:https://github.com/MSOpenTech/redis

Redis的安装

注:这里以centos6.8、Redis3.2.9示例。
由于Redis没有外部依赖,所以安装非常简单。

1、ftp上传或直接下载Redis。

[root@localhost software]# pwd/home/software[root@localhost software]# wget http://download.redis.io/releases/redis-3.2.9.tar.gz

2、解压下载的redis安装包

[root@localhost software]# tar xzf redis-3.2.9.tar.gz [root@localhost software]# lltotal 1516drwxrwxr-x. 6 root root    4096 May 17 08:39 redis-3.2.9-rw-r--r--. 1 root root 1547695 May 17 08:40 redis-3.2.9.tar.gz

3、进入解压后的redis文件夹,编译redis

[root@localhost software]# cd redis-3.2.9[root@localhost redis-3.2.9]# make

到此为止,redis已经安装好了。
编译之后会在redis-3.2.9/src目录下生成redis-server、redis-cli等可执行文件。可执行文件说明:

  • redis-server:Redis服务器
  • redis-cli:Redis命令行客户端
  • redis-benchmark:Redis性能测试工具
  • redis-check-aof:AOF文件修复工具
  • redis-check-dump:RDB文件检查工具

验证redis安装

1、启动redis,可以看到以下内容

[root@localhost redis-3.2.9]# pwd/home/software/redis-3.2.9[root@localhost redis-3.2.9]# src/redis-server 6321:C 13 Jun 19:47:03.406 # Warning: no config file specified, using the default config. In order to specify a config file use src/redis-server /path/to/redis.conf6321:M 13 Jun 19:47:03.408 * Increased maximum number of open files to 10032 (it was originally set to 1024).                _._                                                             _.-``__ ''-._                                                   _.-``    `.  `_.  ''-._           Redis 3.2.9 (00000000/0) 64 bit  .-`` .-```.  ```\/    _.,_ ''-._                                    (    '      ,       .-`  | `,    )     Running in standalone mode |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379 |    `-._   `._    /     _.-'    |     PID: 6321  `-._    `-._  `-./  _.-'    _.-'                                    |`-._`-._    `-.__.-'    _.-'_.-'|                                   |    `-._`-._        _.-'_.-'    |           http://redis.io          `-._    `-._`-.__.-'_.-'    _.-'                                    |`-._`-._    `-.__.-'    _.-'_.-'|                                   |    `-._`-._        _.-'_.-'    |                                    `-._    `-._`-.__.-'_.-'    _.-'                                         `-._    `-.__.-'    _.-'                                                 `-._        _.-'                                                         `-.__.-'                                               6321:M 13 Jun 19:47:03.447 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.6321:M 13 Jun 19:47:03.448 # Server started, Redis version 3.2.96321:M 13 Jun 19:47:03.449 # 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.6321:M 13 Jun 19:47:03.449 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.6321:M 13 Jun 19:47:03.449 * The server is now ready to accept connections on port 6379

2、另开一个命令窗口,打开redis客户端

[root@localhost src]# pwd/home/software/redis-3.2.9/src[root@localhost src]# ./redis-cli 127.0.0.1:6379> pingPONG127.0.0.1:6379> 

上面ping命令收到了服务器的响应PONG,说明服务器正常启动了。

让redis作为服务后台运行

[root@localhost redis-3.2.9]# src/redis-server 

Redis服务器默认使用的是6379端口,也可以通过--port参数自定义端口号,如使用6380端口:

[root@localhost redis-3.2.9]# src/redis-server --port 6380

默认情况下,Redis启动之后,当前session连接就被占用了,不能再进行其他操作,所以上面验证安装时,启动客户端是又开启了一个命令窗口。
如果想让redis以后台进程作为服务启动,只需要改一下配置文件即可:

[root@localhost redis-3.2.9]# vim redis.conf 

打开redis.conf配置文件,找到daemonize参数,将其设置为yes:

# By default Redis does not run as a daemon. Use 'yes' if you need it.# Note that Redis will write a pid file in /var/run/redis.pid when daemonized.daemonize yes

再次启动redis时,redis将以后台服务的方式运行。

[root@localhost redis-3.2.9]# src/redis-server redis.conf 

停止Redis

Redis有可能正在将内存中的数据同步到硬盘中,强行终止Redis进程可能会导致数据丢失。正确的方式是向Redis发送shutdown命令:

[root@localhost redis-3.2.9]# src/redis-cli shutdown

当Redis收到shutdown命令后,会先断开所有客户端连接,然后根据配置执行持久化,最后完成退出。

另外,通过“kill redis进程pid”也可以正常结束Redis,和发送shutdown命令一样:

[root@localhost redis-3.2.9]# ps -ef|grep redis-serverroot      6689     1  0 20:49 ?        00:00:00 src/redis-server 127.0.0.1:6379root      6694  2566  0 20:49 pts/0    00:00:00 grep redis-server[root@localhost redis-3.2.9]# kill 6689

或者直接通过pid进程文件

[root@localhost redis-3.2.9]# kill `cat /var/run/redis_6379.pid`

进程文件/var/run/redis_6379.pid是在redis.conf配置文件通过pidfile参数配置的。

原创粉丝点击