Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use master-slave replication in Redis

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces how to use master-slave replication in Redis, which has a certain reference value, and interested friends can refer to it. I hope you can learn a lot after reading this article.

Redis master-slave replication (Master/Slave) 1. What is it:

This is what we call master-slave replication. After the host data is updated, the master/slver mechanism is automatically synchronized to the standby according to the configuration and policy. Master is write-based and Slave is read-based.

two。 Action

Separation of read and write, disaster recovery

3. How to play 3.1Slave (Library) not Master (Library) 3.2.Slave Library configuration: slaveof Master Library IP Master Library Port

Every time you disconnect from master, you need to reconnect, unless you configure it in the redis.conf file

Info replication

Redis.conf configuration:

# # set when the machine is a slav service, set the IP address and port of the master service. When Redis starts, it automatically synchronizes data from master slaveof # when the master service is set for password protection, the slav service connects to master masterauth 4 to modify the configuration file details.

Copy multiple redis.conf files

Copy out three configuration files and simulate the difference between the three redis service ports

[root@VM_0_7_centos] # cd / myredis/ [root @ VM_0_7_centos myredis] # lltotal 172 root root RW Sep 18 21:38 dump.rdb-rw-r--r-- 1 root root 82646 Sep 23 14:56 redis_aof.conf-rw-r--r-- 1 root root 82645 Sep 23 10:37 cp redis.conf redis6379.conf [root @ VM_0_7_centos myredis] # cp redis.conf redis6379.conf [root @ VM_0_7_centos myredis] # cp redis.conf redis6380.confs [root @ VM_0_7_centos myredis] # cp redis.conf redis6381.conf

Turn on daemonize yes

Pid file name:

Pidfile redis6479.pid

Designated port:

Port 6379

Log file name

Logfile 6379.log

Dump.rdb name:

Dbfilename dump6379.rdb

5 commonly used configuration mode 5.1 one master and two servants:

Copy 3 configuration files according to the above method

Redis6379.conf, redis6380.conf, redis6381.conf

[root@VM_0_7_centos myredis] # lltotal 448 RWKui Sep-1 root root 5684 Sep 24 23:22 6379.Log RWQM RQM-1 root root 5683 Sep 24 23:08 9736.Log RWKui RKui-1 root root 92 Sep 24 23:22 dump6379.rdb-rw-r--r-- 1 root root 92 Sep 24 23:08 dump9736.rdb-rw-r--r-- 1 root root 92 Sep 18 21:38 Dump.rdb-rw-r--r-- 1 root root 82649 Sep 24 23:21 redis6379.conf-rw-r--r-- 1 root root 82649 Sep 24 23:20 redis6380.conf-rw-r--r-- 1 root root 82649 Sep 24 23:20 redis6381.conf-rw-r--r-- 1 root root 82646 Sep 23 14:56 redis_aof.conf-rw-r--r-- 1 root root 82645 Sep 23 10:37 redis.conf

Start three servers respectively

[root@VM_0_7_centos myredis] # ps-ef | grep redisroot 31408 1 0 15:33? 00:00:00 redis-server 0.0.0.0:6379root 31439 16305 0 15:34 pts/0 00:00:00 redis-cli-p 6379root 31503 1 0 15:34? 00:00:00 redis-server 0.0.0.0:6380root 31524 16394 0 15:34 pts/1 00:00 : 00 redis-cli-p 6380root 31573 1 0 15:35? 00:00:00 redis-server 0.0.0.0:6381root 31598 16451 0 15:35 pts/2 00:00:00 redis-cli-p 6381

The three services do not have data.

Use info replication to view information, three services, no association, all master

1. Add data to the host

127.0.0.1 set K1 v1OK127.0.0.1:6379 > set K2 v2OK127.0.0.1:6379 > set K3 v3OK127.0.0.1:6379 > keys * 1) "K2" 2) "K3" 3) "K1"

two。 Subscribe to hosts at 6380 and 6381

127.0.0.1 6379OK127.0.0.1:6381 > clear127.0.0.1:6380 > slaveof 127.0.0.1 6379OK127.0.0.1:6381 > clear127.0.0.1:6381 > slaveof 127.0.0.1 6379OK

