Linux Enterprise Operations and Maintenance - Redis installation and deployment, master-slave replication, master auto-switching

1. What is Redis

Redis is one of the most popular NoSQL databases today. Redis is an open source, multi-data structure, support network, memory-based, optional persistence key-value pair storage database written in ANSI C with the following features:

Memory-based operation for efficient performance
 Supports distributed, theoretically unlimited expansion
key-value storage system
 Open Source Usage ANSI C Language writing, compliance BSD Protocol, support network, memory-based and persistent log type, Key-Value Database and provides multilingual API

Redis has the following features compared to other database types:

C/S Communication Model
 Single Process Single Thread Model
 Rich data types
 Operation Atomic
 High concurrent read-write
 Support lua Script

2. Installation and deployment of redis

server1 configuration redis

Download the redis-6.2.4.tar.gz zip package, unzip the installation

cd /pub/docs/redis
get redis-6.2.4.tar.gz
tar zxf  redis-6.2.4.tar.gz
cd redis-6.2.4
make  #Test the installation, install what is missing based on an error
make When downloading the required dependent plug-ins in error, you need to remove the package and re-decompress the installation, otherwise you will have a previous cache
make install    #install
cd utils   
vim   #Modify the installation script, comment on the if statement of the script about systemd, otherwise execute this script to report errors
./   #Execute the script and return all the way based on the interactive interface


Script Execution Successful Interface, Installation Successful

Installation succeeded, service can be started via absolute path

vim /etc/redis/6379.conf   #Edit profile, comment loopback interface, turn off protection mode
/etc/init.d/redis_6379 stop   #Stop it
/etc/init.d/redis_6379 start  #Start the database
redis-cli   #Enter redis database
set name westos    #Establish data
get name   #Get data


Start to enter redis database

View open ports
netstat -antlp|grep :6379

server2 configuration redis

Download Compile Installation

scp -r redis-6.2.4.tar.gz server2:   #server1 directly transfers the decompressed mulu to server2
 because redis yes c Language development, so download gcc Perform installation compilation
yum install -y gcc    #Download gcc
yum install -y systemd-devel  
vim      #Looking at the manual, you can see that if the service requires a knife systemd, the parameter USE_SYSTEMD=yes needs to be added when making
make USE_SYSTEMD=yes           #redis uses the systemd daemon
make install   #install
vim /etc/redis/6379.conf

Installation complete - as shown below!!

Edit related profile

cd redis-6.2.4/
cd utils   
cp systemd-redis_server.service /usr/lib/systemd/system/redis.service
cd /usr/lib/systemd/system

vim redis.service
ExecStart=/usr/local/bin/redis-server /etc/redis/redis.conf

redis-server    #View redis database information
cd /root/redis-6.2.4
mkdir /etc/redis
cp redis.conf /etc/redis/

vim /etc/redis/redis.conf   #Edit Main Profile
75 #bind -::1             #Note local connection, that is, allow other spacing connections to redis
94 protected-mode no                #yes to no, turn off protection mode
257 daemonize yes                   #no to yes, running as a daemon
302 logfile "/var/log/redis.log"    #logfile log file storage path
454 dir /var/lib/redis              #File Path

mkdir /var/lib/redis                #Create a directory in the path where the configuration file is edited
systemctl daemon-reload             #heavy load
systemctl start redis.service       #systemctl Open Service 

Modify the service file by commenting on both lines except ExecStart and Type=forking

redis-server #View redis database information

Vim/etc/redis/redis.conf #Edit the main configuration file

server3 configuration redis

server3 has the same redis configuration as server1

scp -r redis-6.2.4 server3:     #Copy the redis directory from the decompressed country in server1
 Since the directory is already there make Install tested, so you can install it directly here make install(You can also transfer compressed packages to server2,Then follow server1 Process installation is sufficient)
because redis yes c Language development, so download gcc Perform installation compilation
yum install -y gcc    #Download gcc
cd redis-6.2.4/
make install
cd utils/
/etc/init.d/redis_6379 start   #Open redis

