Redis basics: 8, master-slave replication

What is it?

After the host data is updated, it is automatically synchronized to the master/salve mechanism of the standby machine according to the configuration and strategy. Master is mainly writing, Slave is mainly reading.

What can you do?

  • Read and write separation
  • Disaster recovery

How to play one servant and two masters

1. Matching is never worthy of the master

2. Configure from the library:slaveof 主库ip 主库端口

Every time you disconnect from the master, you need to reconnect, unless configured into the redis.conf file

3. Modify the detailed operation of the configuration file

 cp redis.conf redis6379.conf
 cp redis.conf redis6380.conf
 cp redis.conf redis6381.conf

Then modify or add the port, pidfile, logfile, and dbfilename of the three configuration files to 6379, 6380, 6381, respectively

 [79]终端
 redis-server /opt/redis-3.0.4/redis6379.conf
 redis-cli -p 6379
 
 [80]终端
 redis-server /opt/redis-3.0.4/redis6380.conf
 redis-cli -p 6380
 
 [81]终端
 redis-server /opt/redis-3.0.4/redis6381.conf
 redis-cli -p 6381

4. Modify the master-slave relationship of 80 and 81

[80]
slaveof 127.0.0.1 6379

[81]
slaveof 127.0.0.1 6379

You can use to info replicationview the master-slave relationship:

Replication information of the master: Replication information of the

Insert picture description here


slave:

Insert picture description here

5. It is found that there are data saved in [79] in [80] and [81]. And the data written in [79] can be obtained in [80][81], but [80][81] cannot write data.

6. It is found that if the master is offline, the slave will wait for the master to go online and still keep the slave mode. After the master goes online, it will still keep running in the original state; if the slave goes offline, the master will lose the slave, although the slave will go online again , Still cannot connect to the host, and has exited the slave mode, unless the host is reconnected.

How to play is passed down

What do you mean?

The previous input slave can be the master of the next slave, and the slave can also accept connection and synchronization requests from other slaves. Then the slave can be the next master in the chain, which can effectively reduce the master's writing pressure.

Change direction in the middle:

The previous data will be cleared, and the latest copy will be recreated.

Now, let [79] be the host of [80] and [80] as the host of [81]

[81]
slaveof 127.0.0.1 6380

It can be seen that [80] is used as both master and slave at this time

Insert picture description here

How to play against the guest

Assuming the current model of one servant and two masters, we let [80] become the new host:

[80]
slaveof no one

The function of this command is to stop the current database from synchronizing with other databases and turn it into the main database

Copy principle

  • After Slave starts successfully and connects to master, it will send a sync command
  • The master receives the command to start the background save process, and transfers the entire data file to the slave after execution to complete a complete synchronization.
  • Full copy: send all current data to slave
  • Incremental replication: Pass the new modification command to the slave to complete the synchronization
  • The slave connects to the master for the first time, starts full replication, and then uses incremental replication.

Sentinel mode (sentinel)

1. What is

The automatic version of anti-customer-oriented, can monitor whether the host is faulty in the background, and if it fails, it will automatically switch from the library to the main library according to the number of votes.

2. How to play

Adjust the structure, 6379 with 80, 81

Create a new sentinel.conf file in the redis folder, and the name cannot be wrong

Fill sentinel monitor [被监控数据库的名字(自己起)] 127.0.0.1 6379 1content: . The number 1 here means that after the host hangs up, Salve will vote to see who succeeds as the host, and how many votes will become the host.

Start the sentinel:redis-sentinel /opt/redis-3.0.4/sentinel.conf

Forcibly offline the 79 host:

The sentinel began to organize a vote

Insert picture description here

Check 80's and info replicationfind that it has become the boss! !

Insert picture description here

Check out 81 and info replicationfind that it has become a running dog of 80! !

Insert picture description here

Something funny happened! ! ! Now the 79 server is back online, and it is found that it has become a running dog of 80! !

Disadvantage

  • Delay: Since all write operations are performed on the Master first, and then synchronized to the Slave, there is a certain delay in synchronizing from the Master to the Slave machine. When the system is very busy, the delay problem will be more serious. The increase in the number of Slave machines will also make this problem more serious.