First redis-server /usr/local/etc/redis.conf
start a redis service with a port number of 6379 (default port number)
through the command as the main database, as follows: redis-server /usr/local/etc/redis.conf --port 6380 --slaveof 127.0.0.1 6379
start a redis service with a port number of 6380 through the command , and specify the main database address at the same time, as follows:
at this time two terminals Enter the redis client mode redis-cli -p 6379
with redis-cli -p 6380
two commands respectively , and then enter info replication
to get the replication related information, as follows:
Replication information of the primary database
It can be found that the role of the master database is master, and there is only one slave database currently connected, and the ip address and port information of the slave database are also listed.
Replication information from the database
It can be found that the role of the slave database is slave, and the ip address, port, status and other information of its associated master database are also listed.
Next, set a key in the main database as follows:
Then query the key in the slave database, as follows:
You can find that the value of the key is consistent with the setting in the master database, indicating that the data has been synchronized from the master database to the slave database.
You can also try to modify the value of the key from the database, and the result is as follows: An
error was found, because the slave database is read-only by default.