启动redis

安装redis的过程在官网有,因为是win10内置的ubunut,这里使用apt-get方式安装。

$ apt-get install redis-server

安装完成后,启动redis。

$ redis-server
[1] 299
[299] 21 Oct 11:06:09.006 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
[299] 21 Oct 11:06:09.008 * Increased maximum number of open files to 10032 (it was originally set to 2048).
                _._
           _.-``__ ''-._
      _.-``    `.  `_.  ''-._           Redis 2.8.9 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._
 (    '      ,       .-`  | `,    )     Running in stand alone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 299
  `-._    `-._  `-./  _.-'    _.-'
 |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |           http://redis.io
  `-._    `-._`-.__.-'_.-'    _.-'
 |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |
  `-._    `-._`-.__.-'_.-'    _.-'
      `-._    `-.__.-'    _.-'
          `-._        _.-'
              `-.__.-'

[299] 21 Oct 11:06:09.048 # Server started, Redis version 2.8.9
[299] 21 Oct 11:06:09.049 * DB loaded from disk: 0.001 seconds
[299] 21 Oct 11:06:09.049 * The server is now ready to accept connections on port 6379

Waring提示redis目录里没有配置文件,默认的配置文件在/etc/redis/redis.conf中。

$ redis-server /etc/redis/redis.conf

指定以默认的配置文件启动。

$ redis-cli
Could not connect to Redis at 127.0.0.1:6379: Connection refused
not connected> exit

redis客户端无法连接服务器。
打开/etc/redis/redis.conf。

$ vim /etc/redis/redis.conf

找到内容

# By default Redis listens for connections from all the network interfaces
# available on the server. It is possible to listen to just one or multiple
# interfaces using the "bind" configuration directive, followed by one or
# more IP addresses.
#
# Examples:
#
# bind 192.168.1.100 10.0.0.1
bind 127.0.0.1

修改bind 127.0.0.1为0.0.0.0,重启redis-server,启动redis-cli.

$ redis-cli
127.0.0.1:6379> ping
PONG
127.0.0.1:6379>

连接正常。

建立系统服务

新建一个redis的系统服务,使用server redis_6379 start启动。
redis提供了一个启动配置文件redis_init_script,内容为

#!/bin/sh
#
# Simple Redis init.d script conceived to work on Linux systems
# as it does use of the /proc filesystem.

REDISPORT=6379
EXEC=/usr/local/bin/redis-server
CLIEXEC=/usr/local/bin/redis-cli

PIDFILE=/var/run/redis/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 ..."
                $CLIEXEC -p $REDISPORT 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

其中,EXEC为redis-server位置,apt-get安装时有时候并没有在/usr/local/bin/redis-server中,修改为正确的位置即可。CLIEXEC同理。
REDISPORT为端口号。
根据配置创建好文件夹。
创建配置文件/etc/init.d/redis_端口号,添加上面的内容。
根据上面文件的配置,创建好PIDFILE所需的文件夹,配置CONF的正确地址。

$ update-rc.d redis_端口号 defaults
$ service redis_端口号 start #启动redis
$ service redis_端口号 stop #关闭redis

: unrecognized service

服务启动权限问题

$ chmod 777 /etc/init.d/redis_端口号

启动后无法关闭,持续显示Waiting for Redis to shutdown ...

发现PIDFILE没有创建,检查是否创建了PIDFIEL文件夹,如果有,检查权限。

最后修改日期: 2018年12月6日

作者

留言

撰写回覆或留言

发布留言必须填写的电子邮件地址不会公开。