当前位置:网站首页>Steps of redis installation and self startup configuration under CentOS system

Steps of redis installation and self startup configuration under CentOS system

2022-07-07 11:49:00 Full stack programmer webmaster

I'm sure you all know that Redis It's a C The implementation is memory based 、 Persistent key value pairs for database , It is often used as a caching service in distributed services . So this article will introduce in detail CentOS How to install from scratch to configure and start the service under the system . If necessary, we can refer to .

One . install Redis

Redis The installation of is actually quite simple , The recommended way is to download redis Source code , And install after native compilation .

Enter the download directory of the main folder for the first time , perform wget Download the source code

[[email protected] ~]$
cd
 download 
[[email protected]  download ]$ wget http:
//download
.redis.io
/redis-stable
.
tar
.gz

Next, after decompression , Move to /usr/redis Under the table of contents

[[email protected]  download ]$
tar
-zxvf redis-stable.
tar
.gz
[[email protected]  download ]$
su
mv
redis-stable
/usr/redis

Then enter redis Catalog , perform make command , compile redis Source code

[[email protected]  download ]
# cd /usr/redis/
[[email protected] redis]
# make

After compiling , stay src Directory is 2 An important program generation , One is redis-server, The other is redis-cli; Then enter the src Catalog , perform make install, At this time, these executable programs will be copied to /usr/local/bin Under the table of contents , because /usr/local/bin Is the environment variable in the system $PATH Defined , Therefore, the terminal can execute... At any position redis-server and redis-cli 了 .

[[email protected] redis]
# cd src/
[[email protected] src]
# make install

Install here redis That's it .

Let's see what the compiled programs do :

redis-server: seeing the name of a thing one thinks of its function ,redis service

redis-cli:redis client, Provide a redis client , For connection to redis service , Add, delete, modify, check, etc

redis-sentinel:redis Instance monitoring management 、 Notification and instance failure backup service

redis-benchmark:redis Performance testing tools for

redis-check-aof: If the AOF Way to generate logs , It is used to repair quickly when accidents happen

redis-check-rdb: If the RDB Way to generate logs , It is used to repair quickly when accidents happen

After installation , start-up redis-server, And run redis-cli To test

[[email protected] ~]$ redis-server
[[email protected] ~]$ redis-cli
127.0.0.1:6379> PING
PONG
127.0.0.1:6379>

So to speak redis The service has been working normally , If redis Service not started , Then run redis-cli It will be reported in time Could not connect to Redis at 127.0.0.1:6379: Connection refused Error of .

Two . Configure self startup

In order to make redis-server It can run automatically when the system starts , Need to put redis Services as daemons (daemon) To run the , We go back to /usr/redis/ Found one in the directory redis.conf The file of , This file is redis Configuration loaded by service runtime , Let's take a look at the content first

[[email protected] redis]$
vi
redis.conf

This file is very long , But most of them are annotations , We focus on several of these settings daemonize and pidfile

among daemonize The default value is false,pidfile The default value is pidfile /var/run/redis_6379.pid

The first indicates whether daemon turn , Obviously, we need to change it to daemonize yes;

The second means that when the service is running as a daemon ,redis By default the pid write in /var/run/redis_6379.pid file , The file exists when the service is running , Once the service stops, the file is automatically deleted , So it can be used to judge redis Is it running .

Exit after saving .

With the basic configuration ,redis There also needs to be a management startup 、 close 、 A script to restart .redis In fact, an initialization script has been provided in the source code , Position in /usr/redis/utils/redis_init_script.

Let's see what this script does :

#!/bin/sh#
 
REDISPORT=6379
EXEC=
/usr/local/bin/redis-server
CLIEXEC=
/usr/local/bin/redis-cli
 
PIDFILE=
/var/run/redis_
${REDISPORT}.pid
CONF=
"/etc/redis/${REDISPORT}.conf"
 
case
"$1"
in
 
start)
  
if
[ -f $PIDFILE ]
  
then
    
echo
"$PIDFILE exists, process is already running or crashed"
  
else
    
echo
"Starting Redis server..."
    
$EXEC $CONF
  
fi
  
;;
 
stop)
  
if
[ ! -f $PIDFILE ]
  
then
    
echo
"$PIDFILE does not exist, process is not running"
  
else
    
PID=$(
cat
$PIDFILE)
    
echo
"Stopping ..."
    
$CLIEXEC -p $REDISPORT
shutdown
    
while
[ -x
/proc/
${PID} ]
    
do
     
echo
"Waiting for Redis to shutdown ..."
     
sleep
1
    
done
    
echo
"Redis stopped"
  
fi
  
;;
 
*)
  
echo
"Please use start or stop as first argument"
  
;;
esac 

The port... Is specified in the script 、server route 、cli route 、pidfile Path and conf route , The above yellow areas need to be configured correctly , Many say , When installing make install, So the script here doesn't need to be changed much , because make install hold server and cli All copied to /usr/local/bin The following the .

In addition, see here conf The path of , We need to take redis In the catalog redis.conf File copy to /etc/redis/6379.conf

[[email protected] utils]
# cd /etc
[[email protected] etc]
# mkdir redis
[[email protected] etc]
# cp /usr/redis/redis.conf /etc/redis/6379.conf

And then redis_init_script Copy the script to /etc/init.d/redisd

[[email protected] etc]
# cp /usr/redis/utils/redis_init_script /etc/init.d/redisd

stay /etc/init.d The following scripts are all services that can be started automatically when the system starts , But now there is still a lack of configuration when the system starts :

[[email protected] zhxilin]
# chkconfig redisd on

Then you will find that an error is reported : service redisd I won't support it chkconfig ?

It's because we need to be in redis_init_script Add a small change at the beginning of :

#!/bin/sh
# chkconfig: 2345 90 10
# description: Redis is a persistent key-value database

Save and copy to /etc/init.d/redisd after , Run again chkconfig It's done. .

When everything is ready , You can execute the following command to verify service Whether the setting is successful :

[[email protected] zhxilin]
# service redisd start
[[email protected] zhxilin]
# service redisd stop

Equivalent to

[[email protected] zhxilin]
# /etc/init.d/redisd start
[[email protected] zhxilin]
# /etc/init.d/redisd stop

summary

Finally restart the system , Run directly after entering the system redis-cli test redis Whether the service has run automatically . The above is the whole content of this article , I hope the content of this article can bring some help to your study or work , If you have any questions, you can leave a message .

Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/113810.html Link to the original text :https://javaforall.cn

原网站

版权声明
本文为[Full stack programmer webmaster]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207070905457343.html