class=”markdown_views prism-dracula”>
Foreword:
Due to the high performance of redis, the application relies heavily on it. Sometimes the performance of a redis server is not enough, and a redis cluster needs to be configured. The simplest is one for reading and one for writing. Generally, the demand for reading is relatively large, so you can configure one master (read) and multiple slaves (write).
This time, we set up two virtual machines locally as a master and a slave respectively.
ip is 192.168.2.100 as the master server
ip is 192.168.2.101 as the slave server
1. Install redis
First install redis on the two servers separately. Please refer to
https://blog.csdn.net/u014691098/article/details/80892504
2. Configure the main server
1. Enter the 192.168.2.100 server and open the redis configuration file
[root@localhost redis-4.0.10]# vim /etc/redis/6379.conf
2. Comment out the line bind 127.0.0.1
or specify the ip. (This example is a comment, that is, all ip can be connected)
3. Start the daemon process
4. Set the access password (because the performance of redis is very high , the risk of crashing is extremely high, it is recommended to set the password online is very complicated, it is best to specify the ip in step 2)
Note:
Of course, since the master-slave is used, it means that the dependence on redis is very high , there are several parameters that need to be set according to the server configuration
The first is the maximum number of client connections (maxclients), the default is 10000, which can be changed according to requirements
The second is the maximum memory (the default is not limited, but if there are multiple slave servers, it is recommended to set a lower memory than the server memory value)
The third is the memory strategy, if the memory is enough, it is not used However, if the memory is not enough, it is recommended to set the least recently used policy (LRU). The default is to report an error if the memory is not enough
So far the main server configuration is complete!
Start redis service
[root@localhost redis-4.0.10]# service redisd start
Three, configure slave server
The first four steps are basically the same as the main server configuration
5. Configure the main server ip and port
6. Configuration The password of the main server to which it belongs (again, it is very complicated to set the password, this is just a demonstration)
It should be noted that the slave server is usually read-only, so you need to configure read-only (the default is read-only, just don’t change it)
Configuration complete, start service
[root@localhost redis-4.0.10]# service redisd start
Fourth, test
You can use redis client or telnet
This time use redis client
1. Enter the main server (192.168.2.100)
Enter the redis client
[root@localhost redis-4.0.10]# /usr/local/redis/bin/redis-cli
Since a password is set, authentication is required
Set a value
2, enter the slave server (192.168.2.101)
Use the get command to get the value of name, you can see
represents The configuration is successful
If it is written on the slave server, an error will be reported, as shown in the figure below
So far, the redis master-slave replication configuration is complete. If you need to configure multiple slave servers, you can repeat the third step