CentOS7 安装 Redis

1、检查安装依赖程序

yum install gcc-c++
yum install -y tcl
yum install wget

2、获取安装文件

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

3、解压文件

tar -xzvf redis-3.2.6.tar.gz
cd redis-3.2.6
make

4、编译安装

make PREFIX=/usr/local/redis install

5、拷贝配置文件

cp /root/redis-3.2.6/redis.conf /usr/local/redis

6 启动redis服务端

vim /usr/local/redis/redis.conf

daemonize no -> daemonize yes
/usr/local/redis/bin/redis-server /usr/local/redis/redis.conf

已经启动!!

ps -ef | grep -i redis
image.png

7 关闭redis

./bin/redis-cli shutdown

8 允许远程访问

8.1 打开6379端口

需要先安装firewalld

firewall-cmd --query-port=6379/tcp #查询端口号6379 是否开启
firewall-cmd --permanent --zone=public --add-port=6379/tcp #永久开放6379端口号
firewall-cmd --permanent --zone=public --remove-port=6379/tcp #永久删除6379端口号
systemctl restart firewalld.service #重新启动  防火墙
firewall-cmd --query-port=6379/tcp
image.png

搞定!

8.2 修改配置文件

之前老是连接不了,奶奶的

vim /usr/local/redis/redis.conf

bind字段默认为: bind 127.0.0.1 这样只能本机访问redis

若允许远程主机访问,可注释掉bind行 或者 将bind 127.0.0.1改为: bind 0.0.0.0

bind 0.0.0.0

重启redis

/usr/local/redis/bin/redis-cli shutdown
/usr/local/redis/bin/redis-server /usr/local/redis/redis.conf

9 redis开机启动

vim /etc/init.d/redis:

# chkconfig: 2345 10 90
# description: Start and Stop redis

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/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 exists, 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

执行权限:

chmod +x /etc/init.d/redis

启动测试:

/etc/init.d/redis start

设置开机自启动:

chkconfig redis on

重启

reboot

推荐阅读更多精彩内容