Redis series (a) – installation and startup

Redis series article:
redis series (1) – installing and starting
redis series (two) – syntax and command

I. Introduction of Redis

Redis is one of NoSql, and before you figure out what Redis is, learn what NoSql is.

1, what is NoSql?

Full name: Not NoSql, Only Sql, is a non relational database, it can not replace the relations database, is a supplement to the relational database, is a series of problems can be solved with high concurrency, high availability, high scalability, large data storage and database solution.

NoSql has the following 4 categories: key value (Key-Value), storage database, column storage database, document type database, and graphics (Graph) database. And Redis belongs to the first kind: key value (Key-Value) storage database.

2, what is Redis?

Redis is a high performance key database developed using the C language, which stores data through some key types. The key types supported by Redis include the String character type, the map hash type, the list list type, the set collection type, and the sortedset ordered set type.

Redis’s application scenarios are as follows: caching (data queries, short connections, news content, commodity content, etc.), session separation in distributed cluster architectures, online friends lists, and task queues in chat rooms. (spike, rush, 12306, etc.), application ranking, site access statistics, data expiration processing (can be accurate to milliseconds). Among them, the cache application scene is the most.

Two, redis installation

General redis installed in the Linux server, so this example is introduced under the installation of Linux, if your computer is window or Mac, please install your own Baidu method.

This example uses the Linux system is CentOS6.7, such as Ubuntu and other Linux operating system commands, there may be individual differences, please learn to understand.

1, Download

Can go to redis’s official website to find each Redis version of the download address, such as:

Redis series (a) - installation and startup

2, installation

After using Xshell and other server terminals to connect software to connect to the Linux, do the following:

1) download compressed Redis

Download redis:

Redis series (a) - installation and startup

Use the LL command to view all files in the current directory

Redis series (a) - installation and startup

You can see that redis-3.0.0.tar.gz is the redis compression package that you just downloaded, and now unzip it:

Tar -zxvf redis-3.0.0.tar.gz

Use the LL command to view all files in the current directory

Redis series (a) - installation and startup

2) compile and install Redis

Enter the redis source code:

CD redis-3.0.0
Redis series (a) - installation and startup

Through the LL command, you can see all the files in the Redis source directory, and then you need to compile the source code:

Redis series (a) - installation and startup

Compiling the Redis using the make command requires the C language environment, CentOS comes with the C language environment, and if you use other Linux systems without the C language environment, you need to install, such as Yum installation: yum, install, gcc-c++

After compiling, it is installed, and the commands for installing Redis are as follows:

Make install PREFIX=/usr/local/redis
Redis series (a) - installation and startup

In the command, the “make install PREFIX=” in front of “Redis” is fixed, and “/usr/local/redis” is the directory of the installation, which is usually written. If you need to install it elsewhere, simply change this path.

Finally, check to see if the Redis installation is successful:

CD /usr/local/redis/
Redis series (a) - installation and startup

Using the LL command, you can see the bin folder, indicating that Redis has been installed successfully.

Three, Redis start and stop

Redis has two types of startup, namely, front end start and back end start. To start Redis, you need to execute the startup command in the Redis directory of bin, take a look at the bin directory structure:

Redis series (a) - installation and startup

1 、 front end start and stop

1) command that is started at the front end:

[[email protected] bin]#./redis-server
Redis series (a) - installation and startup

You can see the launch of Redis port 6379 (the default), the process of ID is 5979, at the same time, the front end after starting Redis, the terminal will enter the Redis console, no way to other Linux commands, namely the terminal window on the “scrap”, can only enter the Redis command to yourself.

2) shutdown command initiated by front end:

Force close: Ctrl+c closes normally: [[email protected], bin]#,./redis-cli, shutdown

These two commands are compared:

  • Force closure is simply performed directly on the Redis console (redis may lose some data).
  • Need to open a normally closed terminal window can be implemented (redis without loss of data, recommended).
Redis series (a) - installation and startup

