Redis5.0.5单机服务搭建

预先准备一个目录 /opt

1、进入opt 下载并解压

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

2、编译

make

3、安装: PREFIX 指定了安装到哪个目录

make PREFIX=/opt/redis install

4、可以进入/opt/redis/bin目录查看安装好的redis

[root@hello opt]# cd /opt/redis/bin
[root@hello bin]# ll
total 32736
-rwxr-xr-x 1 root root 4366624 Aug 29 11:27 redis-benchmark
-rwxr-xr-x 1 root root 8111832 Aug 29 11:27 redis-check-aof
-rwxr-xr-x 1 root root 8111832 Aug 29 11:27 redis-check-rdb
-rwxr-xr-x 1 root root 4806848 Aug 29 11:27 redis-cli
lrwxrwxrwx 1 root root      12 Aug 29 11:27 redis-sentinel -> redis-server
-rwxr-xr-x 1 root root 8111832 Aug 29 11:27 redis-server
[root@hello bin]# 

redis-benchmark:是官方自带的Redis性能测试工具;

当AOF文 件或者RDB文件出现语法错误时,可以使用redis-check-aof或者redis- check-rdb修复;

redis-cli是客户端命令行工具;

可以通过命令redis-cli- h {host} -p {port}连接到指定Redis服务器;

redis-sentinel是Redis哨兵启动程序;

redis-server是Redis服务端启动程序。

5、redis的解压目录redis.conf文件,是redis服务启动的模板配置文件。

下面列举了该文章涉及到的几个配置

配置项 描述 默认值
bind 绑定的网口ip 可配置多个 127.0.0.1
port Redis服务的启动端口 6379
daemoniz Redis服务是否后台运行 【yes为开启】 no
pidfile Redis服务PID寄存文件 /var/run/redis_6379.pid
logfile Redis服务输出日志文件 ""
dir Redis持久化文件的输出目录 ./,也就是当前目录
requirepass Redis服务数访问认证密码

对于bind要绑定的ip可以通过ifconfig查看,当配置了10.105.108.39 可通过其他机器访问。

创建/opt/redis/conf目录,将redis.conf 拷贝到该目录下,并重命名为6381.conf【6381为redis启动监听的端口号】。

[root@hello redis-5.0.5]# mkdir -p /opt/redis/conf
[root@hello redis-5.0.5]# cp /opt/redis-5.0.5/redis.conf /opt/redis/conf/6381.conf

创建下面几个目录

mkdir -p /opt/redis/db/6381 #存放redis持久化文件
mkdir -p /opt/redis/log #存放redis的日志文件

修改6381.conf配置文件的下面几个选项

bind 127.0.0.1 10.105.108.39
port 6381
daemonize yes
logfile "/opt/redis/log/6381.log"
dir /opt/redis/db/6381/
requirepass 123

6、启动redis

[root@hello redis-5.0.5]# /opt/redis/bin/redis-server /opt/redis/conf/6381.conf 

[root@hello redis-5.0.5]# ps -ef|grep redis

root     25689     1  0 12:28 ?        00:00:00 /opt/redis/bin/redis-server 127.0.0.1:6381

root     25699  4644  0 12:28 pts/0    00:00:00 grep --color=auto redis

如果启动失败,可以查看 /opt/redis/log/6381.log文件,分析失败的原因

7、通过客户端连接redis

[root@hello redis-5.0.5]# /opt/redis/bin/redis-cli -h 127.0.0.1 -p 6381
127.0.0.1:6381> auth 123 #授权
OK
127.0.0.1:6381> set 101 zs #设置 key为101 value为zs
OK
127.0.0.1:6381> get 101 #获取key为101 的字符串值
"zs"
127.0.0.1:6381> exit #退出客户端
[root@hello redis-5.0.5]

8、停止服务

[root@hello redis-5.0.5]# /opt/redis/bin/redis-cli -h 127.0.0.1 -p 6381
127.0.0.1:6381> auth 123
OK
127.0.0.1:6381> shutdown
not connected> exit

通过编写shell脚本停止服务可以使用下面这种方式。

 /opt/redis/bin/redis-cli -h 127.0.0.1 -p 6381 -a 123 shutdown

9、脚本

在/opt/redis-5.0.5/utils 目录下 有个redis_init_script的脚本,可以方便的启动redis和停止redis。

创建/opt/redis/script 目录 ,将redis_init_script复制到该目录,并重命名为6381

[root@hello opt]# mkdir -p /opt/redis/script
[root@hello opt]# cp /opt/redis-5.0.5/utils/redis_init_script /opt/redis/script/6381

将脚本文件端口号变量改为6381

REDISPORT=6381

停止服务命令加上 -a 123 密码验证

完整脚本如下:

#!/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
#redis服务器监听的端口
REDISPORT=6381
#服务端所处位置
EXEC=/usr/local/bin/redis-server
#客户端位置
CLIEXEC=/usr/local/bin/redis-cli
#redis的PID文件位置
PIDFILE=/var/run/redis_${REDISPORT}.pid

CONF="/etc/redis/${REDISPORT}.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 ..."
                # -a 123 指定密码为123
                $CLIEXEC -p $REDISPORT -a 123 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

启动

[root@hello opt]# /opt/redis/script/6381 start
Starting Redis server...
[root@hello opt]# ps -ef|grep redis
root     30340     1  0 13:03 ?        00:00:00 /opt/redis/bin/redis-server 127.0.0.1:6381
root     30359  4644  0 13:04 pts/0    00:00:00 grep --color=auto redis
[root@hello opt]# 

停止

[root@hello opt]# /opt/redis/script/6381 stop
Stopping ...
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
Redis stopped
[root@hello opt]# ps -ef|grep redis
root     30544  4644  0 13:05 pts/0    00:00:00 grep --color=auto redis
[root@hello opt]#
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章