码迷,mamicode.com
首页 > 系统相关 > 详细

redis在Linux上的安装和jedis简单使用

时间:2017-12-10 17:06:55      阅读:242      评论:0      收藏:0      [点我收藏+]

标签:练习   ane   java代码   his   ima   perm   password   退出   test   

一、官方文档介绍方式

这里演示的版本是Redis4.0.6,Linux系统是CentOS6.7,Jdk1.7,Jedis2.8.1

下载,解压,编译:

$ wget http://download.redis.io/releases/redis-4.0.6.tar.gz
$ tar xzf redis-4.0.6.tar.gz
$ cd redis-4.0.6
$ make

二进制文件是编译完成后在src目录下,通过下面的命令启动Redis服务:

$ src/redis-server

你可以使用内置的客户端命令redis-cli进行使用:

$ src/redis-cli
redis> set foo bar
OK
redis> get foo
"bar"

二、Java程序中jedis操作redis

上面的方式只是一种小练习,我们现在通过Java程序用jedis来操作Linux服务器上的redis。

用maven来引入jedis:

    <dependencies>
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.8.1</version>
        </dependency>
    </dependencies>

Java代码:

    public static void main(String[] args) {
      // 虚拟机设置的ip,redis默认端口号
        Jedis jedis = new Jedis("192.168.133.128", 6379);
        jedis.set("key01", "zhangsan");
        jedis.set("key02", "lisi");
        System.out.println(jedis.get("key01"));
    }

注意上面的代码是有问题的!

三、redis配置文件

上面的代码运行后,会报错

redis.clients.jedis.exceptions.JedisConnectionException: java.net.ConnectException: Connection refused: connect

连接超被拒绝了,这是因为,redis的访问ip默认是127.0.0.1

你需要在redis.conf配置文件中修改:

技术分享图片

文档很长,可以通过"/"命令来查找"bind"字符串,按n搜索下一个

:/bind

把ip添加进去,之后启动redis服务的时候,需要手动加载配置文件

技术分享图片

我的配置文件放在了和server服务的同一个目录里,所以启动服务时输入:

./redis-server redis.conf

注意啊:如果不输入后面的配置文件目录,那么该配置文件不起作用,会提示说启动默认的配置文件。

技术分享图片

 

之后再次运行Java代码

又报错!!

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.

这错报的好长。。

好心的博主帮你谷歌翻译了一下。

简单来说呢?就是给你提供了几个解决方案

1)只需禁用保护模式,即可通过从同一主机连接到Redis,从回送接口发送命令“CONFIG SET protected-mode no”正在运行,但是如果您这样做,请勿使用互联网公开访问互联网。使用CONFIG REWRITE使此更改永久。

2)或者,您可以通过编辑Redis配置文件并将保护模式选项设置为“no”来禁用保护模式,然后重新启动服务器。

3)如果您只是为了测试而手动启动服务器,请使用“ --protected-mode no”选项重新启动服务器。

4)设置绑定地址或认证密码。

这是redis4.0版本的新特性,redis3不会报错。

在这里我选择设置redis密码,同样打开redis.conf配置文件,设置密码为123456,保存退出

技术分享图片

然后启动服务器

技术分享图片

之后你要想在Linux里用命令打开redis客户端,需要输入一些参数

技术分享图片

很显然,-h是redis服务的ip,-p是redis服务的端口号,-a是redis服务的密码,都可以在redis.conf里更改的

然后就好了

技术分享图片

这个时候,Java代码中的问题还没解决完,你还需要在Java代码中增加一条密码设置

    public static void main(String[] args) {
        // 虚拟机的设置的ip,,redis默认端口号
        Jedis jedis = new Jedis("192.168.133.128", 6379);
        // redis访问密码
        jedis.auth("123456");
        jedis.set("key01", "zhangsan");
        jedis.set("key02", "lisi");
        System.out.println(jedis.get("key01"));
    }

技术分享图片

OK,运行正常

 

redis在Linux上的安装和jedis简单使用

标签:练习   ane   java代码   his   ima   perm   password   退出   test   

原文地址:http://www.cnblogs.com/dijia478/p/8016993.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!