redis 安装: Linux 安装redis
1)下载jar包:
使用Jedis需要以下两个jar包:
jedis-2.8.0.jar
commons-pool2-2.4.2.jar
2)测试redis连接:
package com.venn.redis.demo;
import redis.clients.jedis.Jedis;
public class RedisJava {
public static void main(String[] args) {
//连接本地的 Redis 服务
Jedis jedis = new Jedis("10.80.248.24");
// jedis.select(1);
System.out.println("Connection to server sucessfully");
//查看服务是否运行
System.out.println("Server is running: "+jedis.ping());
}
}
如果显示
Connection to server sucessfully
Server is running: PONG
表示服务器连接正常。
3)简单使用redis
package com.venn.redis.demo;
import redis.clients.jedis.Jedis;
public class RedisStringJava {
public static void main(String[] args) {
// 连接本地的 Redis 服务
Jedis jedis = new Jedis("10.80.248.22"); // 默认端口
//Jedis jedis = new Jedis("10.80.248.22",6379); // 指定端口
// jedis.auth("pass") // 指定密码
System.out.println("Connection to server sucessfully");
// 设置 redis 字符串数据
jedis.set("redis", "Redis 1");
// 获取存储的数据并输出
System.out.println("Stored string in redis:: " + jedis.get("redis"));
System.out.println("redis : " + jedis.get("redis"));
}
}
执行,有报错。
4)Connection refused : connect 报错处理
Connection to server sucessfully
Exception in thread "main" redis.clients.jedis.exceptions.JedisConnectionException: java.net.ConnectException: Connection refused: connect
at redis.clients.jedis.Connection.connect(Connection.java:164)
at redis.clients.jedis.BinaryClient.connect(BinaryClient.java:80)
at redis.clients.jedis.Connection.sendCommand(Connection.java:100)
at redis.clients.jedis.BinaryClient.select(BinaryClient.java:163)
at redis.clients.jedis.BinaryJedis.select(BinaryJedis.java:431)
at com.venn.redis.demo.RedisStringJava.main(RedisStringJava.java:11)
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:339)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:579)
at redis.clients.jedis.Connection.connect(Connection.java:158)
... 5 more
经查,redis默认只能本地访问
bind 127.0.0.1 # 不同版本可能是 localhost
解决:
修改启动redis server 使用的redis.conf,注释以上一行
5)保护模式异常
经过3修改后,redis可以在任意地址(局域网)访问,但是redis默认没有配置访问密码,这样就有个报错:
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:117) at redis.clients.jedis.Protocol.process(Protocol.java:151) at redis.clients.jedis.Protocol.read(Protocol.java:205) at redis.clients.jedis.Connection.readProtocolWithCheckingBroken(Connection.java:297) at redis.clients.jedis.Connection.getStatusCodeReply(Connection.java:196) at redis.clients.jedis.BinaryJedis.select(BinaryJedis.java:432) at com.venn.redis.demo.RedisStringJava.main(RedisStringJava.java:11)
英文比较好的同学,可以看到是什么意思。这里给英文不好的同学大概讲一下:
报错的大意就是:redis运行在保护模式,没有绑定访问地址,没有登录密码认证,在这种模式下,连接只接受环回接口(loopback,一种路由接口),
下面有几种解决办法:
1)使用命令:“CONFIG SET protected-mode no” ,禁用保护模式。
2)修改配置文件,禁用保护模式。
3)重新启动redis server 使用 “--protected-mode no” 参数
4) 设置一个绑定ip或设置认证密码
当然使用4了。绑定ip,见3,修改127.0.0.1 to 你的ip
6) redis 设置密码
修改redis.conf 配置文件,找到 “requirepass”
取消注释,在后面添加你的密码
requirepass myRedis
重启redis。
7) 设置密码后,客户端登录
设置密码后,redis-cli可以正常登录,但是不能操作。
(error) ERR operation not permitted
使用:
./redis-cli -a myReids
登录正常。
8) 重新执行
package com.venn.redis.demo;
import redis.clients.jedis.Jedis;
public class RedisStringJava {
public static void main(String[] args) {
// 连接本地的 Redis 服务
Jedis jedis = new Jedis("10.80.248.22",6379);
jedis.auth("myRedis"); // 设置密码
System.out.println("Connection to server sucessfully");
// 设置 redis 字符串数据
jedis.set("redis", "Redis 1");
// 获取存储的数据并输出
System.out.println("redis : " + jedis.get("redis"));
}
}
执行,返回正常
redis : redis
OK。
未完待续。