Redis research (2)-preparation and redis research preparation
1. Install Redis
Install Redis here: http://blog.csdn.net/wtyvhreal/article/details/40980167
Ii. Start and Stop Redis
Description of the Redis executable file:
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 check Tool
Redis can be started directly or through initialization scripts, which are applicable to the development environment and production environment respectively.
2.1 start directly
This method is recommended in the development environment.
redis-server
The server uses port 6379 by default (the number corresponding to merz on the 6379 mobile phone keyboard, and MERZ is the name of an Italian singer). You can use the -- port parameter to customize the port number.
redis-server --port 6380
2.2 start Redis through the initialization script
In linux, you can use the initialization script to start Redis so that Redis can run automatically with the system. This method is recommended in the production environment (not required for student exercises ).
We need to configure the Redis running mode, persistent file, and log file storage location. The specific steps are as follows:
(1) configure the initialization script:
In the utils file in the Redis source code directory, there is an initialization script file named redis_init_script. First, copy the file to/etc/init. in the d directory, the file name is redis _ port number. The client connects to Redis through this port number, and then modifies the REDISPORT variable value of line 6th of the script file to the same port number.
(2) create a required Folder:
/Etc/redis stores the Redis configuration file
/Var/redis/port number stores Redis persistent files
(3) modify the configuration file:
Copy the configuration file to the/etc/redis directory, name it with the port number (6379. conf), and edit some of the following parameters.
Daemonize yes enables Redis to run in daemprocess Mode
Pidfile/var/run/redis _ port number. pid sets the location of the Redis PID File
Set the port number of the Redis listener
Dir/var/redis/port number to set the location for storing persistent files
Now you can run the following command to start Redis,
/Etc/init. d/redis _ port number start
Then you need to execute the following command to make Redis automatically start with the System
Sudo update-rc.d redis _ port defaults
2.3 stop Redis
When you stop Redis, data in the memory may be synced to the hard disk. forcible termination of the Redis process may cause data loss. The correct stop method is to send the SHUTDOWN command to Redis. After receiving the SHUTDOWN command, Redis disconnects all clients, runs persistence based on the configuration, and finally exits.
redis-cli SHUTDOWN
Redis can process SIGTERM signals, so using the PID of the kill Redis process can also be normal technology Redis, the effect is the same as sending the SHUTDOWN command.
Iii. Redis command line Client
3.1 send command
(1) The first method is to execute commands as redis-cli parameters.
Redis-cli-h 127.0.0.1-p 6379
You can use the-h and-p parameters to customize the address and port number.
(2) The second method, without parameters running redis-cli, will enter the interactive mode and can enter the command freely
3.2 command Return Value
1. Status reply
Direct display of status information
2. Error Response
The error response starts with an error and is followed by an error message.
3. Integer reply
Start with integer and follow the integer data
4. String reply
Bulk reply can get a string reply when requesting the key value
5. Multi-line string reply
4. Redis multi-database
Each database in Redis is named progressively from 0. By default, Redis supports 16 databases. You can modify this number by configuring the databases parameter. After the client establishes a connection with Redis, database 0 is automatically selected, but the SELECT command can be used to change the database at any time.
Redis does not support setting different access passwords for each data database. Therefore, a client can either access all databases or even one database. The most important thing is that multiple databases are not completely isolated. For example, the FLUSHALL command can clear data in all databases in a Redis instance.
Therefore, these databases are more like a command space than suitable for storing data of different applications.
Different applications should use different Redis instances to store data.
Because Redis is very lightweight, an empty Redis instance occupies about 1 MB of memory, so there is no need to worry that multiple Redis instances will occupy a lot of memory.