阿里云(ECS中)Redis远程访问

一、更改Redis的配置文件.

1.修改redis.conf文件

  • vim /etc/redis/redis.conf
      将绑定本机注释掉, 在bind 127.0.0.1地址前面加个#
      若是想指定多个ip访问,但并不是全部的ip访问,可以bind多个ip.
# 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<---

# Specify the path for the Unix socket that will be used to listen for
# incoming connections. There is no default, so Redis will not listen
# on a unix socket when not specified.
#
# unixsocket /var/run/redis/redis.sock
# unixsocketperm 700
  • ② 在Redis3.2之后,redis增加了protected-mode

    protected-mode no  # 修改办法:即将protected-mode改为--->no<---

    注! 若不做更改,即使注释掉了bind 127.0.0.1,再访问redisd时候还是报错,如下:

    (error) DENIED Redis is running in protected mode because protected mode is enabled, no bind address was specified, no authentication password is requested to clients. In this mode connections are only accepted from the loopback interface. If you want to connect from external computers to Redis you may adopt one of the following solutions: 1) Just disable protected mode sending the command 'CONFIG SET protected-mode no' from the loopback interface by connecting to Redis from the same host the server is running, however MAKE SURE Redis is not publicly accessible from internet if you do so. Use CONFIG REWRITE to make this change permanent. 2) Alternatively you can just disable the protected mode by editing the Redis configuration file, and setting the protected mode option to 'no', and then restarting the server. 3) If you started the server manually just for testing, restart it with the '--protected-mode no' option. 4) Setup a bind address or an authentication password. NOTE: You only need to do one of the above things in order for the server to start accepting connections from the outside.

2.重启redis服务

  • ① 关闭redis服务
    service redis-server stop
  • ② 启动redis服务
    redis-server /etc/redis/redis.conf

二、使用密码登录redis(非必备选项)

1.修改redis.conf文件

  • vim /etc/redis/redis.conf
      若redis客户端连接成功是操作报异常(error) NOAUTH Authentication required
      错误的含义是说你没有认证,说明没有使用密码连接
################################## SECURITY ###################################

# Require clients to issue AUTH <PASSWORD> before processing any other
# commands.  This might be useful in environments in which you do not trust
# others with access to the host running redis-server.
#
# This should stay commented out for backward compatibility and because most
# people do not need auth (e.g. they run their own servers).
#
# Warning: since Redis is pretty fast an outside user can try up to
# 150k passwords per second against a good box. This means that you should
# use a very strong password otherwise it will be very easy to break.
#
# requirepass foobared    # 看这里将其改为箭头中的内容 --->requirepass 你的密码<---
requirepass 123456  # 例如这样你的密码就是123456

# Command renaming.
#
# It is possible to change the name of dangerous commands in a shared
# environment. For instance the CONFIG command may be renamed into something
# hard to guess so that it will still be available for internal-use tools
# but not available for general clients.
  • ② 关闭redis服务
    service redis-server stop
  • ③ 启动redis服务
    redis-server /etc/redis/redis.conf

2.使用密码连接redis
  redis-cli -h 127.0.0.1 -p 6379 -a 123456

三、添加安全组规则(阿里云控制台)开放redis端口

1.找到实例安全组界面
阿里云控制台安全组
2.添加入方向安全组
添加入方向安全组详情页
3.添加出方向安全组
    出方向的安全租设置和入方向的设置方法一致,只是在上图圈起来的规则方向做下改变就好啦~!

注! 博主这里使用的是阿里云的ECS实例中的redis,故在安全组中开放6739端口(若没做更改默认为6739)

四、Redis机器所在实例防火墙设置

    ☞ 若以上操作您都已经完成,还是不能顺利连接,远程redis,请您看下主机的防火墙是否开放redis的端口~
    ☞ 若没有打开,可以开启下响应端口,或直接关闭防火墙.


!!!版权声明!!!

本系列为博主学心得与体会,所有内容均为原创(✿◡‿◡)

欢迎传播、复制、修改。引用、转载等请注明转载来源。感谢您的配合

用于商业目的,请与博主采取联系,并请与原书版权所有者联系,谢谢!\(≧▽≦)/

!!!版权声明!!!



生活嘛~ 最重要的就是开心喽~ O(∩_∩)O~~

这里写图片描述


猜你喜欢

转载自blog.csdn.net/aa1209551258/article/details/81542082