[] Redis master-slave synchronization: master-slave switchover, redis master-slave synchronization switchover

Source: Internet
Author: User

[] Redis master-slave synchronization: master-slave switchover, redis master-slave synchronization switchover

Environment-OS: Centos7.2 IP Address: 192.168.146.100

Centos7.2 IP Address: 192.168.146.110

Download the redis source code package (146.100 operations)

[root@master ~]# wget http://download.redis.io/releases/redis-3.2.3.tar.gz

Decompress and install redis
[root@master ~]# tar zxf redis-3.2.3.tar.gz [root@master ~]# cd redis-3.2.3/[root@master redis-3.2.3]# make && make install

Switch to the utils directory and run the redis Initialization Script install_server.sh (Press enter by default) as follows:

The figure above shows the master configuration location and log location of apsaradb for redis. Now we need to use systemd, so we need to create a unit file name redis_62.16.servicer under/etc/systems/system as follows:

[root@slave redis-3.2.3]# vi /etc/systemd/system/redis_6379.service[root@slave redis-3.2.3]# cat /etc/systemd/system/redis_6379.service[Unit]Description=Redis on port 6379[Service]Type=forkingExecStart=/etc/init.d/redis_6379 startExecStop=/etc/init.d/redis_6379 stop[Install]WantedBy=multi-user.target


Note: Here Type = forking is the form of background running

Start redis

[root@master utils]# systemctl daemon-reload [root@master utils]# systemctl enable redis_6379.service [root@master utils]# systemctl start redis_6379.service  [root@master utils]# systemctl status redis_6379.service 

Redis is running. The default listening port is port 6379 of 127.0.0.1 (firewall can be disabled in the test)

[root@master utils]# systemctl stop firewalld[root@master utils]# systemctl status firewalld● firewalld.service - firewalld - dynamic firewall daemon   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)   Active: inactive (dead) since Mon 2018-01-01 14:51:04 CST; 7s ago Main PID: 945 (code=exited, status=0/SUCCESS)Jan 01 14:13:22 localhost.localdomain systemd[1]: Starting firewalld - dynamic firewall daemon...Jan 01 14:13:28 localhost.localdomain systemd[1]: Started firewalld - dynamic firewall daemon.Jan 01 14:51:01 master systemd[1]: Stopping firewalld - dynamic firewall daemon...Jan 01 14:51:04 master systemd[1]: Stopped firewalld - dynamic firewall daemon.
Set the redis listening address and add the listening host

[Root @ master utils] # vim/etc/redis/6379. conf

Modify: bind 127.0.0.1 192.168.146.100 6379
Restart the redis service and check whether the listening IP address is successfully modified.

[root@master utils]# systemctl restart redis_6379.service [root@master utils]# netstat -anput | grep redistcp        0      0 192.168.146.100:6379    0.0.0.0:*               LISTEN      30049/redis-server  tcp        0      0 127.0.0.1:6379          0.0.0.0:*               LISTEN      30049/redis-server 
After the configuration is complete, start redis and perform operations.
[root@master utils]# redis-cli -h 192.168.146.100 -p 6379192.168.146.100:6379> keys *(empty list or set)192.168.146.100:6379> set name yankerpOK192.168.146.100:6379> get name"yankerp"192.168.146.100:6379> 
About the redis-cli-h 192.168.146.100-p 6379 command to connect to the redis server, the IP address is 192.168.146.100 port is 6379
Keys * is to view all key-value pairs of redis.
Set name yankerp adds a key value name with the content yankerp.
Get name: view the key value of name.

Environment Description:

Master redis-IP Address: 192.168.146.100

From redis-IP Address: 192.168.146.110

The steps for installing redis are the same as those for the redis master. Refer to the above process. Tip: (set the port to 6380 when installing redis) as follows:

Install from redis Note 1:

NOTE 2:

To install redis, you need to pay attention to the configuration above. I have installed it and started it successfully.

[root@slave utils]# netstat -anput | grep redistcp        0      0 127.0.0.1:6380          0.0.0.0:*               LISTEN      28653/redis-server
1. Master/Slave Configuration

Change port6379 from redis. conf to 6380, add the local address 192.168.146.110, and add slaveof 192.168.146.100 6379 as follows:


After successful configuration, restart the redis Service (disable the firewall from redis)

Test Data Synchronization

Primary redis:

[root@master utils]# redis-cli -h 192.168.146.100 -p 6379192.168.146.100:6379> set a 1OK192.168.146.100:6379> set b 2OK192.168.146.100:6379> set c 3OK192.168.146.100:6379> set d 4OK192.168.146.100:6379> set e 5OK192.168.146.100:6379> set F 2018OK192.168.146.100:6379> keys *1) "b"2) "e"3) "F"4) "a"5) "name"6) "c"7) "d"192.168.146.100:6379> get F"2018"
You have synchronized the data from redis.
[root@slave utils]# redis-cli -h 192.168.146.110 -p 6380192.168.146.110:6380> keys *1) "e"2) "name"3) "a"4) "c"5) "F"6) "b"7) "d"192.168.146.110:6380> get F"2018"
However, we add a key value PPP content from the redis host as follows:


The default value is read/write splitting. It can only be read from redis, but cannot be written!

Ii. Master-slave switchover

1. Stop the master redis

2. Set redis from redis to master (operate from redis server)

[root@slave utils]# redis-cli -p 6380 slaveof NO ONEOK
3. Next, test whether to switch from the redis host to the primary redis as follows:
[root@slave utils]# redis-cli -p 6380127.0.0.1:6380> set PPP yankerpOK127.0.0.1:6380> get PPP"yankerp"
Here we can see that data can be written from the redis host

4. The original primary redis is back to normal, and the switch is back.

1) Save the current master redis data (192.168.146.110)

[root@slave utils]# redis-cli -h 192.168.146.110 -p 6380192.168.146.110:6380> get PPP"yankerp"192.168.146.110:6380> saveOK
2) copy the dump. rdb file under the root directory of the current primary redis to overwrite the root directory (146.110) of the original primary redis.


3) Start the original primary redis (146.100) Operation


4) Switch (146.110) operations in the current primary redis

[root@slave ~]# redis-cli -p 6380 slaveof 192.168.146.100 6379OK
Test again (primary redis)


From redis:

Here, master-slave synchronization and master-slave switchover are completed.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.