Copyright 2025 HNCloud Limited.
                    香港联合通讯国际有限公司
                
            
            CentOS 6.5上安装Redis 3 及启动脚本
        
        
            时间 : 2023-01-06 09:39:54
            编辑 : 华纳云
                        阅读量 : 578
        
                
        CentOS 6.5上安装Redis 3 及启动脚本,Redis的强大就不多说了,直接看下面的文章讲解。
第1步:下载、编译、安装
cd /opt
wget http://download.redis.io/releases/redis-3.0.5.tar.gz
tar zxvf redis-3.0.5.tar.gz
cd redis-3.0.5
make && make install第2步:配置,修改默认端口为7963、数据目录、日志目录、后台运行方式等
mkdir /etc/redis
mkdir /var/log/redis
mkdir -p /data/redis
cd /opt/redis-3.0.5
cp redis.conf /etc/redis/7963.conf
vi /etc/redis/7963.conf
——————–
daemonize no
port 6379
logfile “”
pidfile /var/run/redis.pid
# requirepass foobared
dir ./
改成
daemonize yes
port 7963
logfile “/var/log/redis/7963.log”
pidfile /var/run/redis_7963.pid
requirepass 9k3NgZq%gO!W7x-0y=LI
dir /data/redis通过sed快速修改配置文件命令如下:
sed -i “s/daemonize no/daemonize yes/g” /etc/redis/7963.conf
sed -i “s/port 6379/port 7963/g” /etc/redis/7963.conf
sed -i “s/pidfile \/var\/run\/redis.pid/pidfile \/var\/run\/redis_7963.pid/g” /etc/redis/7963.conf
sed -i “s/logfile \”\”/logfile \”\/var\/log\/redis\/7963.log\”/g” /etc/redis/7963.conf
sed -i “s/# requirepass foobared/requirepass 9k3NgZq%gO!W7x-0y=LI/g” /etc/redis/7963.conf
sed -i “s/dir .\//dir \/data\/redis/g” /etc/redis/7963.conf第3步:修改系统内存策略,保证数据完整性
# 设置内存策略
grep vm.overcommit_memory /etc/sysctl.conf# 如果没有输出,就新增一行
echo “vm.overcommit_memory = 1” >> /etc/sysctl.conf# 如果有输出,就修改
sed -i “s/vm.overcommit_memory = 0/vm.overcommit_memory = 1/g” /etc/sysctl.conf# 让配置生效
sysctl -p
第4步:配置启动脚本
cd /opt/redis-3.0.5
cp utils/redis_init_script /etc/init.d/redis
vi /etc/init.d/redis# 第二行插入以下2行
# chkconfig:  2345 90 10
# description:  Simple Redis init.d scrip# 修改默认端口
REDISPORT=6379# 改成
REDISPORT=7963# 如果设置了Redis密码,则需要进行以下修改才能正常通过stop命令停止Redis服务
# 在CONF=”/etc/redis/${REDISPORT}.conf”下面添加一行自动解析配置文件中的密码
PASS=`grep “requirepass ” $CONF | awk {print’$2′}`# 然后关闭指令根据密码是否为空判断是否需要添加密码参数
$CLIEXEC -p $REDISPORT shutdown# 改成
if [ -z “$PASS” ]
then
    $CLIEXEC -p $REDISPORT shutdown
else
    $CLIEXEC -p $REDISPORT -a $PASS shutdown
fi通过sed快速修改启动脚本命令如下:
sed -i “1 a # chkconfig:  2345 90 10” /etc/init.d/redis
sed -i “2 a # description:  Simple Redis init.d scrip” /etc/init.d/redis
sed -i “s/REDISPORT=6379/REDISPORT=7963/g” /etc/init.d/redis# 设置了密码则修改,不设置密码不用修改
sed -i $’13 a PASS=`grep “requirepass ” $CONF | awk {print\’$2\’}`’ /etc/init.d/redis
sed -i “s/PORT shutdown/PORT -a \$PASS shutdown/g” /etc/init.d/redis第5步:添加启动项,并启动
#设置为开机自启动Redis
chkconfig redis on#验证启动项是否设置成功
chkconfig –list redis#启动Redis服务
service redis start#关闭Redis服务
service redis stopRedis默认端口,默认空密码存在严重的安全性问题,所以上面安装过程修改了默认端口也添加了密码管控。
 
                     中国香港服务器
                            中国香港服务器
                         日本服务器
日本服务器
                         美国服务器
美国服务器
                         新加坡服务器
新加坡服务器
                         
                     推荐文章
                推荐文章
             
                         
                             
                 
        