Centos7安装Redis

作者: 羋学僧 | 来源:发表于2020-11-01 18:33 被阅读0次

    Centos7安装Redis

    一、安装gcc依赖

    由于 redis 是用 C 语言开发,安装之前必先确认是否安装 gcc 环境(gcc -v),如果没有安装,执行以下命令进行安装

    yum install -y gcc
    

    二、下载并解压安装包

    wget https://github.com/antirez/redis/archive/5.0.3.tar.gz
    
    tar -zxvf 5.0.3.tar.gz
    

    三、cd切换到redis解压目录下,执行编译

    cd redis-5.0.3
    
    make
    

    四、安装并指定安装目录

    make install PREFIX=/usr/local/redis
    

    五、启动服务

    5.1前台启动

    cd /usr/local/redis/bin/
    
    ./redis-server
    

    5.2后台启动

    从 redis 的源码目录中复制 redis.confredis 的安装目录

    cp /root/redis-5.0.3/redis.conf /usr/local/redis/bin/
    

    修改 redis.conf文件,把 daemonize no 改为 daemonize yes

    vim redis.conf
    
    
    # By default Redis does not run as a daemon. Use 'yes' if you need it.
    # Note that Redis will write a pid file in /var/run/redis.pid when daemonized.
    daemonize yes
    
    

    后台启动

    ./redis-server redis.conf
    
    ps -ef | grep redis
    

    六、设置开机启动

    添加开机启动服务

    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
    
    

    注意:ExecStart配置成自己的路径

    设置开机启动

    systemctl daemon-reload
    
    systemctl start redis.service
    
    systemctl enable redis.service
    

    创建 redis 命令软链接

    ln -s /usr/local/redis/bin/redis-cli /usr/bin/redis
    

    测试 redis

    redis
    

    服务操作命令

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

    七、设置远程连接访问密码

    vim redis.conf
    

    在配置文件中配置requirepass

    requirepass pasd201314
    

    远程连接

    import redis
    
    host = '47.99.156.219'
    port = 6379
    password = "NewHopeData@123"
    r = redis.Redis(host = host, port = port,password=password)
    
    if __name__ == '__main__':
      r.set("868474045748926","{'id': 168111142, 'deviceType': 2, 'batteryLevel': 75, 'signalStrength': 21, 'sampleData': '410677', 'passNum': 0, 'alarmType': '2', 'unit': 'm', 'dataBody': None, 'time': '2020-11-04 14:17:09.0', 'offlineTime': None, 'lon': None, 'lat': None, 'address': None, 'value': '1.0677', 'sendDate': None, 'status': None, 'pressure': None, 'name': None, 'deviceName': None, 'deviceModelName': None, 'imei': '868474045748926'}")
      for key in r.keys():
          print(key.decode())
          print(r.get(key).decode())
    

    相关文章

      网友评论

        本文标题:Centos7安装Redis

        本文链接:https://www.haomeiwen.com/subject/vgluvktx.html