vim /etc/redis/6379.conf
#bind -::1
protected-mode no

3. redis master-slave replication

Set redis, master for server1, slave for server2 and server3
Edit and add master information in/etc/redis/redis.conf, respectively

In server2
vim /etc/redis/redis.conf
Add master information on the last line
slaveof 6379 (current host, slave port 6379)

In server3
vim /etc/redis/6379.conf

At this point, the master-slave configuration is complete

In server1, enter redis
info View master status information

set name    #insert data
get name    #See
 Now insert data into the main library, from which you can get find out

From libraries you can get to information

4. Sentine Auto-switch

Use Sentry mode to automatically monitor Master nodes and automatically change Slaver nodes to Master nodes when currently suspended

SentinelIs a tool for monitoring Master status in redis clusters, is a high availability solution for Redis, sentinel sentinel mode has been integrated into redis version 2.4. sentinel is a highly available solution for redis, sentinel system can monitor one or more redisMaster services, as well as all the slave services of these master services; when a master service is offline, automatically upgrade one of the master services from the service to the master service instead of the offline master service to continue processing requests.

Sentinel allows redis to replicate master-slave. When a master in a cluster fails, sentinel can elect a new master to automatically take over the master's work. Other redis servers in the cluster automatically point to the new master to synchronize data. It is generally recommended that sentinel use an odd number of stations to prevent one sentinel from failing to connect to the master causing a false switch.

In the case of one master and two slaves, if the master is disconnected from the two slaves or because of a network relationship, the client does not know that the master is disconnected and will continue to write data, but at this time the slave side can no longer replicate data.
If you need to solve this problem, you need two slaves to simultaneously decide that you cannot connect to master or that you cannot connect beyond the set time, then the master side will refuse the client to continue writing, and then re-access into slave will not cause data loss.


Select the leading sentinel and then failover

Pick a new master server from the downline server
 Other downloaded servers replicate new master servers from the server
 The offline master server will act as the slave server 0 for the new server when it comes online again.

1. Configure sentinel file

Configure sentinel in server1 host and copy to server2 and server3

cd redis-6.2.4/
cp sentinel.conf /etc/redis/
cd /etc/redis/
vim sentinel.conf 
sentinel monitor mymaster 6379 2	##master is server1, 2 means two votes are required and the host is identified as down
sentinel down-after-milliseconds mymaster 10000	##Connection timeout is 10s
scp sentinel.conf server2:/etc/redis/
scp sentinel.conf server3:/etc/redis/
redis-sentinel /etc/redis/sentinel.conf 		##You can see two slave ends

Change file sentinel.conf

sentinel file configuration completed!!!

2.master auto-switch

Monitor information in server1, master opens sentry and puts monitor in the background
redis-sentinel /etc/redis/sentinel.conf &

server2, server3 are slave s at this time
Also turn on Sentry mode

redis-sentinel /etc/redis/sentinel.conf &

Open a new shell panel and connect to server1

Turn off redis in server1
Viewing changes in another server1 shell panel will show the process of closing and running for master for server2, server3

master became server2

Serr1 is now off, and when server1 redis is turned on, server1 automatically switches to slave and empties all previous data

5. Cluster cluster for redis

Clustering principles

1. All redis nodes are interconnected (PING-PONG mechanism), and binary protocols are used internally to optimize transmission speed and bandwidth.
2. Node fail is only valid when more than half of the nodes in the cluster detect failures.
3. Clients connect directly to the redis node without the intermediate proxy layer. Clients do not need to connect to all nodes in the cluster, but to any available node in the cluster.
4.redis-cluster maps all physical nodes to [0-16383] slots, and cluster maintains node<->slot<->value

There are 16384 hash slots built into the Redis cluster. When a key-value needs to be placed in the Redis cluster, redis first calculates a result using the crc16 algorithm for the key, then calculates the remainder for 16384, so that each key corresponds to a hash slot numbered between 0 and 16383, and redis maps the hash slot to different nodes based on roughly equal number of nodes

