CentOS 安装Redis
1.找到最新稳定版(当前5.0.5)
redis官网链接: https://redis.io
进入Download可以界面可以看到有Unstable,Stable,Docker三个版本,由于本人是测试所以选择的是Stable稳定版。
Redis版本遵循第二位偶数版为稳定版,选择的时候可以根据当前场景选择。
下面为redis官网介绍redis5.0的改动
Redis 5.0 is the first version of Redis to introduce the new stream data type with consumer groups, sorted sets blocking pop operations, LFU/LRU info in RDB, Cluster manager inside redis-cli, active defragmentation V2, HyperLogLogs improvements and many other improvements. Redis 5 was release as GA in October 2018.
大概意思:
引入了新的流数据类型,包括用户组、阻塞pop操作的排序集、rdb中的lfu/lru信息、redis cli中的cluster manager、active defragmentation v2、hyperlogloglogs改进和许多其他改进
以此输入下面命令:
$ wget http://download.redis.io/releases/redis-5.0.5.tar.gz
$ tar -zxvf redis-5.0.5.tar.gz #解压
$ cd redis-5.0.5 #进入目录
$ make #编译
$ cd src
$ make install PREFIX=/usr/local/redis #安装到/usr/local/redis目录
$ cd …
$ cp redis.conf /usr/local/redis/bin #将配置文件移动到/usr/local/redis/bin目录
2.配置默认启动(redis 默认不后台启动,需要配置)
$ cd /usr/local/redis/bin
$ vim redis.conf #将 daemonize的值改为yes bind 127.0.0.1去掉(如果要远 程访问) protected-mode改为 no
$ ./redis-server ./redis.conf #启动
3.开机自启动
centos 7以上是用Systemd进行系统初始化的,Systemd 是 Linux 系统中最新的初始化系统(init),它主要的设计目标是克服 sysvinit 固有的缺点,提高系统的启动速度。
Systemd服务文件以.service结尾,比如现在要建立redis为开机启动,如果用yum install命令安装的,yum命令会自动创建redis.service文件,直接用命令systemcel enable redis.service设置开机启动即可
vim /etc/systemd/system/redis.service
加入一下内容
[Unit]
Description=redis-server
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/redis/bin/redis-server /usr/local/redis/bin/redis.conf
PrivateTmp=true
[Install]
WantedBy=multi-user.target
先关闭redis-server
systemctl stop redis.service
开启redis-server
systemctl start redis.service #如果服务是开启状态,使用此命令会启动失败。
systemctl enable redis.service #注意后面不能跟空格
重启
reboot #重启
查看服务运行状态
systemctl status redis.service
常用命令
systemctl start redis.service #启动redis服务
systemctl enable redis.service #设置开机自启动
systemctl disable redis.service #停止开机自启动
systemctl status redis.service #查看服务当前状态
systemctl restart redis.service #重新启动服务
systemctl list-units --type=service #查看所有已启动的服务