3. The host sets the value after subscribing to the slave to check the situation of the master and slave

All the information of the host will be copied after the slave subscription.

4. Use info replication to view host and slave status

5. Only the host can write data

6. After the host is shut down, the slave role does not change to slave.

7. After the host is rebooted, the role remains the same as master

8. After the slave is disconnected from the master, it needs to be reconnected unless configured in the redis.conf file

5.2. The fire is handed down from generation to generation

Features: the Master,Slave in which the previous Slave can be the next Slave can also receive connection and synchronization requests from other Slaves, so the Slave as the next master in the chain can effectively reduce the writing pressure of master.

Midway change redirection will erase the previous data and re-establish a copy of the latest

Slaveof new main library IP new main library port

1. Establish a link

two。 Test master-slave replication

5.3. turn from a guest into a host

Key command

SLAVEOF NO ONE makes the current database stop synchronizing with other databases and become the main database.

1. On the basis of one master and two followers

two。 After the host is disconnected, the slave SLAVEOF NO ONE becomes the master library

3. Slave 6380 becomes a host after being monitored by 6381, and its value is successfully copied.

4. If the original 6379 reboot is successful, the host identity has been lost and is not in the cluster. No data in the cluster

6. Principle of replication

When Slave starts a successful connection to master, it sends a sync command.

Master receives the command to start the save process in the background, and collects all the commands received to modify the dataset. After the background process is completed, master will transfer the whole data file to slave to complete a complete synchronization.

Full control for the first time: after receiving the database file data, the slave service saves it and loads it into memory.

Incremental control: master continues to send all new collected modification commands to slave at once to complete synchronization

However, as long as you reconnect to the master, a full synchronization (full copy) will be performed automatically.

7. Sentinel mode (automatic backlash)

Based on one master and two followers.

The automatic version, which is mainly anti-customer, can monitor the failure of the host in the background, and if it fails, it will automatically convert the slave database to the master database according to the number of votes.

1. Create a new sentinel.conf file under the custom / myredis directory with the right name.

two。 Content

Sentinel monitor monitored database name (self-named) 127.0.0.1 6379 1

Sentinel monitor host6379 127.0.0.1 6379 1

The last number 1 above means that slave voted to see who solved the problem to become the host after the host hung up, and the one with more votes became the host.

3. Activate the sentinel.

Redis-sentinel / myredis/ sentinel.confession [root @ VM_0_7_centos myredis] # cd / usr/local/ [root @ VM_0_7_centos bin] # lltotal 37824 RWKui Rukyu-1 root root 139 Sep 23 15:21 appendonly.aof-rw-r--r-- 1 root root 125 Sep 23 16:00 dump.rdb-rwxr-xr-x 1 root root 4739968 Jun 12 23:09 redis-benchmark-rwxr-xr-x 1 root root 9634424 Jun 12 23:09 redis-check-aof-rwxr-xr-x 1 root root 9634424 Jun 12 23:09 redis-check-rdb-rwxr-xr-x 1 root root 5050384 Jun 12 23:09 redis-clilrwxrwxrwx 1 root root 12 Jun 12 23:09 redis-sentinel-> redis-server-rwxr-xr-x 1 root root 9634424 Jun 12 23:09 redis-server-rwxr-xr-x 1 root root 9840 Jun 12 22:55 tclsh8.6 [root @ VM_0_7_centos bin] # redis-sentinel / myredis/sentinel.conf

4. View current data

5. Shut down 6379, wait a moment for the new host 6381 in the election

6. After 6379 reboot, become a slave and monitor 6381

7. Note: a group of sentinel can monitor multiple Master at the same time

8. The shortcomings of replication

Replication delay: since all write operations are first performed on Master, and then synchronously updated to Slave, there is a certain delay in synchronization from Master to Slave machines. When the system is very busy, the delay problem will be more serious, and the increase in the number of Slave machines will also aggravate this problem.

Thank you for reading this article carefully. I hope the article "how to use Master-Slave copy in Redis" shared by the editor will be helpful to everyone. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report