CentOS7 安装并启用 Redis

CentOS7 安装并启用 Redis

  1. 下载并执行安装

    wget http://download.redis.io/releases/redis-3.2.8.tar.gzsudo tar -zxvf redis-3.2.8.tar.gz
    cd redis-3.2.8/
    sudo make
    sudo make install

    如果在make这一步报错

    /bin/sh: cc: command not found
    make[1]: *** [adlist.o] Error 127
    make[1]: Leaving directory `/root/redis-3.2.8/src'
    make: *** [all] Error 2
    [root@localhost redis-3.2.8]# make install
    cd src && make install
    make[1]: Entering directory `/root/redis-3.2.8/src'

    如果提示缺少cc,则需要安装cc

    yum -y install gcc gcc-c++ libstdc++-devel

    然后再次执行安装
    如果再次报错

    make CFLAGS="-march=x86-64"

    则可以顺利安装

    This script will help you easily set up a running redis server
    Please select the redis port for this instance: [6379] 
    Selecting default: 6379
    Please select the redis config file name [/etc/redis/6379.conf] 
    Selected default - /etc/redis/6379.conf
    Please select the redis log file name [/var/log/redis_6379.log] 
    Selected default - /var/log/redis_6379.log
    Please select the data directory for this instance [/var/lib/redis/6379] 
    Selected default - /var/lib/redis/6379
    Please select the redis executable path [/usr/local/bin/redis-server] 
    Selected config:
    Port           : 6379
    Config file    : /etc/redis/6379.conf
    Log file       : /var/log/redis_6379.log
    Data dir       : /var/lib/redis/6379
    Executable     : /usr/local/bin/redis-server
    Cli Executable : /usr/local/bin/redis-cli
    Is this ok? Then press ENTER to go on or Ctrl-C to abort.
    Copied /tmp/6379.conf => /etc/init.d/redis_6379

    上述需要确认的4个文件依次为:配置文件、日志文件、数据文件、启动文件,均可以更改

  2. 创建redis.service并启动

    vim /lib/systemd/system/redis.service

    写入

    [Unit]
    Description=Redis
    [Service]
    Type=forking
    ExecStart=/etc/init.d/redis_6379 start
    ExecStop=/etc/init.d/redis_6379 stop
    [Install]

    当然需要启动服务端

    vim /etc/redis/6379.conf

    将daemonize no改为daemonize yes

    redis-server /etc/redis/6379.conf

    启动成功

    redis-cli
  3. 可能用到的命令

    systemctl start redis.service             #启动redis服务
    systemctl enable redis.service            #设置开机自启动
    systemctl disable redis.service           #停止开机自启动
    systemctl status redis.service            #查看服务当前状态

标签: centos redis linux

发表评论: