migrate
MIGRATE
Apply for Redis Developer Course
|
Redis Regular Inspection / Technical Support
|
Redis Enterprise Server
|
|---|
Move data to another Redis Server
Usage is migrate host port key destination-db.
Moves the key and data to the specified Redis Server (instance).
If the key already exists on the target server, an error is raised and it is not moved.
Example
| Command> | set key value |
| Result> | OK |
| Command> | migrate 192.168.56.102 6379 key 0 1000 |
| Result> | OK |
| Command> | exists key |
| Result> | 0 |
| Command> | set key XXXXX |
| Result> | OK |
| Command> | migrate 192.168.56.102 6379 key 0 1000 |
| Result> | (error) ERR Target instance replied with error: BUSYKEY Target key name already exists. |
| Command> | get key Connect to the target Redis Server and check |
| Result> | value |
| This command internally runs dump, restore, and del. Without the copy and replace options, the source server can be 3.0 and the target server 2.8. To use those two options, both source and target servers must be 3.0 or later. The copy and replace options are available from 3.0. |
Using copy and replace options
Usage is migrate host port key destination-db copy replace.
copy does not delete the key on the source server.
replace overwrites the key if it exists on the target server.
You can use copy and replace together
Example
| Command> | migrate 192.168.56.102 6379 key 0 1000 copy |
| Result> | OK |
| Command> | exists key |
| Result> | 0 |
| Command> | set key YYYYY |
| Result> | OK |
| Command> | migrate 192.168.56.102 6379 key 0 1000 replace |
| Result> | OK |
| Command> | get key Connect to the target Redis Server and check |
| Result> | YYYYY |
AUTH, AUTH2 options
Use AUTH when the requirepass parameter is set (from 4.0.6), and AUTH2 when using ACL (from version 6).
- Move one key: MIGRATE host port key dbid timeout [COPY | REPLACE | AUTH password | AUTH2 username password]
- Move multiple keys: MIGRATE host port "" dbid timeout [COPY | REPLACE | AUTH password | AUTH2 username password] KEYS key1 key2 ... keyN
Move multiple keys
From version 3.0.6 you can migrate multiple keys.
migrate 192.168.56.102 6379 "" 0 1000 KEYS key1 key2 key3
If none of the specified keys exist, 'NOKEY' is returned,
and if some keys are missing it does not raise an error and returns 'OK'.
Example
| Command> | migrate 192.168.56.102 6379 "" 0 1000 KEYS key1 key2 key3 |
| Result> | OK |
|
When using multiple keys, if expire is set on a migrating key,
a bug may set expire on another key.
This bug was fixed in 3.2.7. See here for more details. |
Moving keys in a cluster
In a cluster, to move keys (data) to another master, first announce the slot move.
Run CLUSTER SETSLOT IMPORTING/MIGRATING, then move data with MIGRATE,
and finish with CLUSTER SETSLOT NODE.
redis-cli or redis-trib.rb uses this migrate command when resharding keys.
See CLUSTER SETSLOT here
and cluster design for a real migration example.
Syntax
MIGRATE host port key|"" destination-db timeout [COPY] [REPLACE] [AUTH|AUTH2] [KEYS key [key ...]]
- Available since version 2.6.0.
- Time complexity: on the source server dump + del run internally, and restore runs on the target server.
- The copy and replace options are available from 3.0.
- From 3.0.6 the multi-key KEYS option is available.
- AUTH: available from 4.0.6.
- AUTH2: available from 6.0.
- From 6.0, Event Notification is generated -> 'del' (when COPY is not used)
| Related commands | DUMP, RESTORE |
| Clients for Java | Jedis, Lettuce, Redisson | Clients for C | Hiredis |
| << RESTORE | MIGRATE | TOUCH >> |
|---|
