Redis安装
1 下载
wget https://download.redis.io/releases/redis-6.2.1.tar.gz
2 解压
tar -zxvf redis-6.2.1.tar.gz
3 创建快链(软连接)
ln -s redis-6.2.1 redis (或者直接改: mv redis-6.2.1 redis)
4 进入redis目录
cd redis
5 开始编译:
make
5.1 如果编译出错:
解决:查看 gcc 版本:gcc -v
升级版本:
yum -y install centos-release-scl
yum -y install devtoolset-9-gcc devtoolset-9-gcc-c++ devtoolset-9-binutilsscl enable devtoolset-9 bash
再执行make编译,成功安装如下:
6 编译安装:
make install
7 修改配置文件:
vi redis.conf
(按Esc退出编辑模式下: /protected-mode 即可搜索)
protected-mode no # 关闭保护模式
daemonize yes # 守护进程模式开启
#bind 127.0.0.1 # 绑定IP按需修改,bind指定网段远程访问redis,注释就没有限制了。
port 6379 # 端口(单机默认,集群按需修改)
requirepass 123456 # (搜/requirepass foobared)设置Redis密码
8 redis启动:进入src目录
cd src
启动命令:(启动命令+配置文件+后台启动)
./redis-server 配置文件路径 &
(查看进程命令)
ps -ef | grep redis
自带的客户端连接测试:
./redis-cli -h 127.0.0.1 -p 6379
(连接命令+IP+端口号+密码)
9 相关文件解析:
redis-server 服务器启动命令
redis-cli 客户端启动命令
redis.conf redis核心配置文件
redis-check-dump RDB文件检查工具(快照持久化文件)
redis-check-aof AOF文件修复工具
10 开机自启配置
10.1 etc目录下创建redis目录
mkdir /etc/redis
10.2 将redis配置文件拷贝一份到etc/redis目录下
cp redis.conf /etc/redis/6379.conf
10.3 文件赋权,避免文件执行无权限
chmod 755 /etc/redis/6379.conf
10.4 然后进入/redis/utils 目录下有启动脚本redis_init_script 拷贝并改名为redis
cp redis_init_script /etc/init.d/redis
10.5 修改redis脚本
vi /etc/init.d/redis
1
首行添加:
#chkconfig: 2345 10 90
#description: Start and Stop redis
1
2
3
修改相应启动服务位置
EXEC=/opt/shaw0916/redis/src/redis-server
CLIEXEC=/opt/shaw0916/redis/src/redis-cli
1
2
3
如下图:
详细文件:
#!/bin/sh
chkconfig: 2345 10 90
description: Start and Stop redis
Simple Redis init.d script conceived to work on Linux systems
as it does use of the /proc filesystem.
BEGIN INIT INFO
Provides: redis_6379
Default-Start: 2 3 4 5
Default-Stop: 0 1 6
Short-Description: Redis data structure server
Description: Redis data structure server. See https://redis.io
END INIT INFO
REDISPORT=6379
PASSWORD=123456
EXEC=/opt/shaw0916/redis/src/redis-server
CLIEXEC=/opt/shaw0916/redis/src/redis-cli
PIDFILE=/var/run/redis_ R E D I S P O R T . p i d C O N F = " / e t c / r e d i s / {REDISPORT}.pid CONF="/etc/redis/ REDISPORT.pidCONF="/etc/redis/{REDISPORT}.conf"
case “$1” in
start)
if [ -f
P
I
D
F
I
L
E
]
t
h
e
n
e
c
h
o
"
PIDFILE ] then echo "
PIDFILE]thenecho"PIDFILE exists, process is already running or crashed"
else
echo “Redis服务启动…”
$EXEC $CONF
fi
;;
stop)
if [ ! -f
P
I
D
F
I
L
E
]
t
h
e
n
e
c
h
o
"
PIDFILE ] then echo "
PIDFILE]thenecho"PIDFILE does not exist, process is not running"
else
PID=$(cat $PIDFILE)
echo “Stopping …”
$CLIEXEC -a $PASSWORD -p
R
E
D
I
S
P
O
R
T
s
h
u
t
d
o
w
n
w
h
i
l
e
[
−
x
/
p
r
o
c
/
REDISPORT shutdown while [ -x /proc/
REDISPORTshutdownwhile[−x/proc/{PID} ]
do
echo “Redis服务关闭错误…”
sleep 1
done
echo “Redis服务关闭成功…”
fi
;;
*)
echo “Please use start or stop as first argument”
;;
esac
10.6 开启开机自启
chkconfig redis on
10.7 可以先测试一下启动和关闭是否正常
service redis start
service redis stop