CentOS 7 配置运行多个 Redis Server-Install Redis from Source and Configure Multiple Redis Server on CentOS 7

Prerequisites

  • Tencent Cloud Lighthouse(腾讯云轻量云服务器).
  • CentOS 7.
  • Execute all directive as lighthouse.

Install Redis from Source

Choose a version from Download page.

Download Redis Source and Extract the Installtion.

1
2
3
wget https://download.redis.io/releases/redis-7.0.9.tar.gz
tar -zxvf redis-7.0.9.tar.gz
cd redis-7.0.9

After you have extracted and moved into the directory, start compiling with the make command:

1
make

The build may take somet time, depending on your server resources. Once completed, you will receive a message as follows:

2021-03-23_140018.png

Next, install Redis binaries with the make install command: sudo make install(I want set the installtion path to /usr/local, so sudo is required):

1
sudo make install PREFIX=/usr/local/redis # 路径

Create Multiple Redis configuration file

After installing Redis, you need to configure two Redis.

1
2
3
4
5
sudo mkdir -p /usr/local/redis/conf/6379
sudo mkdir -p /usr/local/redis/conf/6380

sudo cp redis.conf /usr/local/redis/conf/6379/
sudo cp redis.conf /usr/local/redis/conf/6380/

Open the Redis configuration files for editing:

Modifying /usr/local/redis/conf/6379/redis.conf

1
2
3
4
5
6
sudo vim /usr/local/redis/conf/6379/redis.conf

# Set some directives as shown below.
daemonize yes
pidfile /var/run/redis/redis_6379.pid
dir /var/lib/redis/6379/

Modifying /usr/local/redis/conf/6380/redis.conf

1
2
3
4
5
6
7
sudo vim /usr/local/redis/conf/6380/redis.conf

# Set some directives as shown below.
daemonize yes
port 6380
pidfile /var/run/redis/redis_6380.pid
dir /var/lib/redis/6380/

Creat directory to store pidfile and DB, then set correct permissions:

1
2
3
4
5
6
7
sudo mkdir -p /var/run/redis/
sudo chmod -R 766 /var/run/redis/

sudo mkdir -p /var/lib/redis/6379/
sudo chmod -R 777 /var/lib/redis/6379/
sudo mkdir -p /var/lib/redis/6380/
sudo chmod -R 777 /var/lib/redis/6380/

Create the Redis system file

Now that Redis is installed and configured, the find step is to create two systemd unit files so you can manage Redis services like regular systemd service.

1
sudo vim /etc/systemd/system/redis_6379.service

Add the following directives to service file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[Unit]
Description=Redis_6379
After=network.target

[Service]
Type=forking
User=redis
Group=redis
ExecStart=/usr/local/redis/bin/redis-server /usr/local/redis/conf/6379/redis.conf
ExecStop=/usr/local/redis/bin/redis-cli shutdown
Restart=always
LimitNOFILE=65535

[Install]
WantedBy=multi-user.target

1
sudo vim /etc/systemd/system/redis_6380.service

Add the following directives to service file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[Unit]
Description=Redis_6380
After=network.target

[Service]
Type=forking
User=redis
Group=redis
ExecStart=/usr/local/redis/bin/redis-server /usr/local/redis/conf/6380/redis.conf
ExecStop=/usr/local/redis/bin/redis-cli -p 6380 shutdown
Restart=always
LimitNOFILE=65535

[Install]
WantedBy=multi-user.target

Increase the value of somaxconn, Try to execute following command with sudo.

1
echo 1024 > /proc/sys/net/core/somaxconn

Note not required: If have problem about limit of somaxconn, try add net.core.somaxconn=65535 in the /etc/sysctl.conf file:

Enable Redis

1
2
sudo systemctl enable redis_6379
sudo systemctl enable redis_6380

Start Redis

1
2
sudo systemctl start redis_6379
sudo systemctl start redis_6380

Now, the redis-server status is shown as below.

2021-06-14_150405.png

Finally, to verify that the Redis service is functioning correctly, run the command in the picture and the output should shown as well:

2021-06-15_141329.png

That’s it.



CentOS 7 配置运行多个 Redis Server-Install Redis from Source and Configure Multiple Redis Server on CentOS 7
https://hutaoren.cn/2021/10/14/CentOS 7 安装运行多个 Redis Server/
作者
胡桃仁
发布于
2021年10月14日
许可协议