centos 服务器 安装redis后 启用外网连接
给redis.conf 文件修改参数
//绑定外网 如果是指定ip连接 可以使用空格隔开多个ip bind 127.0.0.1 192.168.3.3
bind 0.0.0.0
#bind 127.0.0.1
//使用密码连接
requirepass newpassword
protected-mode no
修改完成后使用如下命令启动服务
redis-server ../etc/redis.conf
这时服务可以正常启动,但是使用service redis stop 停止服务是报错 NOAUTH Authentication required
这是查看服务依然运行中
[root]# ps -ef|grep redis
root 6977 1 0 09:48 ? 00:00:00 /usr/local/redis/bin/redis-server 0.0.0.0:6379
root 7025 6865 0 10:03 pts/0 00:00:00 grep --color=auto redis
[root]# pgrep redis
6977
这些修改按如下标红处增加密码变量 并再停止服务时 增加指定密码的参数 -a $RedisPwd
vim /etc/init.d/redis
1 REDISPORT=6379 2 EXEC=/usr/local/redis/bin/redis-server 3 REDIS_CLI=/usr/local/redis/bin/redis-cli 4 5 PIDFILE=/var/run/redis.pid 6 CONF="/usr/local/redis/etc/redis.conf" 7 RedisPwd=newpassword
8 9 case "$1" in 10 start) 11 if [ -f "$PIDFILE" ]; then 12 echo "$PIDFILE exists, process is already running or crashed" 13 else 14 echo -n "Starting Redis server..." 15 $EXEC $CONF 16 if [ "$?"="0" ]; then 17 echo " done" 18 else 19 echo " failed" 20 fi 21 fi 22 ;; 23 stop) 24 if [ ! -f "$PIDFILE" ]; then 25 echo "$PIDFILE does not exist, process is not running" 26 else 27 PID=$(cat $PIDFILE) 28 echo "Stopping Redis server..." 29 $REDIS_CLI -a $RedisPwd -p $REDISPORT shutdown 30 if [ "$?"="0" ]; then 31 echo " done" 32 else 33 echo " failed" 34 fi 35 fi 36 ;; 37 restart) 38 ${0} stop 39 ${0} start 40 ;; 41 kill) 42 echo "force kill redis server..." 43 killall redis-server 44 if [ "$?"="0" ]; then 45 echo " done" 46 else 47 echo " failed" 48 fi 49 ;; 50 status) 51 if [ -f "$PIDFILE" ]; then 52 echo "Redis server is running." 53 else 54 echo "Redis server is stopped." 55 fi 56 ;; 57 *) 58 echo "Usage: /etc/init.d/redis {start|stop|restart|status|kill}" >&2 59 exit 1 60 esac
这是再次使用停止服务命令,正常执行无报错。