Redis master-slave replication and sentinel mechanism and persistence

1. Redis master-slave replication

    1.1, master-slave replication

        By configuring the master-slave relationship of two (or more) databases, data updates from one database server can be synchronized to another server.

    1.2, redis master-slave replication

        Redis supports master-slave (master-slave) mode, redisserver can be set as the host (slave) of another redis server, and the slave periodically gets data from the host. Specially, a slave can also be set as the host of a redis server, the master is mainly for writing, and the slave is mainly for reading.

        

    1.3. Redis master-slave replication configuration ( with slave not with master )

        1. Start three centos with redis installed
        2. Determine the master-slave relationship and use those two as slaves

        3. Use vim to edit the slave server's redis.conf (your own startup configuration file)

            Command vim /usr/local/redis/etc/redis.conf

            3.1 slaveof 192.168.0.100 6379 format is slaveof host ip host port

            3.2 #bind127.0.0.1 Log out the binding, which allows all ip connections to log in to this redis service

            3.3 protected-mode no Turn off the protected mode

        Note: You also need to open port 6379. The default firewall used by centos7 is firewall. Even if the firewall is turned off, the port still cannot be connected.

            1. systemctl status firewalld View firewall status

            2. systemctl start firewalld to start the firewall

            3. firewall-cmd --zone=public --add-port=6379/tcp --permanent permanently open port 6379

            4. firewall-cmd --reload update firewall rules

            5. systemctl restart firewalld.service restarts the firewall

            6. Firewall-cmd --query-port=6379/tcp Query whether port 6379 is open

            7. systemctl stop firewalld closes the firewall

        4. Check whether the slave can access port 6379 of the host 

            Command telnet 192.168.0.104 6379 test port

            Use Ctrl + ] to quit telnet connection quit quit

    1.4. Redis master-slave replication detection

        1. Start three centos respectively
        2. Start redis separately
            命令 /usr/local/redis/bin/redis-server  /usr/local/redis/etc/redis.conf
        3. Enter into the cli side respectively

            命令 /usr/local/redis/bin/redis-cli

        4. Execute the info command on the cli side separately

        5. Observation results

            master side

                

            slave1 side

                

            slave2 side

                

    1.5. Redis master-slave replication synchronization detection

        On the basis of 1.4, write some data on the master side such as set name z3 set age 30
        Read data on the slave side to see if it can be read (whether synchronization is completed)

            master side

                 

            slave1 side

                 

            slave2 side

                  

    1.6, the characteristics of redis master-slave replication

        1. The master can have multiple slaves

        2. In addition to multiple slaves connected to the same master, slaves can also be connected to other slaves to form a graph structure

        3. Master-slave replication will not block the master, which means that when one or more slaves are connected to the master for replication, the master can continue to process requests from clients

         4. Master-slave replication can be used to improve the scalability of the system. We can use multiple slaves to be responsible for the client's read requests, and we can do data redundancy.

