redis入门简介及linux下安装配置

红太狼 2022-02-16 07:28 319阅读 0赞

redis是一种基于键值对 KEY-VALUE 的nosql数据库
redis的值可以是字符串,hash, list列表, set集合, zset有序集合

一. redis特点:

  1. 速度快
    原因: 1. 所有数据存放在内存中
    单线程,避免锁竞争
    c语言实现
  2. 键值对的数据结构
  3. 稳定简单
    4.支持多种语言
  4. 支持持久化,防止数据丢失
  5. 主从复制
  6. 高可用,支持cluster

二. redis常见使用场景:

缓存
排行系统
计数(redis支持计数功能)
消息队列(虽然相比其他MQ比较菜,但是一般场景足够满足要求了)

三.版本说明:

redis版本命名采用跟linux类似:

版本号第二位如果是奇数则代表的是非稳定版本
如果是偶数则代表稳定版本

一个大的版本号,说明有非常大的改变
目前在版本列表中可以看到
3版本目前在3.2版本
4版本在4.0
5版本也是5.0
而且时间跨度不是很大,说明redis最近更新非常快

四. redis安装

redis可用通过yum进行安装,但是yum安装版本相对比较旧

  1. yum install redis

另外一种使用源码进行安装, 源码安装也很简单,建议使用该种方式
在官网上选择合适版本进行下载:

  1. http://download.redis.io/releases/

目前最新版本是5.0.4
这里选择3.2.12进行安装

  1. wget http://download.redis.io/releases/redis-3.2.13.tar.gz
  2. tar -zxvf redis-3.2.13.tar.gz
  3. ln -s redis-3.2.13 redis (建立软连接,这个在升级redis的时候可以不用考虑版本的目录影响)
  4. cd redis
  5. make

make 完成后在src目录下会有一堆执行文件:

  1. [root@dev src]# ll | grep -i redis
  2. -rw-rw-r-- 1 root root 2276 Mar 18 12:25 redisassert.h
  3. -rwxr-xr-x 1 root root 2432088 Apr 21 09:20 redis-benchmark
  4. -rw-rw-r-- 1 root root 29559 Mar 18 12:25 redis-benchmark.c
  5. -rw-r--r-- 1 root root 108440 Apr 21 09:20 redis-benchmark.o
  6. -rwxr-xr-x 1 root root 25205 Apr 21 09:20 redis-check-aof
  7. -rw-rw-r-- 1 root root 6328 Mar 18 12:25 redis-check-aof.c
  8. -rw-r--r-- 1 root root 33680 Apr 21 09:20 redis-check-aof.o
  9. -rwxr-xr-x 1 root root 5191997 Apr 21 09:20 redis-check-rdb
  10. -rw-rw-r-- 1 root root 12789 Mar 18 12:25 redis-check-rdb.c
  11. -rw-r--r-- 1 root root 54488 Apr 21 09:20 redis-check-rdb.o
  12. -rwxr-xr-x 1 root root 2585163 Apr 21 09:20 redis-cli
  13. -rw-rw-r-- 1 root root 90746 Mar 18 12:25 redis-cli.c
  14. -rw-r--r-- 1 root root 367104 Apr 21 09:20 redis-cli.o
  15. -rwxr-xr-x 1 root root 5191997 Apr 21 09:20 redis-sentinel
  16. -rwxr-xr-x 1 root root 5191997 Apr 21 09:20 redis-server
  17. -rwxrwxr-x 1 root root 60852 Mar 18 12:25 redis-trib.rb

在src目录下执行,检查版本:

  1. [root@dev src]# ./redis-cli -v
  2. redis-cli 3.2.13

安装成功


其中几个执行文件说明及作用如下:
redis-server – 启动redis服务器
redis-cli – Redis命令客户端
redis-benchmark – Redis测试工具
redis-check-aof – AOF文件修复工具
redis-check-dump – RDB文件检查工具
redis-sentinel – Sentinel服务器


