Redis基础(一)--安装与简单配置

一、Redis简介

Redis 是开源免费的,遵守BSD协议,是一个高性能的key-value非关系型数据库。

redis单线程问题
所谓的单线程指的是网络请求模块使用了一个线程(所以不需考虑并发安全性),即一个线程处理所有网络请求,其他模块仍用了多个线程。

redis采用多路复用机制:即多个网络socket复用一个io线程,实际是单个线程通过记录跟踪每一个Sock(I/O流)的状态来同时管理多个I/O流.

Redis特点:
Redis支持数据的持久化,可以将内存中的数据保存在磁盘中,重启的时候可以再次加载进行使用。

Redis不仅仅支持简单的key-value类型的数据,同时还提供String,list,set,zset,hash等数据结构的存储。

Redis支持数据的备份,即master-slave模式的数据备份。

性能极高 – Redis能读的速度是110000次/s,写的速度是81000次/s 。

原子 – Redis的所有操作都是原子性的,同时Redis还支持对几个操作全并后的原子性执行。

丰富的特性 – Redis还支持 publish/subscribe, 通知, 设置key有效期等等特性。

redis作用:
可以减轻数据库压力,查询内存比查询数据库效率高。

Redis典型应用场景:

session共享:常见于web集群中的tomcat或者PHP中多web服务器session共享

消息队列:ELK的日志缓存、部分业务的订阅发布系统

计数器:访问排行榜、商品浏览数等和次数相关的数值通知场景

缓存:数据库缓存查询、电商网站商品信息、新闻内容。

微博/微信社交场合:评论点赞等

二、安装

redis版本信息

[root@centos7 ~]# yum info redis
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
Available Packages
Name        : redis
Arch        : x86_64
Version     : 3.2.12
Release     : 2.el7
Size        : 544 k
Repo        : epel
Summary     : A persistent key-value database
URL         : http://redis.io
License     : BSD
Description : Redis is an advanced key-value store. It is often referred to as a data
            : structure server since keys can contain strings, hashes, lists, sets and
            : sorted sets.
            : 
            : You can run atomic operations on these types, like appending to a string;
            : incrementing the value in a hash; pushing to a list; computing set
            : intersection, union and difference; or getting the member with highest
            : ranking in a sorted set.
            : 
            : In order to achieve its outstanding performance, Redis works with an
            : in-memory dataset. Depending on your use case, you can persist it either
            : by dumping the dataset to disk every once in a while, or by appending
            : each command to a log.
            : 
            : Redis also supports trivial-to-setup master-slave replication, with very
            : fast non-blocking first synchronization, auto-reconnection on net split
            : and so forth.
            : 
            : Other features include Transactions, Pub/Sub, Lua scripting, Keys with a
            : limited time-to-live, and configuration settings to make Redis behave like
            : a cache.
            : 
            : You can use Redis from most programming languages also.

使用epel源上的安装包,yum方式安装

[root@centos7 ~]# yum install redis -y
[root@centos7 ~]# systemctl start redis
##默认只监听本机的127.0.0.1
[root@centos7 ~]# ss -ntl
State      Recv-Q Send-Q                                    Local Address:Port                                                   Peer Address:Port              
LISTEN     0      128                                           127.0.0.1:6379                                                              *:*                  
LISTEN     0      128                                                   *:22                                                                *:*                  
LISTEN     0      100                                           127.0.0.1:25                                                                *:*                  
LISTEN     0      128                                                  :::22                                                               :::*                  
LISTEN     0      100                                                 ::1:25                                                               :::*                  

启动redis客户端,进入交互端口

[root@centos7 ~]# redis-cli
127.0.0.1:6379> set tom jerry  ##设定键值对tom:jerry,由于没有设定过期时间默认永不过期

OK
127.0.0.1:6379> get tom
"jerry"
127.0.0.1:6379> 

源码编译安装
到官网下载源码包,这里下载的是redis-4.0.14.tar.gz
在这里插入图片描述
在这里插入图片描述
放在/data/src/目录下

[root@centos7 src]# pwd
/data/src
[root@centos7 src]# ls
redis-4.0.14.tar.gz

解压到/usr/local/

