Install and configure Redis 2.2.14 in CentOS

Source: Internet
Author: User
Tags redis version install redis

 

1. download the latest redis version 2.2.14

 

Cd/usr/local/src

 

Http://redis.googlecode.com/files/redis-2.2.14.tar.gz wget-c

 

Ii. Compile and install redis

 

Tar zxvf redis-2.2.14.tar.gz

 

Cd redis-2.2.14

 

Make

 

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-server: daemon Startup Program of the Redis server

Redis-cli: Redis command line operation tool. Of course, you can also use telnet to operate based on its plain text protocol.

Redis-benchmark: Redis performance testing tool to test the read/write performance of Redis in your system and your configuration

Redis-check-aof: Update log check

 

Redis-check-dump: used for local database check

 

Installation:

 

Make PREFIX =/usr/local install

 

3. Configure Redis

 

Mkdir/etc/redis

 

Cp redis. conf/etc/redis. conf

 

Mkdir/var/lib/redis

 

You can download the modified redis. conf file from this directory.

 

 

 

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 very small and the value is very large, it will be better to use the VM, because the memory saved is relatively large.

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

This file specifies the kernel memory allocation policy. The value can be 0, 1, or 2.

0 indicates that the kernel will check whether there is enough available memory for use by the process. If there is enough available memory, the memory application will be allowed; otherwise, the memory application will fail, and return the error to the application process.

1 indicates that the kernel allows all physical memory allocation regardless of the current memory status.

2. indicates that the kernel is allowed to allocate more memory than the total physical memory and swap space.

Redis generates a sub-process when dumping data. In theory, the memory occupied by the child process is the same as that of the parent. For example, the parent occupies 8 GB of memory, at this time, 8 GB of memory should also be allocated to the child. If the memory is not enough, it will often cause the redis server to go down or the IO load is too high, and the efficiency is reduced. Therefore, the optimized memory allocation policy should be set to 1 (indicating that the kernel allows allocation of all physical memory, regardless of the current memory status)

 

 

 

4. Run Redis

 

 

 

1. Run the service

 

Redis-server/etc/redis. conf

 

You can start the redis service in the background. After you confirm that the service is running, you can use the redis-benchmark command to test it. You can also use the redis-cli command to perform actual operations, for example:

 

Redis-cli set foo bar

 

OK

 

Redis-cli get foo

 

Bar

 

2. Close the service

 

Redis-cli shutdown

 

If the port changes, you can specify the Port:

 

Redis-cli-p 6380 shutdown

 

3. Save/backup

 

Data backup can be achieved through regular backup of this file.

Because redis writes data to the disk asynchronously, if you want to write data in the memory to the hard disk immediately, run the following command:

Redis-cli save or redis-cli-p 6380 save (specified port)

Note that the preceding deployment operations require certain permissions, such as copying and setting kernel parameters.

When executing the redis-benchmark command, the memory data is also written to the hard disk.

 

4. Synchronization Mechanism

 

The synchronization mechanism implemented by redis is relatively simple, and there is a lack of common check points and verification mechanisms for synchronization mechanisms.

During running, if the master-> slave synchronization request Forwarding is discarded, slave will not be able to restore the relevant information of the request until the slave is restarted to load data from the master. Therefore, redis is recommended to use its key/value and value to support multiple types of features to store relatively unimportant data.

 

 

 

5. self-starting

 

 

 

Download the script from here

 

Declaration: This script is from the network and has been modified, tested, and available.

 

Before using this script for management, you must configure the following kernel parameters. Otherwise, an error will be reported when the Redis script restarts or stops redis, data cannot be automatically synchronized to the disk before the service is stopped:

 

Vi/etc/sysctl. conf

 

Vm. overcommit_memory = 1

 

Then the application takes effect:

Sysctl-p

 

Then add the service and start it automatically:

 

Chmod 755/etc/init. d/redis

Chkconfig -- add redis

Chkconfig -- level 345 redis on

Chkconfig -- list redis

 

Author: Chen Feng

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.