本机连不上虚拟机的redis

首先修改redis.conf

  1. bind 改为0.0.0.0,设置所有机器可访问
  2. 查看requirepass,可设置也可不设置(开发环境)

查看6379端口是否开启

  1. 输入firewall-cmd --query-port=6379/tcp,如果返回no说明没有开启
  2. 输入firewall-cmd --add-port=6379/tcp (加 --permanent 永久有效),返回success说明开启成功
本机连不上虚拟机的redis_第1张图片
image.png

设置开机启动

  1. 在redis目录中找到redis_init_script文件
    我的文件地址是在:/usr/local/redis/utils
  2. 按实际情况修改redis_init_script相关参数,就是redis-server、redis-cli、redis.conf文件的目录,PIDFILE这个参数在安装redis的时候就已经有了,不需要动。
#!/bin/sh
#
# Simple Redis init.d script conceived to work on Linux systems
# as it does use of the /proc filesystem.
# chkconfig: 2345 90 10
# description: Redis is a persistent key-value database

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

PIDFILE=/var/run/redis_${REDISPORT}.pid
CONF="/opt/conf/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

  1. 将redis_init_script脚本复制到/etc/init.d目录下,注意增加了chkconfig和description两行,不然会提示服务redis不支持chkconfig
cp redis_init_script /etc/init.d/redis
  1. 设置为开机自启动服务器
#
chkconfig redis on 
#打开服务
service redis start
#关闭服务
service redis stop

你可能感兴趣的:(本机连不上虚拟机的redis)