#!/bin/bash
# 判断是否为root用户,不是root用户提示退出
if [[ $EUID -ne 0 ]]; then
?? echo "This script. must be run as root" 1>&2
?? exit 1
fi
?
# 安装必要的组件
yum install -y gcc
?
#?下载redis源代码
cd /tmp
wget -P /tmp?http://download.redis.io/releases/redis-3.2.1.tar.gz
?
# 对比文件下载是否正确
echo 26c0fc282369121b4e278523fce122910b65fbbf ?redis-3.2.1.tar.gz > /tmp/download.txt
sha1sum -c download.txt
if [ $? -ne 0 ]
then
? echo "The Download File is not Correct, Please Download it again."
? exit 0
fi
?
# 编译安装
tar xf redis-3.2.1.tar.gz
cd redis-3.2.1
make
make install
?
# 创建组和用户
groupadd redis
useradd -g redis redis
?
# LOG文件存放位置
mkdir /var/log/redis
chown?redis:redis /var/log/redis
?
# 当前IP地址
IP=`ifconfig eth0 | grep "inet " | awk ‘{ print $2}‘`
?
# Redis服务开机运行
echo "[Unit]" >?/usr/lib/systemd/system/redis.service?
echo "Description=Redis persistent key-value database" >>?/usr/lib/systemd/system/redis.service?
echo "After=network.target" >>?/usr/lib/systemd/system/redis.service?
echo "[Service]" >>?/usr/lib/systemd/system/redis.service??
echo "Type=forking" >>?/usr/lib/systemd/system/redis.service??
echo "ExecStart=/usr/local/bin/redis-server /etc/redis/redis.conf" >>?/usr/lib/systemd/system/redis.service??
echo "ExecStop=/usr/local/bin/redis-cli -h ${IP} shutdown" >>?/usr/lib/systemd/system/redis.service??
echo "User=redis" >>?/usr/lib/systemd/system/redis.service??
echo "Group=redis" >>?/usr/lib/systemd/system/redis.service??
echo "[Install]" >>?/usr/lib/systemd/system/redis.service??
echo "WantedBy=multi-user.target" >>?/usr/lib/systemd/system/redis.service
?
# Redis Sentinel 服务开机运行
echo "[Unit]" >?/usr/lib/systemd/system/redis-sentinel.service?
echo "Description=Redis Sentinel" >>?/usr/lib/systemd/system/redis-sentinel.service?
echo "After=network.target" >>?/usr/lib/systemd/system/redis-sentinel.service?
echo "[Service]" >>?/usr/lib/systemd/system/redis-sentinel.service??
echo "Type=forking" >>?/usr/lib/systemd/system/redis-sentinel.service??
echo "ExecStart=/usr/local/bin/redis-sentinel /etc/redis/sentinel.conf" >>?/usr/lib/systemd/system/redis-sentinel.service??
echo "ExecStop=/usr/local/bin/redis-cli -h ${IP} -p 26379 shutdown" >>?/usr/lib/systemd/system/redis-sentinel.service??
echo "User=redis" >>?/usr/lib/systemd/system/redis-sentinel.service??
echo "Group=redis" >>?/usr/lib/systemd/system/redis-sentinel.service??
echo "[Install]" >>?/usr/lib/systemd/system/redis-sentinel.service??
echo "WantedBy=multi-user.target" >>?/usr/lib/systemd/system/redis-sentinel.service
?
# redis 服务限制
mkdir?/etc/systemd/system/redis.service.d/
echo "[Service]" >?/etc/systemd/system/redis.service.d/limits.conf?
echo "LimitNOFILE=65535" >> /etc/systemd/system/redis.service.d/limits.conf
?
# 启用开机自动运行
systemctl enable redis.service
systemctl enable redis-sentinel.service
?
# 禁用selinux
sed -i ‘s/SELINUX=enforcing/SELINUX=disable/‘?/etc/selinux/config
# 系统优化
echo "net.core.somaxconn=4096" >> /etc/sysctl.conf
echo "echo never > /sys/kernel/mm/transparent_hugepage/enabled" >> /etc/rc.local
chmod +x /etc/rc.local
echo "vm.overcommit_memory=1" >> /etc/sysctl.conf
echo "*? soft? nofile? 65535" >>?/etc/security/limits.conf
echo "?* ?hard? nofile? 65535" >>?/etc/security/limits.conf
echo "?* ?soft? nproc? 65535" >>?/etc/security/limits.conf
echo "?* ?hard? nproc? 65535" >>?/etc/security/limits.conf
echo "net.ipv4.tcp_tw_recycle=1"?>> /etc/sysctl.conf
?
# 别名
echo "alias redis=‘redis-cli -h ${IP}‘" >> /etc/profile
echo "alias sentinel=‘redis-cli -h ${IP} -p 26379‘" >> /etc/profile
?
# 修改时区
/bin/cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
?
#Redis配置文件存放位置
mkdir /etc/redis
?
# 修改配置文件redis.conf
echo "bind ${IP}" > /etc/redis/redis.conf
echo "protected-mode no" >> /etc/redis/redis.conf
echo "tcp-backlog 4096" >> /etc/redis/redis.conf
echo "timeout 300" >> /etc/redis/redis.conf
echo "tcp-keepalive 60" >> /etc/redis/redis.conf
echo "daemonize yes" >> /etc/redis/redis.conf
echo "supervised systemd" >> /etc/redis/redis.conf
echo "logfile /var/log/redis/redis.log" >> /etc/redis/redis.conf
echo "dir /var/log/redis" >> /etc/redis/redis.conf
echo "maxclients 10000" >> /etc/redis/redis.conf
echo "maxmemory 4G" >> /etc/redis/redis.conf
echo "maxmemory-policy volatile-lru" >> /etc/redis/redis.conf
echo "appendonly no" >> /etc/redis/redis.conf
echo "slowlog-max-len 512" >> /etc/redis/redis.conf
echo "client-output-buffer-limit slave 512mb 256mb 300" >> /etc/redis/redis.conf
?
# 修改配置文件sentinel.conf
echo "port 26379" > /etc/redis/sentinel.conf
echo "bind ${IP}" >> /etc/redis/sentinel.conf
echo "dir /var/log/redis" >> /etc/redis/sentinel.conf
echo "sentinel monitor mymaster ${IP} 6379 2" >> /etc/redis/sentinel.conf
echo "maxclients 10" >> /etc/redis/sentinel.conf
echo "logfile /var/log/redis/sentinel.log" >> /etc/redis/sentinel.conf
echo "daemonize yes" >> /etc/redis/sentinel.conf
echo "sentinel down-after-milliseconds mymaster 5000" >> /etc/redis/sentinel.conf
echo "sentinel failover-timeout mymaster 60000" >> /etc/redis/sentinel.conf
Redis & Sentinel 安装脚本