【redis】能ping通虚拟机但是端口无法访问

问题

虚拟机上有redis,能ping通虚拟机的ip,但是idea连不上虚拟机里的redis

基本情况

虚拟机网络模式是NAT模式,linux防火墙firewalld已关闭,没有iptables,主机和虚拟机能互相Ping通,主机telnet redis失败

问题解决方案

尝试了比较多的方法,总结如下:

1,防火墙要关闭,如果开启防火墙,要将redis的端口开启防火墙的外部端口

下面我仅列出来firewalld的命令,如果还有iptables,也要使用类似的处理方式,命令自己搜一下。

开启/关闭/开机自启动/禁止自启动防火墙

#防火墙允许开机自启动
systemctl enable firewalld
#防火墙禁止开机自启动
systemctl disable firewalld
#开启防火墙
systemctl start firewalld
#关闭防火墙
systemctl stop firewalld
#查看防火墙状态
systemctl status firewalld


添加外部端口

#添加6379为外部端口,--permanent表示永久的
firewall-cmd --zone=public --permanent --add-port=6379/tcp
#移除6379作为外部端口,--permanent表示永久的
firewall-cmd --zone=public --remove-port=80/tcp --permanent
#允许http服务,redis这里用不到,web服务能用到
firewall-cmd --zone=public --add-service=http --permanent
#重新加载防火墙
firewall-cmd --reload
#查看端口是否开放
firewall-cmd --query-port=6379/tcp
#查看所有开放端口
firewall-cmd --list-port

2,虚拟机网络模式设置为NAT在这里插入图片描述
3,配置文件里面的修改
3.1 protected-mode
保护模式开启且没有密码时,redis会仅接收本机的请求。解决办法是如果你是自己做demo和测试学习用的,protected-mode直接改为no,如果是工作用的,设置密码吧

配置文件中注释如下:

# Protected mode is a layer of security protection, in order to avoid that
# Redis instances left open on the internet are accessed and exploited.
#
# When protected mode is on and the default user has no password, the server
# only accepts local connections from the IPv4 address (127.0.0.1), IPv6 address
# (::1) or Unix domain sockets.
#
# By default protected mode is enabled. You should disable it only if
# you are sure you want clients from other hosts to connect to Redis
# even if no authentication is configured.
protected-mode no

3.2 bind
bind如果注掉,那么就会接受任何请求。
如果开启这一项,由前面的ip加上后面指定网卡类型。
如果是127.0.0.1,那么就会仅接受来自本机的ipv4的请求。如果是0.0.0.0,会接受任何ipv4的请求。如果是绑定指定的ip,那么就会仅接收指定ip来的请求。
其中上面的ip的格式是接受ipv4,如果想同时接受Ipv4和ipv6,可以在后面跟上 ::1。如果是想接受任何类型的连接,后面跟上 -:😗 。

这里我使用的是bind 0.0.0.0。我这里仅限学习测试使用。工作中要指定具体的ip。

bind的注释如下

# By default, if no "bind" configuration directive is specified, Redis listens
# for connections from all available network interfaces on the host machine.
# It is possible to listen to just one or multiple selected interfaces using
# the "bind" configuration directive, followed by one or more IP addresses.
# Each address can be prefixed by "-", which means that redis will not fail to
# start if the address is not available. Being not available only refers to
# addresses that does not correspond to any network interface. Addresses that
# are already in use will always fail, and unsupported protocols will always BE
# silently skipped.
#
# Examples:
#
# bind 192.168.1.100 10.0.0.1     # listens on two specific IPv4 addresses
# bind 127.0.0.1 ::1              # listens on loopback IPv4 and IPv6
# bind * -::*                     # like the default, all available interfaces
#
# ~~~ WARNING ~~~ If the computer running Redis is directly exposed to the
# internet, binding to all the interfaces is dangerous and will expose the
# instance to everybody on the internet. So by default we uncomment the
# following bind directive, that will force Redis to listen only on the
# IPv4 and IPv6 (if available) loopback interface addresses (this means Redis
# will only be able to accept client connections from the same host that it is
# running on).
#
# IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES
# COMMENT OUT THE FOLLOWING LINE.
#
# You will also need to set a password unless you explicitly disable protected
# mode.
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bind 0.0.0.0
#bind * -::*


4 注意开机自启动的redis
有的时候,redis我们配置累开机自启动。自启动的脚本在/etc/init.d/目录下,其中,自启动脚本里面会配置CONF,也就是配置文件的读取路径。
可能你的linux中有多份配置文件或者多个路径下都有配置文件。那么此时要注意的是,我们要修改的配置文件是否和启动的时候使用的配置文件一样。不要出现南辕北辙的情况。

总结

我实际上遇到的问题,

是bind那里为127.0.0.1的本机ip,改为0.0.0.0;

我没有设置密码,同时protected-mode为yes,所以我修改为了no

此外还有我的开机自启动的redis的配置文件在其他路径,和我通过指定配置文件启动的那个路径不一样。所以我有时候按照上面修改了配置文件,但是开机自启动的redis的bind的ip还是显示是127.0.0.1。我刚开始也是有点疑惑的,直到后来打开自启动脚本发现自启动的脚本读的配置文件在另外一个路径里。

修改后,可以通过idea启动后连接并输出结果.

此外,不要尝试用telnet去访问redis端口,因为telnet是http的get请求,测试访问web还好,是访问不了redis的

参考文章
[1],redis 6379端口不通解决方法
[2],Redis 6379端口连接失败
[3],windows本地连不上虚拟机redis服务完美解决
[4],linux设置防火墙,自启动以及关闭禁止防火墙
[5],虚拟机centos7开放端口(8080为例)
[6],Linux查看防火墙状态及开启关闭命令
[7],主机可以ping通虚拟机但无法访问虚拟机某一端口
[8],本机和虚机ping的通,但是虚机ip+端口访问不了(ubuntu)
[9],Redis关闭开机自启和Redis使用配置文件启动不生效解决(Ubantu)
[10],Linux防火墙IPTABLES的安装与配置
[11],Linux:CentOS7下关闭SELINUX

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值