Simple configuration and use of Redis

Source: Internet
Author: User
Tags download redis get ip redis centos server filezilla iptables redis server
I. Understanding of Redis

1, NoSQL overview
Before you know Redis, you should know the NoSQL.

(1) Nosql=not only SQL (read "N" "O" SQL instead of "no" SQL), is a relational database.

(2) Why need to use NoSQL. Gao Performance: High concurrent read and write High Storage: Efficient data storage and access High Scalability && High availability: Highly scalable and highly available

(3) NoSQL products: CouchDB,HBase,redis,mongoDB...

(4) NoSQL database four categories: key-value storage : Typical for Redis, but the data is missing structured column storage : HBase, Riak document database : MongoDB, low query efficiency Graphics database : Infogrid, neo4j, diagram structure

2, Redis overview

(1) Redis is an open source high efficiency key value pair database developed by C language, which provides a lot of data types (string, list, hash, ordered set, collection type).

(2) Application scenario data Cache task queue (seconds kill, snapping, etc.) data expiration processing site access statistics distributed cluster cluster architecture session separation II, Redis installation and configuration

1, prepare work to build the environment:VMWare virtual machine +CentOS Linux system (recommended Redis built in Linux server) SSH client:Secure CRT Uploading server file client:FileZilla CentOS needs gcc environment: compiling redis C language source files environment

2. Installation process

This is the need to have installed the virtual machine, and can successfully install the CentOS, can be shared with the local WinDOS network, that is, CentOS can be networked. But here is the Redis installation under the broken net.

(1) Download Redis compression package official website Download redis:redis-xxx.tar.gz
Select Stable stable version

(2) CentOS configuration Redis

1, first view the CentOS server host IP address, used to connect the server. Input command: IP add


Get IP Address 192.168.42.128
Next, log in to the server to transfer files. Using the FileZilla upload Redis compression package to the root directory: Open FileZilla

2, connect the server 192.168.42.128, enter the Linux server login account and password


Enter decompression tar compression pack command after login

Tar zxvf redis-4.0.2.tar.gz

You can then view the extracted files in the root directory redis-4.0.2

Enter the command into the redis-4.0.2 directory and compile make

CD redis-4.0.2/make

Then continue to enter the command installation in this directory, placing the installed files under the directory/usr/local/redis

Make install Prefix=/usr/local/redis

3, modify redis.conf configuration file: Enter the directory/usr/local/redis/bin

Cd/usr/local/redis/bin

Then modify the redis.conf file, first copy the redis.conf configuration file to the directory/usr/local/redis

CD REDIS-4.0.2/
CP Redis.conf/usr/local/redis

Then enter the/usr/local/redis directory to modify Conf

Cd/usr/local/redis
VI redis.conf

Enter vim to modify the parameters in Redis.conf, daemonize parameter is yes–> press esc-> colon:wq–> Save exit

4, Start Redis server
Enter Bin directory

Cd/usr/local/redis/bin  

Load configuration file Start Redis server

./redis-server  redis.conf

Then see if Redis started successfully

PS Aux|grep Redis  

The following information shows that Redis started successfully.

4281 of which is my Redis server process number, and 6379 is the default port number

5, Start Redis client, test Redis

Enter Bin directory

Cd/usr/local/redis/bin  

Start the client

./redis-cli  

127.0.0.1:6379 will appear, and then test the Redis function:

Third, the Jedis:java environment connection Redis

1, download two jar package: I used the Eclipse+maven
For specific maven How to guide packages can refer to Maven to import jar packages in several ways

2, write code

(1) Single instance of the test

public class Jedisdemo {

    /**
     * A simple way: Single Instance test
     * @author Lijian * *
     @Test public
    void Test1 () { C6/>//1, set IP address and port
        Jedis Jedis = new Jedis ("192.168.42.128", 6379);
        2, save the data
        jedis.set ("name", "Lijian");
        3, obtain the data
        System.out.println (jedis.get ("name")); 
        4, close resource
        jedis.close ();

    }


The result of the final test will be an error, as shown below: Connection timeout

This is because the Linux server is not open to port 6379 and the following configuration iptables Firewall command is possible.

Vim/etc/sysconfig/iptables

But CENTOS6 and 7 have no iptables. The following figure

This will require further setup.
Please refer to solve the problem that CentOS does not have iptables, can solve this problem

Enter command

Vim/etc/sysconfig/iptables

Move to a line with a port of 22 (22 port already open) in the Insert State, and then copy by YY, p for pasting, 22 to 6379, or copy of the following statement

-A input-m state--state new-m tcp-p TCP--dport 6379-j ACCEPT

Then reboot Iptables.service, set to boot

Systemctl Restart Iptables.service #重启防火墙使配置生效

systemctl enable Iptables.service #设置防火墙开机启动

But when you finally test the single-instance, you still get the following error

DENIED Redis is running into protected mode because protected mode is enabled .....

This is because the Protect-mode protection mode in the redis.conf configuration is yes, we can turn off the protection mode to No
Directory entering the Redis client

Open Client Redis-cli

Config set Protected-mode "No"

Last Run successful

(2) Test Connection pool connection mode

    @Test public
    void Test2 () {
        //1) to obtain the configuration object
        jedispoolconfig config = new Jedispoolconfig () for the connection pool;
        2, set the maximum number of connections:
        config.setmaxtotal (m);
        3, set the maximum number of idle connections
        Config.setmaxidle (m);
        4, get connection pool
        jedispool jedispool = new Jedispool (config, "192.168.42.128", 6379);
        5, obtain the core object
        Jedis Jedis =null;
        try{
        //6 and connecting
            Jedis=jedispool.getresource () by connection pool;
        7, set up data
            jedis.set ("Student", "xiaoming");
            String value = Jedis.get ("student");
            System.out.println (value);
        } catch (Exception e) {
            e.printstacktrace ();
        } finally{
            if (Jedis!= null) {
                jedis.close ();
            }
            if (Jedispool!=null) {
                jedispool.close ();}}}


    

Last Run successful

3, the configuration process of attention
(1) The bind parameter in redis.conf: default to

Bind 127.0.0.1

If you change to bind 0.0.0.0, you can access the server from other clients, not just native.

(2) Linux system default firewall is firewall, not iptables reference:

Https://www.cnblogs.com/nick-huang/p/5762565.html
Http://www.jb51.net/article/101576.htm

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.