Redis official website http://redis.io
Chinese manual http://www.cnblogs.com/stephen-liu74/archive/2012/02/27/2370212.html
Download http://redis.io/download
Currently the latest version of redis3.2.11
cd /usr/local/src
wget http://download.redis.io/releases/redis-3.2.11.tar.gz
tar -zxvf redis-3.2.11.tar.gz
cd redis-3.2.11
make
After the make command is executed, six executable files will be generated in the src directory, namely redis-server, redis-cli, redis-benchmark, redis-check-aof, redis-check-dump, redis-sentinel, and their The role is as follows:
redis-server: daemon startup program for Redis server
redis-cli: Redis command line operation tool. Of course, you can also use telnet to operate according to its plain text protocol
redis-benchmark: Redis performance testing tool to test the read and write performance of Redis in your system and your configuration
redis-check-aof: update log check
redis-check-dump: used for local database check
redis-sentinel: cluster management tool
installation:
make PREFIX=/usr/local/redis install
test
make test
Depend on tcl, if not installed, please install first
yum -y install tcl tcl-devel
Three, configure Redis
mkdir -p /usr/local/redis/etc /usr/local/redis/log /usr/local/redis/data #Create configuration file, log file, database storage directory
cp /usr/local/src/redis-3.2.11/redis.conf /usr/local/redis/etc/
touch /usr/local/redis/log/redis.log
daemonize yes
pidfile /var/run/redis.pid
bind 0.0.0.0
port 6379
timeout 300
loglevel notice
logfile "/usr/local/redis/log/redis.log"
databases 16
save 900 1
save 300 10
save 60 10000
rdbcompression yes
dbfilename dump.rdb
dir /usr/local/redis/data
pidfile /usr/local/redis/redis.pid
########### Replication #####################
requirepass foobared
maxclients 30000
appendonly no
appendfsync everysec
################ VIRTUAL MEMORY ###########
vm-enabled no
vm-swap-file /tmp/redis.swap
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
activerehashing yes
Note: The official Redis document puts forward some suggestions on the use of VM:
When your key is small and the value is large, the effect of using VM will be better. Because this saves more memory.
When your key is not small, you can consider using some very methods to turn a large key into a large value.For example, you can consider combining key and value into a new value.
It is best to use a file system that supports sparse files such as linux ext3 to save your swap files.
The parameter vm-max-threads can set the number of threads accessing the swap file. It is best not to exceed the number of cores of the machine. If it is set to 0, then all operations on the swap file are serial. It may cause longer Time delay, but there is a good guarantee for data integrity.
If the memory situation is tight, you need to set the kernel parameters:
echo 1 > /proc/sys/vm/overcommit_memory
Here to talk about the meaning of this configuration: /proc/sys/vm/overcommit_memory
This file specifies the kernel's strategy for memory allocation, and its value can be 0, 1, 2.
0 , Which means that the kernel will check whether there is enough available memory for the application process; if there is enough available memory, the memory application is allowed; otherwise, the memory application fails and an 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 means that the kernel allows the allocation of memory that exceeds the sum of all physical memory and swap space
When Redis dumps data, it forks a child process. Theoretically, the memory occupied by the child process is the same as that of the parent. For example, the memory occupied by the parent is 8G. At this time, 8G of memory should also be allocated to the child. Unable to afford it, it will often cause the redis server to be down or the IO load is too high, and the efficiency is reduced. So the more optimized memory allocation strategy here should be set to 1 (indicating that the kernel allows all physical memory to be allocated regardless of the current memory status)
Add environment variables
echo "export PATH=$PATH:/usr/local/redis/bin" >> /etc/profile
. /etc/profile
redis-server /etc/redis/redis.conf
You can start the redis service in the background. After confirming that it is running, you can use the redis-benchmark command to test and see, or you can actually operate it through the redis-cli command, such as:
redis-cli set foo bar
OK
redis-cli get foo
bar
redis-cli shutdown
If the port changes, you can specify the port:
redis-cli -p 6380 shutdown
Data backup can be achieved by regularly backing up the file.
Because redis is written to disk asynchronously, if you want to write the data in memory to the hard disk immediately, you can execute the following command:
redis-cli save or redis-cli -p 6380 save (specify port)
Note that the above deployment operations require certain permissions, such as copying and setting kernel parameters.
When the redis-benchmark command is executed, the memory data will also be written to the hard disk.
The synchronization mechanism implemented by redis is relatively simple and lacks the common check point and verification mechanism of synchronization mechanisms.
At runtime, if the forwarding of the master -> slave synchronization request is discarded, the slave will not be able to restore the related information of the request until the slave restarts when the data is fully loaded from the master. Therefore, it is recommended to use redis to use its key/value and value to support multiple types of features and store some relatively unimportant data.
vi /etc/init.d/redis #Add the following
#! /bin/bash
source /etc/init.d/functions
BIN="/usr/local/redis/bin"
CONFIG="/usr/local/redis/etc/redis.conf"
PIDFILE="/usr/local/redis/redis.pid"
[ - r "
RETVAL=0
prog="redis-server"
desc="Redis Server"
start() {
if [ -e
exit 1
fi
echo -n $"Starting $desc: "
daemon
echo
[
return $RETVAL
}
stop() {
echo -n $"Stop $desc: "
killproc
echo
[
return $RETVAL
}
restart() {
stop
start
}
case "
RETVAL=$?
;;
status)
status
;;
*)
echo $"Usage: $0 {start|stop|restart|condrestart|status}"
RETVAL=1
esac
exit $RETVAL
Before using this script to manage, you need to configure the following kernel parameters, otherwise the Redis script may report an error when restarting or stopping redis, and it cannot automatically synchronize data to the disk before stopping the service:
vi /etc/sysctl.conf
vm.overcommit_memory = 1
Then the application takes effect:
sysctl -p
( Adjust the memory (if the memory situation is tight, you need to set the kernel parameters)
echo 1 > /proc/sys/vm/overcommit_memory
Description:
This file specifies the kernel's strategy for memory allocation, and its value can be 0, 1, 2.
0 Indicates that the kernel will check whether there is enough available memory for the application process; if there is enough available memory, the memory application is allowed; otherwise, the memory application fails and an error is returned to the application process.
1 Indicates that the kernel allows all physical memory to be allocated, regardless of the current memory state.
2 Indicates that the kernel allows the allocation of memory exceeding the sum of all physical memory and swap space
)
Then add the service and start it automatically:
chmod 755 /etc/init.d/redis
chkconfig redis on
chkconfig --list redis
Centos 7 startup script
/usr/lib/systemd/system/redis.service
#####################################
[ Unit]
Description=Startup script for the Redis
Documentation=http://redis.io
After=network.target remote-fs.target nss-lookup.target
[ Service]
Type=forking
PIDFile=/usr/local/redis/redis.pid
ExecStart=/usr/local/redis/bin/redis-server /usr/local/redis/etc/redis.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[ Install]
WantedBy=multi-user.target
Specify running user
useradd -M -s /sbin/nologin redis
chown -R redis:redis /usr/local/redis/log /usr/local/redis/data pidfile
cat /usr/lib/systemd/system/redis.service
[ Unit]
Description=Startup script for the Redis
Documentation=http://redis.io
After=network.target remote-fs.target nss-lookup.target
[ Service]
User=redis
Group=redis
Type=forking
ExecStart=/usr/local/redis/bin/redis-server /usr/local/redis/etc/redis.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[ Install]
WantedBy=multi-user.target
systemctl enable redis; systemctl start redis
Recommended Posts