Linux(debian) 安装 Redis 教程

redis 安装
ps: 文档记录大部分来自万能的网络资源,感谢众多前辈大咖的分享,此只是个人记录自己的安装过程

一、概述
内存数据库,key-value 存储系统,是当前比较热门的 NOSQL 系统之一。

二、安装 (linux-debin)
1.Redis 官方下载、编译 make
$ wget http://download.redis.io/releases/redis-4.0.9.tar.gz
$ tar xzf redis-4.0.9.tar.gz
$ cd redis-4.0.9
$ make

2.make 编译后,在 src 目录下,有几个可执行文件
mkreleasehdr.sh
redis-benchmark
redis-check-aof
redis-cli
redis-server

此时已经安装完成,但是我们还方便运维,还是需要部署下

三、部署与配置
1. 方便管理,将 Redis 文件中的 conf 配置文件和常用命令移动到统一文件夹中
/usr/local/redis

创建目录

存放执行脚本

mkdir -p /usr/local/redis/bin

存放配置文件

mkdir -p /usr/local/redis/etc

移动文件到 /usr/local/redis/bin; /usr/local/redis/ect 目录
cd /root/redis-4.0.9
mv redis.conf /usr/local/redis/etc

cd /root/redis-4.0.9/src
mv mkreleasehdr.sh /usr/local/redis/bin
mv redis-benchmark /usr/local/redis/bin
mv redis-check-aof /usr/local/redis/bin
mv redis-cli /usr/local/redis/bin
mv redis-server /usr/local/redis/bin

四、执行 redis-server 脚本,启动 redis 服务
1. 前台启动 redis
cd /usr/local/redis/bin/
./redis-server
18847:C 28 May 19:14:10.117 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
18847:C 28 May 19:14:10.118 # Redis version=4.0.9, bits=64, commit=00000000, modified=0, pid=18847, just started
18847:C 28 May 19:14:10.118 # Warning: no config file specified, using the default config. In order to specify a config file use ./redis-server /path/to/redis.conf
.
.-__ ''-._ _.- .. ‘‘-._ Redis 4.0.9 (00000000/0) 64 bit
.-.-```. ```\/ _.,_ ''-._ ( ' , .-` | `, ) Running in standalone mode |`-._`-...-` __...-.-._|’_.-'| Port: 6379 |-._ ._ / _.-' | PID: 18847-._ -._-./ _.-’ .-’
|-._-.
-.__.-' _.-'_.-'| |-.-._ _.-'_.-' | http://redis.io-. -._-..-‘_.-’ .-’
|-._-.
-.__.-' _.-'_.-'| |-.-._ _.-'_.-' |-. -._-.
.-‘_.-’ _.-’
-._-..-’ _.-’
-._ _.-'-.
.-’

18847:M 28 May 19:14:10.120 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
18847:M 28 May 19:14:10.120 # Server initialized
18847:M 28 May 19:14:10.120 # 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.
18847:M 28 May 19:14:10.120 # 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.
18847:M 28 May 19:14:10.120 * Ready to accept connections

直接执行 redis-server 启动的 Redis 服务,是在前台直接运行的。如果 Lunix 关闭当前会话,则 Redis 服务也随即关闭

^C18847:signal-handler (1527506181) Received SIGINT scheduling shutdown…
18847:M 28 May 19:16:21.363 # User requested shutdown…
18847:M 28 May 19:16:21.363 * Saving the final RDB snapshot before exiting.
18847:M 28 May 19:16:21.366 * DB saved on disk
18847:M 28 May 19:16:21.366 # Redis is now ready to exit, bye bye…

  1. 后台启动 redis 服务
    启动 Redis 服务需要从后台启动,并且指定启动配置文件

编辑 redis.conf 文件,设置 daemonize 为 yes,表示后台运行
cd /usr/local/redis/etc/
vim redis.conf

################################# GENERAL #####################################

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 脚本在重启或停止 redis 时,将会报错,并且不能自动在停止服务前同步数据到磁盘上

/etc/sysctl.conf 加上

#vim /etc/sysctl.conf

vm.overcommit_memory = 1

配置完后执行命令

sysctl vm.overcommit_memory=1

指定配置文件 (redis.conf) 启动 redis 服务
cd /usr/local/redis/bin/
./redis-server /usr/local/redis/etc/redis.conf

18944:C 28 May 19:30:07.549 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
18944:C 28 May 19:30:07.549 # Redis version=4.0.9, bits=64, commit=00000000, modified=0, pid=18944, just started
18944:C 28 May 19:30:07.549 # Configuration loaded

启动成功,查看进程
root@Chao:~# ps -ef | grep redis
root 18945 1 0 19:30 ? 00:00:00 ./redis-server 127.0.0.1:6379
root 18958 15412 0 19:31 pts/0 00:00:00 grep redis

五、将 redis 作为 linux service 启动
systemctl 管理 Redis 启动、停止、开机启动

在 /lib/systemd/system 目录下创建一个脚本文件 redis.service
vim /lib/systemd/system/redis.service

#表示基础信息
[Unit]
#描述
Description=Redis
#在哪个服务之后启动
After=syslog.target network.target remote-fs.target nss-lookup.target

#表示服务信息
[Service]
Type=forking
#和 redis.conf 配置文件中的信息一致
PIDFile=/var/run/redis_6379.pid
#启动服务的命令
ExecStart=/usr/local/redis/bin/redis-server /usr/local/redis/etc/redis.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

#安装相关信息
[Install]
WantedBy=multi-user.target

创建软链接
创建软链接是为了下一步系统初始化时自动启动服务

ln -s /lib/systemd/system/redis.service /etc/systemd/system/multi-user.target.wants/redis.service

刷新配置
刚刚配置的服务需要让 systemctl 能识别,就必须刷新配置

systemctl daemon-reload

修改 profile 文件:

vi /etc/profile

在最后行添加:

export PATH=“$PATH:/usr/local/redis/bin”

然后马上应用这个文件:

source /etc/profile

启动、重启、停止
启动 redis

systemctl start redis

重启 redis

systemctl restart redis

停止 redis

systemctl stop redis

开机自启动
redis 服务加入开机启动

systemctl enable redis

禁止开机启动

systemctl disable redis