Redis Installation and Commissioning

Source: Internet
Author: User
Tags benchmark hash install php mkdir redis redis version centos redis server


Redis Installation and commissioning

Redis installation and commissioning Linux version: 64-bit CentOS 6.5

Redis version: 2.8.17 (updated to October 31, 2014)

Redis Official website: http://redis.io/

Redis Common commands: Http://redis.io/commands

1. Installing 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

Zmalloc.h:50:31:error:jemalloc/jemalloc.h:no such file or directory
Zmalloc.h:55:2: Error: #error "Newer version of Jemalloc required"
MAKE[1]: * * * [ADLIST.O] Error 1
MAKE[1]: Leaving directory '/DATA0/SRC/REDIS-2.6.2/SRC '
Make: * * * [ALL] Error 2

The solution is:

Make MALLOC=LIBC


Note: Redis does not implement its own memory pool, and does not add its own stuff to the standard system memory allocator.

redis-2.4 above with Jemalloc, you do not need to add any parameters, through the ZMALLOC.C source code we can see, redis at compile time, will first determine whether to use Tcmalloc, if so, will use tcmalloc corresponding function to replace the standard LIBC function implementation. The second is to determine whether Jemalloc makes, and finally if not used, the standard LIBC memory management functions. So use Tcmalloc optimization carefully, these two splitter fragment rate difference is not small, it is recommended to use the Jemalloc.


If you want to install Tcmalloc you can do this:

# Make Use_tcmalloc=yes

Reference: malloc memory allocation with Tcmalloc replacement nginx and Redis default GLIBC libraries


To Modify the Cflags parameter in order to debug

# Make cflags= "-g-o0"

When the make command is completed, 5 executables are generated in the SRC directory, namely Redis-server, REDIS-CLI, Redis-benchmark, redis-check-aof, Redis-check-dump, Their role is as follows:
Redis-server:redis Server Daemon Startup program
Redis-cli:redis command-line operation tool. Of course, you can also use Telnet to operate on its plain text protocol.
Redis-benchmark:redis Performance testing tools to test the read and write performance of Redis in your system and in your configuration
Redis-check-aof: Update log check

Redis-check-dump: for local database checking

Why not use the standard Linux installation kick. The official wiki says: Redis can run just fine without a configuration file (when executed without a config file a standard Configurati On is used). With Thedefault configuration Redis would log to the standard output so you can check out what happens. Later, you canchange the default settings.

You can also make the install, which is to copy the executable files to/usr/local/bin.

after make, a prompt appears:

Hint:to run ' make test ' was a good idea;)


-----------------------------------------------------------------

In fact, do not test, generally can be used. But since they suggested it, let's go for make test.

Run #make Test

Error, prompt no need ' tclsh8.5 ' in order to run the Redis test

Then go to Tcl's official website http://www.tcl.tk/download version 8.5

Then install tcl8.5:

(The location of configure and make is very special, under the UNIX installation directory, so the following is the official Tcl installation method)

#tar Xvzf tcl8.5.12-src.tar.gz

#cd tcl8.5.13/unix/

#./configure

#make

#make Test

#make Install

Note: Of course, it can be simpler to install with Yum installed Tcl.

OK, after the installation of TCL, you can go to the Redis directory to run make test, this time the normal run through. Tips:

\o/all tests passed without errors!
Cleanup:may take some time ... Ok

Description Redis installation is OK. Can run.

-----------------------------------------------------------------

Installation

# make Install



2. Running Redis

2.8.17 version, Redis-server is placed in the SRC folder.

start Redis service side

#/usr/redis-2.8.17/src/redis-server

If you do not change the Daemonize no configuration, you will see the information running.


notes:

The default port number for ▲redis is 6379, according to Antirez, a redis author, 6379 is Merz the corresponding number on the phone keypad, and Merz is taken from the Italian showgirl Alessia Merz name. And Merz for a long time by Antirez and his friends as a synonym for stupidity. )

▲redis has two storage methods, the default is the snapshot way, the implementation method is to periodically persist the memory snapshot (snapshot) to the hard disk, the disadvantage is that if the crash occurs after the persistence of the loss of data. As a result of the perfectionist's push, the author added the AoF way. AOF is append only mode, which saves the operation command to the log file while writing the memory data.

running Redis in the background

need to read the configuration file mode to start

Note that the daemonize parameter for the default copy of the past redis.conf file is no, so Redis does not run in the background, We can modify the redis.conf file, which is the extracted Redis root directory

