1. Brief description
There should be at least three nodes in the cluster, and each node has a backup node. 6 servers are required.
If conditions are limited, you can build a pseudo-distributed cluster. The following steps are to build a redis cluster with 6 nodes on a Linux server.
2. Steps to create a cluster
2.1. Create a directory
New directory: mkdir /usr/local /redis-cluster
2.2. Download the source code and unzip and compile it
wget http://download.redis.io/releases/redis-5.0.0.tar.gz tar xzf redis-5.0.0.tar.gz cd redis-5.0.0 make make install prefix=/usr/local/redis
3. Create 6 redis configuration files
The six configuration files cannot be in the same directory. Here we define them as follows:
/root/software/redis/redis-cluster-conf/7001/redis.conf
/ root/software/redis/redis-cluster-conf/7002/redis.conf
/root/software/redis/redis-cluster-conf/7003/redis.conf
/root/software/redis/redis- cluster-conf/7004/redis.conf
/root/software/redis/redis-cluster-conf/7005/redis.conf
/root/software/redis/redis-cluster-conf/7006/redis. conf
Some operation commands are for reference only:
cp redis.conf /usr/local/redis/bin cd /usr/local/redis/ cp -r bin ../redis-cluster/redis01 cd /usr/local/redis-cluster/redis01 rm dump.rdb #删除快照 vim redis.conf
The content of the configuration file is:
port 7001 #端口 cluster-enabled yes #启用集群模式 cluster-config-file nodes.conf cluster-node-timeout 5000 #超时时间 appendonly yes daemonize yes #后台运行 protected-mode no #非保护模式 pidfile /var/run/redis_7001.pid bind 172.20.10.7 #127.0.0.1改为本机ip地址,可用ifconfig查看ip
The port and pidfile need to be adjusted according to the folder. .
Create the remaining 5 instances:
[root@master redis-cluster]# cp -r redis01/ redis02 [root@master redis-cluster]# cp -r redis01/ redis03 [root@master redis-cluster]# cp -r redis01/ redis04 [root@master redis-cluster]# cp -r redis01/ redis05 [root@master redis-cluster]# cp -r redis01/ redis06
Modify the port and pidfile under redis.conf of redis02 ~ redis06 respectively
4, start the node
Enter the redis01, redis02,...redis06 directories respectively and execute: ./redis-server ./redis.conf
Create a batch file and start six redis
at the same timevim startall.sh
Add the following content:
cd redis01 ./redis-server redis.conf cd .. cd redis02 ./redis-server redis.conf cd .. cd redis03 ./redis-server redis.conf cd .. cd redis04 ./redis-server redis.conf cd .. cd redis05 ./redis-server redis.conf cd .. cd redis06 ./redis-server redis.conf cd ..
Then execute chmod u x start-all.sh
willstart -all.sh
Become an executable file
Start in the current directory: ./startall.sh
View:ps aux|grep redis
<img src="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/article/000/887/227/168559062896660.jpg?x-oss-process=image/resize,p_40" class="lazy" alt="How to build and use redis5 cluster under Centos7">
5. Start the cluster
Because we are using version 5.0.0 To build a cluster with redis, you only need to copy the redis-cli file in the compiled redis directory to the redis-cluster directory. (After redis version 5.0, it is started directly in C language)
/usr/local/redis-cluster/redis-cli --cluster create 172.20.10.7:7001 172.20.10.7:7002 172.20. 10.7:7003 172.20.10.7:7004 172.20.10.7:7005 172.20.10.7:7006 --cluster-replicas 1
After starting, you can see the success message, as follows:
>>> performing hash slots allocation on 6 nodes... master[0] -> slots 0 - 5460 master[1] -> slots 5461 - 10922 master[2] -> slots 10923 - 16383 adding replica 172.20.10.7:7004 to 172.20.10.7:7001 adding replica 172.20.10.7:7005 to 172.20.10.7:7002 adding replica 172.20.10.7:7006 to 172.20.10.7:7003 >>> trying to optimize slaves allocation for anti-affinity [warning] some slaves are in the same host as their master m: a4128b5e581c3722acd9b093c5f29f5056f680b0 172.20.10.7:7001 slots:[0-5460] (5461 slots) master m: d6fed6f21269b8469a3076ac5fb168bd20f70c26 172.20.10.7:7002 slots:[5461-10922] (5462 slots) master m: 51a0f62dacead745ce5351cdbe0bdbae553ce413 172.20.10.7:7003 slots:[10923-16383] (5461 slots) master s: 45cc35740ac67f7988bb75325871ba12d08a76e4 172.20.10.7:7004 replicates a4128b5e581c3722acd9b093c5f29f5056f680b0 s: 668054fe16cdf8741152cae863f5c636ed18b803 172.20.10.7:7005 replicates d6fed6f21269b8469a3076ac5fb168bd20f70c26 s: ae39b7db285703f8c08412d6b04998c60a634295 172.20.10.7:7006 replicates 51a0f62dacead745ce5351cdbe0bdbae553ce413 can i set the above configuration? (type 'yes' to accept):yes
Enter yes and press Enter
>>> nodes configuration updated >>> assign a different config epoch to each node >>> sending cluster meet messages to join the cluster waiting for the cluster to join ...... >>> performing cluster check (using node 172.20.10.7:7001) m: a4128b5e581c3722acd9b093c5f29f5056f680b0 172.20.10.7:7001 slots:[0-5460] (5461 slots) master 1 additional replica(s) m: d6fed6f21269b8469a3076ac5fb168bd20f70c26 172.20.10.7:7002 slots:[5461-10922] (5462 slots) master 1 additional replica(s) s: 45cc35740ac67f7988bb75325871ba12d08a76e4 172.20.10.7:7004 slots: (0 slots) slave replicates a4128b5e581c3722acd9b093c5f29f5056f680b0 m: 51a0f62dacead745ce5351cdbe0bdbae553ce413 172.20.10.7:7003 slots:[10923-16383] (5461 slots) master 1 additional replica(s) s: 668054fe16cdf8741152cae863f5c636ed18b803 172.20.10.7:7005 slots: (0 slots) slave replicates d6fed6f21269b8469a3076ac5fb168bd20f70c26 s: ae39b7db285703f8c08412d6b04998c60a634295 172.20.10.7:7006 slots: (0 slots) slave replicates 51a0f62dacead745ce5351cdbe0bdbae553ce413 [ok] all nodes agree about slots configuration. >>> check for open slots... >>> check slots coverage... [ok] all 16384 slots covered.
At this point, the reids5 cluster is completed.
6. Cluster operation
6.1. Shut down the cluster
Method 1:
Provided by redis5 Tools for shutting down the cluster are in the following directory:
/root/redis-5.0.0/utils/create-cluster
Open this file and modify the port to our own , as shown below:
The port prot is set to 7000, nodes is 6, and the tool will automatically accumulate 1 to generate six nodes 7001-7006 for operation.
Look down and modify the path and add the IP address. If not added, it will default to the local 127.0.0.1
After modification, execute the following command to shut down the cluster:
/root/redis-5.0.0/utils/create-cluster/create-cluster stop
Method 2:
create-cluster directory Write a script file: vim shutdown.sh
The content is as follows:
/usr/local/redis-cluster/redis-cli -c -h 172.20.10.7 -p 7001 shutdown /usr/local/redis-cluster/redis-cli -c -h 172.20.10.7 -p 7002 shutdown /usr/local/redis-cluster/redis-cli -c -h 172.20.10.7 -p 7003 shutdown /usr/local/redis-cluster/redis-cli -c -h 172.20.10.7 -p 7004 shutdown /usr/local/redis-cluster/redis-cli -c -h 172.20.10.7 -p 7005 shutdown /usr/local/redis-cluster/redis-cli -c -h 172.20.10.7 -p 7006 shutdown
Then execute chmod u x shutdown.sh
to turn shutdown.sh into an executable file
Start in the current directory: ./shutdown.sh
View: ps aux|grep redis
Official:/usr/local/redis-cluster/redis-cli -a xxx -c - h 192.168.5.100 -p 8001
Tips: -a access server password, -c indicates cluster mode, -h specifies the ip address, -p specifies the port number
6.2, Restart the cluster
/root/redis-5.0.0/utils/create-cluster/create-cluster start
6.3. Use script File startup cluster
vim startall.sh Append the following content: (Remember to change your own IP address)
/usr/local/redis-cluster/redis-cli --cluster create 172.20.10.7:7001 172.20.10.7:7002 172.20.10.7:7003 172.20.10.7:7004 172.20.10.7:7005 172.20.10.7:7006 --cluster-replicas
Start: ./startall.sh
7. Execute
redis01/redis-cli -h 192.168.25.153 -p 7002 -c
## in the test cluster
The above is the detailed content of How to build and use redis5 cluster under Centos7. For more information, please follow other related articles on the PHP Chinese website!

