1、yum方式-安装redis
1.1、配置仓库
gzip /etc/yum.repos.d/*
curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
curl -o /etc/yum.repos.d/epel-7.repo http://mirrors.aliyun.com/repo/epel-7.repo
yum clean all
yum makecache
1.2、yum安装
2、源码方式-安装redis
2.1、下载软件
mkdir -p /data/softs && cd /data/softs
curl -o redis-6.2.5.tar.gz https://download.redis.io/releases/redis-6.2.5.tar.gz
2.2、安装
2.2.1、编译安装
yum install gcc make cmake -y
tar xvf redis-6.2.5.tar.gz && cd redis-6.2.5
make && make PREFIX=/usr/local/redis-6.2.5/ install
2.2.2、查看效果
]# tree /usr/local/redis-6.2.5/
/usr/local/redis-6.2.5/
└── bin
├── redis-benchmark # 性能测试工具
├── redis-check-aof -> redis-server # 修复有问题的AOF文件
├── redis-check-rdb -> redis-server # 修复有问题的dump.rdb文件
├── redis-cli # 客户端,操作入口
├── redis-sentinel -> redis-server # Redis集群使用
└── redis-server # Redis服务器启动命令
2.3、配置环境变量
cat << 'CAT_END' > /etc/profile.d/redis.sh
#!/bin/bash
export redis_home=/usr/local/redis-6.2.5
PATH=$PATH:$redis_home/bin
CAT_END
source /etc/profile
2.4、准备配置文件和创建基本目录
mkdir /usr/local/redis-6.2.5/{etc,log,data,run} -p
cd /data/softs/redis-6.2.5 && cp redis.conf sentinel.conf /usr/local/redis-6.2.5/etc/
2.5、服务启动脚本
2.5.1、shell脚本
cat << 'CAT_END' >/usr/local/redis-6.2.5/redis.server
#!/bin/sh
#
# Simple Redis init.d script conceived to work on Linux systems
# as it does use of the /proc filesystem.
REDISPORT=6379
REDIS_HOME=/usr/local/redis-6.2.5
EXEC=$REDIS_HOME/bin/redis-server
CLIEXEC=$REDIS_HOME/bin/redis-cli
PIDFILE=/var/run/redis_${REDISPORT}.pid
CONF=$REDIS_HOME/etc/${REDISPORT}.conf
function start(){
if [ `ps -ef | grep redis-server | grep -v grep | wc -l` -eq 1 ]
then
echo "$PIDFILE exists, process is already running or crashed"
else
echo "Starting Redis server..."
rm -f $PIDFILE
$EXEC $CONF
fi
}
function stop(){
if [ ! -f $PIDFILE ]
then
echo "$PIDFILE does not exist, process is not running"
else
PID=$(cat $PIDFILE)
echo "Stopping ..."
$CLIEXEC -p $REDISPORT shutdown
while [ -x /proc/${PID} ]
do
echo "Waiting for Redis to shutdown ..."
sleep 1
done
echo "Redis stopped"
fi
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop;
sleep 3;
start;
;;
esac
CAT_END
chmod 755 /usr/local/redis-6.2.5/redis.server
cp /usr/local/redis-6.2.5/etc/redis.conf /usr/local/redis-6.2.5/etc/6379.conf
2.5.2、systemd的配置
cat << 'CAT_END' > /lib/systemd/system/redis.service
[Unit]
Description=Redis server daemon
Documentation=/home/application/redis
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/redis-6.2.5/redis.server start --supervised systemd
ExecStop=/usr/local/redis-6.2.5/redis.server stop --supervised systemd
Restart=/usr/local/redis-6.2.5/redis.server restart --supervised systemd
PrivateTmp=True
[Install]
WantedBy=multi-user.target
CAT_END
systemctl daemon-reload
2.6、简单调优
# 根据提示,调优如下:
cat >>/etc/sysctl.conf<<EOF
vm.overcommit_memory=1
EOF
sysctl -p
#最大连接数
echo 511 > /proc/sys/net/core/somaxconn
3、程序启动方式
3.1、前台启动
redis-server /usr/local/redis-6.2.5/etc/redis.conf
3.2、多实例启动
redis-server /usr/local/redis-6.2.5/etc/redis.conf --port 6666
redis-server /usr/local/redis-6.2.5/etc/redis.conf --port 7777
redis-server /usr/local/redis-6.2.5/etc/redis.conf --port 8888
redis-server /usr/local/redis-6.2.5/etc/redis.conf --port 9999
]# netstat -tunlp | grep redis
tcp 0 0 0.0.0.0:9999 0.0.0.0:* LISTEN 24840/redis-server
tcp 0 0 0.0.0.0:8888 0.0.0.0:* LISTEN 24835/redis-server
tcp 0 0 0.0.0.0:7777 0.0.0.0:* LISTEN 24830/redis-server
tcp 0 0 0.0.0.0:6666 0.0.0.0:* LISTEN 24825/redis-server
3.3、后台启动
3.3.1、配置redis
sed -i '/^daemonize/c daemonize yes' /usr/local/redis-6.2.5/etc/redis.conf
sed -i '/^bind/c bind *' /usr/local/redis-6.2.5/etc/redis.conf
3.3.2、命令启动方法
redis-server /usr/local/redis-6.2.5/etc/redis.conf
3.3.3、systemd启动方法
3.4、关闭redis服务
3.4.1、systemd关闭
3.4.2、命令关闭
redis-cli -p 6379 shutdown
3.4.3、kill -9 关闭
yum install lsof -y
kill -9 $(lsof -Pti :6379)