java程序无法连接redis_Ubuntu14.04安装redis和简单配置及Java连接redis遇到的无法访问问题解决...

本文档详细介绍了如何在Ubuntu14.04上安装Redis服务器,包括检查运行状态、使用命令行客户端进行操作以及配置远程访问。此外,还讨论了Java程序连接Redis时可能遇到的'Connection refused'和'DENIED Redis is running in protected mode'错误,并提供了相应的解决方案。
摘要由CSDN通过智能技术生成

转载:https://www.cnblogs.com/zjfjava/p/6881772.html

https://www.cnblogs.com/hltswd/p/6225833.html#undefined

安装

//在终端中安装Redis服务器端

sudo apt-get install redis-server

安装完成后,Redis服务器会自动启动,我们检查Redis服务器程序

//在终端中检查Redis服务器系统进程

ps -aux|grep redis

可以看到:

f2674d3ac43167666f0749f099d0d073.png

//在终端中通过启动命令检查Redis服务器状态

netstat -nlt|grep 6379

显示: tcp 0 0 127.0.0.1:6379 0.0.0.0:* LISTEN

//通过启动命令检查Redis服务器状态

sudo /etc/init.d/redis-server status

显示: redis-server is running

3、通过命令行客户端访问Redis

安装Redis服务器,会自动地一起安装Redis命令行客户端程序。

在本机输入redis-cli命令就可以启动,客户端程序访问Redis服务器。

~ redis-cli

redis 127.0.0.1:6379>

# 命令行的帮助

redis 127.0.0.1:6379> help

redis-cli 2.2.12

Type: "help @" to get a list of commands in

"help " for help on

"help " to get a list of possible help topics

"quit" to exit

# 查看所有的key列表

redis 127.0.0.1:6379> keys *

(empty list or set)

基本的Redis客户端命令操作

增加一条字符串记录key1

# 增加一条记录key1

redis 127.0.0.1:6379> set key1 "hello"

OK

# 打印记录

redis 127.0.0.1:6379> get key1

"hello"

2 . 增加一条数字记录key2

# 增加一条数字记录key2

set key2 1

OK

# 让数字自增

redis 127.0.0.1:6379>INCR key2

(integer) 2

redis 127.0.0.1:6379>INCR key2

(integer) 3

# 打印记录

redis 127.0.0.1:6379> get key2

"3"

3. 增加一条列表记录key3

# 增加一个列表记录key3

redis 127.0.0.1:6379>LPUSH key3 a

(integer) 1

# 从左边插入列表

redis 127.0.0.1:6379>LPUSH key3 b

(integer) 2

# 从右边插入列表

redis 127.0.0.1:6379>RPUSH key3 c

(integer) 3

# 打印列表记录,按从左到右的顺序

redis 127.0.0.1:6379>LRANGE key3 0 3

1) "b"

2) "a"

3) "c"

4.增加一条哈希表记录key4

# 增加一个哈希记表录key4

redis 127.0.0.1:6379> HSET key4 name "John Smith"

(integer) 1

# 在哈希表中插入,email的Key和Value的值

redis 127.0.0.1:6379> HSET key4 email "abc@gmail.com"

(integer) 1

# 打印哈希表中,name为key的值

redis 127.0.0.1:6379> HGET key4 name

"John Smith"

# 打印整个哈希表

redis 127.0.0.1:6379> HGETALL key4

1) "name"

2) "John Smith"

3) "email"

4) "abc@gmail.com"

5.增加一条哈希表记录key5

# 增加一条哈希表记录key5,一次插入多个Key和value的值

redis 127.0.0.1:6379>HMSET key5 username antirez password P1pp0 age 3

OK

# 打印哈希表中,username和age为key的值

redis 127.0.0.1:6379>HMGET key5 username age

1) "antirez"

2) "3"

# 打印完整的哈希表记录

redis 127.0.0.1:6379>HGETALL key5

1) "username"

2) "antirez"

3) "password"

4) "P1pp0"

5) "age"

6) "3"

6.删除记录

# 查看所有的key列表

redis 127.0.0.1:6379> keys *

1) "key2"

2) "key3"

3) "key4"

4) "key5"

5) "key1"

# 删除key1,key5

redis 127.0.0.1:6379> del key1

(integer) 1

redis 127.0.0.1:6379> del key5

(integer) 1

# 查看所有的key列表

redis 127.0.0.1:6379> keys *

1) "key2"

2) "key3"

3) "key4"

4、修改Redis的配置

1、 使用Redis的访问账号

默认情况下,访问Redis服务器是不需要密码的,为了增加安全性我们需要设置Redis服务器的访问密码。设置访问密码为redis。