2. Redis sentinel mechanism

    2.1, redis sentinel mechanism

        With the implementation of master-slave replication, if we want to monitor the master-slave server, a "sentry" mechanism is provided after redis 2.6. The sentinel in version 2.6 is version 1.0, which is not stable, and there will be various problems. After version 2.8, the sentinel function is stable.

        As the name suggests, the meaning of the sentinel is to monitor the running status of the Redis system. You can start multiple sentinels to monitor the running status of the Redis database. It has two main functions:

            1. Monitor whether the master database and slave database are running normally.

            2. When the main database fails, it can automatically switch from the database to the main database to realize automatic switching.

            Sentinel Mechanism Model

                

    2.2. Implementation steps of the sentinel mechanism

        Configure the sentinel on one of the slaves (can also be configured on the master server)

        Note: Because the original slave1 and slave2 will be automatically elected as the master server, it should be configured in its configuration file (#bind127.0.0.1 protected-mode no and open port 6379), it is best to use telnet +ip + port test one time.

           1. Copy the file /usr/local/redis/redis-4.0.8/sentinel.conf (your own redis sentinel.conf file) to the command cp /usr/local/redis/ in the /usr/local/redis/etc path redis-4.0.8/sentinel.conf /usr/local/redis/etc/

           2. Modify the sentinel.conf file vim/usr/local/redis/etc/sentinel.conf

                1) dir "/usr/local/redis/etc" data storage directory

                2) protected-mode no turn off the protected mode

                3) sentinel monitor mymaster192.168.43.201 6379 1 The format is the master's name, ip, address, port number, and the number of votes to elect the master when it fails

                4) sentineldown-after-milliseconds mymaster5000 timeout 5000 milliseconds is downtime

                5) sentinelparallel-syncsmymaster 2 Number of slave servers

            3. Start the sentinel sentinel command /usr/local/redis/bin/redis-server /usr/local/redis/etc/sentinel.conf --sentinel&


            4. View Sentinel information command /usr/local/redis/bin/redis-cli -h 192.168.43.64 -p 26379 info Sentinel


            5. Shut down the main server and view the cluster information command /usr/local/redis/bin/redis-cli shutdown


        It can be seen that when the primary server 192.168.43.201 goes down, 192.168.43.64 is elected as the new primary server

            6. View the information of the remaining two redis servers

            192.168.43.64 terminal

                

                It can be seen that 192.168.43.64 has automatically become the main server

            192.168.43.106 terminal

                

                It can be seen that its main service has become 192.168.43.64
            7. Start the main server again and view the information
                
                It can be seen that it has become a slave server, and its master server is 192.168.43.64

3. Persistence mechanism of redis

    Redis is an in-memory database that supports persistence, which means that redis needs to frequently synchronize the data in memory to the hard disk to ensure data persistence. Redis supports two persistence implementations, the persistence mechanism (snapshot) of snapshotting (rdb) and the persistence mechanism of append-only file (abbreviated aof), which can be specified through the startup configuration file ( redis.conf ), and rdb is used by default

    3.1. Persistence mechanism of snapshotting (rdb)

        The Snapshotting mechanism writes in-memory data to binary files in the form of snapshots. The default file name is dump.rdb. It can be configured to automatically do snapshot persistence. We can configure redis to automatically take a snapshot if there are more than m writes in n seconds.

        Note that the configuration dir " /usr/local/redis/etc/" specifies the storage path of the dump.rdb file

        In the configuration file redis.cof, the default configuration of snapshotting is:

            save 900 1 Execute a write operation within 900 seconds, and automatically take a snapshot.

            save 300 10 Execute 10 write operations within 300 seconds, and take snapshots automatically.

            save 60 10000 Executes 10,000 write operations within 60 seconds, and automatically takes a snapshot.

    3.2. Persistence mechanism of append-only file (aof)

        The append-only file method is somewhat similar to Oracle's log file. Since the snapshot method is to take a snapshot after a certain time interval, all the modified data after the last snapshot may be lost in the event of an accident in redis. aof has better persistence performance than the snapshot method, because when using aof, redis will append each write command received to the file through the write function, and the file will be re-executed when redis restarts Write commands saved in memory to rebuild the contents of the database in memory. The default file name is appendonly.aof

        Note that the configuration dir " /usr/local/redis/etc/" specifies the storage path of the appendonly.aof file

        Aof common settings:

            appendonly yes starts the persistence mechanism of aof, and the persistence mechanism of snapshot mode will lose its effect 

            appendfsync always persists to disk immediately after receiving the write command, which is the slowest, but guarantees complete persistence (usually used)

            appendfsync everysec writes to disk every second, making a good compromise between performance and durability 

            appendfsync no completely relies on os for the best performance, and there is no guarantee of persistence 

        test

            After the configuration is complete, start the redis service, and the appendonly.aof file will appear in the /usr/local/redis/etc/ directory. It is blank because no operation is done.

            do something set name z3 set age 30 set sex nan

                

            Open the appendonly.aof file to view the command vim /usr/local/redis/etc/appendonly.aof 

                

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325512413&siteId=291194637