Redis installation and Jedis on Linux simple to use

Source: Internet
Author: User

First, the Official document introduction way

The version shown here is the Redis4.0.6,linux system is centos6.7,jdk1.7,jedis2.8.1

Download, unzip, compile:

$ 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

Binary files are compiled after completion in the SRC directory, start the Redis service with the following command:

$ src/redis-server

You can use the built-in client command redis-cli:

$ src/redis-cliredisset  foo barokredisget  foo"bar "
second, the Java program Jedis operation Redis

The above approach is only a small exercise, and we now use Jedis with Java programs to operate Redis on Linux servers.

Use Maven to introduce Jedis:

    <Dependencies>        <Dependency>            <groupId>Redis.clients</groupId>            <Artifactid>Jedis</Artifactid>            <version>2.8.1</version>        </Dependency>    </Dependencies>

Java code:

     Public Static void Main (string[] args) {      //  Ip,redis default port number for virtual machine settings        new Jedis (" 192.168.133.128 ", 6379);        Jedis.set ("Key01", "Zhangsan");        Jedis.set ("key02", "Lisi");        System.out.println (Jedis.get ("Key01"));    }

Note that the above code is problematic!

iii. redis configuration file

When the above code is run, an error will be

Redis.clients.jedis.exceptions.JedisConnectionException:java.net.ConnectException:Connection Refused:connect

The connection was rejected because the Redis access IP default is 127.0.0.1

You need to modify it in the redis.conf configuration file:

The document is long, you can find the "bind" string with the "/" command, and press N to search for the next

:/bind

Add the IP, and then when you start the Redis service, you need to load the configuration file manually

My profile is placed in the same directory as the Server service, so when you start the service, you enter:

./redis-server redis.conf

Note: If you do not enter the following profile directory, then the profile does not work and will prompt you to start the default configuration file.

Then run the Java code again

and Error!!

Redis.clients.jedis.exceptions.JedisDataException:DENIED Redis is running in protected mode because protected mode is ENA Bled, no bind address was specified, no authentication password are requested to clients. The This mode connections is only accepted from the loopback interface. If you want-to-connect from external computers to Redis-adopt one of the following solutions:1) Just disable prot ected 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 are not publicly accessible from Internet if your 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 PROTECTE d 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 does one of the above things in order for the server to start accepting connections from the outside.

It's a long, bad quote.

The good blogger helped you Google translate a bit.

In simple terms? is to offer you a couple of solutions.

1) simply disable protected mode and send the command "CONFIG SET protected-mode No" from the loopback interface by connecting to Redis from the same host, but if you do, do not use the Internet for public access. Use config rewrite to make this change permanent.

2) Alternatively, you can disable protected mode by editing the Redis configuration file and setting the protected mode option to "no" and then restart the server.

3) If you are starting the server manually for testing purposes, restart the server using the " --protected-mode no" option.

4) Set the binding address or authentication password.

This is a new feature of the redis4.0 version, REDIS3 will not error.

Here I choose to set up Redis password, also open redis.conf profile, set Password to 123456, save exit

Then start the server

After that you want to open the Redis client with a command in Linux, you need to enter some parameters

Obviously,-H is the ip,-p of the Redis service, and-A is the password for the Redis service, which can be changed in redis.conf

And then it's all right.

This time, the problem in the Java code is not resolved, you also need to add a password in the Java code settings

     Public Static void Main (string[] args) {        //  virtual machine set Ip,,redis default port number        new Jedis (" 192.168.133.128 ", 6379);         // redis access Password        Jedis.auth ("123456");        Jedis.set ("Key01", "Zhangsan");        Jedis.set ("key02", "Lisi");        System.out.println (Jedis.get ("Key01"));    }

OK, normal operation

Redis installation and Jedis on Linux simple to use

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.