Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to install Redis server on CentOS7

2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the knowledge of "how to install a Redis server on CentOS7". Many people will encounter this dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Redis server

Redis is an open source multi-platform data storage software written in ANSI C that uses datasets directly in memory, which makes it very efficient. Redis supports multiple programming languages, including Lua, C, Java, Python, Perl, PHP, and many others. Redis has a small amount of code, only about 30, 000 lines, it only does "very few" things, but it does very well. Although it works in memory, there is still data persistence, and redis is highly reliable and supports clustering, which can ensure your data security.

Build Redis

Redis currently has no official RPM installation package, we need to compile from the source code, and in order to compile, we need to install Make and GCC.

If you don't have GCC and Make installed, use the yum installation.

Yum install gcc make

Download the tar package from the official website.

Curl http://download.redis.io/releases/redis-3.0.4.tar.gz-o redis-3.0.4.tar.gz

Decompress.

Tar zxvf redis-3.0.4.tar.gz

Enter the decompressed directory.

Cd redis-3.0.4

Use Make to compile the source file.

Make

Installation

Go to the directory of the source file.

Make

Copy the server and client of Redis to / usr/local/bin.

Cp redis-server redis-cli / usr/local/bin

* also copy sentinel,benchmark and check.

Cp redis-sentinel redis-benchmark redis-check-aof redis-check-dump / usr/local/bin

Create a redis configuration folder.

Mkdir / etc/redis

Create a valid directory to save the data under / var/lib/redis.

Mkdir-p / var/lib/redis/6379

System parameters

Some kernel parameters need to be configured for redis to work properly.

Configure vm.overcommit_memory to 1, which prevents the data from being truncated, as detailed here.

Sysctl-w vm.overcommit_memory=1

Modify the * value of the number of backlog connections to exceed the tcp-backlog value in redis.conf, which is the default value of 511. You can find more information about sysctl-based ip network tunnels at kernel.org.

Sysctl-w net.core.somaxconn=512

Remove support for transparent giant page memory (transparent huge pages) because this can cause latency and memory access problems during redis usage.

Echo never > / sys/kernel/mm/transparent_hugepage/enabled

Redis.conf

Redis.conf is the configuration file for redis, but you will see that the name of this file is 6379.conf, and this number is the network port on which redis listens. If you want to run more than one instance of redis, this name is recommended.

Copy the redis.conf of the example to / etc/redis/6379.conf.

Echo never > / sys/kernel/mm/transparent_hugepage/enabled

Now edit the file and configure the parameters.

Vi / etc/redis/6379.conf

Daemonize

Setting daemonize to no,systemd requires it to run in the foreground, otherwise redis will suddenly hang up.

Vi / etc/redis/6379.conf

Pidfile

Set pidfile to / var/run/redis_6379.pid.

Vi / etc/redis/6379.conf

Port

If you are not going to use the default port, you can modify it.

Port 6379

Loglevel

Sets the log level.

Port 6379

Logfile

Modify the log file path.

Logfile / var/log/redis_6379.log

Dir

Set the directory to / var/lib/redis/6379

Dir / var/lib/redis/6379

Safety

Here are a few actions that can improve security.

Unix sockets

In many cases, client and server programs run on the same machine, so there is no need to listen to socket on the network. If this is similar to your usage, you can use unix socket instead of network socket. To do this, you need to configure port to 0, and then configure the following options to enable unix socket.

Sets the socket file for unix socket.

Unixsocket / tmp/redis.sock

Restrict permissions on socket files.

Unixsocketperm 700

Now in order for redis-cli to be accessible, you should point to the socket file with the-s parameter.

Redis-cli-s / tmp/redis.sock

Requirepass

You may need remote access, and if so, you should set a password so that you are required to enter a password before each operation.

Requirepass "bTFBx1NYYWRMTUEyNHhsCg"

Rename-command

Imagine the output of the following instructions. Yes, this will output the configuration of the server, so you should deny this access whenever possible.

CONFIG GET *

To restrict or even disable this or other directive, you can use the rename-command command. You must provide a command name and an alternative name. To prohibit it, you need to set the alternate name to an empty string, which prevents anyone from guessing the name of the command.

Rename-command FLUSHDB "FLUSHDB_MY_SALT_G0ES_HERE09u09u" rename-command FLUSHALL "rename-command CONFIG" CONFIG_MY_S4LT_GO3S_HERE09u09u "

Use a password to access through unix socket, and modify commands

Snapshot

By default, redis periodically dumps the dataset to the dump.rdb file in the directory we set up. You can use the save command to configure the frequency of the dump. Its * * parameter is the time frame in seconds, and the second parameter is the number of changes made on the data file.

The keys are changed every 15 minutes and at least once.

Save 900 1

The keys have been modified at least 10 times every 5 minutes.

Save 300 10

The keys have been modified at least 10000 times every 1 minute.

Save 60 10000

The file / var/lib/redis/6379/dump.rdb contains the dump data of the dataset in memory since it was last saved. Because it first creates a temporary file, and then replaces the previous dump file, there is no data corruption problem here, you don't have to worry, you can copy this file directly.

Start when you power on

You can use systemd to add redis to the system boot list.

Copy the sample init_script file to / etc/init.d, and note the port number represented by the script name.

Cp utils/redis_init_script / etc/init.d/redis_6379

Now we're going to use systemd, so create a unit file named redis_6379.service under / etc/systems/system.

Vi / etc/systemd/system/redis_6379.service

Fill in the contents below. For more information, please see systemd.service.

[Unit] Description=Redis on port 6379 [Service] Type=forking ExecStart=/etc/init.d/redis_6379 start ExecStop=/etc/init.d/redis_6379 stop [Install] WantedBy=multi-user.target

Now, add the options for memory overusage and backlog * * values that I modified earlier in / etc/sysctl.conf.

Vm.overcommit_memory = 1 net.core.somaxconn=512

For transparent giant page memory support, there is no direct sysctl command to control, so you need to put the following command at the end of / etc/rc.local.

Echo never > / sys/kernel/mm/transparent_hugepage/enabled

Summary

This enables you to deploy redis services to many simple scenarios by setting these options, but there are many redis options for complex environments in redis.conf. In some cases, you can use replication and Sentinel to improve availability, or spread data across multiple servers to create server clusters.

That's all for "how to install a Redis server on CentOS7". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report