文章目录
安装之前,最好把防火墙关闭,防止因为防火墙导致各个ip之间通讯受阻;
本次实验是采用脚本源码包安装redis;
1,新建redis集群
ps -ef|grep redis
pkill redis
cd /usr/local/
rm -rf redis
cd src/
rm -rf redis-4.0.9
tar -zxvf redis-4.0.9-v1.tar.gz
cd redis-4.0.9/
sh install.sh (需要root用户执行)
1.1,install.sh内容
[weblogic@its-redis01 redis-4.0.9]$ cat install.sh
#!/bin/bash
read -p "1.alone
2.cluster
choice cluster or alone:" num
#create user weblogic
user_flag=`cat /etc/passwd |grep '^weblogic:' -c`
user_name='weblogic'
if [ $user_flag -eq 0 ];then
useradd -d /home/weblogic $user_name
echo "weblogic" | passwd $user_name --stdin
fi
vm_flag=`cat /etc/sysctl.conf |grep 'vm.overcommit_memory=1' -c`
if [ $vm_flag -eq 0 ];then
echo "vm.overcommit_memory=1" >>/etc/sysctl.conf
sysctl -p >/dev/null
fi
cp -r /usr/local/src/redis-4.0.9 /usr/local/redis
#input ip port
read -p "input localhost IP:" ip
read -p "input redis port:" port
if [ $num -eq 1 ];then
sed -i "s/bind 127.0.0.1/bind $ip/" /usr/local/redis/redis-alone.conf
sed -i "s/port 6379/port $port/" /usr/local/redis/redis-alone.conf
chown -R weblogic:weblogic /usr/local/redis
su - weblogic -c "cd /usr/local/redis && sh start.sh"
if [ $? -eq 0 ];then
echo "start redis alone done!"
else
echo "start redis alone fail!"
fi
elif [ $num -eq 2 ];then
mkdir -p /usr/local/redis/cluster/$port
file=/usr/local/redis/cluster/$port/redis-cluster.conf
cp /usr/local/redis/cluster/redis-cluster.conf $file
cp /usr/local/redis/cluster/start.sh /usr/local/redis/cluster/$port
sed -i "s/bind 127.0.0.1/bind $ip/" $file
sed -i "s/port 6379/port $port/" $file
cd /usr/local/redis/cluster/$port
chown -R weblogic:weblogic /usr/local/redis
su - weblogic -c "cd /usr/local/redis/cluster/$port && sh start.sh"
if [ $? -eq 0 ];then
echo "start redis cluster done!"
else
echo "start redis cluster fail!"
fi
fi
1.2,start.sh内容
[weblogic@its-redis01 redis-4.0.9]$ cd /usr/local/redis/cluster/16001
[weblogic@its-redis01 16001]$ cat start.sh
#!/bin/bash
../../src/redis-server redis-cluster.conf
1.3,redis-cluster.conf内容
[weblogic@its-redis01 16001]$ cat redis-cluster.conf | egrep -v "^#|^$"
bind 10.27.31.25
protected-mode yes
port 16001
tcp-backlog 511
timeout 3600
tcp-keepalive 300
daemonize yes
supervised no
pidfile ./redis.pid
loglevel notice
logfile ./redis.log
databases 16
always-show-logo yes
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir ./
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100
maxmemory 8gb
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
slave-lazy-flush no
appendonly no
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble no
lua-time-limit 5000
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
cluster-require-full-coverage no
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes
masterauth "666666"
requirepass "666666"
maxmemory 8gb
2,添加集群节点(在1192.168.0.2执行即可)
cd /usr/local/redis/src/
./redis-cli -c -h 192.168.0.2 -p 16010 -a test123 进入集群命令行(IP端口对应创建的)
CLUSTER MEET 192.168.0.3 16010
CLUSTER MEET 192.168.0.4 16010
补充:若需要添加从节点,得先添加集群节点,然后登录从节点,执行CLUSTER REPLICATE 主节点node_id
3,分配slot(在192.168.0.2执行即可)
cd /usr/local/redis/src/
for i in {0…5460}; do ./redis-cli -c -h 192.168.0.2 -p 16010 -a test123 CLUSTER ADDSLOTS $i > /dev/null;done
for i in {5461…10922}; do ./redis-cli -c -h 192.168.0.3 -p 16010 -a test123 CLUSTER ADDSLOTS $i > /dev/null;done
for i in {10923…16383}; do ./redis-cli -c -h 192.168.0.4 -p 16010 -a test123 CLUSTER ADDSLOTS $i > /dev/null;done
4,redis常用集群命令
4.1 redis常用命令1
登录redis:
cd /usr/local/redis/src && ./redis-cli -c -h 10.104.120.56 -p 16001 -a 666666
授权:
auth 666666
查看它的主从节点:
cluster nodes
查看状态:
cluster info
日志查看:/usr/local/redis/cluster/16001下的 redis.log
关闭redis:
pkill redis
启动redis:
cd /usr/local/redis/cluster/16001 && sh start.sh
master redis清理缓存:
/usr/local/redis/src/redis-cli -c -h 10.104.120.59 -p 16001 -a 666666
flushall
查看所有的key:
keys *
查看key被分配的槽号(slot号):
cluster keyslot "CWgetNameByValueDM_GY_SWJG13502000000"
redis 版本查询:
/usr/local/redis/src/redis-server --version
或者 /usr/local/redis/src/redis-server -v
或者 /usr/local/redis/src/redis-cli --version
或者 /usr/local/redis/src/redis-cli -v
查看集群状态:
cluster info
查看集群节点:
cluster nodes
新增节点加入到集群,默认主节点
cluster meet 127.0.0.1 7000
将当前节点设置为node_id的从节点:
CLUSTER REPLICATE <node_id>
将节点配置保存到磁盘:
cluster saveconfig
从集群中移除指定节点,其他节点还是可以看到,只是不会存数据了:
CLUSTER FORGET <node_id>
分配槽(slot):
for i in {10923..16383};do redis-cli -c -h 127.0.0.1 -p 7000 CLUSTER ADDSLOTS $i>/dev/null;done
Redis命令参考:http://redisdoc.com/persistence/index.html
4.2 对Redis库操作的命令
参考链接:https://blog.csdn.net/qq_45704528/article/details/119706205
Redis默认有16个库,类似数组下标从0开始,初始默认使用0号库
- Select 数据库号 切换数据库
- dbsize 查看当前数据库key的个数
- flushdb 清空当前库
- flushall 清空所有库
4.3 对key的常用操作命令
- keys * 查看所有key
- exists key的名字 判断是否存在这个key
- move key db 将key移动到第db个数据库
- expire key 秒钟 为key设置过期时间
- ttl kye 查看还有多少秒过期 -1表示永不过期 -2表示已经过期