Centos7 installs Redis6, Linux redis installation and startup

================================

©Copyright Sweet Potato Yao2022-01-20

​​Sweet Potato Yao's Blog - CSDN Blog

1. Download the redis file

https://redis.io/download

2. Upload the redis file (redis-6.2.6.tar.gz) to the server for compilation and installation

1. Unzip the file:

tar -zxvf redis-6.2.6.tar.gz

2. Modify the folder name, make it shorter

mv redis-6.2.6 redis6

3. Enter the directory:

cd redis6/

4. Compile the file:

make

If the make command reports an error (yes can be skipped):
make[3]: cc: command not found
make[3]: *** [alloc.o] error 127
make[3]: leaving directory "/java/redis6/ deps/hiredis"
make[2]: *** [hiredis] error 2
make[2]: leaving directory "/java/redis6/deps"
make[1]: [persist-settings] error 2 (ignore)
CC adlist. o
/bin/sh: cc: command not found
make[1]: *** [adlist.o] error 127
make[1]: leaving directory "/java/redis6/src"
make: *** [all] error 2

Solution (cc: command not found):

yum -y install gcc automake autoconf libtool make

If the make command still reports an error (yes, skip it):
zmalloc.h:50:31: fatal error: jemalloc/jemalloc.h: No such file or directory
#include <jemalloc/jemalloc.h>
^
Compilation aborted.
make[1]: *** [adlist.o] error 1
make[1]: leaving directory "/java/redis6/src"
make: *** [all] error 2
solution: make add arguments (fatal error: jemalloc/jemalloc.h):

make MALLOC=libc

5. After compiling, install Redis
PREFIX as the specified installation directory, and automatically create the bin directory

make install PREFIX=/java/redis6

View (one more bin directory):

ll

3. Redis configuration file replication and data storage directory

1. Create a redis data storage directory datas, and all other applications will store data here for easy backup

sudo mkdir -p /var/datas/redis

2. Modify the directory owner

sudo chown -R java:java /var/datas

3. Copy the redis.conf configuration file to the bin directory

cp /java/redis6/redis.conf /java/redis6/bin/

4. Enter the bin directory:

cd /java/redis6/bin/

Fourth, redis.conf configuration file attribute modification

Modify the redis.conf configuration file

vi /java/redis6/bin/redis.conf

1. Redis turns on daemon mode

Find the daemonize configuration item, change it to yes, and enable daemonize mode

daemonize yes

2. Redis cancels IP binding

Comment the line of bind 127.0.0.1, you can only connect to redis locally, otherwise you cannot use a remote connection.

# bind 127.0.0.1

3. Redis turns off protected mode

Change the protected-mode yes to no, also to open the remote connection.

protected-mode no

4. Modify the default port of Redis (can be omitted)

The default port is 6378, modified to 6677

port 6677

位置和示例:
# Accept connections on the specified port, default is 6379 (IANA #815344).
# If port 0 is specified Redis will not listen on a TCP socket.
port 6677

5. Redis modifies the rdb file name, storage directory, log file name

File name and path related configuration

pidfile /var/run/redis_101_6677.pid

logfile "redis-101-6677.log"

dbfilename dump-101-6677.rdb

dir /var/datas/redis/

dir The directory where configuration logs and rdb files are stored, followed by /

位置:
# The working directory.
#
# The DB will be written inside this directory, with the filename specified
# above using the 'dbfilename' configuration directive.
#
# The Append Only File will also be created inside this directory.
#
# Note that you must specify a directory here, not a file name.
dir /var/datas/redis/

6. Redis change password: requirepass password (can be omitted if not required)

Open the comment and change the password to: 123456

requirepass 123456

位置:
# IMPORTANT NOTE: starting with Redis 6 "requirepass" is just a compatibility
# layer on top of the new ACL system. The option effect will be just setting
# the password for the default user. Clients will still authenticate using
# AUTH <password> as usually, or more explicitly with AUTH default <password>
# if they follow the new protocol: both will work.
#
# The requirepass is not compatable with aclfile option and the ACL LOAD
# command, these will cause requirepass to be ignored.
#
# requirepass foobared

