0%

CentOS7编译安装Redis

基于 CentOS7.5操作系统编译安装Redis,在这里可以找到全部的Redis版本源代码:https://download.redis.io/releases/,本次编译选择的是 redis-4.0.10的版本。

下载源码并且安装

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 下载并解压
wget http://download.redis.io/releases/redis-4.0.10.tar.gz
tar -xzf redis-4.0.10.tar.gz
cd redis-4.0.10

# 安装编译工具并编译
yum -y install gcc gcc-c++ kernel-devel
make

# 编译完成后安装
make PREFIX=/usr/local/redis install
mkdir /usr/local/redis/etc/
cp redis.conf /usr/local/redis/etc/
cd /usr/local/redis/bin/
cp redis-benchmark redis-cli redis-server /usr/bin/

编辑配置文件 redis.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
vim /usr/local/redis/etc/redis.conf

# yes -> 守护进程的方式运行 no -> 终端开启
daemonize yes

# 客户端闲置多长时间后断开连接,默认为0关闭此功能
timeout 300

# 设置redis日志级别,默认级别:notice
loglevel verbose

# 日志信息就输出到/dev/null
logfile stdout

# 设置密码
requirepass <设置密码>

# 监听ip
bind 127.0.0.1

配置环境变量

1
2
3
4
vim /etc/profile
export PATH="$PATH:/usr/local/redis/bin"

source /etc/profile

编辑启动脚本 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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/bin/bash
#chkconfig: 2345 80 90
# Simple Redis init.d script conceived to work on Linux systems
# as it does use of the /proc filesystem.

PATH=/usr/local/bin:/sbin:/usr/bin:/bin
REDISPORT=6379
EXEC=/usr/local/redis/bin/redis-server
REDIS_CLI=/usr/local/redis/bin/redis-cli

PIDFILE=/var/run/redis.pid
CONF="/usr/local/redis/etc/redis.conf"

case "$1" in
start)
if [ -f $PIDFILE ]
then
echo "$PIDFILE exists, process is already running or crashed"
else
echo "Starting Redis server..."
$EXEC $CONF
fi
if [ "$?"="0" ]
then
echo "Redis is running..."
fi
;;
stop)
if [ ! -f $PIDFILE ]
then
echo "$PIDFILE does not exist, process is not running"
else
PID=$(cat $PIDFILE)
echo "Stopping ..."
$REDIS_CLI -p $REDISPORT SHUTDOWN
while [ -x ${PIDFILE} ]
do
echo "Waiting for Redis to shutdown ..."
sleep 1
done
echo "Redis stopped"
fi
;;
restart|force-reload)
${0} stop
${0} start
;;
*)
echo "Usage: /etc/init.d/redis {start|stop|restart|force-reload}" >&2
exit 1
esac

开启开机时服务自启

1
2
3
4
5
6
7
8
9
10
11
12
13
# 复制脚本文件到init.d目录下
cp redis /etc/init.d/

chmod +x /etc/init.d/redis

# 查看服务列表
chkconfig --list

# 添加服务
chkconfig --add redis

# 配置启动级别
chkconfig --level 2345 redis on

服务开启和关闭

1
2
3
4
5
6
systemctl start redis
systemctl stop redis

ps -el|grep redis

netstat -an|grep 6379
  • 本文作者: Tim
  • 本文链接: https://zouchanglin.cn/3756444068.html
  • 版权声明: 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!