CentOS系统上安装Redis操作教程
Redis(Remote Dictionary Server)是一个开源的,基于内存的高性能键值对(NoSQL)数据库。它支持多种类型的数据结构,如字符串(strings)、列表(lists)、集合(sets)、有序集合(sorted sets)、哈希(hashes)、位图(bitmaps)、超日志(hyperloglogs)和地理空间(geospatial)索引半径查询。Redis因其出色的性能、可扩展性和广泛的功能集而广受欢迎。
前期准备
在开始安装Redis之前,确保你的CentOS系统已经安装了必要的依赖项,并且系统是最新的。这有助于避免在安装过程中遇到不必要的问题。
更新系统:
shellsudo yum update安装编译工具: 除了
gcc,你可能还需要安装make和tcl,因为它们是编译Redis的依赖项。shellsudo yum install -y gcc make tcl
下载Redis
下载Redis: 选择一个稳定的Redis版本进行下载。这里我们以6.2.6版本为例。
shellwget https://download.redis.io/releases/redis-6.2.6.tar.gz解压下载的压缩包:
shelltar -zxvf redis-6.2.6.tar.gz进入Redis目录:
shellcd redis-6.2.6/
编译安装Redis
编译Redis: 在Redis目录中,执行编译命令。这将自动检测系统环境并编译Redis。
shellmake安装Redis: 使用
make install命令将Redis安装到指定目录。这里我们选择/usr/local/redis作为安装目录。shellsudo make install PREFIX=/usr/local/redis
配置Redis
创建Redis配置文件目录:
shellsudo mkdir /etc/redis复制配置文件: 将默认的配置文件复制到
/etc/redis目录。shellsudo cp redis.conf /etc/redis/编辑配置文件: 使用文本编辑器(如
vi或nano)编辑配置文件,根据需要调整设置。shellsudo vi /etc/redis/redis.conf例如,你可以设置
bind指令来限制Redis服务的访问,或者调整内存使用策略等。
启动Redis服务
启动Redis: 使用以下命令启动Redis服务。
shell/usr/local/redis/bin/redis-server /etc/redis/redis.confshell# 查看进程来确定redis是否启动成功,非必须 ps -ef |grep redis设置Redis开机自启: 创建一个systemd服务文件来管理Redis服务。
shellsudo vim /etc/systemd/system/redis.service在文件中添加以下内容(请根据实际情况修改
ExecStart路径):ini[Unit] Description=Redis In-Memory Data Store After=network.target [Service] User=redis Group=redis ExecStart=/usr/local/redis/bin/redis-server /etc/redis/redis.conf ExecStop=/usr/local/redis/bin/redis-cli shutdown PrivateTmp=true [Install] WantedBy=multi-user.target保存并退出编辑器。
启用Redis服务:
shellsudo systemctl enable redis.service启动Redis服务:
shellsudo systemctl start redis.service
验证Redis安装
检查Redis服务状态:
shellsudo systemctl status redis.service测试Redis: 使用Redis命令行客户端测试服务。
shell/usr/local/redis/bin/redis-cli在客户端中,尝试执行一些基本命令,如
set和get,来验证Redis是否正常工作。
停止和重启Redis服务
停止Redis服务:
shellsudo systemctl stop redis.service重启Redis服务:
shellsudo systemctl restart redis.service
卸载Redis服务
停止并禁用Redis服务:
shellsudo systemctl stop redis.service sudo systemctl disable redis.service删除Redis服务文件:
shellsudo rm /etc/systemd/system/redis.service删除Redis安装目录:
shellsudo rm -rf /usr/local/redis清理Redis配置文件:
shellsudo rm /etc/redis/redis.conf
在执行上述步骤时,请确保你有足够的权限来执行系统命令。如果你不是root用户,你可能需要在命令前加上sudo来获取必要的权限。