1 #安装redis伪集群脚本,先把redis-4.0.9.tar.gz redis-3.2.2.gem及启动脚本放在/data1/redis-cluster目录下,然后执行该脚本即可 2 #!/bin/bash 3 set -e 4 #获取redis本机ip 5 #ip=`hostname -I` 6 ip=`ifconfig | grep inet | grep 10. | awk '{print $2}'` 7 workspace=/data1/redis-cluster #根据需求自定义 想在哪个目录安装就写哪个目录 8 if [ ! -d $workspace ];then 9 mkdir -p $workspace 10 fi 11 #下载并解压安装redis 12 cd $workspace 13 wget http://download.redis.io/releases/redis-4.0.9.tar.gz 14 tar zxvf redis-4.0.9.tar.gz -C $workspace 15 cd $workspace/redis-4.0.9 16 make && make install PREFIX=$workspace 17 18 #配置redis 19 cd $workspace 20 mv bin redis-01 21 cp $workspace/redis-4.0.9/redis.conf redis-01 22 sed -i '69s/127.0.0.1/'$ip'/' redis-01/redis.conf 23 sed -i '92,158s/6379/7001/' redis-01/redis.conf 24 sed -i '136s/no/yes/' redis-01/redis.conf 25 sed -i '672s/no/yes/' redis-01/redis.conf 26 sed -i '814s/#//' redis-01/redis.conf 27 sed -i '822s/#//' redis-01/redis.conf 28 sed -i '828s/#//' redis-01/redis.conf 29 30 31 cp -r redis-01 redis-02 32 cp -r redis-01 redis-03 33 cp -r redis-01 redis-04 34 cp -r redis-01 redis-05 35 cp -r redis-01 redis-06 36 37 sed -i '92,158s/7001/7002/' redis-02/redis.conf 38 sed -i '92,158s/7001/7003/' redis-03/redis.conf 39 sed -i '92,158s/7001/7004/' redis-04/redis.conf 40 sed -i '92,158s/7001/7005/' redis-05/redis.conf 41 sed -i '92,158s/7001/7006/' redis-06/redis.conf 42 43 #启动脚本 44 echo "#/bin/bash 45 46 cd $workspace/redis-01 47 ./redis-server redis.conf 48 49 cd $workspace/redis-02 50 ./redis-server redis.conf 51 52 cd $workspace/redis-03 53 ./redis-server redis.conf 54 55 cd $workspace/redis-04 56 ./redis-server redis.conf 57 58 cd $workspace/redis-05 59 ./redis-server redis.conf 60 61 cd $workspace/redis-06 62 ./redis-server redis.conf 63 " >$workspace/all-start.sh 64 65 #停止脚本 66 echo "#/bin/bash 67 redis-cli -h $ip -p 7001 shutdown 68 redis-cli -h $ip -p 7002 shutdown 69 redis-cli -h $ip -p 7003 shutdown 70 redis-cli -h $ip -p 7004 shutdown 71 redis-cli -h $ip -p 7005 shutdown 72 redis-cli -h $ip -p 7006 shutdown 73 ">$workspace/all-stop.sh 74 75 #自启动脚本 注:EOF前后都不应有空格或其他符号 76 cat > /etc/init.d/redis <<EOF
77 #!/bin/sh 78 # 79 # chkconfig: 2345 70 20 80 # description: Redis-cluster autostart 81 . /etc/init.d/functions 82 83 case "$1" in 84 start) 85 /bin/bash /data1/redis-cluster/all-start.sh 86 ;; 87 stop) 88 /bin/bash /data1/redis-cluster/all-stop.sh 89 ;; 90 *) 91 echo "Usage: $0 (start|stop)" 92 ;; 93 esac 94 EOF 95 96 #启动redis 97 chmod 741 $workspace/*.sh 98 $workspace/all-start.sh 99 sleep 3 100 #安装redis-trib所需的 ruby脚本 101 cp $workspace/redis-4.0.9/src/redis-trib.rb $workspace/redis-trib.rb 102 103 wget https://rubygems.org/downloads/redis-3.2.2.gem 104 yum install -y ruby rubygems 105 gem install redis-3.2.2.gem 106 107 yum install -y expect 108 #配置集群 109 expect -c " 110 cd $workspace; 111 spawn ./redis-trib.rb create --replicas 1 $ip:7001 $ip:7002 $ip:7003 $ip:7004 $ip:7005 $ip:7006; 112 expect "configuration" { send \"yes\r\"; } 113 expect eof 114 " 115 cp $workspace/redis-01/redis-cli /usr/bin/redis-cli 116 echo -e "\nredis-cluster is ok " 117 chkconfig redis on 118
启动脚本
单实例设置自启动脚本
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