1.下载
redis下载
我下载3.2.12版本的,用xshell先连接Linux系统,然后把安装包拖拽到xshell窗口上传到Linux(需要先安装lrzsz),
2.编译
#编译源程序
make
cd src
make install PREFIX=/opt/redis3.2.12/
其中/opt/redis3.2.12/是安装目录
这里提示我们可以测试一下make test,我当时还以为必须执行这个命令,以为没有安装成功,其实到这里已经安装好了。
make test
然后就等待吧,出现下面的样子,就是没问题了
3.启动
把配置文件拷贝到之前指定的安装目录,然后指定配置文件启动redis
cp redis.conf /opt/redis3.2.12/
cd /opt/redis3.2.12/
bin/redis-server redis.conf
这些蓝色的字搞的我迟疑了半天,还以为安装失败了,搞得我又删了重装一下,发现其实已经安装好了,这时候启动的redis是个前台进程
我们重新打开一个窗口,同样到刚才的目录,测试一下是否能连上
bin/redis-cli
看样子应该是没有问题了
4.通过java程序访问
再用java程序访问一下
可以用maven构建工程,也可以自己下载jar包,手动添加依赖
<!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.8.2</version>
</dependency>
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
public class RedisTest {
public static void main(String[] args) {
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxActive(20);
config.setMaxIdle(10);
JedisPool jedisPool = new JedisPool(config, "47.94.232.43", 6379, 1000);
Jedis jedis = jedisPool.getResource();
jedis.set("a".getBytes(), "a".getBytes());
byte[] bs = jedis.get("a".getBytes());
System.out.println(new String(bs));
jedisPool.destroy();
}
}
运行结果如下:
Exception in thread "main" redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool
at redis.clients.util.Pool.getResource(Pool.java:22)
at utils.RedisUtil.main(RedisUtil.java:232)
Caused by: redis.clients.jedis.exceptions.JedisConnectionException: java.net.SocketTimeoutException: connect timed out
at redis.clients.jedis.Connection.connect(Connection.java:120)
at redis.clients.jedis.BinaryClient.connect(BinaryClient.java:52)
at redis.clients.jedis.BinaryJedis.connect(BinaryJedis.java:1656)
at redis.clients.jedis.JedisPool$JedisFactory.makeObject(JedisPool.java:63)
at org.apache.commons.pool.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:840)
at redis.clients.util.Pool.getResource(Pool.java:20)
... 1 more
应该是redis的访问权限问题,修改一下redis的配置文件,也就是redis.conf文件
重启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:54)
at redis.clients.jedis.Protocol.process(Protocol.java:61)
at redis.clients.jedis.Protocol.read(Protocol.java:122)
at redis.clients.jedis.Connection.getBinaryBulkReply(Connection.java:168)
还报错,不过 根据异常的提示说明,有4种可能的解决方案吧,这里我说明一下其中的两种方案:
1.修改redis.conf中的protected-mode改为no
2.基于安全性考虑,要配置一个密码,那就在配一个吧
修改redis.conf,添加设置密码
requirepass wosrds333,
重新启动一下redis服务。
java代码也要加上密码jedis.auth(“wosrds333,”);
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
public class RedisTest {
public static void main(String[] args) {
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxActive(20);
config.setMaxIdle(10);
JedisPool jedisPool = new JedisPool(config, "47.94.232.43", 6379, 1000);
Jedis jedis = jedisPool.getResource();
jedis.auth("wosrds333,");
jedis.set("a".getBytes(), "a".getBytes());
byte[] bs = jedis.get("a".getBytes());
System.out.println(new String(bs));
jedisPool.destroy();
}
}
另外redis后台启动的话,修改redis.conf
daemonize yes
这样就简单的安装测试完成了。