redis启动方法:

  1. redis-server,按照默认配置启动
    启动中会提示一些默认参数,如下:
    端口: 6379
    进程号6809
    如果要使用配置文件,可以使用./redis-server /path/to/redis.conf 形式

    [root@dev src]# ./redis-server
    6809:C 21 Apr 09:25:51.847 # Warning: no config file specified, using the default config. In order to specify a config file use ./redis-server /path/to/redis.conf

    1. _._
    2. _.-``__ ''-._
    3. _.-`` `. `_. ''-._ Redis 3.2.13 (00000000/0) 64 bit

    .-.-```. ```\/ _.,_ ''-._ ( ' , .-` | `, ) Running in standalone mode |`-._`-...-` __...-.-.|’` .-‘| Port: 6379
    | -._. / .-‘ | PID: 6809
    -._-. `-./ .-‘ .-‘
    |`-.
    -._-..-‘ .-‘.-‘|
    | -._-. .-‘.-‘ | http://redis.io
    `-.
    -._-.
    .-‘.-‘ .-‘
    |-._-. `-.__.-‘ .-‘.-‘|
    | `-.
    -._ _.-'_.-' |-. `-.`-._.-‘.-‘ _.-‘

    1. `-._ `-.__.-' _.-'
    2. `-._ _.-'
    3. `-.__.-'

    6809:M 21 Apr 09:25:51.848 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
    6809:M 21 Apr 09:25:51.848 # Server started, Redis version 3.2.13
    6809:M 21 Apr 09:25:51.848 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add ‘vm.overcommit_memory = 1’ to /etc/sysctl.conf and then reboot or run the command ‘sysctl vm.overcommit_memory=1’ for this to take effect.
    6809:M 21 Apr 09:25:51.848 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command ‘echo never > /sys/kernel/mm/transparent_hugepage/enabled’ as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
    6809:M 21 Apr 09:25:51.848 * The server is now ready to accept connections on port 6379


  1. 参数启动:
    查看帮助

    [root@dev src]# ./redis-server -h
    Usage: ./redis-server [/path/to/redis.conf] [options]

    1. ./redis-server - (read config from stdin)
    2. ./redis-server -v or --version
    3. ./redis-server -h or --help
    4. ./redis-server --test-memory <megabytes>

    Examples:

    1. ./redis-server (run the server with default conf)
    2. ./redis-server /etc/redis/6379.conf
    3. ./redis-server --port 7777
    4. ./redis-server --port 7777 --slaveof 127.0.0.1 8888
    5. ./redis-server /etc/myredis.conf --loglevel verbose

    Sentinel mode:

    1. ./redis-server /etc/sentinel.conf --sentinel

例如:

  1. ./redis-server --port 6888
  1. 配置文件启动:redis-server configPath(配置文件)
    新建一个配置文件/root/redis.conf

    bind 127.0.0.1
    daemonize yes
    pidfile “/root/redis.pid”
    logfile “/var/log/redis/redis.log”

    ./redis-server /root/redis.conf

这里采用的是守护进程启动,所以不会有任何提示
查看进程

  1. [root@dev src]# ps -ef | grep -i redis
  2. root 35758 1 0 09:32 ? 00:00:00 ./redis-server 127.0.0.1:6379
  3. root 35764 2321 0 09:33 pts/0 00:00:00 grep --color=auto -i redis

停止redis:

  1. ./redis-cli shutdown

redis 停止最好不要使用kill -9 命令, 就算要使用一般使用kill命令即可
由于退出也没有任何提示, 可以在日志中查看:

  1. [root@dev src]# tail /var/log/redis/redis.log
  2. 35758:M 21 Apr 09:32:58.897 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
  3. 35758:M 21 Apr 09:32:58.897 # Server started, Redis version 3.2.13
  4. 35758:M 21 Apr 09:32:58.897 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
  5. 35758:M 21 Apr 09:32:58.897 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
  6. 35758:M 21 Apr 09:32:58.897 * DB loaded from disk: 0.000 seconds
  7. 35758:M 21 Apr 09:32:58.897 * The server is now ready to accept connections on port 6379
  8. 35758:M 21 Apr 09:36:16.623 # User requested shutdown...
  9. 35758:M 21 Apr 09:36:16.624 * Removing the pid file.
  10. 35758:M 21 Apr 09:36:16.624 # Redis is now ready to exit, bye bye.

检查进程:

  1. [root@dev src]# ps -ef | grep -i redis
  2. root 35770 2321 0 09:36 pts/0 00:00:00 grep --color=auto -i redis

发表评论

表情:
评论列表 (有 0 条评论,319人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Linux 安装配置heartbeat

    > Heartbeat 是一个基于Linux开源的,被广泛使用的高可用集群系统。主要包括心跳服务和资源接管两个高可用集群组件。本文简要描述了在Linux环境下安装heartbe