1. Cluster building

Build clusters manually

Serr1 host is master, restart redis, open AOF mode, set up medis directory, cluster 7001-7006 directory, configure configuration files under directory, start service

In server1

/etc/init.d/redis_6379 restart
vim /etc/redis/6379.conf
appendonly yes			##Turn on AOF mode

cd /usr/local
mkdir redis     #Create directory
cd redis/
mkdir 700{1..6}    #Create a directory of six nodes
cd 7001

vim redis.conf    #Edit Profile
port 7001
cluster-enabled yes					##Open Cluster
cluster-config-file nodes.conf		##Cluster Profile
cluster-node-timeout 5000			##Node Timeout
appendonly yes						##Turn on AOF mode
daemonize yes						##Start with a daemon thread

redis-server redis.conf 			##Start Services
ps ax								##redis-server *:7001 [cluster]
cp redis.conf ../7002/				##Copy profile to 7002-7006
cp redis.conf ../7003/
cp redis.conf ../7004/
cp redis.conf ../7005/
cp redis.conf ../7006/

Enter each node directory to modify the configuration file separately: port 7001
 For example:
cd 7002/     								
vim redis.conf 
port 7002
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
daemonize yes

redis-server redis.conf   #Start Services 

Turn on AOF mode in /etc/redis/6379.conf file

After creating the redis directory, edit the configuration file redis.conf (files in the 7001 directory) in each node directory

Ultimately, you can use ps to view when the services for all nodes are started

ps ax     ##View process

Use redis-cli for cluster interaction, clients connect to any node, use -c to sign in as a cluster, and -p to specify a port. info view, currently master, no slave end.

Create a cluster master-slave node, with the cluster-replicas parameter as a number, 1 indicating that each master node requires a slave node. Then check the cluster to see the cluster information.

redis-cli --cluster create --cluster-replicas 1    #Create Cluster
redis-cli --cluster check	##Check Cluster
redis-cli --cluster info		##Viewing Cluster Information

Create six nodes, 1, 2, 3 as master, one master, one slave

Check the cluster and see that there is one master and one slave

Viewing Cluster Information

Script Build Cluster

cd redis-6.2.4/
cd utils/create-cluster/
./create-cluster start		##Open instances Starting 30001~30006
ps ax
./create-cluster stop		##Stop all instances and use the manually built cluster for the next experiment
ps ax   #See

2. Node master-slave switching

View node information before we go to node master-slave information

As shown in the figure, 7004 is a slave node of 7003. When port 7003 is connected and the current node is turned off, 7004 automatically switches to the primary node, but not from the node. Turn on server2, then check the cluster for the slave node, and you can see that 7004 has a slave node of 7003.

redis-cli -c -p 7003
ps ax										##7003 disappeared
redis-cli --cluster check	##7004 0 slaves
cd ..
cd 7003
redis-server redis.conf                     ##Start the service again
ps ax										##redis-server *:7003 [cluster]
redis-cli --cluster check	##7004 1 slaves is 7003

Close 7003

ps ax view found 7003 disappeared

redis-cli --cluster check
Checking all node information found 7004 to be master and not from a node

Turn on 7003 again, look at the node information again and find that 7003 has become a slave node of 7004, that is, switch master-slave node automatically

3. Add Nodes and Slices

Create directories and configuration files for 7007 and 7008 nodes

mkdir 700{7,8}
cd 7007
cp ../7001/redis.conf .
vim redis.conf 
port 7007
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
daemonize yes
redis-server redis.conf 

7008 identical
ps ax

View the processes of two nodes

The 7007 nodes were added to the cluster, but slots were not allocated, so this node did not really start sharing the cluster work, so it had to be partitioned. Re-partitioning basically means moving hash slots from one set of nodes to another, just like cluster creation.

redis-cli --cluster add-node	 #Adding 7007 nodes to the cluster
redis-cli --cluster check	#7007 0 slaves slots: (0 slots) master