Redis plays a key role in data storage and management, and has become the core of modern applications through its multiple data structures and persistence mechanisms. 1) Redis supports data structures such as strings, lists, collections, ordered collections and hash tables, and is suitable for cache and complex business logic. 2) Through two persistence methods, RDB and AOF, Redis ensures reliable storage and rapid recovery of data.

Redis is a NoSQL database suitable for efficient storage and access of large-scale data. 1.Redis is an open source memory data structure storage system that supports multiple data structures. 2. It provides extremely fast read and write speeds, suitable for caching, session management, etc. 3.Redis supports persistence and ensures data security through RDB and AOF. 4. Usage examples include basic key-value pair operations and advanced collection deduplication functions. 5. Common errors include connection problems, data type mismatch and memory overflow, so you need to pay attention to debugging. 6. Performance optimization suggestions include selecting the appropriate data structure and setting up memory elimination strategies.

The applications of Redis in the real world include: 1. As a cache system, accelerate database query, 2. To store the session data of web applications, 3. To implement real-time rankings, 4. To simplify message delivery as a message queue. Redis's versatility and high performance make it shine in these scenarios.

Redis stands out because of its high speed, versatility and rich data structure. 1) Redis supports data structures such as strings, lists, collections, hashs and ordered collections. 2) It stores data through memory and supports RDB and AOF persistence. 3) Starting from Redis 6.0, multi-threaded I/O operations have been introduced, which has improved performance in high concurrency scenarios.

