redis 学习笔记 | 安装与启动

一、下载

官网地址:https://redis.io/

目前最新稳定版本是:redis-5.0.3

下载链接:http://download.redis.io/releases/redis-5.0.3.tar.gz

官方快速入门文档:https://redis.io/topics/quickstart

如果不想安装 redis,官网提供了个网页版体验模拟器:Web Redis

二、安装

步骤 1: 本文安装环境为:CentOS Linux release 7.4.1708 (Core)

查看 linux 系统版本
1
2
3
cat /etc/redhat-release
或者
cat /proc/version

步骤 2: 将 gz 安装包在/usr/目录下进行解压:

解压 redis 安装包
1
tar xvzf redis-stable.tar.gz

步骤 3: 编译

进入 redis 安装文件夹,执行make命令进行编译:

编译 redis
1
make

编译完成之后,出现下面的信息提示,就表示安装成功:

编译完成之后的日志提示
1
2
3
4
5
6
7
8
    CC redis-benchmark.o
LINK redis-benchmark
INSTALL redis-check-rdb
INSTALL redis-check-aof

Hint: It's a good idea to run 'make test' ;)

make[1]: Leaving directory `/usr/redis-5.0.3/src'

步骤4: 编译完成之后,会在当前目录下生成了一个src文件,进入此目录进行安装操作:

安装redis
1
2
cd src/
make install

提示下面信息,表示安装成功:

安装完成后的日志提示
1
2
3
4
5
6
7
8
9
    CC Makefile.dep

Hint: It's a good idea to run 'make test' ;)

INSTALL install
INSTALL install
INSTALL install
INSTALL install
INSTALL install

并且可以在src目录下看到:redis-serverredis-cli等可执行文件。

另外,在/usr/local/bin/文件目录下已经存在了下面这些可执行文件:

redis 安装之后的 src 文件目录结构
1
2
3
4
5
redis-benchmark  # Redis服务器端启动程序
redis-check-aof # Redis客户端操作工具。也可以用telnet根据其纯文本协议来操作
redis-check-rdb # Redis性能测试工具
redis-cli # 数据修复工具
redis-server # 检查导出工具

注意:

1、make 安装过程如果报找不到 cc 命令错:
/bin/sh: cc: command not found
上面的错误提示表示系统没有 gcc 编译器,需要安装 gcc 编译器: yum install gcc

2、安装 gcc 编译工具之后,继续出现下面错误:
zmalloc.h:50:31: error: jemalloc/jemalloc.h: No such file or directory
则需要将 make 命令改为 make MALLOC=libc 再执行
或者使用 make distclean 进行清理再执行 make 命令

三、启动

src目录下必须先启动服务:

启动 redis
1
redis-server

系统提示了 redis 启动画面,即可表示启动成功:

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@bogon src]# ./redis-server
5688:C 27 Dec 2018 02:00:10.120 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
5688:C 27 Dec 2018 02:00:10.120 # Redis version=5.0.3, bits=64, commit=00000000, modified=0, pid=5688, just started
5688:C 27 Dec 2018 02:00:10.120 # Warning: no config file specified, using the default config. In order to specify a config file use ./redis-server /path/to/redis.conf
5688:M 27 Dec 2018 02:00:10.121 * Increased maximum number of open files to 10032 (it was originally set to 1024).
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 5.0.3 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
| `-._ `._ / _.-' | PID: 5688
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'

5688:M 27 Dec 2018 02:00:10.122 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
5688:M 27 Dec 2018 02:00:10.122 # Server initialized
5688:M 27 Dec 2018 02:00:10.122 # 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.
5688:M 27 Dec 2018 02:00:10.122 # 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.
5688:M 27 Dec 2018 02:00:10.122 * Ready to accept connections

注意:redis 服务默认端口是:6379,此时进程一直停留在了 redis 服务界面,如果要停止就得ctrl + C强行终止程序。

验证启动是否成功:
1、查看是否有redis服务 : ps -ef | grep redis
2、查看端口:netstat -tunpl | grep 6379

四、设置后台运行

回到 redis 安装目录,在此目录下,会存在一个redis.conf配置文件,修改配置信息设置 redis 服务为后台启动,将daemonize的属性由no改为yes

redis 后台启动配置设置
1
2
3
# 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.conf文件需要重启 redis 并读取最新的配置文件。

五、设置快捷启动

将 redis 安装文件目录中的redis.conf文件复制一份到/etc/目录:

复制一份原生配置到系统配置目录
1
cp ./redis.conf /etc/

进入/usr/local/bin目录,启动 redis 服务:

启动 redis
1
2
cd /usr/local/bin
./redis-server /etc/redis.conf

如果启动成功,则提示下面信息:

redis 启动成功的系统日志
1
2
3
4
5
[root@bogon src]# cd /usr/local/bin/
[root@bogon bin]# redis-server /etc/redis.conf
5780:C 27 Dec 2018 02:28:34.575 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
5780:C 27 Dec 2018 02:28:34.576 # Redis version=5.0.3, bits=64, commit=00000000, modified=0, pid=5780, just started
5780:C 27 Dec 2018 02:28:34.576 # Configuration loaded

六、查看 redis 服务启动情况

检查 redis 是否启动
1
ps -ef | grep redis

看到下面信息也表示 redis 服务启动成功:

redis 进程日志
1
2
3
[root@bogon bin]# ps -ef | grep redis
root 5781 1 0 02:28 ? 00:00:00 redis-server 127.0.0.1:6379
root 5786 1229 0 02:29 pts/0 00:00:00 grep --color=auto redis

七、开启防火墙端口

开启端口
1
2
3
4
# 永久开启 6379 端口
firewall-cmd --zone=public --add-port=6379/tcp --permanent
# 重启端口
firewall-cmd --reload

八、使用 redis-cli 连接 redis 服务

/usr/local/bin目录下,存在 redis 的客户端可执行文件:redis-cli

redis-cli 执行命令:redis-cli -h 主机IP -p 端口号 -a 密码
例: redis-cli -h 127.0.0.1 -p 6379 -a root123

使用redis-cli连接 redis 服务,进行 ping-pong 测试及插入 key-value 进行进测试:

客户端连接 redis
1
2
3
4
5
6
[root@bogon bin]# redis-cli -h 127.0.0.1 -p 6379
127.0.0.1:6379> set mykey abc
OK
127.0.0.1:6379> get mykey
"abc"
127.0.0.1:6379> shutdown

注意:
redis-cli连接 redis 服务成功之后,会进入服务 IP + 端口的命令控制台,
停止 redis 服务命令:shutdown
退出当前redis-cli与 redis 服务连接命令:quit

九、设置 redis 服务开机自启动

让 redis 服务开机运行,可以将其添加到rc.local文件,或者将其添加为系统的 service 服务。

1. 配置 rc.local 文件

本文使用rc.local的方式,命令:

配置 redis 启动命令到系统启动文件内
1
echo "/usr/local/bin/redis-server /etc/redis.conf" >> /etc/rc.local

2. 配置到 service 服务

配置到系统服务的基本原理:
系统开机启动时会去加载/etc/init.d/文件目录下面的所有脚本文件,通常每个脚本文件会自定义实现程序的启动;若想将新的程序开机自启动,只需在该目录下添加一个自定义启动程序的脚本,然后设置相应启动规则即可。

如在这里我们在/etc/init.d/文件目录下(和/etc/rc.d/init.d/目录相同)创建一个redis脚本,

比较方便的是:在 redis 安装目录下的utils目录下存在一个:redis_init_script可执行脚本,将其拷贝到  /etc/init.d 下,并修改名字为redis

复制 redis 启动脚本
1
cp /usr/redis-5.0.3/utils/redis_init_script /etc/init.d/redis

在脚本文件中增加启动注释信息(下方的最后两行):

增加系统 chkconfig 注释
1
2
3
4
5
6
#!/bin/sh
#
# Simple Redis init.d script conceived to work on Linux systems
# as it does use of the /proc filesystem.
# chkconfig: 2345 90 10
# description: Redis is a persistent key-value database

说明:redis 服务必须在运行级 2,3,4,5 下被启动或关闭,启动的优先级是 90,关闭的优先级是 10。

修改该脚本中CONF的配置文件路径为本地自定义的配置文件路径:

自定义配置路径
1
CONF="/etc/redis.conf"

配置脚本文件初始化及设置开机自启:

授权脚本并设置开机自启
1
2
chmod +x /etc/init.d/redis
chkconfig redis on
chkconfig 常用命令
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
chkconfig --list			    #列出所有的系统服务
chkconfig --add httpd #增加httpd服务
chkconfig --del httpd #删除httpd服务
chkconfig --level httpd 2345 on #设置httpd在运行级别为2、3、4、5的情况下都是on(开启)的状态
chkconfig --list #列出系统所有的服务启动情况
chkconfig --list mysqld #列出mysqld服务设置情况
chkconfig --level 35 mysqld on #设定mysqld在等级3和5为开机运行服务,--level 35表示操作只在等级3和5执行,on表示启动,off表示关闭
chkconfig mysqld on #设定mysqld在各等级为on,“各等级”包括2、3、4、5等级

等级0表示:表示关机
等级1表示:单用户模式
等级2表示:无网络连接的多用户命令行模式
等级3表示:有网络连接的多用户命令行模式
等级4表示:不可用
等级5表示:带图形界面的多用户模式
等级6表示:重新启动

测试脚本运行情况,启动:service redis start,停止:service redis stop

redis 服务脚本启动或停止
1
2
service redis start
service redis stop

十、设置密码

1. 初始化 redis 密码

redis 没有实现访问控制这个功能,但是它提供了一个轻量级的认证方式,可以编辑redis.conf配置来启用认证。在redis.conf配置文件中找到requirepass

密码配置
1
# requirepass foobared

将其注释符号去掉,并修改成自己的密码,如:requirepass root123即可。

配置修改完毕之后,需要重新启动 redis 服务。

注意:

redis 查询速度极快,auth这种命令每秒能处理 10w 次以上,简单的 redis 的密码极容易被攻击者暴破,因此建议requirepass至少长度 20 位以上,为方便可使用一个特殊串 sha256sum 命令生成64位的无特殊字符串。

1
2
$echo "dfasdERQEWRQEW31341dfadsfadsf" | sha256sum
af970b3691a0774b2a5adae1375e14cd9e5db3591564f0eb789c2324cc02362f

2. 不重启 redis 设置密码

在配置文件中配置requirepass的密码(当 redis 重启时密码依然有效)。

设置密码
1
redis 127.0.0.1:6379> config set requirepass  ed4c39b015b0e46f074dbfd0a9a4ab278f63340a6d640999f25c68a932fef815 

查询密码:

查询密码
1
2
redis 127.0.0.1:6379> config get requirepass
(error) ERR operation not permitted

密码验证:

密码验证
1
2
3
redis 127.0.0.1:6379> auth ed4c39b015b0e46f074dbfd0a9a4ab278f63340a6d640999f25c68a932fef815

OK

再次查询:

查询密码配置
1
2
3
redis 127.0.0.1:6379> config get requirepass
1) "requirepass"
2) "ed4c39b015b0e46f074dbfd0a9a4ab278f63340a6d640999f25c68a932fef815"

注意:如果配置文件中没添加密码,那么 redis 重启后,密码会失效。

3. 登陆带密码的 redis

在登录的时候的时候输入密码:

登录时需要密码
1
redis-cli -h 127.0.0.1 -p 6379 -a ed4c39b015b0e46f074dbfd0a9a4ab278f63340a6d640999f25c68a932fef815

先登陆后验证:

先验证密码再连接
1
2
3
4
redis-cli -p 6379
redis 127.0.0.1:6379> auth ed4c39b015b0e46f074dbfd0a9a4ab278f63340a6d640999f25c68a932fef815

OK

AUTH命令跟其他 redis 命令一样,是不加密的,因此不能完全阻止攻击者窃取密码。

认证层的目标是提供多一层的轻量级保护。如果防火墙或者用来保护 redis 的系统防御外部攻击失败的话,外部用户如果没有通过密码认证还是无法访问 redis 服务。

十一、禁用或重命名危险命令

攻击者常利用config / save两个命令完成 redis 攻击 (因为 redis 无用户权限限制),因此建议对这两个危险的命令,使用rename配置项进行禁用或重命名,这样外部不了解重命名规则攻击者,就不能执行这类命令。

redis.conf配置文件禁用FLUSHDBFLUSHALL两个命令,或者重命名CONFIGSHUTDOWN命令(添加一个特殊的后缀)。这样 redis 服务启动后,只能运行重命名之后的命令,不能执行原始的CONFIG命令:

禁用 redis 命令
1
2
3
4
5
6
7
# It is also possible to completely kill a command by renaming it into
# an empty string:
#
rename-command CONFIG CONFIG_b9fc8327c4dee7
rename-command SHUTDOWN SHUTDOWN_b9fc8327c4dee7
rename-command FLUSHDB "" #禁用此命令
rename-command FLUSHALL "" #禁用此命令

注意:如果重命名命令之后,再启动 redis 服务报下面错误:

重命名命令之后提示错误
1
2
3
DENIED Redis is running protected mode because protected mode is enabled,
no bind address was specified, no authentication password is requested to clients.
In this mode connections are only accepted from the loopback interface.

可以参见官方文档解释:http://redis.io/topics/persistence

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
############################## APPEND ONLY MODE ###############################

# 默认情况下,Redis是异步的把数据导出到磁盘上。这种模式在很多应用里已经足够好,但Redis进程
# 出问题或断电时可能造成一段时间的写操作丢失(这取决于配置的save指令)。
#
# AOF是一种提供了更可靠的替代持久化模式,例如使用默认的数据写入文件策略(参见后面的配置)
# 在遇到像服务器断电或单写情况下Redis自身进程出问题但操作系统仍正常运行等突发事件时,Redis
# 能只丢失1秒的写操作。
#
# AOF和RDB持久化能同时启动并且不会有问题。
# 如果AOF开启,那么在启动时Redis将加载AOF文件,它更能保证数据的可靠性。
#
# 请查看 http://redis.io/topics/persistence 来获取更多信息.

appendonly no

# 纯累加文件名字(默认:"appendonly.aof")

appendfilename "appendonly.aof"

把配置改了appendonly的 yes 改为 no,重启 redis 服务即可。

updated updated 2019-01-01 2019-01-01
本文结束感谢阅读

本文标题:redis 学习笔记 | 安装与启动

本文作者:

微信公号:木鲸鱼 | woodwhales

原始链接:https://woodwhales.cn/2019/01/01/008/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

0%