It should be noted that once the front end shutdown command is executed, the redis console is closed and the redis service is stopped.

2, start and stop backend

Backend startup is the way we will definitely use it in development, but you need to do the following steps after using the backend startup command:

1) configuration of backend startup:

The first step is to copy the redis.conf file from the redis source directory to the bin directory of the redis installation directory.

Redis series (a) - installation and startup

The second step, modify the redis.conf file, change the value of daemonize to yes and save it.

[[email protected] bin]# VIM redis.conf
Redis series (a) - installation and startup

Linux’s VIM editor instructions, please learn by yourself. Here are some of the most commonly used instructions in VIM: I: enter edit mode. ESC: quit edit mode, go into browse mode (enter VIM editor, default is browse mode). : WQ: save, modify, and exit the VIM editor.

After the above configuration, there is no need to configure it later, and then you can start the redis background with the command.

2) command that is started by the backend:

[[email protected] bin]#./redis-server redis.conf
Redis series (a) - installation and startup

You can find back start command just start command more than the front end of the “redis.conf”, the meaning is clear, is to let the redis run according to the configuration of the configuration file. At the same time, can also see in the end, start the redis background, the terminal will not enter the redis console, which is the redis running in the background, we can now check system is there redis process:

[[email protected] bin]# PS -aux grep redis |
Redis series (a) - installation and startup

You can see that redis is running, the port is 6379, and the process ID is 6087.

3) the shutdown command that is started by the backend:

Force to close: [[email protected] bin]# kill -9 process ID normal shutdown: [[email protected], bin]#,./redis-cli, shutdown

Because the backend startup redis doesn’t work as directly as the front end of the redis, ctrl+c forces the redis to close, and if you need to force it off, you can only stop redis by killing the process. The normal shutdown of the redis command is the same as the front end, and there’s no nonsense here.

The project is advised to use normal shutdown. Because redis is used as a cache, the data is stored into memory, and if it is normally shut down, the memory data is persisted to the local and then closed. If forced to close, no persistent operations are performed, which can cause partial data loss.

Four, Redis client

1, redis comes with client

When you introduced the structure of the bin directory in the redis installation directory earlier, you have marked the redis client, which is redis-cli. This client has two commonly used functions:

  • Used to shut down redis services normally.
  • Let the terminal go into the redis console (used in the background running redis).

1) start

Start client commands:

[[email protected], bin]#,./redis-cli, -h,, -p, 6379
  • -h: Specifies the IP address of the redis server to access
  • -p: Specifies the port port of the redis server to access

If the IP address and port are used by default, the command can do so:

[[email protected] bin]#./redis-cli

Use the default configuration: the default IP [], the default port [6379]

2) off

Ctrl+c> quit

To shut down the client, any of the two commands above will not affect the preservation of the redis data.

2, graphical interface client

There is a graphical interface for redis client software, called redis-destop-manager. This is the software download page, Mac OS X, support Windows, Linux, according to its own computer system choose to download, here in the case of the windows, said the use of this software is simple, installation is simple, the way the next step can be installed after opening the application.

1) open the redis server connection configuration

Redis series (a) - installation and startup

2) add the redis server connection and test whether the connection is successful

Redis series (a) - installation and startup

3) check all the databases in the redis server

Redis series (a) - installation and startup

4) check key data in individual databases for data

Redis series (a) - installation and startup

3 code control client

Redis not only can use commands to operate, at present, basically the mainstream language has client support, such as: Java, C, C#, C++, PHP, Node.js, Go and so on.

In the official website, there are some Java clients, such as: Jedis, Redisson, Jredis, JDBC-Redis and so on. Among them, the official recommendation is to use Jedis and Redisson. The most used in the enterprise is Jedis.
Jedis is hosted on GitHub, click open.

1) simple redis usage

Step 1: import the following 2 jar packages

  • Jedis-2.7.0.jar
  • Commons-pool2-2.3.jar

The second step: creating the Jedis connection using the Redis server

