redis开启远程访问
在Linux虚拟机上安装了redis后,启动redis,首先尝试本地访问连接
可以正常访问连接,尝试插入key-value,成功
接下来尝试远程连接,连接到我的虚拟机IP:192.168.182.120,提示以下错误:
Exception in thread "main" redis.clients.jedis.exceptions.JedisDataException: 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.
at redis.clients.jedis.Protocol.processError(Protocol.java:127)
at redis.clients.jedis.Protocol.process(Protocol.java:161)
at redis.clients.jedis.Protocol.read(Protocol.java:215)
at redis.clients.jedis.Connection.readProtocolWithCheckingBroken(Connection.java:340)
at redis.clients.jedis.Connection.getStatusCodeReply(Connection.java:239)
at redis.clients.jedis.BinaryJedis.ping(BinaryJedis.java:196)
at com.example.redis.JedisTest.main(JedisTest.java:24)
出现这个错误的原因很简单,redis默认只支持本地访问,不支持远程访问,要想远程访问IP,需要在配置文件中开启redis的远程访问即可,具体操作如下:
【解决方案】
1.输入命令:pstree -p,可树状展示进程并且展示进程号,找到redis-server的进程号
2.杀死当前的redis-server
3.再次查看运行的进程,已经没有redis的进程
4.修改redis目录下的redis.conf文件
5.找到如下图所示配置项
更改为
将redis服务设置为后台方式启动
保存更改的配置文件,退出
6.启动redis服务时加载修改后的配置文件重新以配置文件启动redis
7.远程模式访问redis,成功