[root@centos7 src]# tar xvf redis-4.0.14.tar.gz -C /usr/local/src
redis-4.0.14/
redis-4.0.14/.gitignore
redis-4.0.14/00-RELEASENOTES
redis-4.0.14/BUGS
redis-4.0.14/CONTRIBUTING
redis-4.0.14/COPYING
redis-4.0.14/INSTALL
redis-4.0.14/MANIFESTO
redis-4.0.14/Makefile
redis-4.0.14/README.md
redis-4.0.14/deps/
redis-4.0.14/deps/Makefile
redis-4.0.14/deps/README.md
redis-4.0.14/deps/hiredis/
......

进入/usr/local/src/redis-4.0.14目录下编译安装
在这里插入图片描述

[root@centos7 ~]# cd /usr/local/src/redis-4.0.14
[root@centos7 redis-4.0.14]# ls
00-RELEASENOTES  CONTRIBUTING  deps     Makefile   README.md   runtest          runtest-sentinel  src    utils
BUGS             COPYING       INSTALL  MANIFESTO  redis.conf  runtest-cluster  sentinel.conf     tests

##安装目录放在/apps/redis目录下
[root@centos7 redis-4.0.14]# make PREFIX=/apps/redis install

出现报错:
在这里插入图片描述
原因分析

在README 有这样一段话。

Allocator
---------

Selecting a non-default memory allocator when building Redis is done by setting
the `MALLOC` environment variable. Redis is compiled and linked against libc
malloc by default, with the exception of jemalloc being the default on Linux
systems. This default was picked because jemalloc has proven to have fewer
fragmentation problems than libc malloc.

To force compiling against libc malloc, use:

    % make MALLOC=libc

To compile against jemalloc on Mac OS X systems, use:

    % make MALLOC=jemalloc

Verbose build
-------------

通过设置MALLOC环境变量,可以在构建Redis时选择非默认的内存分配器。redis在linux上编译链接时,默认是使用jemalloc分配器,并不是libc 。因为jemalloc被证明解决fragmentation problems(内存碎片化问题)比libc更好。

Redis 2.4版本之后,默认使用jemalloc来做内存管理。但是如果你又没有jemalloc而只有libc,当make出错时,使用libc就可以了。

make MALLOC=libc

如果想用jemalloc,安装jemalloc即可。

如果使用yum安装的话需要安装EPEL源。

[root@centos7 redis-4.0.14]# yum install jemalloc

也可以编译安装,先下载jemalloc:这里使用的是编译安装

https://github.com/jemalloc/jemalloc/releases/download/4.2.1/jemalloc-4.2.1.tar.bz2

[root@centos7 src]# tar xvf jemalloc-4.2.1.tar.bz2 
[root@centos7 src]# cd jemalloc-4.2.1
[root@centos7 jemalloc-4.2.1]# ls
autogen.sh  build-aux  config.stamp.in  configure.ac  coverage.sh  include  jemalloc.pc.in  msvc    src   VERSION
bin         ChangeLog  configure        COPYING       doc          INSTALL  Makefile.in     README  test

[root@centos7 jemalloc-4.2.1]# ./configure --prefix=/usr/local/jemalloc
[root@centos7 jemalloc-4.2.1]# make && make install
[root@centos7 jemalloc-4.2.1]#  ll /usr/local/jemalloc/
total 0
drwxr-xr-x 2 root root  62 Nov  3 16:57 bin
drwxr-xr-x 3 root root  22 Nov  3 16:57 include
drwxr-xr-x 3 root root 115 Nov  3 16:57 lib
drwxr-xr-x 4 root root  28 Nov  3 16:57 share

然后再编译redis的时候指定MALLOC,如下:

[root@centos7 redis-4.0.14]# make MALLOC=/usr/local/jemalloc/lib PREFIX=/apps/redis install

当Redis进程跑起来之后,在你的实例中使用info命令可以查看你所使用的内存管理器。

mem_allocator:jemalloc-4.2.1

如果你使用的是libc,那么mem_allocator的参数就会是libc。

创建配置文件目录、日志目录、数据目录及进出的pid目录

[root@centos7 apps]# mkdir -v /apps/redis/{etc,logs,data,run}
mkdir: created directory ‘/apps/redis/etc’
mkdir: created directory ‘/apps/redis/logs’
mkdir: created directory ‘/apps/redis/data’
mkdir: created directory ‘/apps/redis/run’

