Data4Strategy

——合抱之木,生于毫末;九层之台,起于累土

【原创】CentOS 7 安装redis 5

1、下载redis安装包

cd /softwares/

wget http://download.redis.io/releases/redis-5.0.5.tar.gz

2、解压redis-5.0.5.tar.gz

tar -xzf redis-5.0.5.tar.gz

3、进入解压的redis目录,通过make命令进行编译

cd redis-5.0.5/

make #编译

make test #验证

#当make test显示以下结果时,表示编译成功

clip_image001

4、安装和启动服务

#安装

make install

#将配置文件复制以可执行文件同一目录

cp redis.conf src/

#按默认方式启动 redis-server ,仅监听 127.0.0.1 ,若监听其他 ip 修改为 bind 0.0.0.0

cd src

./redis-server redis.conf

#测试连接

./redis-cli

#输入如下截图命令,得到PONG结果

clip_image002

5、配置redis

#设置可以远程登录

#编辑redis.conf配置文件,注释掉bind 127.0.0.1或者修改为 bind 0.0.0.0

clip_image003

#设置redis服务后台运行

#将daemonize设置为yes

clip_image004

#设置访问密码

#去掉requirepass行的注释或添加一行

clip_image005

#关闭redis服务后重启redis server,再测试

./redis-server redis.conf

./redis-cli

auth 123456

clip_image006

6、设置redis开机启动

1、复制开机启动脚本,在redis目录的utils包下,有一个redis_init_script文件,我们复制到/etc/init.d/目录下,并将文件名改为redis

sudo cp /softwares/redis-5.0.5/utils/redis_init_script /etc/init.d/redis

2、(非必须)修改/etc/redis/redis.conf文件名为/etc/redis/6380.conf,此修改非必须,不修改的话可以在步骤3指定配置文件路径

sudo mv /etc/redis/redis.conf /etc/redis/6380.conf

3、编辑/etc/init.d/redis启动脚本,脚本说明如下:

clip_image007


#!/bin/sh
#
# 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
EXEC=/softwares/redis-5.0.5/src/redis-server
CLIEXEC=/softwares/redis-5.0.5/src/redis-cli

PIDFILE=/var/run/redis_${REDISPORT}.pid
CONF="/softwares/redis-5.0.5/src/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
                 ;;
         stop)
                 if [ ! -f $PIDFILE ]
                 then
                                 echo "$PIDFILE does not exist, process is not running"
                 else
                                 PID=$(cat $PIDFILE)
                                 echo "Stopping ..."
                                 $CLIEXEC -p $REDISPORT -a 123456 shutdown
                                 while [ -x /proc/${PID} ]
                                 do
                                         echo "Waiting for Redis to shutdown ..."
                                         sleep 1
                                 done
                                 echo "Redis stopped"
                 fi
                 ;;
         *)
                 echo "Please use start or stop as first argument"
                 ;;
esac

7、赋权,加入开机启动服务

#设置文件redis的权限,让Linux可以执行

chmod 755 /etc/init.d/redis

#开启服务自启动

chkconfig redis on

#查看是否加入成功

chkconfig --list

#启动测试,如果能顺利启动则证明可以了,可以reboot测试下是否可以主动启动起来

sudo systemctl start redis


8、安全提示

要修改端口号,设置强密码,redis存在漏洞,我曾经被挖矿程序利用漏洞攻击了,导致CPU等全线飙升,服务器重装了3次。


如果您觉得此文章对您有帮助,请点击右下方【推荐】让更多人看到,thanks!

posted @ 2019-11-11 15:50  John.Xiong  阅读(876)  评论(0编辑  收藏  举报