@Test public void jedisClient (Jedis Jedis) {/ / create jedis = new Jedis ("", 6379); / / jedis.set assignment by Jedis ("S2", "222"); / / Jedis String S2 by value = jedis.get ("S2"); System.out.println (S2) Jedis (jedis.close); / / close;}

The third step: use the Jedis connection pool to get Jedis

@Test public void jedisPool (JedisPool JedisPool) {/ / jedisPool = new JedisPool ("", 6379); / / through the connection pool to get the jedis object Jedis jedis = jedisPool.getResource (String); S2 = jedis.get ("S2"); System.out.println (S2); jedis (jedis.close) / / close client; / / close connection pool jedisPool.close ();}

2) Spring integrate jedisPool

First step:

  • Add spring’s jar package
  • Configure the spring configuration file applicationContext.xml
< XML? Version= "1" encoding= "UTF-8" > < beans? Xmlns= "" xmlns:xsi= "" xmlns:mvc= "" xmlns: context= "" xmlns:aop= "" xmlns:tx= "" xsi:schemaLocation= " http:// Http:// spring-tx-3.2.xsd > <! -- connection pool configuration --> < bean id= "jedisPoolConfig" class= "redis.clients.jedis.JedisPoolConfig" > <! -- the maximum number of connections --> < property name= "maxTotal" value= "30" /> <! -- maximum idle connection number --> < property; name= "maxIdle" value= "10" /> <! -- each release of the maximum number of connections --> < property name= "numTestsPerEvictionRun" value= "1024" /> <! -- release scanning interval connection (MS) --> < property; name= "timeBetweenEvictionRunsMillis" value= "30000" /> <! -- connection idle time minimized --> < property name= "minEvictableIdleTimeMillis" value= "1800000" /> < release;! -- connection idle long after, when the idle time of > the > value and the free connection; direct release of --> maximum idle connections; < property name= "softMinEvictableIdleTimeMillis" value= "10000" /> <! -- get connected when the maximum number of milliseconds to wait, less than zero: the blocking time of uncertainty by default, -1 --> < property; name= "maxWaitMillis" value= "1500" /> <! -- in access to check the validity of the connection, the default false --> < property; name= "testOnBorrow" value= "false" /> <! -- check the validity in the spare time, the default false --> < property; name= "testWhileIdle" value= "true" /> < - whether blocking! The connection is depleted, false anomaly, ture blocked until the timeout, the default true --> < property; name= "blockWhenExhausted" value= "false" /> < /bean> <! -- redis single through the connection pool --> < bean id= "jedisPool" class= "redis.clients.jedis.JedisPool" destroy-method= "close" > < constructor-arg name= "poolConfig" ref= "jedisPoolConfig" /> < constructor-arg Name= "host", "", "/>", "<", "constructor-arg", "name=", "port", "value=", "6379", "/>"; "<"; "/bean>"; "value="; "<"; "/beans>";

The second step: test code

@Test public void (testJedisPool) {JedisPool = pool (JedisPool) applicationContext.getBean ("jedisPool"); Jedis jedis = null; try = {jedis (pool.getResource); jedis.set ("name", "Lisi"); String name = jedis.get ("name"); System.out.println (name) catch (Exception ex;} {(ex.printStackTrace)}); finally {if (jedis! = null) {(jedis.close); / / close connection}}}

Five, others

1, redis database number configuration

Redis creates 16 databases by default, and if you need to modify the number of libraries created, you can change the value of the databases in the redis.conf configuration file to change.

Redis series (a) - installation and startup

2, redis database selection

Redis defaults to a database of subscript 0, and if you need to switch the database, you can select the database using the select below label in the redis console.> select 15
Redis series (a) - installation and startup

3, problems

If you use the terminal software like Xshell to connect to server, please check whether the Linux system open port 22, by modifying the IPtable blocking rules open port 22, if using Ali cloud server, you need to modify the security group rules, a pile of concrete steps online, please modify their search learning.