3. Redis startup and testing

1. Start Redis

/java/redis6/bin/redis-server /java/redis6/bin/redis.conf

2. View the redis process:

ps -ef | grep redis

3. Start the Redis client

#默认启动命令(默认端口是6379)
/java/redis6/bin/redis-cli


#指定IP地址和指定端口启动
#-h host 指定IP地址
#-p port 指定端口号
/java/redis6/bin/redis-cli -h 127.0.0.1 -p 6677


#指定IP地址、端口号、密码启动
#-h host 指定IP地址
#-p port 指定端口号
#-a auth 指定密码,首先得在配置文件设置密码:requirepass 123456
/java/redis6/bin/redis-cli -h 127.0.0.1 -p 6677 -a redisPassword

Fourth, Centos7 sets Redis to start up, and Redis starts automatically

1. Create the redis.service file in the system service directory

sudo vi /etc/systemd/system/redis.service

2. Paste the content of the redis.service file (press the letter i before pasting to enter the editing mode):

Modify the path to your own installation path

[Unit]
#Description:描述服务
Description=Redis
#After:描述服务类别 
After=network.target

#服务运行参数的设置 
[Service]
#Type=forking是后台运行的形式 
Type=forking
#ExecStart为服务的具体运行命令,路径必须是绝对路径 
ExecStart=/java/redis6/bin/redis-server /java/redis6/bin/redis.conf
#ExecReload为重启命令 ,路径必须是绝对路径 
ExecReload=/java/redis6/bin/redis-server -s reload
#ExecStop为停止命令 ,路径必须是绝对路径 
ExecStop=/java/redis6/bin/redis-server -s stop
#PrivateTmp=True表示给服务分配独立的临时空间 
PrivateTmp=true

#运行级别下服务安装的相关设置,可设置为多用户,即系统运行级别为3
[Install]
WantedBy=multi-user.target

3. Reload system services:

sudo systemctl daemon-reload

4. The redis.service service is added to auto-start at boot (note that the space after redis.service cannot be followed)

sudo systemctl enable redis.service

5. Restart the server

reboot -f

6. After the system restarts, check the service running status:

sudo systemctl status redis.service

 7. Other commands

sudo systemctl start redis.service #启动redis服务 
sudo systemctl enable redis.service #设置开机自启动 
sudo systemctl disable redis.service #停止开机自启动 
sudo systemctl status redis.service #查看服务当前状态 
sudo systemctl restart redis.service  #重新启动服务 
sudo systemctl list-units --type=service |grep redis #查看所有已启动的服务

Five, the port of the Redis connection is open

1. Open ports:

sudo firewall-cmd --zone=public --add-port=6677/tcp --permanent

2. Make the port take effect:

sudo firewall-cmd --reload

3. View all open ports on the firewall

sudo firewall-cmd --zone=public --list-ports

Six, Linux Redis restart data loss solution

It can be solved by setting a parameter (vm.overcommit_memory) in the Linux system.

Proceed as follows:

1. Edit the sysctl.conf configuration file

sudo vi /etc/sysctl.conf 

2. Add the parameter vm.overcommit_memory configuration in another line below the file comment, as follows

vm.overcommit_memory = 1

3. Make the configuration file take effect

sudo sysctl -p

4. Test

#指定IP地址和指定端口启动
#-h host 指定IP地址
#-p port 指定端口号
/java/redis6/bin/redis-cli -h 127.0.0.1 -p 6677
set aa 11

get aa

5. Restart the server

Restart the server and repeat step 4 to connect to the redis client to view the data

reboot -f

(Time is precious, sharing is not easy, donate and give back, ^_^)

================================

©Copyright Sweet Potato Yao2022-01-20

​​Sweet Potato Yao's Blog - CSDN Blog

Guess you like

Origin blog.csdn.net/w995223851/article/details/122603347