I. Installation of Redis
The first step must be installed. Before installing the version rule, the Redis contract version number (number after the first decimal point) for even is stable version, the odd is not stable version, here is recommended to use a stable version of learning, development and production environment.
1, in the POSIX system installation
Redis compatible with most POSIX systems, Linux, OS X, BSD, etc., it is recommended to use the source code to compile the installation. Http://redis.io/download Download the stable version here.
In Linux, unzip after downloading, here I use the 2.8.6 version, use the following command to decompress:
Tar xvzf redis-2.8.6.tar.gz
After decompression, enter the folder, the direct make command can complete the installation. If you want to put some of the executable programs needed in the Redis into the/usr/local/bin directory so that you can execute the program later, you do not have to enter the full path, the Make install command is executed.
Redis also recommends using a make Test command to test whether the Redis is compiled correctly.
2. Installing in Windows system
Redis official does not support Windows. Here's an official link.
Https://github.com/MSOpenTech/redis/releases/download/win-3.2.100/Redis-x64-3.2.100.msi Second, start and stop Redis
If the make install command is not executed, the default executable program is to recommend or execute make install under the SRC folder of the Redis directory.
file name |
Description |
Redis-server |
Redis Server |
Redis-cli |
Redis Command Line Client |
Redis-benchmark |
Redis Performance Testing Tool |
Redis-check-aof |
AOF File Repair Tool |
Redis-check-dump |
RDB File Checking Tool |
Our most common use is the front two.
1, start
In both ways, the direct startup and initialization scripts start two, which apply to the development environment, and the latter to the production environment.
① Direct boot, run Redis-server, redis the default listener 6379 port, through the –port parameters can customize the port number.
② initialization script started: In order for the Redis to start automatically, this method is used in production environments. There is a Redis_init_script initialization script file under the Redis Source Directory Utils folder, and we need to configure how Redis is run and persisted, where the log files are stored, and so on.
The contents are as follows: Shell
#!/bin/sh # Simple Redis INIT.D script conceived to work on Linux systems # as it does the use of The/proc filesystem. redisport=6379 Exec=/usr/local/bin/redis-server cliexec=/usr/local/bin/redis-cli PIDFILE=/var/run/redis_${
Redisport}.pid conf= "/etc/redis/${redisport}.conf" case "$" in start If[-f$pidfile] Then echo "$PIDFILE exists, process is already running or crashed" else echo "starting Redis server.
.."
$EXEC $conf fi;; Stop) If[!-f$pidfile] then echo "$PIDFILE does not exist, the process is not running" E
LSE pid=$ (Cat$pidfile) echo "Stopping ..." $CLIEXEC-p$redisportshutdown
WHILE[-X/PROC/${PID}] do echo ' waiting for Redis to shutdown ... '
Sleep1 done echo "Redis stopped" FI;
*) echo "Please use start or stop as the" "argument";; Esac
The steps are as follows:
(1) Talk about the configuration file copied to the/ETC/INIT.D, named Redis_ port number, the client through the secondary port coupling redis, and need to modify the script Redisport variable value for the above port number.
(2) Create the desired folder.
/etc/redis ===> Storage Redis config file
/var/redis/Port number ===> store Redis persisted file
(3) configuration file modification
The configuration file here is a redis.conf file in the Redis directory that needs to be copied to the/etc/redis directory and renamed to the port number. conf, then make the following modifications.
Parameters |
value |
Description |
Daemonize |
Yes |
To enable Redis to run in daemon mode |
Pidfile |
/var/run/redis_ port number. PID |
To set the Redis PID file location |
Port |
Port number |
Set the port number for Redis listening |
Dir |
/var/redis/Port number |
Set Persistent file storage location |
You can now use the/etc/init.d/redis_ port number start to start the Redis. The following command is then executed Redis with the system boot.
#sudo UPDATE-RC.D redis_ Port number Defaults
For Windows, the basics are simple: cd C:\Program files\redis redis-server. \redis.windows.conf
2. Stop
Forcing the termination of the Redis process may lose data, given that Redis is likely to be writing memory data to disk. The correct way is as follows: Shell
#redis-cli SHUTDOWN
For WINDOWS:CD C:\Program Files\redis
REDIS-CLI SHUTDOWN
When the Redis receives the command, it disconnects the client and then exits after it is persisted based on the configuration.
Redis can properly handle the sigterm signal, so using the KILL command can also end the Redis normally, as is the shutdown command.
If you use the Redis JAVA API in Eclipse, you need the following two jars to support
Jedis-2.4.2.jar
Commons-pool2-2.0.jar
Source Code https://github.com/xetorthio/jedis/releases/tag/jedis-2.7.2
Jar Download Address http://download.csdn.net/detail/teamlet/8914387
Compiling jedis2.7.2 requires the use of Apache Commons Pool
http://commons.apache.org/proper/commons-pool/download_pool.cgi
or http://download.csdn.net/detail/teamlet/8914319.
Third, Redis client command
I've also said a REDIS-CLI command to execute, and here are some other commands.
#redis-cli-h 127.0.0.1-p 6379
The above command should be very nice to understand, the-h parameter specifies that the HOST,-P command specifies the port, and the default if no arguments are entered are the locally connected local service.
#redis-cli PING
The above command is Redis provided a test client and Redis connection is normal test, if normal, will return to Pong.
Four, multiple databases
Redis supports 16 databases by default, and can be modified by configuration parameters database. Redis is not the same as our common database, his database name is named after the 0~15 number (for example, the default 16 databases), the client connects to the default selection of the No. 0 database, if you need to switch can use the SELECT command switch.
Redis 127.0.0.1:6379 & gt; Select 1 OK redis [1] 127.0.0.1:6379 & gt; Select 0 OK
First, Redis does not support the custom database name, go to coarse redis also does not support each database one account password, as long as can access the database 0, can access each other database. And each database is not completely isolated, such as the Flushall command, you can empty all the data in the entire Redis instance. So this kind of database is more like a namespace, not suitable for storing different programs.
In fact, we can use different programs to different Redis instances, an empty Redis instance occupies only about 1 m, so don't worry about multiple Redis instances will occupy a lot of memory.
The preparation environment is finished, and the following is where to start learning specific commands.