如何安装Redis


前言

Redis是基于内存的的数据库,和MySQL一样,是C-S架构,在特定的主机和端口启动服务器后,便可以使用Redis客户端对其进行读写。

安装步骤

Windows

下载

Redis作者只发布Linux版本,并不兼容Windows. 微软开放技术小组提供并维护Redis的Windows版本。

Redis Windows版本下载地址: Releases · tporadowski/redis · GitHub

安装

(1)将redis zip解压,拷贝至您想要作为redis的安装目录的文件夹中。解压后如下:

(2)点击redis-server.exe,或者打开powershell,输入.\redis-server.exe启动Redis

PS C:\Users\Liuwe> cd C:\redis
PS C:\redis> .\redis-server.exe
[2192] 27 Dec 21:09:10.802 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
[2192] 27 Dec 21:09:10.802 # Redis version=5.0.14, bits=64, commit=a7c01ef4, modified=0, pid=2192, just started
[2192] 27 Dec 21:09:10.802 # Warning: no config file specified, using the default config. In order to specify a config file use c:\redis\redis-server.exe /path/to/redis.conf
                _._
           _.-``__ ''-._
      _.-``    `.  `_.  ''-._           Redis 5.0.14 (a7c01ef4/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 2192
  `-._    `-._  `-./  _.-'    _.-'
 |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |           http://redis.io
  `-._    `-._`-.__.-'_.-'    _.-'
 |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |
  `-._    `-._`-.__.-'_.-'    _.-'
      `-._    `-.__.-'    _.-'
          `-._        _.-'
              `-.__.-'

[2192] 27 Dec 21:09:10.808 # Server initialized
[2192] 27 Dec 21:09:10.808 * Ready to accept connections

上述的启动方式,使用的是Redis的默认设置,也可以使用指定的配置文件启动Redis,

PS C:\redis> .\redis-server.exe .\redis.windows.conf
[10392] 27 Dec 21:12:03.461 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
[10392] 27 Dec 21:12:03.461 # Redis version=5.0.14, bits=64, commit=a7c01ef4, modified=0, pid=10392, just started
[10392] 27 Dec 21:12:03.461 # Configuration loaded
                _._
           _.-``__ ''-._
      _.-``    `.  `_.  ''-._           Redis 5.0.14 (a7c01ef4/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6666
 |    `-._   `._    /     _.-'    |     PID: 10392
  `-._    `-._  `-./  _.-'    _.-'
 |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |           http://redis.io
  `-._    `-._`-.__.-'_.-'    _.-'
 |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |
  `-._    `-._`-.__.-'_.-'    _.-'
      `-._    `-.__.-'    _.-'
          `-._        _.-'
              `-.__.-'

[10392] 27 Dec 21:12:03.465 # Server initialized
[10392] 27 Dec 21:12:03.465 * DB loaded from disk: 0.000 seconds
[10392] 27 Dec 21:12:03.465 * Ready to accept connections

redis.windows.conf和redis.windows-service.conf是redis的配置文件,配置文件中是与redis相关的配置,如IP,端口,连接密码等,两个文件完全相同,我们配置好其中一个文件然后使用配置文件启动redis即可。另外一个配置文件可以不进行配置,相当于一个原始备份,但是务必记得我们改动的配置是哪个配置文件,不要在启动redis时指定成未更改的配置文件。

配置文件中的重要配置项:

bind 127.0.0.1

port 6379

(4)启动redis服务器后,我们便可以启动redis-cli.exe访问redis.

(5)综上,步骤是:下载压缩包-解压-修改配置文件-使用配置文件启动redis-启动客户端访问redis。只有启动redis后才能读写redis,每次开机我们需要手动启动redis,这样太过麻烦,我们可以将redis设置成服务,让其开机自启。

PS C:\redis> .\redis-server.exe --service-install redis.windows.conf --service-name redis
[7928] 27 Dec 21:20:01.904 # Granting read/write access to 'NT AUTHORITY\NetworkService' on: "C:\redis" "C:\redis\"
[7928] 27 Dec 21:20:01.905 # Redis successfully installed as a service.

最后一个参数是服务的名称,直接命名为redis即可;倒数第二个参数是服务启动时使用的配置文件,这个参数我们千万不能混淆了redis.windows.conf和redis.windows-service.conf.

卸载步骤

  1. 停止redis服务
  2. 卸载redis服务
.\redis-server.exe --service-uninstall --service-name redis
  1. 删除redis安装文件夹
  2. 重启电脑

Linux