Daemonize Yes

If you want to provide your redis.conf and you have the to run it using a additional
Parameter (the path of the configuration file):

% CD src
%./redis-server/path/to/redis.conf

#/usr/redis-2.8.17/src/redis-server/usr/redis-2.8.17/redis.conf

View the Redis process

#ps aux |grep Redis


start multiple Redis instances

Copy the default redis.conf to redis6383.conf, open the redis6383.conf configuration file, find the port 6379 line, 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


Invoke service :

#/USR/REDIS-2.8.17/SRC/REDIS-CLI

New

Redis> set Foo Bar

Get

Redis> get foo "bar"

Delete

Redis>del Foo



Fuzzy Lookup

Redis>keys f*

Redis>keys F?o?


View Info Info

Enter the # Info command to see the memory fragmentation rate : mem_fragmentation_ratio=2.59

The default is to use the Jemalloc memory allocator



3. Configure Redis

The Redis configuration file is in your installation directory. The name is: redis.conf.

A few simple redis.conf:

Redis is not a daemon by default, if necessary, change daemonize No to daemonize yes. (You can check the printing information without changing the test.) )

If you are uncomfortable with Redis default port 6379, you can change port 6379

If you want to place the data file in a specified folder , change the dir/opt/data/

The default is dir./is also placed by default in the installation directory.

Connection timeout time, timeout 300, nothing to change the head ...

Dir is the path to the data file. The default is in the installation directory.

* Select one of the following configuration two, as described in note section 2.

###### snapshotting ##### #内存快照方式:

The default memory snapshot policy is that

At least 1 data changes within 900 seconds (15 minutes);

Or within 300 seconds, there are at least 10 data changes;

Or 60 seconds, there are at least 1000 data changes, time + data change times, together affect the occurrence of memory snapshots.

###### APPEND only mode ###### aof mode

Appendfsync everysec sync per second. This can be commented out, open the following option Appendfsync No

The rest of the configuration, conf inside the notes written quite clear, I will not much nonsense. Everyone looked at their own match.

Can copy configuration files to etc

Mkdir/etc/redis

CP redis.conf/etc/redis/redis.conf

Mkdir/var/lib/redis 1. redis.conf Configuration parameters:

#是否作为守护进程运行

Daemonize Yes

#如以后台进程运行, you need to specify a PID, which defaults to/var/run/redis.pid

Pidfile Redis.pid

#绑定主机IP, the default value is 127.0.0.1

#bind 127.0.0.1

#Redis默认监听端口

Port 6379

#客户端闲置多少秒后, disconnect, default 300 (SEC)

Timeout 300

#日志记录等级, there are 4 selectable values, Debug,verbose (default), notice,warning

LogLevel verbose

#指定日志输出的文件名, the default value is stdout, or it can be set to/dev/null masking log

LogFile stdout

#可用数据库数, the default value is 16 and the default database is 0

Databases 16

#保存数据到disk的策略

#当有一条Keys数据被改变是, 900-second refresh to disk

Save 900 1

#当有10条Keys数据被改变时, 300-second refresh to disk

Save 300 10

#当有1w条keys数据被改变时, 60-second refresh to disk

Save 60 10000

Whether to compress data objects when #当dump. Rdb Database

Rdbcompression Yes

#本地数据库文件名, the default value is Dump.rdb

Dbfilename Dump.rdb

#本地数据库存放路径, the default value is./

dir/var/lib/redis/

########### Replication #####################

#Redis的复制配置

# slaveof <masterip> <masterport> Set the IP and port of the main service when this machine is from service

# Masterauth <master-password> Set the connection password for the primary service when this machine is from service

#连接密码

# Requirepass Foobared

#最大客户端连接数, no Limit by default

# maxclients 128

#最大内存使用设置, when the maximum memory setting is reached, Redis attempts to clear the expired or expiring key first, and when this method is processed, the maximum memory setting is reached and no further write operations can be made.

# maxmemory <bytes>

#是否在每次更新操作后进行日志记录, if not turned on, may result in loss of data over a period of time when power is lost. Because the Redis itself synchronizes data files in sync with the save conditions above, some data will only exist in memory for a period of time. Default value is no

AppendOnly No

#更新日志文件名, the default value is Appendonly.aof

#appendfilename

#更新日志条件, a total of 3 selectable values. No indicates that the data cache of the operating system is synchronized to disk, always means that the data is written to disk manually after each update operation, and EVERYSEC is synchronized once per second (the default value).

# Appendfsync Always

