Home > Database > Redis > How to install Redis server on CentOS 7

How to install Redis server on CentOS 7

WBOY
Release: 2023-05-31 08:25:21
forward
1475 people have browsed it

Redis is an open source multi-platform data storage software written in ANSI C. Redis can support Lua, C, Java, Python, Perl, PHP and many other languages.

Building Redis

redis currently does not have an official RPM installation package. We need to compile from source code, and in order to compile, we need to install Make and GCC.

If GCC and Make have not been installed, use yum to install them.

yum install gcc make
Copy after login

Download the tar compressed package from the official website.

curl http://download.redis.io/releases/redis-3.0.4.tar.gz -o redis-3.0.4.tar.gz
Copy after login

Unzip.

tar zxvf redis-3.0.4.tar.gz
Copy after login

Enter the decompressed directory.

cd redis-3.0.4
Copy after login

Use Make to compile source files.

make
Copy after login

Installation

Enter the directory of the source file.

cd src
Copy after login

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

cp redis-server redis-cli /usr/local/bin
Copy after login

It is best to copy sentinel, benchmark and check.

cp redis-sentinel redis-benchmark redis-check-aof redis-check-dump /usr/local/bin
Copy after login

Create redis configuration folder.

mkdir /etc/redis
Copy after login

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

mkdir -p /var/lib/redis/6379
Copy after login
System parameters

In order for redis to work properly, some kernel parameters need to be configured.

Configure vm.overcommit_memory to 1, which can avoid data being truncated. See here for details.

sysctl -w vm.overcommit_memory=1
Copy after login

Modify the maximum number of backlog connections to exceed the tcp-backlog value in redis.conf, which is the default value of 511. More information about sysctl-based IP network tunneling can be found at kernel.org.

sysctl -w net.core.somaxconn=512
Copy after login

Cancel support for transparent huge pages, because this will cause delays and memory access problems during the use of redis.

echo never > /sys/kernel/mm/transparent_hugepage/enabled
Copy after login
Copy after login

redis.conf

redis.conf is the configuration file of redis. However, you will see that the name of this file is 6379.conf, and this number is the network port that redis listens on. To run multiple redis instances, the following naming scheme is recommended.

Copy the sample redis.conf to /etc/redis/6379.conf.

cp redis.conf /etc/redis/6379.conf
Copy after login

Now edit this file and configure parameters.

vi /etc/redis/6379.conf
Copy after login
daemonize

Set daemonize to no, systemd needs it to run in the foreground, otherwise redis will hang suddenly.

daemonize no
Copy after login
pidfile

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

pidfile /var/run/redis_6379.pid
Copy after login
port

If you do not plan to use the default port, you can modify it.

port 6379
Copy after login
loglevel

Set the log level.

loglevel notice
Copy after login
logfile

Modify the log file path.

logfile /var/log/redis_6379.log
Copy after login
dir

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

dir /var/lib/redis/6379
Copy after login

Security

There are several operations that can improve security.

Unix sockets

Because the client program and the server program usually run on the same machine, there is no need to listen to the network socket. If this is similar to your use case, you can use a unix socket instead of a network socket. To do this, you need to configure port to 0, and then configure the following options to enable the unix socket.

Set the socket file of unix socket.

 unixsocket /tmp/redis.sock
Copy after login

Restrict the permissions of socket files.

unixsocketperm 700
Copy after login

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

redis-cli -s /tmp/redis.sock
Copy after login
requirepass

You may need remote access, if so, then you should set a password so that it is required before each operation.

requirepass "bTFBx1NYYWRMTUEyNHhsCg"
Copy after login
rename-command

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

CONFIG GET *
Copy after login

You can use the "rename-command" command to limit or prohibit the use of this or other instructions. You must provide a command name and an alternative name. To make it safer to ban a command, its alternative name should be set to an empty string so that no one can guess the name of the command.

rename-command FLUSHDB "FLUSHDB_MY_SALT_G0ES_HERE09u09u"rename-command FLUSHALL ""rename-command CONFIG "CONFIG_MY_S4LT_GO3S_HERE09u09u"
Copy after login

如何在CentOS 7上安装Redis服务器

Use password to access through unix socket, and modify the command

Snapshot

By default, redis The data set will be periodically dumped to the dump.rdb file in the directory we set. You can configure the frequency of dumps using the save command, whose first parameter is the time frame in seconds and the second parameter is the number of modifications to be made on the data file.

Every 15 minutes and the key has been modified at least once.

save 900 1
Copy after login

Every 5 minutes and the key has been modified at least 10 times.

save 300 10
Copy after login

Every 1 minute and the key has been modified at least 10,000 times.

save 60 10000
Copy after login

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

Start at boot

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

Copy the example init_script file to /etc/init.d, pay attention to the port number represented by the script name.

cp utils/redis_init_script /etc/init.d/redis_6379
Copy after login

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

vi /etc/systemd/system/redis_6379.service
Copy after login

Fill in the following content, see systemd.service for details.

[Unit]Description=Redis on port 6379[Service]Type=forkingExecStart=/etc/init.d/redis_6379 startExecStop=/etc/init.d/redis_6379 stop[Install]WantedBy=multi-user.target
Copy after login

现在添加我之前在 /etc/sysctl.conf 里面修改过的内存过量使用和 backlog 最大值的选项。

vm.overcommit_memory = 1net.core.somaxconn=512
Copy after login

对于透明巨页内存支持,并没有直接 sysctl 命令可以控制,所以需要将下面的命令放到 /etc/rc.local 的结尾。

echo never > /sys/kernel/mm/transparent_hugepage/enabled
Copy after login
Copy after login

这样就可以启动了,通过设置这些选项你就可以部署 redis 服务到很多简单的场景,然而在 redis.conf 还有很多为复杂环境准备的 redis 选项。在一些情况下,你可以使用 replication 和 Sentinel 来提高可用性,或者将数据分散在多个服务器上,创建服务器集群。

什么是Linux系统

Linux是一种免费使用和自由传播的类UNIX操作系统,是一个基于POSIX的多用户、多任务、支持多线程和多CPU的操作系统,使用Linux能运行主要的Unix工具软件、应用程序和网络协议。

The above is the detailed content of How to install Redis server on CentOS 7. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template