Linux安装redis

  1. 作者QQ:67065435 QQ群:821635552

  2. 本站内容全部为作者原创,转载请注明出处!

  3. 安装前的准备

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

    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
    
  5. 创建配置中要求的数据文件存储位置

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

    cp ./redis.conf /usr/local/redis
    chmod -R 600 /usr/local/redis/redis.conf
    vim /usr/local/redis/redis.conf
    
    # IP绑定
    bind 127.0.0.1 10.173.0.0/16 172.17.0.0/16 192.168.0.0/16
    # 设置密码(大小写字母、数字、特殊符符号都用)
    requirepass **************
    # 保护模式(开启条件为各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
    # 禁用安全命令
    rename-command DEL      ""
    rename-command KEYS     ""
    rename-command EVAL     ""
    rename-command CONFIG   ""
    rename-command FLUSHDB  ""
    rename-command FLUSHALL ""
    rename-command SHUTDOWN ""
    
    ESC
    :wq
    
  7. 性能优化

    # 编辑/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
    
  8. 修改目录归属

    useradd -s /sbin/nologin -M redis
    chown -R redis:redis /data/redis
    chown -R redis:redis /usr/local/redis
    
  9. 启动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
    
  10. 存入数据

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

    redis-cli keys [匹配规则]|xargs redis-cli del
    
  12. 参考链接

redis集群(cluster方式)

  1. 集群前的配置(单机两服务测试6379,6380)

    # 内网IP:192.168.0.100
    
  2. 修改redis.conf

    vim /usr/local/redis/redis.conf
    
    # 启用cluster
    cluster-enabled yes
    # 调用cluster配置文件
    cluster-config-file nodes-6379.conf
    # 集群超时
    cluster-node-timeout 15000
    # 关闭宕机全停
    cluster-require-full-coverage no
    # 集群从属元素个数
    cluster-slave-validity-factor 10
    # 从机数最低迁移值(master至少有两台slave,才能做slave迁移)
    cluster-migration-barrier 1
    
    ESC
    :wq
    
  3. 配置文件添加

    yum install ruby
    gem install redis
    
    cp /usr/local/redis/redis.conf /usr/local/redis/redis.conf.backup
    cp /usr/local/redis/redis.conf /usr/local/redis/redis-6379.conf
    cp /usr/local/redis/redis.conf /usr/local/redis/redis-6380.conf
    
    mkdir /usr/local/redis/data/6379
    mkdir /usr/local/redis/data/6380
    
  4. 修改redis-6379.conf

    vim /usr/local/redis/redis-6379.conf
    
    unixsocket /usr/local/redis/data/6379/redis.sock
    pidfile /usr/local/redis/log/redis_6379.pid
    logfile /usr/local/redis/log/redis_6379.log
    dir /usr/local/redis/data/6379/
    bind 192.168.0.100
    port 6379
    
    ESC
    :wq
    
  5. 修改redis-6380.conf

    vim /usr/local/redis/redis-6380.conf
    
    unixsocket /usr/local/redis/data/6380/redis.sock
    pidfile /usr/local/redis/log/redis_6380.pid
    logfile "/usr/local/redis/log/redis_6380.log"
    dir /usr/local/redis/data/6380/
    bind 192.168.0.100
    port 6380
    
    ESC
    :wq
    
  6. 修改开机启动

    vim /etc/rc.local
    
    /usr/local/redis/bin/redis-server /usr/local/redis/redis-6379.conf
    /usr/local/redis/bin/redis-server /usr/local/redis/redis-6380.conf
    
    ESC
    :wq
    
  7. 创建cluster

    cd /root/redis/src/
    # 查看帮助
    ./redis-trib.rb help
    # 添加绑定
    ./redis-trib.rb create --replicas 1 192.168.0.100:6379 192.168.0.100:6380
    
  8. 集群测试

    /usr/local/redis/bin/redis-cli -h 192.168.0.100 -p 6379 -c
    
  9. 正常关闭

    /usr/local/redis/bin/redis-cli -h 192.168.0.100 -p 6379 shutdown
    
  10. 集群原理

    集群原理图

  11. 容错机制

    集群容错机制

Copyright © 豆包嘿嘿~ 2012-∞ 冀ICP备17033181号 all right reserved,powered by Gitbook修订: 2020-08-19 14:05:41

results matching ""

    No results matching ""