RedisisclassifiedasaNoSQLdatabasebecauseitusesakey-valuedatamodelinsteadofthetraditionalrelationaldatabasemodel.Itoffersspeedandflexibility,makingitidealforreal-timeapplicationsandcaching,butitmaynotbesuitableforscenariosrequiringstrictdataintegrityo

Redis improves application performance and scalability by caching data, implementing distributed locking and data persistence. 1) Cache data: Use Redis to cache frequently accessed data to improve data access speed. 2) Distributed lock: Use Redis to implement distributed locks to ensure the security of operation in a distributed environment. 3) Data persistence: Ensure data security through RDB and AOF mechanisms to prevent data loss.

Redis's data model and structure include five main types: 1. String: used to store text or binary data, and supports atomic operations. 2. List: Ordered elements collection, suitable for queues and stacks. 3. Set: Unordered unique elements set, supporting set operation. 4. Ordered Set (SortedSet): A unique set of elements with scores, suitable for rankings. 5. Hash table (Hash): a collection of key-value pairs, suitable for storing objects.

Redis's database methods include in-memory databases and key-value storage. 1) Redis stores data in memory, and reads and writes fast. 2) It uses key-value pairs to store data, supports complex data structures such as lists, collections, hash tables and ordered collections, suitable for caches and NoSQL databases.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.