Redis编译源码安装(一看就会)

操作系统:CentOS Linux release 7.6.1810 (Core) 
所需安装包:redis-4.0.9.tar.gz
下载地址:http://download.redis.io/releases/redis-4.0.9.tar.gz
安装编译工具: yum install gcc gcc-c++ 

1、解压安装包

[root@Node01 ~]# tar -zxvf redis-4.0.9.tar.gz 

2、进行编译安装

[root@Node01 ~]# cd redis-4.0.9

[root@Node01 redis-4.0.9]# make

编译之后检查一下。

[root@Node01 redis-4.0.9]# echo $?
0

看到输出的结果为“0”就说明编译无错误。

3、创建相对应的文件夹,并cp主要配置文件、主要程序

[root@Node01 ~]# mkdir -p /usr/local/redis

[root@Node01 ~]# cp /root/redis-4.0.9/src/redis-server /usr/local/redis/   ###服务端的程序

[root@Node01 ~]# cp /root/redis-4.0.9/src/redis-cli /usr/local/redis/         ###客户端的程序

[root@Node01 ~]# cp /root/redis-4.0.9/redis.conf /usr/local/redis/            ###redis的主配置文件

[root@Node01 ~]# ls /usr/local/redis/

redis-cli  redis.conf  redis-server

4、修改主配置文件

[root@Node01 ~]# sed -i '/^bind 127.0.0.1$/s/127.0.0.1/10.124.241.11/g' /usr/local/redis/redis.conf 

[root@Node01 ~]# sed -i '/protected-mode/s/yes/no/g' /usr/local/redis/redis.conf   ###关闭redis保护模式

[root@Node01 ~]# sed -i '/daemonize/s/no/yes/g' /usr/local/redis/redis.conf   ###允许redis后台启动

[root@Node01 ~]# sed -i '/requirepass/s/foobared/123456/g' /usr/local/redis/redis.conf  ###设置redis的登录密码为123456

[root@Node01 ~]# sed -i '/requirepass 123456/s/^#//g' /usr/local/redis/redis.conf      ###开启redis使用密码登录

5、编写启动脚本

[root@Node01 ~]#cat <<END >>/etc/init.d/redis

#!/bin/sh

# chkconfig: 2345 80 90

# description: Start and Stop redis

#PATH=/usr/local/bin:/sbin:/usr/bin:/bin

REDISPORT=6379

EXEC=/usr/local/redis/redis-server

REDIS_CLI=/usr/local/redis/redis-cli

PIDFILE=/var/run/redis_6379.pid

CONF="/usr/local/redis/redis.conf"

AUTH="123123"

LISTEN_IP=\$(netstat -utpln |grep redis-server |awk '{print \$4}'|awk -F':' '{print \$1}')

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 -h \$LISTEN_IP -p \$REDISPORT -a \$AUTH 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

END
[root@Node01 ~]# chmod 755 /etc/init.d/redis
[root@Node01 ~]# chkconfig --add redis

6、启动redis

[root@Node01 ~]# /etc/init.d/redis start
/etc/init.d/redis: line 11: netstat: command not found
Starting Redis server...
20713:C 22 Feb 18:46:11.510 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
20713:C 22 Feb 18:46:11.511 # Redis version=4.0.9, bits=64, commit=00000000, modified=0, pid=20713, just started
20713:C 22 Feb 18:46:11.511 # Configuration loaded
Redis is running...

6、登录测试

[root@Node01 ~]# ln -s /usr/local/redis/redis-cli /usr/bin/redis
[root@Node01 ~]# redis -h 10.124.241.11 -p 6379 -a 123456
10.124.241.11:6379> 
10.124.241.11:6379> 
10.124.241.11:6379> set name Redis
OK
10.124.241.11:6379> get name
"Redis"
10.124.241.11:6379> 

「点点赞赏,手留余香」

    还没有人赞赏,快来当第一个赞赏的人吧!
0 条回复 A 作者 M 管理员
    所有的伟大,都源于一个勇敢的开始!
欢迎您,新朋友,感谢参与互动!欢迎您 {{author}},您在本站有{{commentsCount}}条评论