Redis Learning Notes (1)
Redis is an open source, high performance, key-value-pair-based caching and storage system. Redis is the abbreviation of REmote DIctionary Server.
1. Download and install
wget http://download.redis.io/redis-stable.tar.gz//download, copy to/usr/local
tar -xzvf redis-stable.tar.gz //decompression
cd redis-stable //Enter the unzipped directory
make //Compilation and installation
make install //Assign executables to the / usr/local/bin directory, and when executed, do not enter a complete path
make test //Test whether redis is compiled correctly
2. Start and stop Redis
After the make install command is executed, executable files are generated in the / usr/local/bin directory
file name | Explain |
---|---|
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-rdb | RDB File Checking Tool |
redis-sentinel | Sentinel Server |
2.1 Start Redis
-
Direct Start (Development Environment)
You can start redis by running redis-server directly
The startup interface has the following information: Redis 3.2.8 (00000000/0) 64 bit Running in standalone mode//stand-alone mode Port: 6379//port number 6379 PID: 15448//Process PID 15448
Customize the port number by adding the port number to the ____________
-
Start through initialization script (production environment)
- Configure the initialization script. Copy the redis_init_script initialization script in utils directory under redis source directory to / etc/init.d, and modify the file name redis_6379.
-
Create a directory.
- / etc/redis: The configuration file for storing redis
- / var/redis/6379: Persistent files for redis
-
Modify the configuration file. Copy redis.conf from the source code file to the / etc/redis directory, named 6379.conf, and modify it as follows
parameter value Explain daemonize yes redis runs in daemon mode pidfile /var/run/redis_6379.pid Set the location of the redis PID file port Port number Setting the port number for redis listening dir /var/redis/6379 Setting the location of persistent files Start redis with / etc/init.d/redis_6379 start and execute sudo update-rc.d redis_6379 defaults command to enable it to boot automatically
If insserv: warning: script'redis6379'missing LSB tags and overrides errors occur, add the following in the / etc/init.d/redis_6379 file:
### BEGIN INIT INFO
# Provides: redis6379
# Required-Start: $local_fs $network
# Required-Stop: $local_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: redis6379
# Description: penavico redis 6379
### END INIT INFO
Then, the sudo update-rc.d redis_6379 defaults command is executed to start the service automatically, and the service redis_6379 start is executed to start the service. The service is checked whether sudo lsof-i:6379 is started or not. The successful start-up is as follows:
➜ redis sudo lsof -i:6379
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
redis-ser 16316 root 4u IPv4 189448 0t0 TCP localhost:6379 (LISTEN)
2.3 Stop redis
Forced termination of redis process will result in data loss, so redis should be stopped correctly and commands should be sent:
redis-cli SHUTDOWN
Or use the kill command to end the redis process with the same effect.
3. Command return value
- State response: Direct return of state information, including PING commands
127.0.0.1:6379> PING
PONG
127.0.0.1:6379> SET 'test' 1
OK
- Error Response: Start with (errno) and keep up with the error message
127.0.0.1:6379> TESTERR
(error) ERR unknown command 'TESTERR'
- Integer reply: Start with integer and keep up with integer data
127.0.0.1:6379> DBSIZE
(integer) 1
- String response: wrapped in double quotation marks
127.0.0.1:6379> GET test
"1"
127.0.0.1:6379> GET testtest
(nil) //Represents no value, empty result
- Multi-line string reply: Each line begins with a serial number
127.0.0.1:6379> KEYS * //Key Name Existing in Current Database
1) "hello"
2) "TEST"
3) "test"
4. configuration
4.1 Dynamic Configuration
Modify by CONFIG SET name value command
127.0.0.1:6379> CONFIG GET loglevel
1) "loglevel"
2) "notice"
127.0.0.1:6379> CONFIG SET loglevel warning
OK
127.0.0.1:6379> CONFIG GET loglevel
1) "loglevel"
2) "warning"
//The first line of string represents the option name, and the second line is the option value.
4.2 redis configuration file redis.conf option
- Connection options
bind 127.0.0.1 //Default binding host address
port 6379 //Default port
timeout 0 //When the client is idle for how long to close the connection, 0 means that the option is not started.
loglevel notice //Logging level
// debug (very detailed information, suitable for development and testing)
// verbose
// Note (Suitable for Production Environment)
// warning
logfile "" //Logging mode, default to standard output
databases 16 //The default number of databases is 16, numbered from 0 to 15
- Snapshot Options (SNAPSHOTTING)
save <seconds> <changed> //How many sec s and at least how many changes synchronize them into data files on disk
save 900 1 //900 seconds (15 minutes) at least once
save 300 10 //300 seconds (5 minutes) at least 10 times
save 60 10000 //At least 10,000 times in 60 seconds (1 minute)
rdbcompression yes //Store local database with LZF compression enabled by default yes
dbfilename dump.rdb //Specify the local database file name by default dump.rdb
dir ./ //Specify the local database storage directory, default is the current directory
5. Multi-database
From the redis.conf file, you can see that redis has 16 databases by default. After redis starts the connection, it will automatically select database 0, but it can be replaced by SELECT command.
127.0.0.1:6379> SELECT 15
OK
127.0.0.1:6379[15]>
Note: This article refers to the second edition of "Introduction Guide to redis"