介绍 (Introduction)
Redis is an in-memory, key-value data store known for its flexibility, performance, wide language support, and built-in features like replication. Replication is the practice of regularly copying data from one database to another in order to have a replica that always remains an exact duplicate of the primary instance. One common use of Redis replication is to migrate an existing Redis data store to a new server, as one might do when scaling up their infrastructure for better performance.
Redis是一个内存中的键值数据存储,以其灵活性,性能,广泛的语言支持以及诸如复制之类的内置功能而闻名。 复制是一种定期将数据从一个数据库复制到另一个数据库,以使副本始终始终与主实例完全相同的做法。 Redis复制的一种常见用法是将现有Redis数据存储迁移到新服务器,就像在扩展其基础架构以提高性能时可能所做的那样。
This tutorial outlines the process of using Redis’s built-in replication features to migrate data from one Ubuntu 18.04 server (the “source”) to another (the “target”). This involves making a few configuration changes to each server, setting the target server to function as a replica of the source, and then promoting the replica back to a primary after the migration is completed.
本教程概述了使用Redis的内置复制功能将数据从一台Ubuntu 18.04服务器(“源”)迁移到另一台(“目标”)的过程。 这涉及对每个服务器进行一些配置更改,将目标服务器设置为充当源服务器的副本,然后在迁移完成后将副本提升回主服务器。
先决条件 (Prerequisites)
To complete this tutorial, you will need:
要完成本教程,您将需要:
Two servers running Ubuntu 18.04. Each server should have a user configured with administrative privileges and a firewall set up with
ufw
. To set up this environment, follow our initial server setup guide for Ubuntu 18.04 for both servers.两台运行Ubuntu 18.04的服务器。 每个服务器应具有配置了管理权限的用户和使用
ufw
设置的防火墙。 要设置该环境,请针对两个服务器都遵循我们针对Ubuntu 18.04的初始服务器设置指南 。The latest version of Redis installed on each server. To set this up, follow our guide on How To Install Redis from Source on Ubuntu 18.04.
每台服务器上都安装了最新版本的Redis。 要进行此设置,请遵循有关如何在Ubuntu 18.04上从源安装Redis的指南。
步骤1 —(可选)使用示例数据加载源Redis实例 (Step 1 — (Optional) Loading Your Source Redis Instance with Sample Data)
This optional step involves loading your source Redis instance with some sample data so you can experiment with migrating data to your target instance. If you already have data that you want to migrate over to your target, you can move ahead to Step 2 which will go over how to back it up.
此可选步骤涉及使用一些示例数据加载源Redis实例,以便您可以尝试将数据迁移到目标实例。 如果已经有要迁移到目标的数据,则可以继续执行步骤2 ,该步骤将介绍如何备份数据。
To begin, connect to the Ubuntu server you’ll use as your source Redis instance as your non-root user:
首先,以非root用户身份连接到将用作源Redis实例的Ubuntu服务器:
ssh sammy@source_server_ip
ssh sammy @ source_server_ip
Then run the following command to access your Redis server:
然后运行以下命令来访问您的Redis服务器:
- redis-cli redis-cli
If you’ve configured your Redis server to require password authentication, run the auth
command followed by your Redis password:
如果已将Redis服务器配置为要求密码认证,请运行auth
命令,然后输入Redis密码:
auth source_redis_password
验证source_redis_password
Next, run the following commands. These will create a number of keys holding a few strings, a hash, a list, and a set:
接下来,运行以下命令。 这些将创建一些包含几个字符串,哈希,列表和集合的键:
- mset string1 "Redis" string2 "is" string3 "fun!" mset string1“ Redis” string2“是” string3“有趣!
- hmset hash1 field1 "Redis" field2 "is" field3 "fast!" hmset hash1 field1“ Redis” field2“ is” field3“ fast!”
- rpush list1 "Redis" "is" "feature-rich!" rpush list1“ Redis”“是”“功能丰富!”
- sadd set1 "Redis" "is" "free!"