redis伪集群脚本
#/bin/bash #set -e #安装redis伪集群脚本,先把redis-5.0.4.tar.gz redis-3.2.2.gem及启动脚本放在/data1/redis-cluster目录下,然后执行该脚本即可 #!/bin/bash set -e #获取redis本机ip ip=`hostname -I` workspace=/data1/redis-cluster #根据需求自定义 想在哪个目录安装就写哪个目录 if [ ! -d $workspace ];then mkdir -p $workspace fi #下载并解压安装redis cd $workspace wget http://download.redis.io/releases/redis-5.0.4.tar.gz tar zxvf redis-5.0.4.tar.gz -C $workspace cd $workspace/redis-5.0.4 make && make install PREFIX=$workspace #配置redis cd $workspace mv bin redis-01 cp $workspace/redis-5.0.4/redis.conf redis-01 sed -i "s/^bind 127.0.0.1/bind $ip/" redis-01/redis.conf sed -i 's/6379/7001/g' redis-01/redis.conf sed -i 's/daemonize no/daemonize yes/' redis-01/redis.conf sed -i 's/appendonly no/appendonly yes/' redis-01/redis.conf sed -i 's/^# cluster-enabled/cluster-enabled/' redis-01/redis.conf sed -i 's/^# cluster-config-file/cluster-config-file/' redis-01/redis.conf sed -i 's/^# cluster-node-timeout/cluster-node-timeout/' redis-01/redis.conf sed -i 's/appendonly yes/appendonly no/' redis-01/redis.conf cp -r redis-01 redis-02 cp -r redis-01 redis-03 cp -r redis-01 redis-04 cp -r redis-01 redis-05 cp -r redis-01 redis-06 sed -i 's/7001/7002/g' redis-02/redis.conf sed -i 's/7001/7003/g' redis-03/redis.conf sed -i 's/7001/7004/g' redis-04/redis.conf sed -i 's/7001/7005/g' redis-05/redis.conf sed -i 's/7001/7006/g' redis-06/redis.conf #启动脚本 echo "#/bin/bash cd $workspace/redis-01 ./redis-server redis.conf cd $workspace/redis-02 ./redis-server redis.conf cd $workspace/redis-03 ./redis-server redis.conf cd $workspace/redis-04 ./redis-server redis.conf cd $workspace/redis-05 ./redis-server redis.conf cd $workspace/redis-06 ./redis-server redis.conf " >$workspace/all-start.sh #停止脚本 echo "#/bin/bash redis-cli -h $ip -p 7001 shutdown redis-cli -h $ip -p 7002 shutdown redis-cli -h $ip -p 7003 shutdown redis-cli -h $ip -p 7004 shutdown redis-cli -h $ip -p 7005 shutdown redis-cli -h $ip -p 7006 shutdown ">$workspace/all-stop.sh #自启动脚本 cat > /etc/init.d/redis <<EOF #!/bin/sh # # chkconfig: 2345 70 20 # description: Redis-cluster autostart . /etc/init.d/functions case "$1" in start) /bin/bash /data1/redis-cluster/all-start.sh ;; stop) /bin/bash /data1/redis-cluster/all-stop.sh ;; *) echo "Usage: $0 (start|stop)" ;; esac EOF #启动redis chmod 741 $workspace/*.sh $workspace/all-start.sh sleep 3 cp $workspace/redis-01/redis-cli /usr/bin/redis-cli yum install -y expect #配置集群 expect -c " spawn redis-cli --cluster create $ip:7001 $ip:7002 $ip:7003 $ip:7004 $ip:7005 $ip:7006 --cluster-replicas 1; expect "configuration" { send \"yes\r\"; } expect eof " echo -e "\nredis-cluster is ok " chkconfig redis on
启动脚本
单实例设置自启动脚本
1、复制redis启动脚本
redis启动脚本一般在redis根目录的utils,如果不知道路径,可以先查看路径
[root@slj-redis data1]# find / -name redis_init_script
/data1/redis-cluster/redis-4.0.9/utils/redis_init_script
复制启动脚本到/etc/init.d/redis文件中
cp /data1/redis-cluster/redis-4.0.9/utils/redis_init_script /etc/init.d/redis-alone
2、更改redis-alone脚本
首先添加如下俩行
# chkconfig: 2345 90 10
# description: Redis is a persistent key-value database
其次更改EXEC CLIEXEC CONF 为实际所安装目录即可
REDISPORT=6379
EXEC=/data1/redis-alone/bin/redis-server
CLIEXEC=/data1/redis-alone/bin/redis-cli
PIDFILE=/var/run/redis_${REDISPORT}.pid
CONF="/data1/redis-alone/redis.conf"
chkconfig redis-alone on
centos7
[Unit] Description=Redis After=network.target remote-fs.target nss-lookup.target [Service] Type=forking ExecStart=/usr/local/redis/src/redis-server /usr/local/redis/redis.conf ExecReload=/bin/kill -s HUP $MAINPID ExecStop=/usr/bin/redis-cli -h 127.0.0.1 -p 6379 shutdown [Install] WantedBy=multi-user.target
集群设置自启动
先写好集群启动脚本
[root@slj-redis redis-cluster]# cat all-start.sh
cd /data1/redis-cluster/redis-01 ./redis-server redis.conf cd /data1/redis-cluster/redis-02 ./redis-server redis.conf cd /data1/redis-cluster/redis-03 ./redis-server redis.conf cd /data1/redis-cluster/redis-04 ./redis-server redis.conf cd /data1/redis-cluster/redis-05 ./redis-server redis.conf cd /data1/redis-cluster/redis-06 ./redis-server redis.conf
关闭脚本
[root@slj-redis redis-cluster]# vim all-stop.sh
redis-cli -h 10.253.0.1 -p 7001 shutdown redis-cli -h 10.253.0.1 -p 7002 shutdown redis-cli -h 10.253.0.1 -p 7003 shutdown redis-cli -h 10.253.0.1 -p 7004 shutdown redis-cli -h 10.253.0.1 -p 7005 shutdown redis-cli -h 10.253.0.1 -p 7006 shutdown
[root@slj-redis init.d]# vim redis-cluster
#!/bin/sh # # chkconfig: 2345 70 20 # description: Redis-cluster autostart . /etc/init.d/functions case "$1" in start) /bin/bash /data1/redis-cluster/all-start.sh ;; stop) /bin/bash /data1/redis-cluster/all-stop.sh ;; *) echo "Usage: $0 (start|stop)" ;; esac chkconfig redis-cluster on
批量删除key
redis-cli -h <host> -a <password> keys "<key>" | xargs redis-cli -h <host> -a <password> del