用vi打开Redis服务器的配置文件redis.conf

~ sudo vi /etc/redis/redis.conf

#取消注释requirepass

requirepass redis

2、 让Redis服务器被远程访问

默认情况下,Redis服务器不允许远程访问,只允许本机访问,所以我们需要设置打开远程访问的功能。

用vi打开Redis服务器的配置文件redis.conf

~ sudo vi /etc/redis/redis.conf

~ sudo gedit /etc/redis/redis.conf

#注释bind

#bind 127.0.0.1

修改后,重启Redis服务器。

~ sudo /etc/init.d/redis-server restart

Stopping redis-server: redis-server.

Starting redis-server: redis-server.

未使用密码登陆Redis服务器

~ redis-cli

redis 127.0.0.1:6379> keys *

(error) ERR operation not permitted

发现可以登陆,但无法执行命令了。

登陆Redis服务器,输入密码

~ redis-cli -a redis

redis 127.0.0.1:6379> keys *

1) "key2"

2) "key3"

3) "key4"

登陆后,一切正常。

我们检查Redis的网络监听端口

//检查Redis服务器占用端口

~ netstat -nlt|grep 6379

tcp 0 0 0.0.0.0:6379 0.0.0.0:* LISTEN

我们看到从之间的网络监听从 127.0.0.1:6379 变成 0 0.0.0.0:6379,表示Redis已经允许远程登陆访问。

我们在远程的另一台Linux访问Redis服务器

~ redis-cli -a redis -h 192.168.1.199

redis 192.168.1.199:6379> keys *

1) "key2"

2) "key3"

3) "key4"

远程访问正常。通过上面的操作,我们就把Redis数据库服务器,在Linux Ubuntu中的系统安装完成。

48304ba5e6f9fe08f3fa1abda7d326ab.png

Exception in thread "main" redis.clients.jedis.exceptions.JedisConnectionException: java.net.ConnectException: Connection refused: connect

at redis.clients.jedis.Connection.connect(Connection.java:207)

at redis.clients.jedis.BinaryClient.connect(BinaryClient.java:93)

at redis.clients.jedis.Connection.sendCommand(Connection.java:126)

at redis.clients.jedis.Connection.sendCommand(Connection.java:121)

at redis.clients.jedis.BinaryClient.ping(BinaryClient.java:106)

at redis.clients.jedis.BinaryJedis.ping(BinaryJedis.java:195)

at practice.RedisJava.main(RedisJava.java:13)

Caused by: java.net.ConnectException: Connection refused: connect

at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)

at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85)

at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)

at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)

at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)

at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)

at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)

at java.net.Socket.connect(Socket.java:589)

at redis.clients.jedis.Connection.connect(Connection.java:184)

... 6 more

48304ba5e6f9fe08f3fa1abda7d326ab.png

这可能是两个原因造成的,首先可能是redis的6379端口无法访问,请先在cmd中输入命令

telnet 127.0.0.1 6379

看看可不可以访问redis-server 机器的6379端口,如果不能访问,需要在远程机器关掉防火墙或者添加允许通过

1)使用root用户登录,vi /etc/sysconfig/iptables,添加如图所以一行

e68e6fe90abad5cca57ee7d38e02fc53.png

2)输入命令service iptables restart重启防火墙

或者可以直接root用户使用命令service iptables stop关闭防火墙。

防火墙检查完后,如果还是出现上述问题,说明redis还有地方需要配置,redis默认是只有本机可以访问的,想要远程访问需要修改redis.conf配置文件。

进入redis.conf目录,并使用vi命令打开,找到bind那行修改后,wq保存退出,重启redis-server。

9ac9c5d1abee50d7b2589666e20fe84a.png

bind 后加的是允许访问的ip

bind 127.0.0.1代表只有本机可以访问,可以将允许访问的ip加入,也可以直接注释掉这一行,这样所有机器都可以访问。

解决上述问题后出现的新问题:DENIED Redis is running in protected mode

报错信息如下:

48304ba5e6f9fe08f3fa1abda7d326ab.png

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.Jedis.set(Jedis.java:121)

at roy.redis.test.Test.init(Test.java:13)

at roy.redis.test.Test.main(Test.java:8)

48304ba5e6f9fe08f3fa1abda7d326ab.png

报错信息很长,但是主要是说redis开启了protected mode,这也是Redis3.2加入的新特性,开启保护模式的redis只允许本机登录,同样设置在配置文件redis.conf中,如图

78cac2b66e9601f3d256357c1ec04976.png 

这里原来是yes代表开启了保护模式,后面可以填密码也可以填no代表关闭,我们这里选择关闭保护模式,wq保存退出后再重启redis-server

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值