Install redis6.2.6 on CentOS 7 (including self-starting services and open ports)

1. Download the redis file from the official website

Two ways to download
1. Use the wget command to download directly to the server directory
wget https://download.redis.io/releases/redis-6.2.6.tar.gz
2. Go directly to the official website to download, and then upload to the specified directory on the server.
Please add a picture description
The placement directory is as follows
insert image description here

2. Verify installation dependencies

2.1 Install the system default version gcc

Use gcc -v to view the current environment, the system default version 4.8.5
Please add a picture description
If it is: command not found, you need to install gcc first.
Install the default version of gcc (you need to input during the installation process, just keep typing [y]):
yum install gcc-c++
When [Complete!] appears, the installation is successful.

2.2 Upgrade gcc version

CentOS7 is installed with a default GCC environment, the default version is 4.8.5! To compile redis-6.x, a compiler above C5.3 is required, otherwise a lot of errors will be encountered. The main reason is that the multi-threaded code starting from redis-6.x relies on the new type _Atomic in the C standard library. But note that gcc officially and completely supports stdatomic from version 4.9 (gcc-4.8.5 partially supports it). The default gcc version of centos7 is: 4.8.5 < 5.3 cannot be compiled.

Need to upgrade gcc to version 9:

yum -y install centos-release-scl
yum -y install devtoolset-9-gcc devtoolset-9-gcc-c++ devtoolset-9-binutils
#设置环境变量:
scl enable devtoolset-9 bash
#验证版本
gcc -v

Please add a picture description

3. Unzip, compile and install

# 解压下载文件
tar -xzvf redis-6.2.6.tar.gz
#进入解压目录
cd /home/redis-6.2.6
#编译并安装在指定目录
make install PREFIX=/usr/local/redis

If you encounter an error:

zmalloc.h:50:10: fatal error: jemalloc/jemalloc.h: 没有那个文件或目录
   50 | #include <jemalloc/jemalloc.h>
      |          ^~

Solution:

#make 时指定分配器为libc
make MALLOC=libc
#再执行编译并安装在指定目录
make install PREFIX=/usr/local/redis

Check the bin folder in the installation directory, the following indicates success
insert image description here

4. Modify the configuration redis.conf

4.1 Run as a daemon process
Copy redis.conf from the source directory of redis to the installation directory of redis

cp /home/redis-6.2.6/redis.conf /usr/local/redis/bin/

Modify the configuration file to make the redis service run as a daemon

cd /usr/local/redis/bin/
vi redis.conf

Change daemonize no to daemonize yes, then save and exit
insert image description here

4.2 Set password

insert image description here

4.3 Bind ip (optional)

The default local machine, if you want all hosts to be accessible, you can modify it to bind 0.0.0.0
insert image description here

5. Start redis service and test

5.1 start redis-server

cd /usr/local/redis/bin/
./redis-server redis.conf

Check whether the startup is successful

ps -ef |grep redis

The following figure indicates that the startup is successful
Please add a picture description

5.2 Test whether the installation is successful

#进入安装目录
cd /usr/local/redis/bin
./redis-cli -p 6379

#如果设置了密码,需要验证
auth 你的密码

Use the ping command, if it returns pong, it means the installation is successful
insert image description here
Test set, get
insert image description here

5.3 redis startup configuration

#进入/lib/systemd/system/目录
cd /lib/systemd/system/
#创建redis.service文件
vim redis.service

redis.service:

[Unit]
Description=redis-server
After=network.target

[Service]
Type=forking
# ExecStart需要按照实际情况修改成自己的地址
ExecStart=/usr/local/redis/bin/redis-server /usr/local/redis/bin/redis.conf
PrivateTmp=true

[Install]
WantedBy=multi-user.target

Set redis to start on boot

systemctl enable redis.service

Check if it has been started before

ps -ef|grep redis
# 杀死redis线程
kill -9 pid

insert image description here
Start the redis service

systemctl start redis.service

View service status

systemctl status redis.service

insert image description here
Other redis commands

# 停止服务
systemctl stop redis.service
# 取消开机自动启动(卸载服务)
systemctl disabled redis.service

6. Open the firewall port

#查看防火墙状态
systemctl status firewalld 
#查看开放的端口
firewall-cmd --query-port=6379/tcp
#添加端口
firewall-cmd --zone=public --add-port=6379/tcp --permanent 
#重载防火墙
firewall-cmd --reload 
#再次查看端口是否已经开放
firewall-cmd --query-port=6379/tcp

insert image description here

Guess you like

Origin blog.csdn.net/qq_29864051/article/details/129783234