The node joined the cluster but did not allocate slot s, so the node did not really start sharing the cluster work

Check cluster information, 7007 node is master

Allocate slot s

redis-cli --cluster reshard								#Re-slicing

Fragmentation operation
How many slots do you want to move (from 1 to 16384)? 1000				#Number of moving slots
What is the receiving node    ##ID of 7007 node (ID of receiving node)
Source node #1: all	  #From which nodes do you get these keys, input all gets these hash slots from all other primary nodes
Do you want to proceed with the proposed reshard plan (yes/no)? yes		#Determine if you want to continue repartitioning, enter yes
redis-cli --cluster check   #Check cluster information

Check cluster information again

At this point, 7007 nodes have 1000 hash slots and no slave nodes, so 7008 nodes are added to the 7001-node cluster as slave nodes of 8a0bcd08256e3f0790097ee26a62450440fe3a41. If not specified--cluster-master-id is randomly assigned to any of the primary nodes.

redis-cli --cluster check	

redis-cli --cluster add-node --cluster-slave --cluster-master-id 8a0bcd08256e3f0790097ee26a62450440fe3a41  #The 7008 node is added to the 7001 node cluster and acts as the slave node ID with node_id 7007.
If the primary node is not specified ID --cluster-master-id Randomly assigned to any primary node
redis-cli --cluster check	#Check Cluster
redis-cli --cluster info     #View cluster information

Add 7008 to Cluster

Check Cluster
redis-cli --cluster check

Show cluster information, 7007 now has a slave node
redis-cli --cluster info

6. Redis+Mysql

Redis as the cache server for mysql

The Psmisc package contains three programs to help manage/proc directories

fuser Displays processes that use the specified file or file system PID. 
killall Kill a process by name that signals all processes running the specified command.
pstree Tree displays currently running processes.

Kill all is used to kill processes by name. We can use the kill command to kill processes with the specified process PID. If we want to find processes that we need to kill, we need to use ps and other commands before we can use grep to find processes. Kill all combines these two processes into one
Format: killall [command-name]

The fuser command is used to display all process information that is using the specified file, file system, or sockets.

Common Options
-a Displays all the files specified on the command line so that the accessed files are displayed by default.
-c, like-m, is used for POSIX compatibility.
-k Kills the process that accesses the file. If -signal is not specified, a SIGKILL signal is sent.
-i Ask the user before killing the process, if no-k option is ignored.
-l Lists all known signal names.
-m name specifies a file mounted on the file system or a mounted block device (name). All processes accessing this file or file system will be listed. If a directory is specified, it will be automatically converted to "name/" and all file systems mounted under that directory will be used.
-n space specifies a different name space. Different space files (filename, default here), TCP (local TCP port), udp (local udp port) are supported. For ports, you can specify the port number or name, and if this does not cause ambiguity, you can use simple representations such as name/space (i.e., 80/tcp).
-s silent mode, when-u,-v is ignored. -a cannot be used with-s.
-signal uses the specified signal, not the SIGKILL, to kill the process. The signal can be represented by a name or number (e.g. -HUP,-1). This option should be used with-k, otherwise it will be ignored.
-u Add the user name of the process owner after each PID.
-v verbose mode. Output like ps command contains many domains such as PID,USER,COMMAND, etc. If accessed by kernel then PID is kernel. -V Output version number.
-4 uses IPV4 sockets and cannot be used with-6. Naming of-n tcp and udp is not ignored only if they exist.
-6 uses IPV6 sockets and cannot be used with-4. Naming of-n tcp and udp is not ignored.

  • Reset all options and set the signal to SIGKILL.

In server1 host (nginx+php)

Install process management tools to facilitate killall commands and shut down all previous redis processes

yum install -y psmisc	##Easy to use killall command
killall redis-server
ps ax					##No redis and normal nginx and php

Install packages php, gearmand, libevent-devel, libgearman related packages

Local Host Get Package
cd /pub/docs/redis
mirror rhel7/

