环境介绍
CentOS8.0 Redis6.0.8
安装步骤
1. 安装gcc依赖
由于 redis 是用 C 语言开发,安装之前必先确认是否安装 gcc 环境(gcc -v),如果没有安装,执行以下命令进行安装。
[root@zdata ~]# yum install -y gcc
Last metadata expiration check: 1:09:08 ago on Tue 25 Oct 2022 01:40:51 PM CST.
Package gcc-8.5.0-4.el8_5.x86_64 is already installed.
Dependencies resolved.
Nothing to do.
Complete!
[root@zdata ~]#
2. 下载并解压安装包
[root@zdata ~]# wget http://download.redis.io/releases/redis-6.0.8.tar.gz
--2022-10-25 14:54:30-- http://download.redis.io/releases/redis-6.0.8.tar.gz
Resolving download.redis.io (download.redis.io)... 45.60.125.1
Connecting to download.redis.io (download.redis.io)|45.60.125.1|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 2247528 (2.1M) [application/octet-stream]
Saving to: ‘redis-6.0.8.tar.gz’
redis-6.0.8.tar.gz 100%[====================================================================================================>] 2.14M 5.88MB/s in 0.4s
2022-10-25 14:54:31 (5.88 MB/s) - ‘redis-6.0.8.tar.gz’ saved [2247528/2247528]
[root@zdata ~]# tar xzf redis-6.0.8.tar.gz
[root@zdata ~]# ls
mysql-5.7.40-linux-glibc2.12-x86_64.tar.gz redis-6.0.8 redis-6.0.8.tar.gz
[root@zdata ~]#
3. cd切换到redis解压目录下,执行编译
[root@zdata ~]# cd redis-6.0.8
[root@zdata redis-6.0.8]# make
4. 安装并指定安装目录
[root@zdata redis-6.0.8]# make install PREFIX=/usr/local/redis
cd src && make install
make[1]: Entering directory '/root/redis-6.0.8/src'
CC Makefile.dep
Hint: It's a good idea to run 'make test' ;)
INSTALL install
INSTALL install
INSTALL install
INSTALL install
INSTALL install
make[1]: Leaving directory '/root/redis-6.0.8/src'
[root@zdata redis-6.0.8]#
5. 启动服务
1). 前台启动
[root@zdata redis-6.0.8]# cd /usr/local/redis/bin/
[root@zdata bin]# ./redis-server
64245:C 25 Oct 2022 15:00:57.188 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
64245:C 25 Oct 2022 15:00:57.188 # Redis version=6.0.8, bits=64, commit=00000000, modified=0, pid=64245, just started
64245:C 25 Oct 2022 15:00:57.188 # Warning: no config file specified, using the default config. In order to specify a config file use ./redis-server /path/to/redis.conf
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 6.0.8 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
| `-._ `._ / _.-' | PID: 64245
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
64245:M 25 Oct 2022 15:00:57.189 # Server initialized
64245:M 25 Oct 2022 15:00:57.189 # 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.
64245:M 25 Oct 2022 15:00:57.189 # 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 madvise > /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 (set to 'madvise' or 'never').
64245:M 25 Oct 2022 15:00:57.189 * Ready to accept connections
2). 后台启动
从 redis 的源码目录中复制 redis.conf 到 redis 的安装目录:
[root@zdata bin]# cp /usr/local/redis-6.0.8/redis.conf /usr/local/redis/bin/
cp: cannot stat '/usr/local/redis-6.0.8/redis.conf': No such file or directory
[root@zdata bin]# cp /root/redis-6.0.8/redis.conf /usr/local/redis/bin/
[root@zdata bin]#
修改 redis.conf 文件,把 daemonize no 改为 daemonize yes:
[root@zdata bin]# vim redis.conf
后台启动:
[root@zdata bin]# ./redis-server redis.conf
[root@zdata bin]# ps -ef|grep redis
root 64378 1 0 15:08 ? 00:00:00 ./redis-server 127.0.0.1:6379
root 64387 59795 0 15:08 pts/0 00:00:00 grep --color=auto redis
[root@zdata bin]#
6. 设置开机启动
添加开机启动服务:
[root@zdata bin]# vi /etc/systemd/system/redis.service
[root@zdata bin]# cat /etc/systemd/system/redis.service
[Unit]
Description=redis-server
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/redis/bin/redis-server /usr/local/redis/bin/redis.conf
PrivateTmp=true
[Install]
WantedBy=multi-user.target
[root@zdata bin]#
注意:ExecStart配置成自己的路径
设置开机启动:
[root@zdata bin]# systemctl daemon-reload
[root@zdata bin]# systemctl start redis.service
[root@zdata bin]# systemctl enable redis.service
Created symlink /etc/systemd/system/multi-user.target.wants/redis.service → /etc/systemd/system/redis.service.
[root@zdata bin]#
创建 redis 命令软链接 :
[root@zdata bin]# ln -s /usr/local/redis/bin/redis-cli /usr/bin/redis
[root@zdata bin]#
7. 服务操作命令
服务操作命令
systemctl start redis.service #启动redis服务
systemctl stop redis.service #停止redis服务
systemctl restart redis.service #重新启动服务
systemctl status redis.service #查看服务当前状态
systemctl enable redis.service #设置开机自启动
systemctl disable redis.service #停止开机自启动