您的位置:金沙游乐场85155 > 大数据库 > Centos下配置Redis开机启动脚本

Centos下配置Redis开机启动脚本

发布时间:2020-04-06 08:04编辑:大数据库浏览(159)

    一、环境介绍

    金沙游乐场85155,1、下载安装

    Redis—master 172.18.8.19Redis—slave 172.18.8.20

    wget http://redis.googlecode.com/files/redis-2.2.13.tar.gz 
    tar -zxf redis-2.2.13.tar.gz 
    cd redis-2.2.13 
    make 
    sudo make install  
    cp redis.conf /etc 
    

    二、redis主的配置

    install的时候,redis的命令会被拷贝到/usr/local/bin下面

    #创建redis数据目录mkdir -p /data0/redis_trade#redis主配置文件root# cat redis_6379.conf |grep -Ev "^$|^#"bind 172.18.8.19protected-mode yesport 6379tcp-backlog 511timeout 0tcp-keepalive 300daemonize yessupervised nopidfile /var/run/redis_6379.pidloglevel noticelogfile "/var/log/redis_6379.log"databases 16save 900 1save 300 10save 60 10000stop-writes-on-bgsave-error yesrdbcompression yesrdbchecksum yesdbfilename dump_6379.rdbdir /data0/redis_tradeslave-serve-stale-data yesslave-read-only yesrepl-diskless-sync norepl-diskless-sync-delay 5repl-disable-tcp-nodelay noslave-priority 100requirepass Allwelltokokappendonly yesappendfilename "appendonly_6379.aof"appendfsync everysecno-appendfsync-on-rewrite noauto-aof-rewrite-percentage 100auto-aof-rewrite-min-size 64mbaof-load-truncated yeslua-time-limit 5000slowlog-log-slower-than 10000slowlog-max-len 128latency-monitor-threshold 0notify-keyspace-events ""hash-max-ziplist-entries 512hash-max-ziplist-value 64list-max-ziplist-size -2list-compress-depth 0set-max-intset-entries 512zset-max-ziplist-entries 128zset-max-ziplist-value 64hll-sparse-max-bytes 3000activerehashing yesrename-command FLUSHALL ZYzv6FOBdwflW2nXrename-command EVAL S9UHPKEpSvUJMMrename-command FLUSHDB D60FPVDJuip7gy6lclient-output-buffer-limit normal 0 0 0client-output-buffer-limit slave 256mb 64mb 60client-output-buffer-limit pubsub 32mb 8mb 60hz 10aof-rewrite-incremental-fsync yes
    

    2,建立用户与日志目录

    三、redis从配置

    第一次启动Redis前,建议为Redis单独建立一个用户,并新建data和日志文件夹

    root# cat redis_6379.conf |grep -Ev "^$|^#"bind 172.18.8.20protected-mode yesport 6379tcp-backlog 511timeout 0tcp-keepalive 300daemonize yessupervised nopidfile /var/run/redis_6379.pidloglevel noticelogfile "/var/log/redis_6379.log"databases 16save 900 1save 300 10save 60 10000stop-writes-on-bgsave-error yesrdbcompression yesrdbchecksum yesdbfilename dump_6379.rdbdir /data0/redis_tradeslaveof 172.18.8.19 6379 -----从库比主库多这2行配置参数masterauth Allwelltokok -----从库比主库多这2行配置参数slave-serve-stale-data yesslave-read-only yesrepl-diskless-sync norepl-diskless-sync-delay 5repl-disable-tcp-nodelay noslave-priority 100requirepass Allwelltokokappendonly yesappendfilename "appendonly_6379.aof"appendfsync everysecno-appendfsync-on-rewrite noauto-aof-rewrite-percentage 100auto-aof-rewrite-min-size 64mbaof-load-truncated yeslua-time-limit 5000slowlog-log-slower-than 10000slowlog-max-len 128latency-monitor-threshold 0notify-keyspace-events ""hash-max-ziplist-entries 512hash-max-ziplist-value 64list-max-ziplist-size -2list-compress-depth 0set-max-intset-entries 512zset-max-ziplist-entries 128zset-max-ziplist-value 64hll-sparse-max-bytes 3000activerehashing yesrename-command FLUSHALL ZYzv6FOBdwflW2nXrename-command EVAL S9UHPKEpSvUJMMrename-command FLUSHDB D60FPVDJuip7gy6lclient-output-buffer-limit normal 0 0 0client-output-buffer-limit slave 256mb 64mb 60client-output-buffer-limit pubsub 32mb 8mb 60hz 10aof-rewrite-incremental-fsync yes
    
    sudo useradd redis 
    sudo mkdir -p /var/lib/redis 
    sudo mkdir -p /var/log/redis 
    sudo chown redis.redis /var/lib/redis #db文件放在这里,要修改redis.conf 
    sudo chown redis.redis /var/log/redis 
    

    四、redis启动脚本

    3,配置init脚本

    root# cat /etc/init.d/redis_6379 #!/bin/sh## Simple Redis init.d script conceived to work on Linux systems# as it does use of the /proc filesystem.# chkconfig: 2345 90 10source /etc/init.d/functionsREDISPORT=6379EXEC=/usr/local/bin/redis-serverCLIEXEC=/usr/local/bin/redis-cli PIDFILE=/var/run/redis_${REDISPORT}.pidCONF="/usr/local/redis/etc/redis_${REDISPORT}.conf"AUTH="Allwelltokok"BIND_IP='172.18.8.19' 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 ..." $CLIEXEC -h $BIND_IP -a $AUTH -p $REDISPORT SHUTDOWN sleep 1 while [ -x /proc/${PID} ] do echo "Waiting for Redis to shutdown ..." sleep 1 done echo "Redis stopped" fi} restart(){ stop start }status(){ ps -ef|grep redis-server|grep -v grep /dev/null 2&1 if [ $ -eq 0 ];then echo "redis server is running" else echo "redis server is stopped" fi } case "$1" in start) start ;; stop) stop ;; restart) restart ;; status) status ;; *) echo "Usage: /etc/init.d/redis {start|stop|status|start}" &2 exit 1 esac
    

    其实github上很多老外写好的启动脚本,不过大部分都是ubuntu的,对于Centos。

    五、启动服务

    经过修改,如下:

    root# /etc/init.d/redis_6379 start 
    
    ########################### 
    PATH=/usr/local/bin:/sbin:/usr/bin:/bin 
    
    REDISPORT=6379 
    EXEC=/usr/local/bin/redis-server 
    REDIS_CLI=/usr/local/bin/redis-cli 
    
    PIDFILE=/var/run/redis.pid 
    CONF="/etc/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 does not exist, 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 
    ############################## 
    

    查看日志

    把上述代码存为redis,放到/etc/init.d/下面

    本文由金沙游乐场85155发布于大数据库,转载请注明出处:Centos下配置Redis开机启动脚本

    关键词: