redis 在 windows 中的安装

redis 官方并没有提供 redis 的 windows 版本。但是微软提供了 redis 的 windows 版本。有2.8和3.0版本。3.0版本支持集群。

微软提供的redis的windows版本地址
https://github.com/microsoftarchive/redis/releases

下载后解压

启动

# 指定配置文件
D:\chengxu\redis-3.0.504-x64>redis-server redis.windows.conf
                _._
           _.-``__ ''-._
      _.-``    `.  `_.  ''-._           Redis 3.0.504 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 10072
  `-._    `-._  `-./  _.-'    _.-'
 |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |           http://redis.io
  `-._    `-._`-.__.-'_.-'    _.-'
 |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |
  `-._    `-._`-.__.-'_.-'    _.-'
      `-._    `-.__.-'    _.-'
          `-._        _.-'
              `-.__.-'

[10072] 19 Oct 20:51:38.828 # Server started, Redis version 3.0.504
[10072] 19 Oct 20:51:38.832 * The server is now ready to accept connections on port 6379

# 双击redis-cli.exe 打开客户端成功
127.0.0.1:6379> set foo 1
OK

设置服务

Windows Service Documentation.docx 文档中有描述。

安装服务

# 默认服务名为redis
D:\chengxu\redis-3.0.504-x64>redis-server --service-install redis.windows-service.conf --loglevel verbose

# 指定服务名为Redis3.0 端口号为6381
D:\chengxu\redis-3.0.504-x64>redis-server --service-install redis.windows.conf --loglevel verbose --port 6381 --service-name Redis3.0

[12912] 19 Oct 21:04:45.014 # Granting read/write access to 'NT AUTHORITY\NetworkService' on: "D:\chengxu\redis-3.0.504-x64" "D:\chengxu\redis-3.0.504-x64\"
[12912] 19 Oct 21:04:45.014 # Redis successfully installed as a service.

卸载服务

# 不指定服务名,使用默认服务名redis
D:\chengxu\redis-3.0.504-x64>redis-server --service-uninstall
[17268] 19 Oct 21:17:29.102 # Redis service successfully uninstalled

# 卸载前先停止服务
D:\chengxu\redis-3.0.504-x64>redis-server --service-uninstall --service-name redis3.0
[17476] 19 Oct 21:17:50.561 # Redis service successfully uninstalled.

停止服务

# 服务名称如果不指定默认为redis
D:\chengxu\redis-3.0.504-x64>redis-server --service-stop --service-name redis3.0
[12360] 19 Oct 21:23:08.044 # Redis service successfully stopped.

启动服务

# 服务名称如果不指定默认为redis。如果服务已经启动,再次执行会报错。
D:\chengxu\redis-3.0.504-x64>redis-server --service-start --service-name redis3.0
[16536] 19 Oct 21:24:08.587 # Redis service successfully started.

客户端连接

D:\chengxu\redis-3.0.504-x64>redis-cli --help
redis-cli 3.0.504

Usage: redis-cli [OPTIONS] [cmd [arg [arg ...]]]
  -h <hostname>      Server hostname (default: 127.0.0.1).
  -p <port>          Server port (default: 6379).
  -s <socket>        Server socket (overrides hostname and port).
  -a <password>      Password to use when connecting to the server.

实战

D:\chengxu\redis-3.0.504-x64>redis-cli -h localhost -p 6381
localhost:6381> get foo
(nil)
localhost:6381> set foo 1
OK
localhost:6381> exit

bugs

# 没有指定配置文件,将会使用默认的配置。而且是需要密码的,但是我们并不知道密码。
D:\chengxu\redis-3.0.504-x64>redis-server
[1460] 19 Oct 20:31:59.351 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
[1460] 19 Oct 20:31:59.353 # Creating Server TCP listening socket *:6379: bind: No such file or directory

D:\chengxu\redis-3.0.504-x64>netstat -ano |findstr 6379
  TCP    0.0.0.0:6379           0.0.0.0:0              LISTENING       5188
  TCP    [::]:6379              [::]:0                 LISTENING       5188

# 打开客户端,没有认证
127.0.0.1:6379> get foo
(error) NOAUTH Authentication required.
127.0.0.1:6379> auth redis
(error) ERR invalid password
127.0.0.1:6379>
# 先停止服务再卸载服务。不然报错。
D:\chengxu\redis-3.0.504-x64>redis-server --service-uninstall --service-name redis3.0
[14972] 19 Oct 21:18:09.837 # HandleServiceCommands: system error caught. error code=1072, message = DeleteService failed: unknown error
# 如果服务已经启动,执行启动命令将报错。
D:\chengxu\redis-3.0.504-x64>redis-server --service-start --service-name redis3.0
[7304] 19 Oct 21:24:21.685 # HandleServiceCommands: system error caught. error code=1056, message = StartService failed: unknown error
# 找到不到服务名则报错。默认为redis
D:\chengxu\redis-3.0.504-x64>redis-server --service-stop
[4412] 19 Oct 21:22:47.868 # HandleServiceCommands: system error caught. error code=1060, message = OpenService failed: unknown error
01-20 20:41