一、 编译安装Redis
VERSION=redis-6.2.4 #安装的版本
PASSWORD=000000 #密码
INSTALL_DIR=/apps/redis #安装目录
color () {
RES_COL=60
MOVE_TO_COL="echo -en \\033[${RES_COL}G"
SETCOLOR_SUCCESS="echo -en \\033[1;32m"
SETCOLOR_FAILURE="echo -en \\033[1;31m"
SETCOLOR_WARNING="echo -en \\033[1;33m"
SETCOLOR_NORMAL="echo -en \E[0m"
echo -n "$1" && $MOVE_TO_COL
echo -n "["
if [ $2 = "success" -o $2 = "0" ] ;then
${SETCOLOR_SUCCESS}
echo -n $" OK "
elif [ $2 = "failure" -o $2 = "1" ] ;then
${SETCOLOR_FAILURE}
echo -n $"FAILED"
else
${SETCOLOR_WARNING}
echo -n $"WARNING"
fi
${SETCOLOR_NORMAL}
echo -n "]"
echo
}
install() {
yum -y install gcc jemalloc-devel || { color "安装软件包失败,请检查网络配置" 1 ; exit; }
wget http://download.redis.io/releases/${VERSION}.tar.gz || { color "Redis 源码下载失败" 1 ; exit; }
tar xf ${VERSION}.tar.gz
cd ${VERSION}
make -j 4 PREFIX=${INSTALL_DIR} install && color "Redis 编译安装完成" 0 || { color "Redis 编译安装失败" 1 ;exit ; }
ln -s ${INSTALL_DIR}/bin/redis-* /usr/bin/
mkdir -p ${INSTALL_DIR}/{etc,log,data,run}
cp redis.conf ${INSTALL_DIR}/etc/
sed -i -e 's/bind 127.0.0.1/bind 0.0.0.0/' -e "/# requirepass/a requirepass $PASSWORD" -e "/^dir .*/c dir ${INSTALL_DIR}/data/" -e "/logfile .*/c logfile ${INSTALL_DIR}/log/redis-6379.log" -e "/^pidfile .*/c pidfile ${INSTALL_DIR}/run/redis_6379.pid" ${INSTALL_DIR}/etc/redis.conf
if id redis &> /dev/null ;then
color "Redis 用户已存在" 1
else
useradd -r -s /sbin/nologin redis
color "Redis 用户创建成功" 0
fi
chown -R redis.redis ${INSTALL_DIR}
cat >> /etc/sysctl.conf <<EOF
net.core.somaxconn = 1024
vm.overcommit_memory = 1
EOF
sysctl -p
echo 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' >> /etc/rc.d/rc.local
chmod +x /etc/rc.d/rc.local
/etc/rc.d/rc.local
cat > /usr/lib/systemd/system/redis.service <<EOF
[Unit]
Description=Redis persistent key-value database
After=network.target
[Service]
ExecStart=${INSTALL_DIR}/bin/redis-server ${INSTALL_DIR}/etc/redis.conf --supervised systemd
ExecStop=/bin/kill -s QUIT \$MAINPID
#Type=notify
User=redis
Group=redis
RuntimeDirectory=redis
RuntimeDirectoryMode=0755
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable --now redis &> /dev/null && color "Redis 服务启动成功,Redis信息如下:" 0 || { color "Redis 启动失败" 1 ;exit; }
sleep 2
redis-cli -a $PASSWORD INFO Server 2> /dev/null
}
install
查看Redis端口是否开启
关于Redis启动时告警的问题,脚本末尾已做修改(告警不做修改也不会影响使用)
编译安装后存在的一些工具
[root@localhost redis]# ls /usr/bin|grep redis
redis-benchmark #Redis性能测试工具
redis-check-aof #AOF文件检查工具
redis-check-rdb #RDB文件检查工具
redis-cli#客户端工具
redis-sentinel#哨兵,软连接到server
redis-server#Redis服务启动命令
二、Redis多实例
进入编译安装的目录
[root@localhost etc]# pwd
/apps/redis/etc
之后配置注意所在的目录位置
复制配置文件
[root@localhost etc]# cp redis.conf redis_6379.conf
[root@localhost etc]# cp redis.conf redis_6380.conf
[root@localhost etc]# cp redis.conf redis_6381.conf
修改配置文件中对应的端口号
[root@localhost etc]# sed -i 's/port 6379/port 6380/' redis_6380.conf
[root@localhost etc]# sed -i 's/port 6379/port 6381/' redis_6381.conf
[root@localhost etc]# grep "^port" redis_6380.conf
port 6380
[root@localhost etc]# grep "^port" redis_6381.conf
port 6381
[root@localhost etc]# grep "^port" redis_6379.conf
port 6379
修改对应的数据文件指向
[root@localhost etc]# sed -i 's/dbfilename dump.rdb/dbfilename dump_6379.rdb/' redis_6379.conf
[root@localhost etc]# sed -i 's/dbfilename dump.rdb/dbfilename dump_6380.rdb/' redis_6380.conf
[root@localhost etc]# sed -i 's/dbfilename dump.rdb/dbfilename dump_6381.rdb/' redis_6381.conf
[root@localhost etc]# grep "^dbfilename" redis_6379.conf
dbfilename dump_6379.rdb
[root@localhost etc]# grep "^dbfilename" redis_6380.conf
dbfilename dump_6380.rdb
[root@localhost etc]# grep "^dbfilename" redis_6381.conf
dbfilename dump_6381.rdb
修改对应的日志文件指向
[root@localhost redis]# grep log/redis-6379.log etc/redis_6379.conf
logfile /apps/redis/log/redis-6379.log
[root@localhost redis]# sed -i 's/redis-6379.log/redis-6380.log/' etc/redis_6380.conf
[root@localhost redis]# sed -i 's/redis-6379.log/redis-6381.log/' etc/redis_6381.conf
修改对应的PID文件指向
[root@localhost redis]# grep run/redis_6379.pid etc/redis_6379.conf
pidfile /apps/redis/run/redis_6379.pid
[root@localhost redis]#
[root@localhost redis]# sed -i 's/redis_6379.pid/redis_6380.pid/' etc/redis_6380.conf
[root@localhost redis]# sed -i 's/redis_6379.pid/redis_6381.pid/' etc/redis_6381.conf
修改对应的service文件
[root@localhost redis]# cp -a /lib/systemd/system/redis.service /lib/systemd/system/redis6379.service
[root@localhost redis]# cp -a /lib/systemd/system/redis.service /lib/systemd/system/redis6380.service
[root@localhost redis]# cp -a /lib/systemd/system/redis.service /lib/systemd/system/redis6381.service
修改对应的service文件中,redis_6381.conf 文件指向
[root@localhost redis]# vim /lib/systemd/system/redis6379.service
[root@localhost redis]# vim /lib/systemd/system/redis6380.service
[root@localhost redis]# vim /lib/systemd/system/redis6381.service
[root@localhost redis]# cat /lib/systemd/system/redis6381.service
[Unit]
Description=Redis persistent key-value database
After=network.target
[Service]
ExecStart=/apps/redis/bin/redis-server /apps/redis/etc/redis_6381.conf --supervised systemd #
ExecStop=/bin/kill -s QUIT $MAINPID
User=redis
Group=redis
RuntimeDirectory=redis
RuntimeDirectoryMode=0755
[Install]
WantedBy=multi-user.target
三、测试结果
先停止原有编译安装的Redis服务
Redis原有服务端口为6379
[root@localhost redis]# systemctl stop redis
[root@localhost redis]# systemctl daemon-reload
[root@localhost redis]# systemctl start redis6379 redis6380 redis6381