Redis installation and cluster installation



gcc version is too low, it will report an error

gcc -v                             # 查看gcc版本
yum -y install centos-release-scl  # 升级到9.1版本
yum -y install devtoolset-9-gcc devtoolset-9-gcc-c++ devtoolset-9-binutils
scl enable devtoolset-9 bash
echo "source /opt/rh/devtoolset-9/enable" >>/etc/profile

Install redis

Create redis installation directory

运行命令行:mkdir -p /usr/local/6379

Unzip

cd /usr/local/src
tar -xvf redis-3.2.8.tar.gz

Enter the unzipped installation package directory

cd /usr/local/src/redis-3.2.8/


installation

make prefix=/usr/local/redis/ install


Redis configuration

Create Redis cache folder

运行命令行:mkdir -p /var/redis/6379


Create a configuration file storage path

运行命令行:mkdir -p /usr/local/6379

Note: Before creating a folder, check the disk usage and try to use a folder with a larger remaining space.


Copy the configuration file template and rename it

运行命令行:cp /usr/local/src/redis-3.2.8/redis.conf /usr/local/6379/


Modify the configuration file

vim /usr/local/6379/redis.conf


Specify port number: port 6379
Insert picture description here
Configure background operation: daemonize yes
Insert picture description here
Specify process file: pidfile /var/run/redis_6379.pid
Insert picture description here
Specify cache path: dir /var/redis/6379
Insert picture description here
Specify monitor: bind 192.168.168.179 Specify password according to local IP
Insert picture description here

Insert picture description here

Start redis

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


Set boot up

运行命令行:vim /etc/rc.d/rc.local
在最后添加/usr/local/bin/redis-server /usr/local/6379/redis.conf
保存退出后修改此文件的权限:chmod +x /etc/rc.d/rc.local


Test Redis

View the Redis process. The
Insert picture description here


client connects to Redis and
runs the command line: redis-cli -p 6379 -h 192.168.168.107 -a slme2010
Insert picture description here
Description : -p is followed by the redis port, -h is followed by the bound IP, -a is followed by the redis password

ping

Insert picture description here

Automatic backup

6.2.1 Create a redis regular backup script
Create a backup file storage folder:
run the command line: mkdir -p /redis/backup
Redis data file regular backup delete script:
vim /root/redisbackup.sh
add the following content at the end of the document

#!/bin/bash
DATE=`date +%Y%m%d%H%M%S`
tar -zcvf /redis/backup/datafile$DATE.tgz /var/redis/*
find /redis/backup -mtime +2 -name "*.tgz" -exec rm -rf {} \;
修改文件权限:chmod +x /root/redisbackup.sh
6.2.2创建计划任务,每小时执行一次
vim /etc/crontab/
在文档最后面加上以下内容
*/1 * * * * root sh /root/redisbackup.sh
保存后退出

注意:脚本中的/redis/backup路径要与你创建的备份文件存放路径一致
脚本中的/var/redis/路径是你redis配置文件中存放redis数据文件的路径
此脚本需要配合


Cluster deployment

Create redis installation directory

mkdir -p /usr/local/redis_cluster/{
    
    7001..7006}
mkdir -p /var/redis/{
    
    7001..7006}

Insert picture description here

unzip

tar xf redis-6.0.9.tar.gz

Insert picture description here

installation

Enter the unzipped installation package directory

cd /usr/local/src/redis-6.0.9


installation

make prefix=/usr/local/redis/ install


Copy profile template

echo /usr/local/redis_cluster/{
    
    7001..7006} | xargs -n 1 cp -v /usr/local/src/redis-6.0.9/redis.conf


Modify the configuration file

Here is 7001 as an example, each one needs to be changed

vim /usr/local/redis_cluster/7001/redis.conf

port 7001
daemonize yes
pidfile /var/run/redis_7001.pid
dir /var/redis/7001
bind 192.168.168.107
cluster-enabled yes   #开启集群


Here I wrote a script to start redis in batches

vim redis_allstart.sh  

#!/bin/bash
for i in {
    
    7001..7006}
do
  /usr/local/bin/redis-server /usr/local/redis_cluster/$i/redis.conf
done  

Insert picture description here



Use redis-cli to create a cluster

redis-cli --cluster create 192.168.168.107:7001 192.168.168.107:7002 192.168.168.107:7003 192.168.168.107:7004 192.168.168.107:7005 192.168.168.107:7006 --cluster-replicas 1

Insert picture description here

test

redis-cli -p 7001 -c

The main library is indeed switching
Insert picture description here

Guess you like

Origin blog.csdn.net/zyy130988/article/details/110119893