CentOS 7 源码编译安装 Redis

  • 内容
  • 评论
  • 相关

1.下载源码并解压

wget http://download.redis.io/releases/redis-6.0.8.tar.gz
tar -xzf redis-6.0.8.tar.gz
cd redis-6.0.8

2.安装依赖包

yum -y install gcc gcc-c++ kernel-devel

等待完成

3.编译安装

make PREFIX=/server/redis install

这里报了如下错误

make[1]: *** [server.o] 错误 1
make[1]: 离开目录“/usr/redis-6.0.1/src”
make: *** [all] 错误 2

解决办法:升级gcc版本

# 查看gcc版本
gcc -v

# 升级到9.1版本
yum -y install centos-release-scl
yum -y install devtoolset-9-gcc devtoolset-9-gcc-c++ devtoolset-9-binutils
scl enable devtoolset-9 bash

*以上为临时启用,如果要长期使用gcc 9.1的话:
echo "source /opt/rh/devtoolset-9/enable" >>/etc/profile

再执行编译:

make PREFIX=/server/redis install
mkdir /server/redis/etc/
cp redis.conf /server/redis/etc/
cd /server/redis/bin/
cp redis-benchmark redis-cli redis-server /usr/bin/

4.更改配置

vim /server/redis/etc/redis.conf

# 修改一下配置
# redis以守护进程的方式运行
# no表示不以守护进程的方式运行(会占用一个终端)  
daemonize yes

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

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

# 设置日志文件的输出方式,如果以守护进程的方式运行redis 默认:"" 
# 并且日志输出设置为stdout,那么日志信息就输出到/dev/null里面去了 
logfile stdout
# 设置密码授权
requirepass <设置密码>
# 监听ip
bind 127.0.0.1

5.配置环境变量

vim /etc/profile
export PATH="$PATH:/server/redis/bin"
# 保存退出

# 让环境变量立即生效
source /etc/profile

*不想查找修改可以在命令行输入以下命令:

sed -i 's/daemonize no/daemonize yes/g' /server/redis/etc/redis.conf &&\
sed -i 's/timeout 0/timeout 300/g' /server/redis/etc/redis.conf &&\
sed -i 's/loglevel notice/loglevel verbose/g' /server/redis/etc/redis.conf &&\
sed -i 's/logfile ""/logfile stdout/g' /server/redis/etc/redis.conf &&\
sed -i '/# bind 127.0.0.1 ::1/a\\bind 127.0.0.1' /server/redis/etc/redis.conf

6.配置启动脚本

vim /etc/init.d/redis
#!/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=/server/redis/bin/redis-server
REDIS_CLI=/server/redis/bin/redis-cli
   
PIDFILE=/var/run/redis.pid
CONF="/server/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

7.开启自启动设置

# 给脚本增加运行权限
chmod +x /etc/init.d/redis

# 查看服务列表
chkconfig --list

# 添加服务
chkconfig --add redis

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

8.启动测试

systemctl start redis   #或者 /etc/init.d/redis start  
systemctl stop redis   #或者 /etc/init.d/redis stop

# 查看redis进程
ps -el|grep redis

# 端口查看
netstat -an|grep 6379

参考资料:

https://segmentfault.com/a/1190000003084917

https://www.cnblogs.com/smail-bao/p/6164132.html

https://www.cnblogs.com/joshua317/p/5635483.html

评论

0条评论

发表评论

电子邮件地址不会被公开。 必填项已用*标注