源码安装
Redis安装前建议要进行残留数据检查,排除后期存在的各种隐患
官网:https://redis.io/,Linux版本只会提供源码,不提供二进制安装包,因此需要编译源码进行安装,本次使用CentOS8 VMware虚拟机进行安装,可直接到 https://download.redis.io/releases/ 下载压缩包然后传入虚拟机。这里直接通过命令行下载,选择redis-6.2.3.tar.gz这个版本
wget https://download.redis.io/releases/redis-6.2.3.tar.gz
tar -xvzf redis-6.2.3.tar.gz
安装gcc和make
安装redis之前先安装gcc和make,如果有网络就用yum在线安装即可
yum -y install gcc && yum -y install gcc-c++
采用此方式安装的 GCC 编译器,版本通常较低,如果想编译 C++11 标准下的 C++ 程序,则至少要安装 4.8 版本的 GCC 编译器,低版本的 GCC 编译器是不支持 C++11 标准的。
安装redis
切换到redis目录下,该目录下面有Makefile文件
# 编译源码,过程可能比较耗时
make
# 执行测试,可不做
make test
# 安装
make install
# make失败后的清理工作
make distclean
安装位置在/usr/local/bin目录
安装TCL
如果报错:You need tcl 8.5 or newer in order to run the Redis test,说明系统中缺少 tcl 或者 tcl 版本比较老
wget http://downloads.sourceforge.net/tcl/tcl8.6.1-src.tar.gz
sudo tar xzvf tcl8.6.1-src.tar.gz -C /usr/local
cd /usr/local/tcl8.6.1/unix
sudo ./configure
sudo make && make install
从源代码编译 Redis,默认情况下 Redis 会被安装到 /usr/local/bin、/usr/local/lib 和 /usr/local/etc 等目录。通常可以通过 make install 命令指定安装目录:make install PREFIX=/usr/local/redis
,安装后是下面这样,bin目录下存放二进制可执行文件
[root@localhost redis]# tree . # /usr/local/redis
.
└── bin
├── redis-benchmark
├── redis-check-aof -> redis-server
├── redis-check-rdb -> redis-server
├── redis-cli
├── redis-sentinel -> redis-server
└── redis-server
安装脚本
在CentOS8上安装redis-6.2.3版本
#!/bin/bash
echo "Redis install script starting ======= $0 =================="
# 默认值
redis_version=""
install_dir="/usr/local/redis"
offline_mode=false
skip_compile=false
# 解析命令行参数
while [[ "$#" -gt 0 ]]; do
case $1 in
-version) redis_version="$2"; shift ;;
-target) install_dir="$2"; shift ;;
--source) offline_mode=true; source_dir="$2"; shift ;;
--skipCompile) skip_compile=true ;;
*) echo "Error: Unknown parameter $1"; exit 1 ;;
esac
shift
done
# 检查参数
if [[ "$offline_mode" == true && -z "$source_dir" ]]; then
echo "Error: Source directory must be provided in offline mode."
exit 1
fi
if [[ "$offline_mode" == true && -z "$redis_version" ]]; then
redis_version=$(basename "$source_dir")
fi
if [[ -z "$install_dir" || -z "$redis_version" ]]; then
echo "Error: Either -version or --source must be provided."
exit 1
fi
# 检查目标目录
if [ -d "$install_dir" ]; then
read -t 60 -p "Directory $install_dir already exists. Delete? (y/n): " confirm
if [[ $confirm =~ ^[yY]$ ]]; then
echo "Deleting existing directory $install_dir."
rm -rf "$install_dir" || { echo "Error deleting directory $install_dir"; exit 1; }
else
echo "Installation directory $install_dir exists. Exiting installation."
exit 1
fi
fi
mkdir -p "$install_dir" || { echo "Error creating directory $install_dir"; exit 1; }
echo "Installation directory $install_dir created."
# 检查源码目录
if [ "$offline_mode" == true ]; then
source_dir=$(realpath "$source_dir") || { echo "Error resolving source directory $source_dir"; exit 1; }
cd "$source_dir" || { echo "Error: Cannot access source directory $source_dir"; exit 1; }
else
cd "$install_dir" || { echo "Error: Cannot access installation directory $install_dir"; exit 1; }
redis_url="https://download.redis.io/releases/$redis_version.tar.gz"
echo "================= Downloading Redis $redis_version ================="
wget "$redis_url" -O "$redis_version.tar.gz" || { echo "Failed to download $redis_url. Please check your network."; exit 1; }
echo "================= Download complete, extracting... ================="
tar -zxvf "$redis_version.tar.gz" || { echo "Failed to extract $redis_version.tar.gz"; exit 1; }
cd "$install_dir/$redis_version" || { echo "Error: Cannot access extracted directory $install_dir/$redis_version"; exit 1; }
fi
# 安装依赖
if [ "$skip_compile" == false ]; then
echo "================= Installing dependencies... ================="
yum install gcc-c++ -y || { echo "Failed to install dependencies. Please check your package manager."; exit 1; }
fi
# 编译或安装
if [ "$skip_compile" == true ]; then
echo "================= Skipping compilation. Installing directly... ================="
make install PREFIX="$install_dir" || { echo "Installation failed during 'make install'. Please check for errors."; exit 1; }
else
echo "================= Compiling Redis... ================="
make || { echo "'make' command failed. Please check for errors in the compilation process."; exit 1; }
echo "================= Installing Redis... ================="
make install PREFIX="$install_dir" || { echo "Installation failed during 'make install'. Please check for errors."; exit 1; }
fi
# 配置文件和启动脚本
cp redis.conf "$install_dir/redis.conf" || { echo "Failed to copy redis.conf"; exit 1; }
cp sentinel.conf "$install_dir/sentinel.conf" || { echo "Failed to copy sentinel.conf"; exit 1; }
cp redis.conf "$install_dir/bin/redis.conf" || { echo "Failed to copy redis.conf backup"; exit 1; }
cp sentinel.conf "$install_dir/bin/sentinel.conf" || { echo "Failed to copy sentinel.conf backup"; exit 1; }
# 创建启动脚本
startup_script="$install_dir/bin/start_redis.sh"
echo -e "#!/bin/bash\n$install_dir/bin/redis-server $install_dir/redis.conf" > "$startup_script" || { echo "Failed to create startup script $startup_script"; exit 1; }
chmod +x "$startup_script" || { echo "Failed to make $startup_script executable"; exit 1; }
echo "================= Redis installation complete ================="
echo "You can start Redis using the command: $startup_script"
使用方式
# 给予执行权限
chmod +x install_redis.sh
# 在线模式
./install_redis.sh -version 6.2.3 -target /usr/local/redis
# 离线模式
./install_redis.sh --source ./redis-6.2.3 -target /usr/local/redis
# 跳过编译
./install_redis.sh --source ./redis-6.2.3 --skipCompile
安装结果如下
包管理器安装
使用包管理器(如 apt 或 yum)安装 Redis 时,默认安装目录通常是:
可执行文件:/usr/bin/redis-server
配置文件:/etc/redis/redis.conf
数据目录:/var/lib/redis
日志目录:/var/log/redis/redis-server.log
修改配置脚本
脚本名称redis-config.sh,赋予权限:chmod +x redis-config.sh
#!/bin/bash
# 检查参数数量
if [ "$#" -lt 2 ]; then
echo "usage: $0 set <config_key> <new_value> </path/to/redis.conf>"
exit 1
fi
# 从参数中获取操作类型、配置项、新值和配置文件路径
OPERATION="$1"
CONFIG_KEY="$2"
CONFIG_FILE="${@:3}" # 允许传入多个参数,最后一个参数是文件路径
# 检查配置文件是否存在
if [ ! -f "$CONFIG_FILE" ]; then
echo "Error: config file $CONFIG_FILE not found!"
exit 1
fi
case "$OPERATION" in
set)
if [ "$#" -ne 3 ]; then
echo "usage: $0 set <config_key> <new_value> </path/to/redis.conf>"
exit 1
fi
CONFIG_VALUE="$3"
# 检查配置项是否存在
if grep -q "^$CONFIG_KEY" "$CONFIG_FILE"; then
# 如果存在,替换值
sed -i "s/^$CONFIG_KEY .*/$CONFIG_KEY $CONFIG_VALUE/" "$CONFIG_FILE"
echo "config $CONFIG_KEY is updated to $CONFIG_VALUE。"
else
# 如果不存在,添加到文件末尾
echo "$CONFIG_KEY $CONFIG_VALUE" >> "$CONFIG_FILE"
echo "config $CONFIG_KEY added, value is $CONFIG_VALUE。"
fi
;;
get)
# 获取配置项的值
VALUE=$(grep "^$CONFIG_KEY" "$CONFIG_FILE" | awk '{print $2}')
if [ -z "$VALUE" ]; then
echo "config $CONFIG_KEY doee not exist!"
else
echo "$CONFIG_KEY = $VALUE"
fi
;;
*)
echo "invalid operation: $OPERATION, command set or get is supported currently!"
exit 1
;;
esac
# 提示用户重启 Redis 以应用更改(仅在 set 操作后)
if [ "$OPERATION" = "set" ]; then
echo "please restart Redis to check if the config is active!"
fi
使用方法:
redis-config.sh set maxmemory 256mb /path/to/your/redis.conf
redis-config.sh get maxmemory /path/to/your/redis.conf
卸载清理
以下步骤无先后顺序
- 确认Redis是否已经安装在系统中
#检查有没有残留客户端
whereis redis-cli
#检查有没有残留服务
whereis redis-server
- 停止Redis服务
#查看是否有redis的进程
ps -ef | grep redis
#杀掉redis进程
kill -9 进程号
- 如果需要,删除Redis的配置文件和数据。注意:删除文件时一定要确保自己知道删的是什么
# 查找系统中所有与redis相关的目录文件
find / -name redis
# 删掉查找出来的目录
rm -rf /etc/redis
rm -rf /var/lib/redis
如果redis确定有装过,可通过下面等操作找相关文件进行删除
# 查看当前redis的版本,这个查出来是6.2.3
redis-server --version
# 根据版本得知安装目录名称是redis-6.2.3,查找它的安装目录
find / -name redis-6.2.3
# 清除redis安装目录
rm -rf /data/local/redis-6.2.3
# 再次查看版本或查看客户端,如果都报错说明卸载成功了
redis-server --version
redis-cli
- 清除PATH环境变量
- 清除Redis服务,开机启动等设置