Redis master-slave configuration and switching

Source: Internet
Author: User
Tags file copy

Recently, the company's projects have been used. But there is a point that is not clear.

is requirepass only for client authentication, or is it also for Redis certification? (Masterauth)

The general idea is that the primary server does not open aof, and the AOF is opened from the server in order to have higher primary server performance.

The relevant documents on the Web are as follows:

Environment Description:

Master redis:192.168.10.1 6379

From redis:192.168.10.2 6380

First, master-slave configuration

1redis config file redis.conf AEMONIZE NO  yes

2. Change the port 6379 from redis profile redis.conf to 6380 , add slaveof 192.168.10.1 6379

If the master Redis has a password masterauth password

3. Start-up and subordinate service

Master redis:

[Email protected] redis-2.8.3]# src/redis-server/soft/redis-2.8.3-master/redis-2.8.3/redis.conf

From redis:

[Email protected] redis-2.8.3]# src/redis-server/soft/redis-2.8.3-slave/redis-2.8.3/redis.conf

4. Test Data Synchronization

Master redis:

[Email protected] redis-2.8.3]# src/redis-cli-p 6379

127.0.0.1:6379> set name ABC

Ok

127.0.0.1:6379> Get Name

"ABC"

127.0.0.1:6379>

From redis:

[Email protected] redis-2.8.3]# src/redis-cli-p 6380

127.0.0.1:6380> Get Name

"ABC"

127.0.0.1:6380>

5, the default is read and write separation

In from redis:

[Email protected] redis-2.8.3]# src/redis-cli-p 6380

127.0.0.1:6380> Set name 123

(Error) READONLY you can ' t write against a read only slave.

Second, master-slave switching

1. Stop Master Redis

[Email protected] redis-2.8.3]# src/redis-cli-n 6379 shutdown

[Email protected] redis-2.8.3]# src/redis-cli-p 6379

Could not connect to Redis at 127.0.0.1:6379:connection refused

Not connected>

2. Set the master Redis from Redis

[Email protected] redis-2.8.3]# src/redis-cli-p 6380 slaveof NO One

Ok

3. Test Whether or not the Redis switch from the master Redis

[Email protected] redis-2.8.3]# src/redis-cli-p 6380

127.0.0.1:6380> Set name 123

Ok

127.0.0.1:6380> Get Name

"123"

127.0.0.1:6380>

4, the original master Redis back to normal, to re-switch back

1) Save the current master redis data

[Email protected] redis-2.8.3]# src/redis-cli-p 6380

127.0.0.1:6380> Get Name

"ABC"

127.0.0.1:6380> Set name 123

Ok

127.0.0.1:6380> Get Name

"123"

127.0.0.1:6380> Save

Ok

127.0.0.1:6380> Get Name

"123"

127.0.0.1:6380>

2) Overwrite the dump.rdb file copy ofthe current master redis root directory with the original master redis root directory

3) Start the original master Redis

[Email protected] redis-2.8.3]# src/redis-server/soft/redis-2.8.3-master/redis-2.8.3/redis.conf

4) switch in the current master Redis

[Email protected] redis-2.8.3]# src/redis-cli-p 6380 slaveof 192.168.10.1 6379

Ok

~~~~~~~~~~~~~~~~~~~~~~~~~~~

I. how do I initialize a redis password?

A total of 2 steps:

A. There is a parameter in the configuration file: Requirepass This is the parameter that configures the Redis access password.

Like Requirepass test123.

B. The parameters in the configuration file need to be restarted to restart Redis.

Ii. do not restart Redis How do I configure a password?

A. Configure the Requirepass password in the configuration file (the password is still valid when Redis restarts).

# Requirepass Foobared
As modified to:

Requirepass test123

B. Enter Redis redefine parameters

To view the current password:

[Email protected] redis-2.4.16]#/src/redis-cli-p 6379
Redis 127.0.0.1:6379>
Redis 127.0.0.1:6379> config Get requirepass
1) "Requirepass"
2) (nil)

The display password is empty,

Then set the password:

Redis 127.0.0.1:6379> Config set Requirepass test123
Ok

Check password again:

Redis 127.0.0.1:6379> config Get requirepass
(Error) ERR Operation not permitted

This is an error!

Now you just need the password authentication.

Redis 127.0.0.1:6379> Auth test123
Ok

Check password again:

Redis 127.0.0.1:6379> config Get requirepass
1) "Requirepass"
2) "Test123"

The password has been modified.

When the Redis is ready to restart, the password will automatically take effect because the configuration parameters have been modified.

If the configuration parameter does not add a password then the Redis restart password will be equivalent to no settings.

Three. how do I log in to a redis with a password?

A. Enter the password when you log in

[Email protected] redis-2.4.16]#/src/redis-cli-p 6379-a test123
Redis 127.0.0.1:6379>
Redis 127.0.0.1:6379> config Get requirepass
1) "Requirepass"
2) "Test123"

B. Log in again to verify:

[Email protected] redis-2.4.16]#/src/redis-cli-p 6379
Redis 127.0.0.1:6379>
Redis 127.0.0.1:6379> Auth test123
Ok
Redis 127.0.0.1:6379> config Get requirepass
1) "Requirepass"
2) "Test123"
Redis 127.0.0.1:6379>

Four. master has password, slave How to configure?

When Master has a password, the corresponding password parameters should be configured appropriately when the slave is configured. Otherwise, slave is not able to replicate normally.

The corresponding parameters are:

#masterauth

Like what:

Masterauth Mstpassword

Redis master-slave configuration and switching

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.