[root@centos7 apps]# ls
redis
[root@centos7 apps]# ll redis/bin/
total 11552
-rwxr-xr-x 1 root root  353984 Nov  3 17:22 redis-benchmark
-rwxr-xr-x 1 root root 3649160 Nov  3 17:22 redis-check-aof
-rwxr-xr-x 1 root root 3649160 Nov  3 17:22 redis-check-rdb
-rwxr-xr-x 1 root root  520272 Nov  3 17:22 redis-cli
lrwxrwxrwx 1 root root      12 Nov  3 17:22 redis-sentinel -> redis-server
-rwxr-xr-x 1 root root 3649160 Nov  3 17:22 redis-server
[root@centos7 apps]# tree redis/
redis/
├── bin
│   ├── redis-benchmark
│   ├── redis-check-aof
│   ├── redis-check-rdb
│   ├── redis-cli
│   ├── redis-sentinel -> redis-server
│   └── redis-server
├── data
├── etc
├── logs
└── run

到redis的源码存放目录中把模板配置文件复制到/apps/redis/etc/目录下

[root@centos7 redis-4.0.14]# cp redis.conf /apps/redis/etc/

测试启动redis

##查看使用帮助
[root@centos7 apps]# /apps/redis/bin/redis-server --help
Usage: ./redis-server [/path/to/redis.conf] [options]
       ./redis-server - (read config from stdin)
       ./redis-server -v or --version
       ./redis-server -h or --help
       ./redis-server --test-memory <megabytes>

Examples:
       ./redis-server (run the server with default conf)
       ./redis-server /etc/redis/6379.conf
       ./redis-server --port 7777
       ./redis-server --port 7777 --slaveof 127.0.0.1 8888
       ./redis-server /etc/myredis.conf --loglevel verbose

Sentinel mode:
       ./redis-server /etc/sentinel.conf --sentinel

[root@centos7 apps]# /apps/redis/bin/redis-server /apps/redis/etc/redis.conf 
9981:C 03 Nov 17:41:29.648 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
9981:C 03 Nov 17:41:29.648 # Redis version=4.0.14, bits=64, commit=00000000, modified=0, pid=9981, just started
9981:C 03 Nov 17:41:29.648 # Configuration loaded
9981:M 03 Nov 17:41:29.648 * Increased maximum number of open files to 10032 (it was originally set to 1024).
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 4.0.14 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 9981
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

9981:M 03 Nov 17:41:29.649 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
9981:M 03 Nov 17:41:29.649 # Server initialized
9981:M 03 Nov 17:41:29.649 # 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.
9981:M 03 Nov 17:41:29.650 # 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.
9981:M 03 Nov 17:41:29.650 * Ready to accept connections

端口监听情况

[root@centos7 src]# ss -ntl | grep 6379
LISTEN     0      128    127.0.0.1:6379                     *:*                  

客户端访问测试

[root@centos7 src]# /apps/redis/bin/redis-cli 
127.0.0.1:6379> set jack ross
OK
127.0.0.1:6379> get jack
"ross"
127.0.0.1:6379> 

现在redis是可以正常使用了,但是在启动redis服务时有警告信息,因此我们只是简单的初始化没有做过太多配置,但是后期如果数据量增大,有可能出现问题,最好处理一下。
在这里插入图片描述
根据上面的报警提示信息
第一个警告:
由于/ proc / sys / net / core / somaxconn设置为较低的值,因此修改相应的内核参数
backlog参数控制的是三次握手的时候server端接收到client ack确认号之后的队列值。

[root@centos7 src]# vim /etc/sysctl.conf 

net.core.somaxconn = 512
~     

[root@centos7 src]# sysctl -p
net.core.somaxconn = 512
                       

第二个警告:
overcommit_memory设置为0! 在内存不足的情况下,后台保存可能会失败。 要解决此问题,请在/etc/sysctl.conf中添加’vm.overcommit_memory = 1’,然后重新引导或运行命令’sysctl vm.overcommit_memory = 1’,以使其生效。

0: 表示内核将检查是否有足够的可用内存供应用进程使用;如果有足够的可用内存,内存允许申请;否则,内存申请失败,并把错误返回给应用进程。

1: 表示内核允许分配所有物理内存,而不管当前的内存状态如何。

2: 表示内核允许分配

[root@centos7 system]# vim /etc/sysctl.conf
vm.overcommit_memory = 1
[root@centos7 system]# sysctl -p
net.ipv4.ip_forward = 0
net.core.somaxconn = 512
vm.overcommit_memory = 1

第三个警告:
大页内存动态分配,需要关闭让redis负责管理内存(一个大叶内存2M)

 echo never > /sys/kernel/mm/transparent_hugepage/enabled

vim /etc/rc.local
echo never > /sys/kernel/mm/transparent_hugepage/enabled
chmod a+x /etc/rc.local

