介绍安装以及简单的配置。

Redis 安装

下载安装,进官网:https://redis.io/

建议将解压目录放到 /opt 下:

1
2
3
[root@AndyCentOS7Basic ~]# wget http://download.redis.io/releases/redis-5.0.5.tar.gz
[root@AndyCentOS7Basic ~]# tar xzf redis-5.0.5.tar.gz
[root@AndyCentOS7Basic ~]# mv redis-5.0.5 /opt/

参照官网,安装步骤:

1
2
[root@AndyCentOS7Basic ~]# cd /opt/redis-5.0.5/
[root@AndyCentOS7Basic ~]# make

make test

1
2
3
4
5
6
7
8
9
10
[root@localhost redis-5.0.7]# make test
cd src && make test
make[1]: Entering directory `/root/redis-5.0.7/src'
CC Makefile.dep
make[1]: Leaving directory `/root/redis-5.0.7/src'
make[1]: Entering directory `/root/redis-5.0.7/src'
You need tcl 8.5 or newer in order to run the Redis test
make[1]: *** [test] Error 1
make[1]: Leaving directory `/root/redis-5.0.7/src'
make: *** [test] Error 2

解决:

1
yum install -y tcl

Redis 运行

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
[root@AndyCentOS7Basic ~]# /opt/redis-5.0.5/src/redis-server
6105:C 30 May 2019 08:26:04.160 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
6105:C 30 May 2019 08:26:04.160 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=6105, just started
6105:C 30 May 2019 08:26:04.160 # Warning: no config file specified, using the default config. In order to specify a config file use /opt/redis-5.0.5/src/redis-server /path/to/redis.conf
6105:M 30 May 2019 08:26:04.161 * Increased maximum number of open files to 10032 (it was originally set to 1024).
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 5.0.5 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
| `-._ `._ / _.-' | PID: 6105
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'

6105:M 30 May 2019 08:26:04.161 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
6105:M 30 May 2019 08:26:04.162 # Server initialized
6105:M 30 May 2019 08:26:04.162 # 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.
6105:M 30 May 2019 08:26:04.162 # 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.
6105:M 30 May 2019 08:26:04.162 * Ready to accept connections

以后台程序方式运行:

1
[root@AndyCentOS7Basic ~]# /opt/redis-5.0.5/src/redis-server &

检查后台进程是否正在运行

没有运行:

1
2
[root@AndyCentOS7Basic ~]# ps -ef |grep redis
root 1726 1707 0 09:01 pts/1 00:00:00 grep --color=auto redis

运行后:

1
2
3
[root@AndyCentOS7Basic ~]# ps -ef |grep redis
root 6105 1998 0 08:26 pts/2 00:00:00 /opt/redis-5.0.5/src/redis-server *:6379
root 6127 6109 0 08:27 pts/3 00:00:00 grep --color=auto redis

检测6379端口是否在监听

1
2
3
[root@AndyCentOS7Basic ~]# netstat -lntp | grep 6379
tcp 0 0 0.0.0.0:6379 0.0.0.0:* LISTEN 6130/redis-server *
tcp6 0 0 :::6379 :::* LISTEN 6130/redis-server *

退出redis服务器

1
2
3
4
5
6
[root@AndyCentOS7Basic ~]# /opt/redis-5.0.5/src/redis-cli shutdown
6130:M 30 May 2019 09:14:18.481 # User requested shutdown...
6130:M 30 May 2019 09:14:18.481 * Saving the final RDB snapshot before exiting.
6130:M 30 May 2019 09:14:18.548 * DB saved on disk
6130:M 30 May 2019 09:14:18.548 # Redis is now ready to exit, bye bye...
[1]+ Done /opt/redis-5.0.5/src/redis-server

因为Redis可以妥善处理SIGTERM信号,所以直接 kill -9 PID 也是可以的。

1
2
3
4
5
6
7
[root@AndyCentOS7Basic ~]# ps -ef |grep redis
root 6199 1998 0 09:20 pts/2 00:00:00 /opt/redis-5.0.5/src/redis-server *:6379
root 6205 1998 0 09:22 pts/2 00:00:00 grep --color=auto redis
[root@AndyCentOS7Basic ~]# kill -9 6199
[root@AndyCentOS7Basic ~]# ps -ef |grep redis
root 6207 1998 0 09:22 pts/2 00:00:00 grep --color=auto redis
[1]+ Killed /opt/redis-5.0.5/src/redis-server

redis 设置密码登录后,要用 redis-cli 关闭 redis 服务器,需要 redis-cli -a 密码 shutdown

修改配置项

1
vim /opt/redis-5.0.5/redis.conf
  1. 设置访问需要密码:# requirepass foobared 改为:requirepass andyRedisPass,其中 andyRedisPass 为密码,改为你自己的密码。
  2. bind 127.0.0.1 直接注释掉,改为:# bind 127.0.0.1
  3. daemonize no 改为 daemonize yes,作用是默认启动时为后台启动。

protected-mode 属性

redis3.2 版本后新增 protected-mode 配置,默认是 protected-mode yes,即开启。
设置外部网络连接 redis 服务时,与该配置相关,如下:

  1. 关闭 protected-mode 模式,即改为:protected-mode no,此时外部网络可以直接访问(连接)。
  2. 开启 protected-mode 保护模式,需配置 bind ip 或者设置访问密码才能访问(连接)。

指定配置文件启动

1
2
3
4
5
6
7
8
9
[root@AndyCentOS7Basic ~]# /opt/redis-5.0.5/src/redis-server /opt/redis-5.0.5/redis.conf &
[1] 6209
[root@AndyCentOS7Basic ~]# 6209:C 30 May 2019 09:25:05.539 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
6209:C 30 May 2019 09:25:05.539 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=6209, just started
6209:C 30 May 2019 09:25:05.539 # Configuration loaded
[1]+ Done /opt/redis-5.0.5/src/redis-server /opt/redis-5.0.5/redis.conf
[root@AndyCentOS7Basic ~]# ps -ef |grep redis
root 6210 1 0 09:25 ? 00:00:00 /opt/redis-5.0.5/src/redis-server *:6379
root 6215 1998 0 09:25 pts/2 00:00:00 grep --color=auto redis

开机启动

vim /lib/systemd/system/redis.service

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[Unit]
Description=Redis
After=network.target

[Service]
Type=forking
PIDFile=/var/run/redis_6379.pid
ExecStart=/opt/redis-5.0.7/src/redis-server /opt/redis-5.0.7/redis.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target

官网文档

官方文档

windows 版

下载地址:https://github.com/microsoftarchive/redis/releases

目前2016年7月份发布的 Redis-x64-3.2.100 版本为最新版,下载 Redis-x64-3.2.100.msi 文件,一路默认配置安装即可。安装之后,服务即已自动启动,可以连接。