Redis的安装和启停
# 10.Redis的安装和启停
Redis支持Linux 和 Windows版本,本文分别介绍下两个操作系统下的安装和启停。Redis没有其他外部依赖,安装过程很简单。
# Windows
# 下载Redis
Redis官方不支持Windows。2011年微软向Redis提交了一个补丁,以使Redis可以在Windows下编译运行,但被Salvatore Sanfilippo拒绝了,原因是在服务器领域上Linux已经得到了广泛的使用,让Redis能在Windows下运行相比而言显得不那么重要,并且Redis使用了如写时复制等很多操作系统相关的特性,兼容 Windows 会耗费太大的精力而影响Redis其他功能的开发。
后来微软为了让Redis支持在Windows下运行,做了一些工作将Redis迁移到了Windows上,使得Windows也能运行Redis。虽然已经很久没更新了,但仅仅用作学习的话还是够用的。
下载地址:Releases · microsoftarchive/redis · GitHub (opens new window)
解压后内容如下:
我们重点关注如下文件:
-
redis.windows.conf
:配置文件 -
redis-server.exe
:Redis 服务器端,双击即可启动Redis -
redis-cli.exe
:Redis 的客户端,全称Redis Command Line Interface,可以连接到Redis服务器,然后输入相关命令操作Redis。
其他可执行文件了解即可,用到再说:
文件名 | 说明 |
---|---|
redis-server | Redis服务器 |
redis-cli | Redis命令行客户端 |
redis-benchmark | Redis性能测试工具 |
redis-check-aof | AOF文件修复工具 |
redis-check-dump | RDB文件检查工具 |
# 启动Redis
我们双击redis-server.exe
:
[139888] 27 Sep 22:19:24.256 # Warning: no config file specified, using the default config. In order to specify a config file use D:\Projects\Redis-x64-3.2.100\redis-server.exe /path/to/redis.conf
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 3.2.100 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
| `-._ `._ / _.-' | PID: 139888
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
[139888] 27 Sep 22:19:24.259 # Server started, Redis version 3.2.100
[139888] 27 Sep 22:19:24.259 * The server is now ready to accept connections on port 6379
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
可以看到Redis正常启动了。第一行首先告诉我们没有指定配置文件(no config file specified),将使用默认的配置。如果想要指定配置文件,格式为:
redis-server.exe 配置文件路径
接下来Redis告诉我们,运行的版本是Redis 3.2.100,64bit模式;
最后是Redis已经启动完成,准备好接受请求了(6379端口)
# 关闭Redis
要关闭Redis,只需关掉窗口即可
# 命令行启停Redis
除了通过鼠标双击之外,我们还可以通过命令行来启停Redis服务端。
例如直接在命令行里输入redis-server
或 redis-server.exe
:
D:\Projects\Redis-x64-3.0.504>redis-server
[248764] 25 Apr 21:15:25.008 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 3.0.504 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
| `-._ `._ / _.-' | PID: 248764
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
[248764] 25 Apr 21:15:25.011 # Server started, Redis version 3.0.504
[248764] 25 Apr 21:15:25.011 * The server is now ready to accept connections on port 6379
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
停止Redis:可以关闭cmd窗口,或按下Ctrl + C快捷键,该快捷键是用来停止正在运行的命令的,当然也可以停止Redis
# 查看版本版本
如果我们想查看Redis的版本,但不想启动Redis,可以通过如下命令来查看版本号:
redis-server --version
# 或者
redis-server -v
Redis server v=3.0.504 sha=00000000:0 malloc=jemalloc-3.6.0 bits=64 build=a4f7a6e86f2d60b3
2
3
4
5
# 指定配置文件
我们可以指定配置文件:用 cmd 进入到Redis的目录后,输入命令 redis-server.exe redis.windows.conf
redis-server.exe redis.windows.conf
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 3.2.100 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
| `-._ `._ / _.-' | PID: 151004
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
[151004] 27 Sep 22:29:16.995 # Server started, Redis version 3.2.100
[151004] 27 Sep 22:29:16.996 * DB loaded from disk: 0.000 seconds
[151004] 27 Sep 22:29:16.996 * The server is now ready to accept connections on port 6379
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# 启动Redis客户端
我们点击 redis-cli.exe,即可连接到Redis服务端,并可以输入相关的命令操作Redis(注意先启动Redis服务端):可以看到默认连接了本机的6379端口的Redis。
然后,我们就可以输入一些命令,与Redis交互了,例如:
停止Redis客户端:可以关闭该窗口,或者输入命令quit。
除了通过鼠标双击之外,我们还可以通过命令行来启停Redis客户端,例如在cmd里输入redis-cli或者redis-cli.exe
# 通过客户端关闭Redis
Redis的数据都是在内存中的,后续我们会学习如何将数据保存到硬盘中。在关闭Redis的时候,如果强行终止 Redis 进程可能会导致数据丢失。正确停止Redis的方式应该是向Redis发送SHUTDOWN命令,方法为:
$ redis-cli SHUTDOWN
当Redis收到SHUTDOWN命令后,会先断开所有客户端连接,然后根据配置执行持久化,最后完成退出。
Redis可以妥善处理 SIGTERM信号,所以使用 kill Redis 进程的 PID也可以正常结束Redis,效果与发送SHUTDOWN命令一样。
# Linux
在工作中,一般Redis都是部署在Linux环境下的,因为Linux下的安装也很重要。
由于我们使用到了make 安装,因此我们得有gcc工具,没有安装的话请先自行安装,例如Centos可以这样安装:
yum -y install gcc gcc-c++ libstdc++-devel
# 下载和安装Redis
假设我们安装到/opt/redis
目录下,我们先准备下目录:
mkdir /opt/redis
cd /opt/redis
2
下载和解压Redis:这里下载最新的稳定版,并解压
wget http://download.redis.io/redis-stable.tar.gz
tar xzf redis-stable.tar.gz
2
或者从官网下载:Download | Redis (opens new window)
安装Redis:
cd redis-stable
make PREFIX=/opt/redis
make test
make PREFIX=/opt/redis install
2
3
4
第2行,我们编译Redis,并指定目录是在/opt/redis
第3行,我们使用 make test命令测试Redis是否编译正确,如果有问题能提早发现(如果使用的是不稳定版本的Redis时,那么该命令很有必要)
第4行,我们make install 安装Redis,并且指定安装位置为/opt/redis。
安装完成后,我们可以在/opt/redis/bin
目录下看到 Redis相关的可执行文件:
$ pwd
/opt/redis
$ ls bin
redis-benchmark redis-check-aof redis-check-rdb redis-cli redis-sentinel redis-server
2
3
4
5
可以看到比Windows多了一个redis-sentinel文件,这是Sentinel服务器(V2.8版本后增加),后续会讲
# 启动Redis
和Windows下类似,直接执行相应的可执行文件即可:
$ cd /opt/redis/bin
$ ./redis-server
2
运行结果和在Windows下差不多:
相应的,停止Redis服务端、启停Redis客户端也是差不多的,这里不在重复演示了
# 使用包管理器安装Redis
除了手工编译外,还可以使用操作系统中的软件包管理器来安装Redis,例如Centos可以通过yum安装:
yum install redis
但目前大多数软件包管理器中的Redis的版本都较古老。考虑到 Redis 的每次升级都提供了对以往版本的问题修复和性能提升,使用最新版本的 Redis 往往可以提供更加稳定的体验,推荐使用编译安装的方式
# 其他版本
之前我们是下载最新的稳定版,有时候为了兼容旧项目,可能得下载旧版本的,此时我们可以去官网 (opens new window)下载:
关于版本号的选择:Redis约定次版本号(即第一个小数点后的数字)为偶数的版本是稳定版(如2.8版、3.0版),奇数版本是非稳定版(如2.7版、2.9版),生产环境下一般需要使用稳定版本。
# 总结
本文我们讲解了下Redis在Windows和Linux下的安装,后续我们会讲解Redis的一些使用步骤,希望读者能一起试着使用Redis,所以请先安装好Redis,再继续学习Redis。
随着Redis的更新,安装步骤可能会过时,最新的安装步骤,毫无疑问得去官网 (opens new window)看啦:
have fun.