Redis installation and debugging
Install and debug Redis linux: 64-bit CentOS 6.5
Redis version: 2.8.17 (updated to April October 31, 2014)
Redis Official Website: http://redis.io/
Redis Common commands: http://redis.io/commands
1. Install Redis
# Wget http://download.redis.io/releases/redis-2.8.17.tar.gz
# Tar xzf redis-2.8.17.tar.gz
# Cd redis-2.8.17
# Make
# Make install
If an error is reported
Zmalloc. h: 50: 31: error: jemalloc/jemalloc. h: No such file or directory <br> zmalloc. h: 55: 2: = "" error: = "" # error = "" & quot; newer = "" version = "" of = "" jemalloc = "" required & quot; <br = ""> make [1]: = "" *** = "" [adlist. o] = "" error = "" 1 <br = "" leaving = "" directory = "" '= "" data0 = "" src = "" redis-2.6.2 = "" src'
Solution:
Make MALLOC = libc
Note: Redis does not implement its own memory pool and does not add its own stuff to the standard system memory distributor.
Redis-2.4 above comes with jemalloc, you do not need to add any parameters through zmalloc. in the c source code, we can see that Redis will first determine whether to use tcmalloc during compilation. If yes, it will replace the function implementation in the standard libc with the function implementation corresponding to tcmalloc. The second step is to determine whether jemalloc is enabled. If it is not used, the memory management function in the standard libc will be used. Therefore, we recommend that you use jemalloc to optimize tcmalloc.
To install tcmalloc, you can:
# Make USE_TCMALLOC = yes
Reference: Use TCMalloc to replace the malloc memory allocation of Nginx and Redis default glibc Libraries
Modify the CFLAGS parameter for debugging.
# Make CFLAGS = "-g-O0"
After the make command is executed, five executable files are generated in the src directory, they are redis-server, redis-cli, redis-benchmark, redis-check-aof, and redis-check-dump. Their functions are as follows:
Redis-check-dump: used for local database check
Why is the installation of three axes useless in standard Linux? The official wiki says this: Redis can run just fine without a configuration file (when executed without a config file a standard configuration is used ). with thedefault configuration Redis will log to the standard output so you can check what happens. later, you canchange the default settings.
You can also make install to copy the executable file to/usr/local/bin.
After make, a prompt will appear:
Hint: To run 'make test' is a good idea ;)
-----------------------------------------------------------------
In fact, it is not tested and can be used in general. But since people have suggested it, let's take a look at make test.
Run # make test
Error reported, prompting You need 'tclsh8. 5' in order to run the Redis test
Go to the official Tcl website http://www.tcl.tk/download Version 8.5
Then install tcl8.5:
(The configure and make locations are special. In the unix installation directory, the following is the official installation method of tcl)
# Tar xvzf tcl8.5.12-src.tar.gz
# Cd tcl8.5.13/unix/
#./Configure
# Make
# Make test
# Make install
Note: Of course, you can also use yum install tcl to install it.
After installing tcl, run make test in the redis directory. Tip:
\ O/All tests passed without errors!
This indicates that redis is properly installed. Yes.
-----------------------------------------------------------------
Install
# Make install
2. Run Redis
In version 2.8.17, The redis-server is stored in the src folder.
Start the Redis Server
#/Usr/redis-2.8.17/src/redis-server
If no daemonize no configuration is changed, the running information is displayed.
Note:
▲The default port number of redis is 6379. (According to the redis author antirez's blog post, 6379 is the MERZ number on the mobile phone button, and MERZ is taken from the name of Alessia Merz, the Italian karaoke girl. MERZ has long been synonymous with antirez and its friends as stupid .)
▲There are two storage methods for Redis. The default method is the snapshot method. The implementation method is to regularly save the snapshot of the memory to the hard disk, the disadvantage of this method is that if a crash occurs after persistence, a piece of data will be lost. Therefore, driven by the perfectionist, the author adds the aof method. Aof is append only mode. When writing memory data, the Operation Command is saved to the log file.
Run Redis later
You need to read the configuration file to start
Note that redis is copied by default. the daemonize parameter in the conf file is no, so redis will not run in the background. We can modify redis. conf file. This file is under the decompressed redis root directory.
Daemonize yes
If you want to provide your redis. conf, you have to run it using an additional
% Cd src
#/Usr/redis-2.8.17/src/redis-server/usr/redis-2.8.17/redis. conf
View redis Processes
# Ps aux | grep redis
Start Multiple redis instances
Copy the default redis. conf file to redis6383.conf, open the redis6383.conf configuration file, find port 6379, and change 6379 to 6383.
#/Usr/redis-2.8.17/src/redis-server
#/Usr/redis-2.8.17/src/redis-server/usr/redis-2.8.17/redis6383.conf
Call the service:
#/Usr/redis-2.8.17/src/redis-cli
New
Redis> set foo bar
Obtain
Redis> get foo "bar"
Delete
Redis> del foo
Fuzzy search
Redis> keys f *
Redis> keys f? O?
View info Information
Run the # info command to check the memory fragmentation rate: mem_fragmentation_ratio = 2.59.
The jemalloc memory distributor is used by default.
3. Configure Redis
The redis configuration file is in your installation directory. The name is redis. conf.
To put it simply, redis. conf:
Redis does not use daemonize by default. If you need to change daemonize no to daemonize yes. (You can check the printed information without changing it during the test .)
If you are not comfortable with redis's default port 6379, you can change port 6379.
If you want to put the data file in a specified folder, change dir/opt/data/
The default value is dir./, that is, it is stored in the installation directory by default.
Connection timeout, timeout 300, no changes to the header ......
Dir is the data file path. By default, it is in the installation directory.
* Select either of the following configurations. For more information, see section 2 in this article.
###### SNAPSHOTTING ##### memory snapshot method:
The default memory snapshot policy is,
There should be at least one data change within 900 seconds (15 minutes;
Or at least 10 data changes within 300 seconds;
Or within 60 seconds, there are at least 1000 data changes. The time + number of data changes affect the appearance of memory snapshots.
###### Append only mode ###### AOF Method
Appendfsync everysec synchronization per second. Comment out the following option: appendfsync no
For other configurations, the comments in the conf file are quite clear, so I will not talk nonsense. Let's just look at your configuration.
You can copy the configuration file to etc.
Mkdir/etc/redis
Cp redis. conf/etc/redis. conf
Mkdir/var/lib/redis
1. redis. conf configuration parameters:
# Running as a daemon
Daemonize yes
# If a later process runs, you must specify a pid. The default value is/var/run/redis. pid.
Pidfile redis. pid
# Bind the Host IP address. The default value is 127.0.0.1.
# Bind 127.0.0.1
# Redis default listening port
Port 6379
# How many seconds after the client is idle, disconnect. The default value is 300 (seconds)
Timeout 300
# Log record level, with four optional values: debug, verbose (default), notice, and warning
Loglevel verbose
# Specify the log output file name. The default value is stdout. You can also set it to/dev/null to shield logs.
Logfile stdout
# Number of available databases; default value: 16; default value: 0
Databases 16
# Policy for saving data to disk
# If one of the Keys data is changed, it will be refreshed to disk once every 900 seconds.
Save 900 1
# When 10 Keys data items are changed, refresh them to disk once every 300 seconds
Save 300 10
# When pieces of keys data are changed, refresh to disk once every 60 seconds
Save 60 10000
# Whether to compress data objects when dump. rdb Databases
Rdbcompression yes
# Local database file name, default value: dump. rdb
Dbfilename dump. rdb
# Local database storage path. The default value is ./
Dir/var/lib/redis/
########### Replication #####################
# Redis replication Configuration
# Slaveof <masterip> <masterport> when the local machine is a slave service, set the master service IP address and port
# Masterauth <master-password> when the local machine is a slave service, set the master service connection password
# Connection password
# Requirepass foobared
# Maximum number of client connections, unlimited by default
# Maxclients 128
# Maximum memory usage settings. When the maximum memory is reached, Redis will first try to clear expired or expiring keys. After this method is processed, any Key that has reached the maximum memory settings will be cleared, no more write operations can be performed.
# Maxmemory <bytes>
# Whether to record logs after each update operation. If not enabled, data may be lost for a period of time during power failure. Because redis synchronizes data files according to the save conditions above, some data will only exist in the memory for a period of time. The default value is no.
Appendonly no
# Update the log file name. The default value is appendonly. aof.
# Appendfilename
# Update log conditions. There are three optional values. "No" indicates that data is cached and synchronized to the disk by the operating system. "always" indicates that data is manually written to the disk by calling fsync () after each update operation. "everysec" indicates that data is synchronized once per second (default ).
# Appendfsync always
Appendfsync everysec
# Appendfsync no
############### Virtual memory ###########
# Whether to enable the VM function. The default value is no.
Vm-enabled no
# Vm-enabled yes
# Virtual memory file path. The default value is/tmp/redis. swap. It cannot be shared by multiple Redis instances.
Vm-swap-file/tmp/redis. swap
# Store all data greater than vm-max-memory into the virtual memory. No matter how small the vm-max-memory settings are, all the index data is stored in the memory (Redis's index data is keys ), that is to say, when vm-max-memory is set to 0, all values exist on the disk. The default value is 0.
Vm-max-memory 0
Vm-page-size 32
Vm-pages 134217728
Vm-max-threads 4
############ Advanced config ###############
Glueoutputbuf yes
Hash-max-zipmap-entries 64
Hash-max-zipmap-value 512
# Whether to reset the Hash table
Activerehashing yes
Note: The official Redis documentation provides some suggestions on VM usage:
When your key is small and the value is large, it will be better to use VM. this saves a lot of memory. when your key is not hour, you can consider using some very method to convert a large key into a large value. For example, you can consider combining the key and value into a new value. it is best to use linux ext3 and other file systems that support sparse files to save your swap files. the vm-max-threads parameter can be used to set the number of threads accessing the swap file. It is best to set the number of threads not to exceed the number of machine cores. if it is set to 0, all operations on swap files are serial. it may cause a long delay, but it guarantees data integrity. 2. adjust System Kernel Parameters
If the memory is insufficient, you need to set the kernel parameters:
Echo 1>/proc/sys/vm/overcommit_memory
Here we will talk about the meaning of this configuration:/proc/sys/vm/overcommit_memory
4. debug
Note: Because redis enables memory optimization by default, you must modify the compilation option. Otherwise, the prompt "<value optimized out>" is displayed when variables are printed in gdb. This is mostly due to gcc optimization. we can add the-O0 option to forcibly disable gcc compilation optimization.
Compiling Redis without optimizations
By default Redis is compiled with the-O2 switch, this means that compiler optimizations are enabled. this makes the Redis executable faster, but at the same time it makes Redis (like any other program) harder to inspect using GDB.
Therefore, you need to modify the Makefile. The file is in the/src directory and OPTIMIZATION? =-O2 option. Change it to O0.
OPTIMIZATION? =-O0
Delete the original redis, modify makefile, and make again to start redis.
Debugging tips: View pointer Variables
View redis Processes
# Ps aux | grep redis
Attach gdb to process
# Gdb-p process id
(Gdb) r start again, otherwise it will not start from the main function.
(Gdb) break main sets breakpoints
(Gdb) list to view code
(Gdb) p variable name to view the variable content, use p to view the variable, this time can be viewed
Redis debugging skills
Redis will process events cyclically in the aeMain function of AE. c:
// Void aeMain (aeEventLoop * eventLoop) {eventLoop-> stop = 0; while (! EventLoop-> stop) {// if a function needs to be executed before event processing, run if (eventLoop-> beforesleep! = NULL) eventLoop-> beforesleep (eventLoop); // start to process the event aeProcessEvents (eventLoop, AE _ALL_EVENTS );}}
#/Usr/redis-2.6.14/src/redis-cli
Redis> set foo bar
OK
Redis> get foo "bar"
The aeMain function intercepts the request from redis-cli. Only when the aeMain function processes the request, redis-cli returns OK. Otherwise, it will wait.
5. Benchmark Test
First, start redis with the startup command.
Then run the following command in any directory:
# Redis-benchmark-h localhost-p 6379-c 100-n 100000
Simulate 100 concurrent connections and 100000 requests, and check the performance of the redis server whose host is localhost and port is 6379.
Address: http://blog.csdn.net/unix21/article/details/9526295
------------------------------------------------------------------------
More references
For details about the configuration, refer to Redis 2.2.14 installation and configuration in CentOS.
Installation Reference: Redis (1) Installation
Debugging reference: http://redis.io/topics/debugging using gdb to debug redis-server
Benchmark parameter: Redis installation and deployment
In addition, to use redis in PHP, you need to install PHP extensions. For details, refer:
Php extension installation for Redis and Redis
Phpredis usage
Common redis commands