Appendfsync everysec

# Appendfsync No

################ VIRTUAL MEMORY ###########

#是否开启VM功能, the default value is no

Vm-enabled No

# vm-enabled Yes

#虚拟内存文件路径, the default value is/tmp/redis.swap and cannot be shared by multiple Redis instances

Vm-swap-file/tmp/redis.swap

#将所有大于vm-max-memory data into virtual memory, regardless of the vm-max-memory settings, all index data is memory stored (REDIS index data is keys), that is, when the vm-max-memory is set to 0, In fact, all value is present on 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

#是否重置Hash表

activerehashing Yes

Note: The Redis official documentation makes some suggestions for using VMS: When your key is small and the value is large, the effect of using a VM is better. Because this saves more memory.  When your key is not small, consider using some very important methods to turn a large key into a large value, such as you might consider combining key,value into a new value.  It is best to use Linux ext3 to support a better file system for sparse files to save your swap files. Vm-max-threads This parameter, you can set the number of threads to access the swap file, and it is best not to exceed the number of cores of the machine. If set to 0, then all operations on swap files are serial. This can result in a long delay, but with good assurance of data integrity.

2. Adjust system kernel Parameters

If the memory situation is very tense, you need to set the kernel parameters:

Echo 1 >/proc/sys/vm/overcommit_memory

Here's what this configuration means:/proc/sys/vm/overcommit_memory
This file specifies the kernel's policy for memory allocation, which can be 0, 1, 2.
0, indicates that the kernel will check for sufficient available memory to be used by the process, and if sufficient memory is available, the memory request is allowed; otherwise, the memory request fails and the error is returned to the application process.
1, which means that the kernel allows all physical memory to be allocated regardless of the current memory state.
2, which indicates that the kernel allows allocating more memory than the sum of all physical memory and swap space
Redis in the dump data, will fork out a sub-process, in theory the child process occupies the same memory and the parent is the same, such as the parent occupies 8G of memory, this time also to allocate 8G of memory to child, if the memory is not affordable, Will often cause the Redis server down or IO load is too high, inefficient. So the more optimized memory allocation policy here should be set to 1 (indicating that the kernel allows all physical memory to be allocated regardless of the current memory state)

4. Debugging Debug

Note Because Redis is enabled for memory optimization by default, you must modify the compilation options. Otherwise, the "<value optimized out>" is prompted when printing variables within GDB, mostly because of the GCC optimizations, and we can add the-O0 option to force the GCC compilation optimizations to be disabled. compiling Redis without optimizations

By default Redis was compiled with THE-O2 switch, this means, which compiler optimizations is enabled. This makes the Redis executable faster, and at the same time it makes Redis ECT using GDB.

So to modify the makefile file, the file in the/SRC directory, modify the optimization?=-o2 option can be modified to O0

Optimization?=-o0

Delete the original Redis re-modify makefile re-make, start Redis

Specific debugging techniques to view pointer variables

View the Redis process

# PS Aux|grep Redis

GDB attaches to process

# Gdb-p Process ID

(GDB) R starts again or does not start with the main function

(GDB) Break main set Breakpoint

(GDB) List View Code

(GDB) p variable name view variable content, use p to view the variable, this time can be viewed

Redis Debugging Tips

Redis loops through the event at the ae.c aemain function:

The event processor's main loop
void Aemain (Aeeventloop *eventloop) {

    eventloop->stop = 0;

    while (!eventloop->stop) {

        //If there is a function that needs to be executed before the event is processed, run it if
        (eventloop->beforesleep! = NULL)
            Eventloop->beforesleep (eventloop);

        Start processing 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, and only the Aemain function handles the request, REDIS-CLI returns OK, or Waits


5. Benchmark Test

First start Redis with the start command

Then use the command in any directory:

# redis-benchmark-h localhost-p 6379-c 100-n 100000

Simulates 100 concurrent connections, 100,000 requests, detects Redis server performance with host localhost port 6379


Original address: http://blog.csdn.net/unix21/article/details/9526295

------------------------------------------------------------------------

More references

Configuration reference: Redis 2.2.14 installation configuration under CentOS

Installation reference: Redis (i) installation

Debug Reference: Http://redis.io/topics/debugging using GDB debug Redis-server

Benchmark parameter: Redis installation Deployment


In addition to the need to use Redis in PHP need to install PHP extensions can be consulted:

The PHP extensions for Redis and Redis are installed in error free version

Phpredis How to use

Redis Common Command Reference




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.