#安装redis伪集群脚本,先把redis-..gem及启动脚本放在/data1/redis-cluster目录下,然后执行该脚本即可 #!/bin/bash set -e #获取redis本机ip #ip=`hostname -I` ip=`. | awk '{print $2}'` workspace=/data1/redis-cluster #根据需求自定义 想在哪个目录安装就写哪个目录 if [ ! -d $workspace ];then mkdir -p $workspace fi #下载并解压安装redis cd $workspace wget http://download.redis.io/releases/redis-4.0.9.tar.gz .tar.gz -C $workspace cd $workspace/redis- make && make install PREFIX=$workspace #配置redis cd $workspace /redis.conf redis- /redis.conf /redis.conf /redis.conf /redis.conf /redis.conf /redis.conf /redis.conf redis- redis- redis- redis- redis- /redis.conf /redis.conf /redis.conf /redis.conf /redis.conf #启动脚本 echo "#/bin/bash cd $workspace/redis- ./redis-server redis.conf cd $workspace/redis- ./redis-server redis.conf cd $workspace/redis- ./redis-server redis.conf cd $workspace/redis- ./redis-server redis.conf cd $workspace/redis- ./redis-server redis.conf cd $workspace/redis- ./redis-server redis.conf " >$workspace/all-start.sh #停止脚本 echo "#/bin/bash redis-cli -h $ip -p shutdown redis-cli -h $ip -p shutdown redis-cli -h $ip -p shutdown redis-cli -h $ip -p shutdown redis-cli -h $ip -p shutdown redis-cli -h $ip -p shutdown ">$workspace/all-stop.sh #自启动脚本 注:EOF前后都不应有空格或其他符号 cat > /etc/init.d/redis <<EOF 77 #!/bin/sh # # chkconfig: # 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 $workspace/*.sh $workspace/all-start.sh sleep 3 #安装redis-trib所需的 ruby脚本 cp $workspace/redis-4.0.9/src/redis-trib.rb $workspace/redis-trib.rb wget https://rubygems.org/downloads/redis-3.2.2.gem yum install -y ruby rubygems gem install redis-3.2.2.gem yum install -y expect #配置集群 expect -c " cd $workspace; spawn ./redis-trib.rb create --replicas 1 $ip:7001 $ip:7002 $ip:7003 $ip:7004 $ip:7005 $ip:7006; expect "configuration" { send \"yes\r\"; } expect eof " cp $workspace/redis-01/redis-cli /usr/bin/redis-cli 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 shutdown [Install] WantedBy=multi-user.target
集群设置自启动
先写好集群启动脚本
[root@slj-redis redis-cluster]# cat all-start.sh
cd /data1/redis-cluster/redis- ./redis-server redis.conf cd /data1/redis-cluster/redis- ./redis-server redis.conf cd /data1/redis-cluster/redis- ./redis-server redis.conf cd /data1/redis-cluster/redis- ./redis-server redis.conf cd /data1/redis-cluster/redis- ./redis-server redis.conf cd /data1/redis-cluster/redis- ./redis-server redis.conf
关闭脚本
[root@slj-redis redis-cluster]# vim all-stop.sh
redis-cli -h shutdown redis-cli -h shutdown redis-cli -h shutdown redis-cli -h shutdown redis-cli -h shutdown redis-cli -h shutdown
[root@slj-redis init.d]# vim redis-cluster
#!/bin/sh # # chkconfig: # 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