再次启动Redis
在这里插入图片描述

编辑redis服务启动脚本(借鉴yum安装的edis.service脚本)

[root@centos7 system]# cat /usr/lib/systemd/system/redis.service 
[Unit]
Description=Redis persistent key-value database
After=network.target
After=network-online.target
Wants=network-online.target

[Service]
#ExecStart=/usr/bin/redis-server /etc/redis.conf --supervised systemd
ExecStart=/apps/redis/bin/redis-server /apps/redis/etc/redis.conf --supervised systemd
#ExecStop=/usr/libexec/redis-shutdown
ExecReload=/bin/kill -s HUP $MAINPID  ##向redis服务的主进程发送HUP信号
ExecStop=/bin/kill -s QUIT $MAINPID   ##向redis服务的主进程发送QUIT信号
Type=notify
User=redis
Group=redis
RuntimeDirectory=redis
RuntimeDirectoryMode=0755

[Install]
WantedBy=multi-user.target

创建redis用户和用户组

[root@centos7 system]# useradd redis -s /sbin/nologin 
[root@centos7 system]# id redis
uid=1002(redis) gid=1002(redis) groups=1002(redis)

修改安装目录的权限属主

[root@centos7 redis]# chown -R redis.redis /apps/redis/
[root@centos7 redis]# ll /apps/redis/
total 0
drwxr-xr-x 2 redis redis 134 Nov  3 17:22 bin
drwxr-xr-x 2 redis redis   6 Nov  3 17:24 data
drwxr-xr-x 2 redis redis  24 Nov  3 17:31 etc
drwxr-xr-x 2 redis redis   6 Nov  3 17:24 logs
drwxr-xr-x 2 redis redis   6 Nov  3 17:24 run

编辑redis服务启动脚本后,执行systemctl daemon-reload

[root@centos7 system]#systemctl daemon-reload

测试服务脚本是否能正常启动、关闭、重启redis服务

##测试启动、关闭
[root@centos7 redis]# systemctl start redis
[root@centos7 redis]# ss -ntl | grep 6379
LISTEN     0      511    127.0.0.1:6379                     *:*                  
[root@centos7 redis]# systemctl stop redis
[root@centos7 redis]# ss -ntl | grep 6379
##测试重启服务
##查看重启之前的进程编号
[root@centos7 redis]# ps aux | grep redis
redis      1974  0.0  0.1 141468  2180 ?        Ssl  17:27   0:00 /apps/redis/bin/redis-server 127.0.0.1:6379
##执行重启服务后再次查看进程编号,发现进程编号变了
[root@centos7 redis]# systemctl restart redis
[root@centos7 redis]# ps aux | grep redis
redis      1990  0.0  0.1 141468  2184 ?        Ssl  17:28   0:00 /apps/redis/bin/redis-server 127.0.0.1:6379

客户端连接
redis/apps/redis/bin/redis-cli -h IP/HOSTNAME -p PORT -a PASSWORD

[root@centos7 system]# /apps/redis/bin/redis-cli 
127.0.0.1:6379> set tom terry  EX 30
OK
127.0.0.1:6379> get tom
"terry"
127.0.0.1:6379> 

因为是编译安装,redis的命令二进制程序放在/apps/redis/bin目录下,为了方便使用需要把/apps/redis/bin写进环境变量中,或者软连接指向/usr/bin。

写进环境变量方式

[root@centos7 redis]# vim /etc/profile.d/redis_path.sh 
PATH=$PATH:/apps/redis/bin
export PATH
[root@centos7 redis]# chmod +x /etc/profile.d/redis_path.sh
[root@centos7 redis]# source /etc/profile.d/redis_path.sh

创建命令软连接方式

[root@centos7 redis]# ln -sv /apps/redis/bin/redis-* /usr/bin/
‘/usr/bin/redis-benchmark’ -> ‘/apps/redis/bin/redis-benchmark’
‘/usr/bin/redis-check-aof’ -> ‘/apps/redis/bin/redis-check-aof’
‘/usr/bin/redis-check-rdb’ -> ‘/apps/redis/bin/redis-check-rdb’
‘/usr/bin/redis-cli’ -> ‘/apps/redis/bin/redis-cli’
‘/usr/bin/redis-sentinel’ -> ‘/apps/redis/bin/redis-sentinel’
‘/usr/bin/redis-server’ -> ‘/apps/redis/bin/redis-server’

至此redis安装与简单配置完成。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值