# 安装redis-shake
root@redis-7:~# cd /usr/local/src/
root@redis-7:/usr/local/src# wget https://github.com/tair-opensource/RedisShake/releases/download/v4.0.0/redis-shake-linux-amd64.tar.gz
root@redis-7:/usr/local/src# tar xvf redis-shake-linux-amd64.tar.gz
./redis-shake
./shake.toml
root@redis-7:/usr/local/src# vim shake.toml
root@redis-7:~# cat /usr/local/src/shake.toml function=""# 读取数据的配置 源主机 单节点[sync_reader]
cluster =false
address ="172.18.10.147:6379"
username =""# keep empty if not using ACL
password ="q1w2e3r4ys@123"# keep empty if no authentication is required
tls =false# [scan_reader]# cluster = false# address = "127.0.0.1:6379"# username = "" # keep empty if not using ACL# password = "" # keep empty if no authentication is required# tls = false# [rdb_reader]# filepath = "/tmp/dump.rdb"# 配置写数据的主机,cluster模式 目的主机[redis_writer]
cluster =true
address ="172.18.10.141:6379"
username =""# keep empty if not using ACL
password ="q1w2e3r4ys@123"# keep empty if no authentication is required
tls =false[advanced]dir="data"
ncpu =0# runtime.GOMAXPROCS, 0 means use runtime.NumCPU() cpu cores
pprof_port =0# pprof port, 0 means disable
status_port =0# status port, 0 means disable# log
log_file ="shake.log"
log_level ="info"# debug, info or warn
log_interval =5# in seconds# redis-shake gets key and value from rdb file, and uses RESTORE command to# create the key in target redis. Redis RESTORE will return a "Target key name# is busy" error when key already exists. You can use this configuration item# to change the default behavior of restore:# panic: redis-shake will stop when meet "Target key name is busy" error.# rewrite: redis-shake will replace the key with new value.# ignore: redis-shake will skip restore the key when meet "Target key name is busy" error.
rdb_restore_command_behavior ="panic"# panic, rewrite or skip# redis-shake uses pipeline to improve sending performance.# This item limits the maximum number of commands in a pipeline.
pipeline_count_limit =1024# Client query buffers accumulate new commands. They are limited to a fixed# amount by default. This amount is normally 1gb.
target_redis_client_max_querybuf_len = 1024_000_000
# In the Redis protocol, bulk requests, that are, elements representing single# strings, are normally limited to 512 mb.
target_redis_proto_max_bulk_len = 512_000_000
# If the source is Elasticache or MemoryDB, you can set this item.
aws_psync =""# example: aws_psync = "10.0.0.1:6379@nmfu2sl5osync,10.0.0.1:6379@xhma21xfkssync"# 写入测试数据
root@redis-7:~# cat redis-client.sh #!/bin/bash#Author: ZhangJieNUM=`seq15000`foriin${NUM};do
redis-cli -h172.18.10.147 -a q1w2e3r4ys@123 set key-${i} value-${i}echo"key-${i} value-${i} 写入完成"doneecho"十万个key写入到Redis完成"
root@redis-7:~# sh redis-client.sh # 执行数据同步,此过程会持续同步
root@redis-7:/usr/local/src# ./redis-shake shake.toml
root@redis-7:/usr/local/src# ./redis-shake shake.toml 2023-09-16 00:53:57 INF load config from file: shake.toml
2023-09-16 00:53:57 INF log_level: [info], log_file: [/usr/local/src/data/shake.log]2023-09-16 00:53:57 INF changed work dir to [/usr/local/src/data]2023-09-16 00:53:57 INF GOMAXPROCS defaults to the value of runtime.NumCPU [2]2023-09-16 00:53:57 INF not set pprof port
2023-09-16 00:53:57 INF no function script
2023-09-16 00:53:57 INF create SyncStandaloneReader: 172.18.10.147:6379
2023-09-16 00:53:57 INF redisClusterWriter load cluster nodes. line=4b0d5f291f6a93c7e0b751e4321bb3738e695009 172.18.10.142:6379@16379 master - 0169479683500010 connected 0-1365 6827-10922
2023-09-16 00:53:57 INF redisClusterWriter load cluster nodes. line=fb451eed765a8af9efec665776991b24b7321f22 172.18.10.143:6379@16379 master - 0169479683600011 connected 1366-2730 12288-16383
2023-09-16 00:53:57 INF redisClusterWriter load cluster nodes. line=1aa8ac9cf8b36a403eb999aec95528c7ba2baa8d 172.18.10.141:6379@16379 myself,master - 016947968350009 connected 2731-6826 10923-12287
2023-09-16 00:53:57 INF redisClusterWriter connected to redis cluster successful. addresses=[172.18.10.142:6379 172.18.10.143:6379 172.18.10.141:6379]2023-09-16 00:53:57 INF create RedisClusterWriter: 172.18.10.141:6379
2023-09-16 00:53:57 INF not set status port
2023-09-16 00:53:57 INF start syncing...
2023-09-16 00:54:02 INF read_count=[5000], read_ops=[0.00], write_count=[5000], write_ops=[0.00], syncing aof, diff=[5000]2023-09-16 00:54:07 INF read_count=[5000], read_ops=[0.00], write_count=[5000], write_ops=[0.00], syncing aof, diff=[0]2023-09-16 00:54:17 INF read_count=[5000], read_ops=[0.00], write_count=[5000], write_ops=[0.00], syncing aof, diff=[0]# 目的端redis验证
root@redis-1:~# redis-cli -a q1w2e3r4ys@123 -h 172.18.10.141 -p 6379 --no-auth-warning dbsize(integer)1653
root@redis-1:~# redis-cli -a q1w2e3r4ys@123 -h 172.18.10.142 -p 6379 --no-auth-warning dbsize(integer)1673
root@redis-1:~# redis-cli -a q1w2e3r4ys@123 -h 172.18.10.143 -p 6379 --no-auth-warning dbsize(integer)1675
七.基于mysql和Redis,实现PHP页面的数据缓存效果
# 数据库准备
root@redis-7:~/soft/docker# tar -xf runtime-docker24.0.2-containerd1.6.21-binary-install.tar.gz
root@redis-7:~/soft/docker# bash runtime-install.sh containerd
root@redis-7:~/soft/docker# mkdir -p /data/mysql
root@redis-7:~/soft/docker# nerdctl run -d -p 3306:3306 --name mysql-container-test -v /data/mysql:/var/lib/mysql -e MYSQL_ROOT_PASSWORD="mypass@123" registry.cn-hangzhou.aliyuncs.com/zhangshijie/mysql:5.7.36
root@redis-7:~/soft/docker# apt install mysql-client-core-8.0
root@redis-7:~/soft/docker# mysql -u root -h 172.31.4.3 -pmypass@123
root@redis-7:~/soft/docker# mysql -u root -h 172.18.10.147 -pmypass@123
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 5.7.36 MySQL Community Server (GPL)
Copyright (c)2000, 2023, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h'for help. Type '\c' to clear the current input statement.
mysql>
mysql> create database test_store;
Query OK, 1 row affected (0.00 sec)
mysql> use test_store;
Database changed
mysql> create table products(product_id BIGINT PRIMARY KEY AUTO_INCREMENT,product_name VARCHAR(50),price DOUBLE) Engine = InnoDB;
Query OK, 0 rows affected (0.01 sec)
mysql> INSERT INTO products(product_name, price) VALUES ('Virtual Private Servers','5.00');
Query OK, 1 row affected (0.01 sec)
mysql> INSERT INTO products(product_name, price) VALUES ('Managed Databases', '15.00')
->;
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO products(product_name, price) VALUES ('Block Storage', '10.00');
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO products(product_name, price) VALUES ('Managed Kubernetes','60.00');
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO products(product_name, price) VALUES ('Load Balancer', '10.00');
Query OK, 1 row affected (0.00 sec)
mysql>select * from products;
+------------+-------------------------+-------+
| product_id | product_name | price |
+------------+-------------------------+-------+
|1| Virtual Private Servers |5||2| Managed Databases |15||3| Block Storage |10||4| Managed Kubernetes |60||5| Load Balancer |10|
+------------+-------------------------+-------+
5 rows inset(0.00 sec)
mysql> quit;
Bye
# web服务器准备
root@redis-7:~/soft/docker# apt-get install software-properties-common
root@redis-7:~/soft/docker# add-apt-repository ppa:ondrej/php
root@redis-7:~/soft/docker# apt-get update
root@redis-7:~/soft/docker# apt install apache2 php-fpm libapache2-mod-php php7.4-mysql php7.4-redis
root@redis-7:/var/www/html# cat redis-mysql.php <?php
$redis= new Redis();$redis->connect('172.18.10.147', 6379);$redis->auth('q1w2e3r4ys@123');$key='PRODUCTS';if(!$redis->get($key)){$source='未命中缓存,数据来源MySQL Server';$database_name='test_store';$database_user='root';$database_password='mypass@123';$mysql_host='172.17.10.147';$pdo= new PDO('mysql:host='.$mysql_host.'; dbname='.$database_name,
$database_user, $database_password);$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);$sql="SELECT * FROM products";$stmt=$pdo->prepare($sql);$stmt->execute();while($row=$stmt->fetch(PDO::FETCH_ASSOC)){$products[]=$row;}$redis->set($key, serialize($products));$redis->expire($key, 60);}else{$source='成功命中缓存,数据来源 Redis Server';$products= unserialize($redis->get($key));}echo$source.': <br>';
print_r($products);# 页面访问测试 http://172.18.10.147/redis-mysql.php
八.对Redis cluster进行集群扩容、并为新的master分配槽位
# 添加节点到集群汇总,默认为master
root@redis-1:~# redis-cli -a q1w2e3r4ys@123 --cluster add-node 172.18.10.147:6379 172.18.10.141:6379# 查看集群节点信息
root@redis-1:~# redis-cli -h 172.18.10.141 -a q1w2e3r4ys@123 cluster info
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:7
cluster_size:3
cluster_current_epoch:6
cluster_my_epoch:1
cluster_stats_messages_ping_sent:1550
cluster_stats_messages_pong_sent:1522
cluster_stats_messages_sent:3072
cluster_stats_messages_ping_received:1521
cluster_stats_messages_pong_received:1550
cluster_stats_messages_meet_received:1
cluster_stats_messages_received:3072
total_cluster_links_buffer_limit_exceeded:0
# 节点信息
root@redis-1:~# redis-cli -h 172.18.10.141 -a q1w2e3r4ys@123 cluster nodes
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
4b0d5f291f6a93c7e0b751e4321bb3738e695009 172.18.10.142:6379@16379 master - 016947946545182 connected 5461-10922
1023fa7bc9f28b262416db720b5eb1e45678f023 172.18.10.145:6379@16379 slave 1aa8ac9cf8b36a403eb999aec95528c7ba2baa8d 016947946535111 connected
f2f96b952bf55548e5523589d8041ec6122f8905 172.18.10.147:6379@16379 master - 016947946514900 connected
fde4479035b00ee149e64a6aff93c3fbfb4e975a 172.18.10.146:6379@16379 slave 4b0d5f291f6a93c7e0b751e4321bb3738e695009 016947946504762 connected
fb451eed765a8af9efec665776991b24b7321f22 172.18.10.143:6379@16379 master - 016947946510003 connected 10923-16383
1aa8ac9cf8b36a403eb999aec95528c7ba2baa8d 172.18.10.141:6379@16379 myself,master - 016947946520001 connected 0-5460
bdd580521db5369ad85791e4e5b8568c8baa887d 172.18.10.144:6379@16379 slave fb451eed765a8af9efec665776991b24b7321f22 016947946530003 connected
# 添加节点172.18.10.148:6379 并设置为172.18.10.147:6379的slave
root@redis-1:~# redis-cli -a q1w2e3r4ys@123 --cluster add-node 172.18.10.148:6379 172.18.10.141:6379 --cluster-slave --cluster-master-id f2f96b952bf55548e5523589d8041ec6122f8905# 查看集群节点
root@redis-1:~# redis-cli -h 172.18.10.141 -a q1w2e3r4ys@123 cluster nodes
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
4b0d5f291f6a93c7e0b751e4321bb3738e695009 172.18.10.142:6379@16379 master - 016947951203322 connected 5461-10922
1023fa7bc9f28b262416db720b5eb1e45678f023 172.18.10.145:6379@16379 slave 1aa8ac9cf8b36a403eb999aec95528c7ba2baa8d 016947951200001 connected
f2f96b952bf55548e5523589d8041ec6122f8905 172.18.10.147:6379@16379 master - 016947951223520 connected
607a1c0cf8e469be07df402aac9eb31f525b348f 172.18.10.148:6379@16379 slave f2f96b952bf55548e5523589d8041ec6122f8905 016947951213430 connected
fde4479035b00ee149e64a6aff93c3fbfb4e975a 172.18.10.146:6379@16379 slave 4b0d5f291f6a93c7e0b751e4321bb3738e695009 016947951200002 connected
fb451eed765a8af9efec665776991b24b7321f22 172.18.10.143:6379@16379 master - 016947951210003 connected 10923-16383
1aa8ac9cf8b36a403eb999aec95528c7ba2baa8d 172.18.10.141:6379@16379 myself,master - 016947951190001 connected 0-5460
bdd580521db5369ad85791e4e5b8568c8baa887d 172.18.10.144:6379@16379 slave fb451eed765a8af9efec665776991b24b7321f22 016947951200003 connected
# 重新分配槽位
root@redis-1:~# redis-cli -a q1w2e3r4ys@123 --cluster reshard 172.18.10.141:6379
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Performing Cluster Check (using node172.18.10.141:6379)
M: 1aa8ac9cf8b36a403eb999aec95528c7ba2baa8d 172.18.10.141:6379
slots:[0-5460](5461 slots) master
1 additional replica(s)
M: 4b0d5f291f6a93c7e0b751e4321bb3738e695009 172.18.10.142:6379
slots:[5461-10922](5462 slots) master
1 additional replica(s)
S: 1023fa7bc9f28b262416db720b5eb1e45678f023 172.18.10.145:6379
slots: (0 slots) slave
replicates 1aa8ac9cf8b36a403eb999aec95528c7ba2baa8d
M: f2f96b952bf55548e5523589d8041ec6122f8905 172.18.10.147:6379
slots: (0 slots) master
1 additional replica(s)
S: 607a1c0cf8e469be07df402aac9eb31f525b348f 172.18.10.148:6379
slots: (0 slots) slave
replicates f2f96b952bf55548e5523589d8041ec6122f8905
S: fde4479035b00ee149e64a6aff93c3fbfb4e975a 172.18.10.146:6379
slots: (0 slots) slave
replicates 4b0d5f291f6a93c7e0b751e4321bb3738e695009
M: fb451eed765a8af9efec665776991b24b7321f22 172.18.10.143:6379
slots:[10923-16383](5461 slots) master
1 additional replica(s)
S: bdd580521db5369ad85791e4e5b8568c8baa887d 172.18.10.144:6379
slots: (0 slots) slave
replicates fb451eed765a8af9efec665776991b24b7321f22
[OK] All nodes agree about slots configuration.
>>> Check foropen slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
#How many slots do you want to move (from 1 to 16384)? 4096 要分配的槽位数量#What is the receiving node ID? f2f96b952bf55548e5523589d8041ec6122f8905 接收槽位的节点
Please enter all the sourcenode IDs.
Type 'all' to use all the nodes as source nodes for the hash slots.
Type 'done' once you entered all the source nodes IDs.
Source node#1: all # 从那些源服务器进行分配,all是自动再所有的redis node上划分,如果redie cluster删除主机可以使用此方式将主机上的槽位全部移动到别的redis主机上
Ready to move 4096 slots.
Source nodes:
M: 1aa8ac9cf8b36a403eb999aec95528c7ba2baa8d 172.18.10.141:6379
slots:[0-5460](5461 slots) master
1 additional replica(s)
M: 4b0d5f291f6a93c7e0b751e4321bb3738e695009 172.18.10.142:6379
slots:[5461-10922](5462 slots) master
1 additional replica(s)
M: fb451eed765a8af9efec665776991b24b7321f22 172.18.10.143:6379
slots:[10923-16383](5461 slots) master
1 additional replica(s)
Destination node:
M: f2f96b952bf55548e5523589d8041ec6122f8905 172.18.10.147:6379
slots: (0 slots) master
1 additional replica(s)
Resharding plan:
Moving slot 5461 from 4b0d5f291f6a93c7e0b751e4321bb3738e695009
Moving slot 5462 from 4b0d5f291f6a93c7e0b751e4321bb3738e695009
Moving slot 5463 from 4b0d5f291f6a93c7e0b751e4321bb3738e695009
Moving slot 5464 from 4b0d5f291f6a93c7e0b751e4321bb3738e695009
Moving slot 5465 from 4b0d5f291f6a93c7e0b751e4321bb3738e695009
Moving slot 5466 from 4b0d5f291f6a93c7e0b751e4321bb3738e695009
Moving slot 5467 from 4b0d5f291f6a93c7e0b751e4321bb3738e695009
Moving slot 5468 from 4b0d5f291f6a93c7e0b751e4321bb3738e695009
...
Moving slot 12287 from fb451eed765a8af9efec665776991b24b7321f22
Do you want to proceed with the proposed reshard plan (yes/no)? yes
Moving slot 5461 from 172.18.10.142:6379 to 172.18.10.147:6379:
Moving slot 5462 from 172.18.10.142:6379 to 172.18.10.147:6379:
Moving slot 5463 from 172.18.10.142:6379 to 172.18.10.147:6379:
Moving slot 5464 from 172.18.10.142:6379 to 172.18.10.147:6379:
Moving slot 5465 from 172.18.10.142:6379 to 172.18.10.147:6379:
Moving slot 5466 from 172.18.10.142:6379 to 172.18.10.147:6379:
...
# 验证集群槽位状态
root@redis-1:~# redis-cli -a q1w2e3r4ys@123 --cluster info 172.18.10.141:6379
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
172.18.10.141:6379 (1aa8ac9c...) ->0 keys |4096 slots |1 slaves.
172.18.10.142:6379 (4b0d5f29...) ->0 keys |4096 slots |1 slaves.
172.18.10.147:6379 (f2f96b95...) ->1 keys |4096 slots |1 slaves.
172.18.10.143:6379 (fb451eed...) ->0 keys |4096 slots |1 slaves.
[OK]1 keys in4 masters.
0.00 keys per slot on average.
九.对Redis cluster进行集群缩容、并提前迁移走之前的数据
# 迁移槽位
root@redis-1:~# redis-cli -a q1w2e3r4ys@123 --cluster reshard 172.18.10.141:6379
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Performing Cluster Check (using node172.18.10.141:6379)
M: 1aa8ac9cf8b36a403eb999aec95528c7ba2baa8d 172.18.10.141:6379
slots:[1365-5460](4096 slots) master
1 additional replica(s)
M: 4b0d5f291f6a93c7e0b751e4321bb3738e695009 172.18.10.142:6379
slots:[6827-10922](4096 slots) master
1 additional replica(s)
S: 1023fa7bc9f28b262416db720b5eb1e45678f023 172.18.10.145:6379
slots: (0 slots) slave
replicates 1aa8ac9cf8b36a403eb999aec95528c7ba2baa8d
M: f2f96b952bf55548e5523589d8041ec6122f8905 172.18.10.147:6379
slots:[0-1364],[5461-6826],[10923-12287](4096 slots) master
1 additional replica(s)
S: 607a1c0cf8e469be07df402aac9eb31f525b348f 172.18.10.148:6379
slots: (0 slots) slave
replicates f2f96b952bf55548e5523589d8041ec6122f8905
S: fde4479035b00ee149e64a6aff93c3fbfb4e975a 172.18.10.146:6379
slots: (0 slots) slave
replicates 4b0d5f291f6a93c7e0b751e4321bb3738e695009
M: fb451eed765a8af9efec665776991b24b7321f22 172.18.10.143:6379
slots:[12288-16383](4096 slots) master
1 additional replica(s)
S: bdd580521db5369ad85791e4e5b8568c8baa887d 172.18.10.144:6379
slots: (0 slots) slave
replicates fb451eed765a8af9efec665776991b24b7321f22
[OK] All nodes agree about slots configuration.
>>> Check foropen slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
How many slots do you want to move (from 1 to 16384)? 4096# 接收的节点,先迁移到172.18.10.141上
What is the receiving node ID? 1aa8ac9cf8b36a403eb999aec95528c7ba2baa8d
Please enter all the sourcenode IDs.
Type 'all' to use all the nodes as source nodes for the hash slots.
Type 'done' once you entered all the source nodes IDs.
Source node#1: f2f96b952bf55548e5523589d8041ec6122f8905
Source node#2: done# done 完成# 检查槽位
root@redis-1:~# redis-cli -a q1w2e3r4ys@123 --cluster info 172.18.10.141:6379
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
172.18.10.141:6379 (1aa8ac9c...) ->1 keys |8192 slots |3 slaves.
172.18.10.142:6379 (4b0d5f29...) ->0 keys |4096 slots |1 slaves.
172.18.10.143:6379 (fb451eed...) ->0 keys |4096 slots |1 slaves.
[OK]1 keys in3 masters.
0.00 keys per slot on average.
# 从集群中删除指定节点
root@redis-1:~# redis-cli -a q1w2e3r4ys@123 --cluster del-node 172.18.10.141:6379 f2f96b952bf55548e5523589d8041ec6122f8905
root@redis-1:~# redis-cli -a q1w2e3r4ys@123 --cluster del-node 172.18.10.141:6379 607a1c0cf8e469be07df402aac9eb31f525b348f# 重新平衡槽位
root@redis-1:~# redis-cli -a q1w2e3r4ys@123 --no-auth-warning --cluster rebalance 172.18.10.141:6379>>> Performing Cluster Check (using node172.18.10.141:6379)[OK] All nodes agree about slots configuration.
>>> Check foropen slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
>>> Rebalancing across 3 nodes. Total weight =3.00
Moving 1366 slots from 172.18.10.141:6379 to 172.18.10.142:6379
######################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################
Moving 1365 slots from 172.18.10.141:6379 to 172.18.10.143:6379
###################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################### 查看槽位分配
root@redis-1:~# redis-cli -a q1w2e3r4ys@123 --cluster info 172.18.10.141:6379
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
172.18.10.141:6379 (1aa8ac9c...) ->0 keys |5461 slots |1 slaves.
172.18.10.142:6379 (4b0d5f29...) ->1 keys |5462 slots |1 slaves.
172.18.10.143:6379 (fb451eed...) ->0 keys |5461 slots |1 slaves.
[OK]1 keys in3 masters.
0.00 keys per slot on average.