cd rhel7/
yum install -y *.rpm      #Install All
systemctl daemon-reload   
systemctl start php-fpm   #Open php

Packages in rhel7 directory

Modify the test page test.php content, modify master-slave ip

mv test.php /usr/local/nginx/html/

vim test.php
  3         $redis->connect('',6379)
 10                         $connect = mysql_connect('','redis','westos');

Test Page test.php Source

        $redis = new Redis();
        $redis->connect('',6379) or die ("could net connect redis server");
  #      $query = "select * from test limit 9";
        $query = "select * from test";
        for ($key = 1; $key < 10; $key++)
                if (!$redis->get($key))
                        $connect = mysql_connect('','redis','westos');
                        $result = mysql_query($query);
                        //Cache the results of the query sql to redis if $key is not found
                        while ($row = mysql_fetch_assoc($result))
                        $myserver = 'mysql';
                        $myserver = "redis";
                        $data[$key] = $redis->get($key);
        echo $myserver;
        echo "<br>";
        for ($key = 1; $key < 10; $key++)
                echo "number is <b><font color=#FF0000>$key</font></b>";
                echo "<br>";
                echo "name is <b><font color=#FF0000>$data[$key]</font></b>";
                echo "<br>";

In server2 host (redis)
Turn on redis, info view information, currently server1's slave. Modify the configuration file/etc/redis/redis.conf, delete the settings about the master and restart redis. info view server2 is now the master host

In server3 host (mariadb database)

On the server3 host, stop rdis and cancel boot-up. Remove redis environment variable and take effect

/etc/init.d/redis_6379 stop
chkconfig redis_6379 off		##Cancel boot-up self-start
vim .bash_profile
source .bash_profile 

Install mariadb to restore mysql's configuration file, delete all mysql's data files, and open mariadb

yum install -y mariadb-server.x86_64 

vim /etc/my.cnf

systemctl start mariadb

Enter the database, you will see the test library, import the local file test.sql file, is the contents of the test library. Enter the database, you will see the imported data, and authorize redis users

> cd pub/docs/redis/
> get test.sql 

> show databases;			##test

mysql < test.sql     #Import test library contents
> use test
> show tables;
> select * from test;		##test1~test9
> grant all on test.* to redis@'%' identified by 'westos';    #To grant authorization

Before importing data

View database after importing test.sql data

Restart the database after importing data
systemctl restart mariadb.service

test.sql source

use test;
INSERT INTO `test` VALUES (1,'test1'),(2,'test2'),(3,'test3'),(4,'test4'),(5,'test5'),(6,'test6'),(7,'test7'),(8,'test8'),(9,'test9');

#    SET @RECV=gman_do_background('syncToRedis', json_object( as `id`, as `name`)); 
#  END$$

Access in browser
This data can only be copied synchronously, not asynchronously. It can only be overwritten and read on redis. The mysql side changes will not take effect

We can change the data in server2 redis

Verify data is synchronized in browser

Read and write data on mysql side

In servre3

cd /pub/docs/redis/

yum install -y unzip 
cd lib_mysqludf_json-master/
yum install -y gcc 
gcc $(mysql_config --cflags) -shared -fPIC -o lib_mysqludf_json.c 
cp  /usr/lib64/mysql/plugin/

Get into mysql See
mysql> show global variables like 'plugin_dir';

mysql> select * from mysql.func;

tar zxf gearman-mysql-udf-0.6.tar.gz  
cd gearman-mysql-udf-0.6/
./configure --libdir=/usr/lib64/mysql/plugin/ 
yum install -y libgearman* libevent* 
./configure --libdir=/usr/lib64/mysql/plugin/   #Re-detect
make   #Compile
make install    #install

cd /usr/local/lib (Directory is empty) 
mysql> CREATE FUNCTION gman_do_background RETURNS STRING SONAME ''; 
mysql> select * from mysql.func;

Install Package

select * from mysql.func;

vim test.sql

Posted on Mon, 20 Sep 2021 06:59:17 -0400 by micah1701