Linux (CentOS) installation Redis tutorial

1. Install and configure Redis (this article takes the installation of Redis 7 under CentOS 7 as an example)

1.1 Execute the command su to switch the root account (the password will not be displayed when entering the password, but it has actually been entered)

su

1.2 Execute the command wget+download link to download the compressed package

Redis version warehouse[click to jump]

#下载redis压缩包
wget https://download.redis.io/releases/redis-7.0.0.tar.gz

#如果wget命令无效,先执行下面这个命令安装wget
yum -y install wget

Copy the download link in the version warehouse

1.3 Execute the command tar -zxvf to decompress the redis compressed package to the specified directory

        Generally placed under /usr/local, you can adjust it yourself

#将下载好的redis压缩包解压到指定目录/usr/local下
tar -zxvf redis-7.0.0.tar.gz -C /usr/local

1.4 Execute commands to install C language dependencies

        Since Redis is written in C language, dependencies must be installed

#安装C语言依赖
yum install gcc-c++

1.5 Execute the command cd to switch to the /usr/local directory and view all files in this directory

#切换到指定目录/usr/local下
cd /usr/local
#查询当前目录下全部文件
ll

View the directory name after redis decompression

1.6 Execute the command to switch to the Redis-xxx directory, and then execute make to compile

#切换到解压完成的redis-xxx目录
cd redis-7.0.0
#编译
make

1.7  After compiling, switch to the src directory under the Redis-xxx directory and execute the command ./redis-server to start the Redis service

#切换到redis-xxx目录下的src目录中(默认安装在redis-xxx下的src目录)
cd src
#启动redis服务
./redis-server
Redis service started successfully

1.8 Close the window or Ctrl+C to exit Redis, and the Redis service is closed while exiting

        The default startup method of Redis is to run in the foreground . This startup method will block the entire session window and the Redis service will also stop once you exit or close the window. If you change it to run in the background, you need to modify the configuration file

1.8.1 Return to the Redis-xxx directory and find the Redis configuration file

#返回上一级目录
cd ..
#查询当前目录下全部文件
ll

1.9 Edit Redis configuration file

1.9.1 Open the redis.conf configuration file

        Press the INSERT key to edit, press the ESC key to exit the edit, and after exiting the edit, enter : wq! to save the modification and exit

#打开redis配置文件
vi redis.conf

1.9.2 daemonize

        The default is daemonize no, that is, it runs in the foreground, and it can be changed to daemonize yes to run in the background

1.9.3 port number port

        The default is 6379

1.9.4 IP binding bind

Introduction to Redis bind[click to jump]                 Set the IP address to a static IP address[click to jump]

        Before configuring bind , set the local IP address to a static IP address , otherwise the Redis configuration file must be re-modified every time you restart

1.9.5 protection mode protect-mode ( not recommended to modify )

        The default is protect-mode yes, that is, the protection mode is turned on (only the local connection is allowed in the protection mode), and the external connection is allowed by changing it to protect-mode no (it is not safe, it is recommended to set a password and then access it through the password)

1.9.6 Redis password requirepass 

        By default, this parameter does not exist, you need to add it yourself, requirepass and add a password , if you don’t add this parameter, you don’t need a password to access by default ( if you set a password, you can perform external access when the protection mode is turned on )

1.10 Close the Redis service

1.10.1 Execute the command cd src to switch to the src directory under the Redis-xxx directory and execute the command to start the Redis service with the modified configuration file

#以指定的redis-xxx下的redis配置文件启动redis服务
./redis-server /usr/local/redis-7.0.0/redis.conf
#查看redis相关进程
ps -ef|grep redis

1.10.2 If no password is set , directly execute the command ./redis-cli shutdown to close the Redis service

#关闭redis
./redis-cli shutdown

1.10.3  If the password has been set , first execute the command ./redis-cli to enter the Redis command line interface, then enter the auth password to log in to Redis, and finally enter shutdown to close the Redis service and enter exit/quit to exit the Redis command line interface

2. Redis starts automatically at boot

2.1 Create and edit the redis.service file

#在/etc/systemd/system路径下创建redis.service文件
vi /etc/systemd/system/redis.service

2.2 redis.service file content ( remember to change the path in ExecStart to your own) 

        Press the INSERT key to edit, press the ESC key to exit the edit, and after exiting the edit, enter : wq! to save the modification and exit

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

[Service]
Type=forking
#前面是redis-server的路径,后面是redis.conf的路径,填错了会无效
ExecStart=/usr/local/redis-7.0.0/src/redis-server /usr/local/redis-7.0.0/redis.conf
PrivateTmp=true

[Install]
WantedBy=multi-user.target

2.3 Execute the startup command and view the process

#启动服务
systemctl start redis
#查看redis相关进程
ps -ef|grep redis

2.4 Command overview ( systemctl enable redis starts automatically at boot)

#启动redis
systemctl start redis
#停止redis
systemctl stop redis
#重启redis
systemctl restart redis
#查看redis运行状态
systemctl status redis
#开机redis开机自启动
systemctl enable redis
#关闭redis开机自启动
systemctl enable redis

3. Remotely connect to Redis

3.1 Redis visualization tool Redis Desktop Manager connects to Redis

Redis Desktop Manager download link [click to jump]

Connection failed (this is caused by CentOS firewall)

3.2 Firewall ( CentOS7 )

        CentOS7 and CentIOS6 firewall commands are different, please find the CentOS6 firewall commands by yourself

3.2.1 Execute the following command to directly close the firewall (not recommended, not safe)

#关闭防火墙
systemctl stop firewalld.service

3.2.2 Execute the following command to open the specified port to allow external access

#永久允许该端口被外部访问(6379是Redis默认端口号)
firewall-cmd --permanent --add-port=6379/tcp
#重启防火墙
firewall-cmd --reload

3.2.3 Overview of Firewall Commands

#关闭防火墙
systemctl stop firewalld.service
#禁止防火墙开机自启动
systemctl disable firewalld.service
#永久允许该端口被外部访问
firewall-cmd --permanent --add-port=6379/tcp
#临时允许该端口被外部访问
firewall-cmd --add-port=6379/tcp
#禁止该端口被外部访问
firewall-cmd --remove-port=6379/tcp
#重启防火墙
firewall-cmd --reload

3.3 Reconnect successfully  

Guess you like

Origin blog.csdn.net/Coin_Collecter/article/details/130176696