问题:java的application.properties文件内配置redis的IP、端口、密码仍连接不了redis服务器
云服务器系统:CentOS 8
一、修改redis配置
我用的是宝塔面板操作的,比较方便。
修改第一处:将 bind:127.0.0.1 注释掉,变为 # bind:127.0.0.1
修改第二处:将 protected mode yes 改为 protected mode no
修改第三处:在 requirepass 添加密码,自己设置
保存设置,重启,仍然无法连接上
二、先检查是否能连接到服务器
2.1 win+R输入cmd进入命令行窗口,输入ping 腾讯云公网ip地址,结果显示没问题
ping 4.3.2.1
2.2 输入telnet 腾讯云公网ip地址 端口号,结果显示在端口6379连接失败
telnet 1.2.3.4 6379
备注:若结果显示在端口XX连接失败,则在三、四步需要添加对应的端口号
三、修改腾讯云服务器防火墙
四、修改服务器防火墙,有两个:iptables和firewall
需要检查是否开放了端口6379,如果没有开放,需要添加端口。
4.1 iptables操作
//查看端口开放情况
iptables -L -n
//添加端口
iptables -I INPUT 1 -p tcp -m state --state NEW -m tcp --dport 6379 -j ACCEPT
//保存
service iptables save
//报错
The service command supports only basic LSB actions (start, stop, restart, try-restart, reload, force-reload, status). For other actions, please try to use systemctl.
//如果报错则需要先安装服务
yum install iptables-services
//然后再保存
service iptables save
//保存成功
iptables: Saving firewall rules to /etc/sysconfig/iptables:[ OK ]
4.2 firewall操作
//查看端口开放情况
firewall-cmd --zone=public --list-ports
//添加端口
firewall-cmd --add-port=6379/tcp --permanent
五、重启防火墙、redis
我直接重启了腾讯云服务器,然后顺利连上了。
redis-cli -h 服务器ip -p 端口号 -a 密码