安装redis

  1. 安装前的准备

     yum install \
     vim \
     wget \
     make \
     gcc \
     gcc-c++ \
     automake \
     autoconf \
     -y \
    
  2. 下载解压并安装

     cd /root
     wget http://download.redis.io/releases/redis-4.0.8.tar.gz
     tar -zxvf redis-4.0.8.tar.gz
     cd redis-4.0.8
     make PREFIX=/usr/local/redis/ install
    
  3. 创建配置中要求的数据文件存储位置

     mkdir /data
     mkdir /data/redis
     mkdir /usr/local/redis/log
    
  4. 添加修改配置文件

     cp ./redis.conf /usr/local/redis
     vim /usr/local/redis/redis.conf
    
     # IP绑定
     bind 127.0.0.1 192.168.0.111
     # 保护模式(开启条件为各redis之间可以互相通信,做集群不可开启)
     protected-mode yes
     # 访问端口
     port 6379
     # 连接超时,单位S,0为不启用超时
     timeout 0
     # 以守护进程运行
     daemonize yes
     # 数据文件路径
     dir /data/redis
     # 进程ID文件的路径
     pidfile /usr/local/redis/log/redis.pid
     # 日志文件路径
     logfile /usr/local/redis/log/redis.log
     # 开启键过期删除通知
     notify-keyspace-events Ex
    
     ESC
     :wq
    
     # 以下安全配置选项仅作参考
         # 禁用部分危险命令
         rename-command FLUSHALL ""
         rename-command CONFIG   ""
         rename-command EVAL     ""
         # 添加访问密码
         requirepass ********
         # IP绑定本机
         bind 127.0.0.1
    
  5. 性能优化

     # 编辑/etc/rc.local
     vim /etc/rc.local
    
     echo never > /sys/kernel/mm/transparent_hugepage/enabled
    
     ESC
     :wq
    
     # 添加/etc/rc.local执行权限
     chmod +x /etc/rc.d/rc.local
    
     # 编辑/etc/sysctl.conf
     vim /etc/sysctl.conf
    
     vm.overcommit_memory = 1
     net.core.somaxconn = 1024
    
     ESC
     :wq
    
     # 立即解决
     echo never > /sys/kernel/mm/transparent_hugepage/enabled
     echo 1024 > /proc/sys/net/core/somaxconn
     sysctl vm.overcommit_memory=1
     sysctl -p
    
  6. 修改目录归属

     useradd -s /sbin/nologin -M redis
     chown -R redis:redis /data/redis
     chown -R redis:redis /usr/local/redis
    
  7. 启动redis并设置开机启动

     # 进入单元文件目录
     cd /etc/systemd/system
    
     # 创建redis单元文件,格式为: [单元文件名].[单元文件类型]
     vim redis.service
    
     [Unit]
     Description=Start redis on boot.
     After=default.target network.target
    
     [Service]
     User=redis
     Group=redis
     Type=forking
     ExecStart=/usr/local/redis/bin/redis-server /usr/local/redis/redis.conf
     PrivateTmp=false
     Restart=always
    
     [Install]
     WantedBy=multi-user.target
    
     ESC
     :wq
    
     # 修改文件权限为只有root用户可以编辑该文件
     chown -R root:root /etc/systemd/system/redis.service
     chmod -R 644 /etc/systemd/system/redis.service
    
     # 更新systemd
     systemctl daemon-reload
     systemctl enable redis
     systemctl start redis
    
  8. 存入数据

     /usr/local/redis/bin/redis-cli -h 127.0.0.1
    
     set num 123
     save
     get num
     quit
    
  9. 批量删除匹配规则数据

     redis-cli keys [匹配规则]|xargs redis-cli del
    

results matching ""

    No results matching ""