Hello everyone, the theme of this article is Redis, we're going to install it on CentOS 7. Compile the source code, install the binary files, create the installation file. After installing its components, we will configure redis, just configure the operating system parameters, the goal is to make redis run more reliably and quickly.
Redis is an open source multi-platform data storage software, written in ANSI C, direct memory usage in the data set, enabling it to achieve very high efficiency. Redis supports multiple programming languages, including Lua, C, Java, Python, Perl, PHP and many other languages. Redis code amount is very small, only about 30,000 lines, it is only "little" thing, but doing well. Although work is in memory, but the data persistence preservation is still there, and redis reliability is high, also supports clusters, which can be a good guarantee your data security.
redis currently no official RPM package, we need to compile from source code, but you need to install in order to compile Make and GCC.
If not installed GCC and Make, then use yum to install.
yum install gccmake
Downloaded from the official website of the tar archive.
curl http://download.redis.io/releases/redis-3.0.4.tar.gz -o redis-3.0.4.tar.gz
tar zxvf redis-3.0.4.tar.gz
Enter the unpacked directory.
Make use to compile the source file.
Enter the directory of the source file.
Copy Redis server and client to / usr / local / bin.
cp redis-server redis-cli / usr / local / bin
At best the sentinel, benchmark and check copy past.
cp redis-sentinel redis-benchmark redis-check-aof redis-check-dump / usr / local / bin
Create redis configuration folder.
mkdir / etc / redis
Create a directory of valid data stored in / var / lib / redis
mkdir -p / var / lib / redis / 6379
To make redis work properly you need to configure some kernel parameters.
Configure vm.overcommit_memory 1, which prevents data is truncated, as detailed here.
sysctl-w vm.overcommit_memory = 1
Modify the maximum number of connections exceeds the backlog redis.conf the tcp-backlog value, which is the default value of 511. You can find more information about the ip based sysctl network tunnel at kernel.org.
sysctl-w net.core.somaxconn = 512
Cancel support for transparent huge page memory (transparent huge pages), as this will cause delay and redis use to generate memory access issue.
echo never> / sys / kernel / mm / transparent_hugepage / enabled
redis.conf is redis configuration files, but you will see the name of this file is 6379.conf, and this figure is redis monitor network ports. If you want to run more than one instance of redis, it recommended the use of such names.
Copy the example of redis.conf to /etc/redis/6379.conf.
cp redis.conf /etc/redis/6379.conf
Now edit this file and configuration parameters.
vi / etc / redis / 6379.conf
Daemonize set to no, systemd need it to run in the foreground, otherwise redis suddenly hang.
Setting pidfile to /var/run/redis_6379.pid.
If you are not using the default port, you can be modified.
Set the logging level.
Modify the log file path.
Set the directory to / var / lib / redis / 6379
dir / var / lib / redis / 6379
Here are a few you can improve the security of the operation.
In many cases, the client program and the server program is running on the same machine, so no listening socket on the network. If you use this and similar situations, you can use an alternative unix socket network socket, for this you need to configure the port to 0, and then configure the following options to enable unix socket.
Unix socket set socket file.
Restrict permissions socket file.
Now in order to make redis-cli can access, you should use the -s parameter to point to the socket file.
redis-cli -s /tmp/redis.sock
You might need remote access, if it is, then you should set a password before each operation like this requires a password.
Imagine the following output instruction. Yes, it will output the configuration of the server, so you should refuse such access under any possible circumstances.
CONFIG GET *
In order to limit or even prohibit this or he can use the command rename-command command. You must supply a command name and alternative name. To disable then need to set up an alternative name for the empty string, which prohibits any person to guess the name of the command would be more secure.
rename-command FLUSHDB "FLUSHDB_MY_SALT_G0ES_HERE09u09u"
rename-command FLUSHALL ""
rename-command CONFIG "CONFIG_MY_S4LT_GO3S_HERE09u09u"
Unix socket by using a password to access and modify the command
By default, redis periodically to dump the data set we set dump.rdb file directory under. You can use the save command dumps the configuration of frequency, its first parameter is the time frame in seconds, and the second parameter is the number of modifications in the data file.
Least every 15 minutes and modified key again.
Every 5 minutes and a minimum of 10 times the modified key.
Every 1 minute and least modified 10,000 bond.
/var/lib/redis/6379/dump.rdb File contains the dump data from memory since the last save data sets. Since it first creates a temporary file and then replace the previous dump file, data corruption problem does not exist here, and do not worry, you can directly copy the file.
When you start the boot
You can use systemd to add redis system boot list.
Copy the sample files to the init_script /etc/init.d, note the name of the script represents the port number.
cp utils / redis_init_script /etc/init.d/redis_6379
Now we want to use systemd, so create a file named redis_6379.service unit under / etc / systems / system.
vi / etc / systemd / system / redis_6379.service
Fill in the following, details visible systemd.service.
Description = Redis on port 6379
Type = forking
ExecStart = / etc / init.d / redis_6379 start
ExecStop = / etc / init.d / redis_6379 stop
WantedBy = multi-user.target
Now add the option before I /etc/sysctl.conf modified in your memory overcommit and backlog maximum.
vm.overcommit_memory = 1
net.core.somaxconn = 512
Support for transparent huge page memory, and there is no direct sysctl command can control, you need the following command into the end of /etc/rc.local.
echo never> / sys / kernel / mm / transparent_hugepage / enabled
to sum up
This can be started by setting these options you can deploy redis service to many simple scene, but there are many redis options redis.conf complex environment to prepare. In some cases, you can use replication to increase availability and Sentinel, or data is distributed across multiple servers to create a server cluster.