1 redis介绍


         redis是一个内存数据库,属于NOSQL的类型,可以用来做高速缓存服务器,也可以直接使用redis作为存储数据库,将内存中的数据写入到磁盘中。

运维环境搭建之redis简介_java


2redis的安装


         redis的安装很简单,由于redis默认的监听的端口为6379,从而无需使用root用户进行安装,具体步骤如下:

[root@RHANCL opt]# useradd redisuser创建系统用户redisuser

[root@RHANCL opt]# echo redisuser|passwd --stdinredisuser

Changing password for user redisuser.

passwd: all authentication tokens updatedsuccessfully.

[root@RHANCL opt]# ls -l

total 1252

-rw-r--r--. 1 root root 1277871 Mar 21 14:48redis-2.8.24.tar.gz

[root@RHANCL opt]# tar -xf redis-2.8.24.tar.gz

[root@RHANCL opt]# chown -R redisuser.redisuserredis-2.8.24

[root@RHANCL opt]# ls -l

total 1256

drwxrwxr-x. 6 redisuser redisuser    4096 Dec 18 2015 redis-2.8.24

-rw-r--r--. 1 root      root     1277871 Mar 21 14:48 redis-2.8.24.tar.gz

[root@RHANCL opt]# mkdir redis

[root@RHANCL opt]# chown redisuser.redisuser redis

[root@RHANCL opt]# ls -l

total 1260

drwxr-xr-x. 2 redisuser redisuser    4096 Mar 23 01:42 redis

drwxrwxr-x. 6 redisuser redisuser    4096 Dec 18 2015 redis-2.8.24

-rw-r--r--. 1 root      root     1277871 Mar 21 14:48 redis-2.8.24.tar.gz

[root@RHANCL opt]# su - redisuser切换到redisuser进行安装redis

[redisuser@RHANCL ~]$ cd /opt/

[redisuser@RHANCL opt]$ cd redis-2.8.24

[redisuser@RHANCL redis-2.8.24]$ makePREFIX=/opt/redis/ install将安装的二进制文件放在指定的目录中

[redisuser@RHANCL redis-2.8.24]$ cp redis.conf/opt/redis/复制redis的配置文件

[redisuser@RHANCL redis-2.8.24]$ cd /opt/redis

[redisuser@RHANCL redis]$ tree安装的二进制文件,检查是否安装成功

.

|-- bin

|   |--redis-benchmark

|   |--redis-check-aof

|   |--redis-check-dump

|   |--redis-cli

|   |--redis-sentinel -> redis-server

|   `--redis-server

`-- redis.conf

 

1 directory, 7 files

[redisuser@RHANCL redis]$ ls -l

total 40

drwxrwxr-x. 2 redisuser redisuser  4096 Mar 23 01:46 bin

-rw-rw-r--. 1 redisuser redisuser 36298 Mar 23 01:47redis.conf

[redisuser@RHANCL redis]$ cd bin

[redisuser@RHANCL bin]$ /opt/redis/bin/redis-server/opt/redis/redis.conf 启动redis

[23764] 23 Mar 01:48:40.524 # You requestedmaxclients of 10000 requiring at least 10032 max file descriptors.

[23764] 23 Mar 01:48:40.525 # Redis can't set maximum open files to 10032 because of OS error: Operationnot permitted.

[23764] 23 Mar 01:48:40.525 # Current maximum openfiles is 1024. maxclients has been reduced to 992 to compensate for low ulimit.If you need higher maxclients increase 'ulimit -n'.

               _._                                                 

          _.-``__ ''-._                                            

     _.-``    `.  `_. ''-._           Redis 2.8.24(00000000/0) 64 bit

  .-``.-```.  ```\/    _.,_ ''-._                                  

 (    '     ,       .-`  | `,   )     Running in stand alone mode

 |`-._`-...-`__...-.``-._|'` _.-'|     Port: 6379

 |    `-._  `._    /     _.-'   |     PID: 23764

  `-._    `-._ `-./  _.-'    _.-'                                   

 |`-._`-._    `-.__.-'   _.-'_.-'|                                 

 |    `-._`-._        _.-'_.-'    |          http://redis.io       

  `-._    `-._`-.__.-'_.-'    _.-'                                  

 |`-._`-._    `-.__.-'    _.-'_.-'|                                 

 |    `-._`-._        _.-'_.-'    |                                 

  `-._    `-._`-.__.-'_.-'    _.-'                                  

      `-._    `-.__.-'   _.-'                                      

         `-._        _.-'                                          

             `-.__.-'                                              

 

[23764] 23 Mar 01:48:40.550 # Server started, Redisversion 2.8.24

[23764] 23 Mar 01:48:40.552 # WARNINGovercommit_memory is set to 0! Background save may fail underlow 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.

[23764] 23 Mar 01:48:40.555 # WARNING: The TCPbacklog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconnis set to the lower value of 128.

[23764] 23 Mar 01:48:40.555 * The server is now readyto accept connections on port 6379

[redisuser@RHANCL ~]$ netstat -tnlp|grep redis查看redis监听的端口,默认监听端口为6379

(Not all processes could be identified, non-ownedprocess info

 will not beshown, you would have to be root to see it all.)

tcp       0      0 0.0.0.0:6379                0.0.0.0:*                   LISTEN      23764/redis-server 

tcp       0      0 :::6379                     :::*                        LISTEN      23764/redis-server


2.1 告警之打开的文件数

         在启动redis的时候,会有一个告警信息,表示进程打开的文件数量的限制,从而修改limit的限制,同时修改能启动的进程数量,如下所示:

[root@RHANCL ~]# rm -rf /etc/security/limits.d/90-nproc.conf删除默认的用户打开的最大进程数量,默认的数量为1024

[root@RHANCL ~]# vim /etc/security/limits.conf

[root@RHANCL ~]# tail -5 /etc/security/limits.conf 增加如下内容,从而打开的文件数量为51200,打开的进程数量为102400

*       soft  nproc                 10240

*       hard nproc                 10240

*       soft  nofile                  51200

*       hard nofile                  51200

[root@RHANCL ~]# ulimit -a重启生效,或者使用ulimit直接设置

core file size          (blocks, -c) 0

data seg size           (kbytes, -d) unlimited

scheduling priority             (-e) 0

file size               (blocks, -f) 51200

pending signals                 (-i) 7824

max locked memory       (kbytes, -l) 64

max memory size         (kbytes, -m) unlimited

open files                      (-n) 51200

pipe size           (512 bytes, -p) 8

POSIX message queues     (bytes, -q) 819200

real-time priority              (-r) 0

stack size              (kbytes, -s) 10240

cpu time              (seconds, -t) unlimited

max user processes              (-u) 10240

virtual memory          (kbytes, -v) unlimited

file locks                      (-x) unlimited


2.2 告警之保存数据可能失败

         在进行使用redis的时候,可以将内存中的数据保存在磁盘上,但是当参数overcommit设置为0时,可能导致保存数据失败,主要影响为内存的分配策略,从而修改一下配置(1表示 表示内核允许分配所有的物理内存,而不管当前的内存状态如何):

[root@RHANCL ~]# echo "vm.overcommit_memory =1">> /etc/sysctl.conf 添加申请内存分配机制

[root@RHANCL ~]# tail -1 /etc/sysctl.conf

vm.overcommit_memory = 1

[root@RHANCL ~]# sysctl -p让修改直接生效,无需重启


2.3 告警之TCP监听连接数过小

         在进行连接redis的时候,tcp监听的数量取决于somaxconn的数量,主要用来解决慢客户端连接的问题,从而可修改配置如下:

[root@RHANCL ~]# echo"net.core.somaxconn=65535" >>/etc/sysctl.conf 修改默认情况下tcp监听连接数量

[root@RHANCL ~]# sysctl -p让修改直接生效,无需重启


2.4 其他内核配置

         其他内核配置主要是优化IPv4下的TCP连接,如下所示:

[root@RHANCL ~]# tail -5 /etc/sysctl.conf

net.ipv4.tcp_fin_timeout=30

net.ipv4.tcp_keepalive_time=300

net.ipv4.tcp_tw_reuse=1

net.ipv4.tcp_tw_recycle=1

net.ipv4.ip_local_port_range=5000 65000

 

2.5 redis重新启动

         修改上述配置之后,重新启动如下所示:

[redisuser@RHANCL bin]$ ./redis-server./../redis.conf 不要使用这种方式启动redis

               _._                                                 

          _.-``__ ''-._                                            

     _.-``    `.  `_. ''-._           Redis 2.8.24(00000000/0) 64 bit

  .-``.-```.  ```\/    _.,_ ''-._                                   

 (    '     ,       .-`  | `,   )     Running in stand alone mode

 |`-._`-...-`__...-.``-._|'` _.-'|     Port: 6379

 |    `-._  `._    /     _.-'   |     PID: 1418

  `-._    `-._ `-./  _.-'    _.-'                                   

 |`-._`-._    `-.__.-'   _.-'_.-'|                                 

 |    `-._`-._        _.-'_.-'    |          http://redis.io       

  `-._    `-._`-.__.-'_.-'    _.-'                                  

 |`-._`-._    `-.__.-'   _.-'_.-'|                                  

 |    `-._`-._        _.-'_.-'    |                                 

  `-._    `-._`-.__.-'_.-'    _.-'                                  

      `-._    `-.__.-'   _.-'                                      

          `-._        _.-'                                          

             `-.__.-'                                              

 

[1418] 23 Mar 03:36:10.874 # Server started, Redisversion 2.8.24

[1418] 23 Mar 03:36:10.877 * DB loaded from disk:0.000 seconds

[1418] 23 Mar 03:36:10.877 * The server is now readyto accept connections on port 6379

         可以看到相关的告警均消失。

         在使用如上的方式启动redis的坏处是,无法找到redis的安装路径,如下所示:

[root@RHANCL ~]# ps -ef|grep redis无法找到redis的启动路径

501       1418 1000  0 03:36 pts/0    00:00:03./redis-server *:6379  

         最好是使用如下的方式启动:

[redisuser@RHANCL ~]$ /opt/redis/bin/redis-server/opt/redis/redis.conf使用绝对路径启动redis

[redisuser@RHANCL ~]$ ps -ef|grep redis便于查看redis的安装路径

501      1539  1515  0 03:55 pts/3    00:00:00 /opt/redis/bin/redis-server *:6379

 

3 redis基本配置及命令


3.1 运行的时候后台运行

         在上面的启动中,redis都是在前台进行运行,从而修改配置文件的参数项如下:

daemonize no

         修改为:

daemonizeyes

         即可在启动的时候后台启动。


3.2 超时时间

         可以修改客户端的连接超时时间,如下:

timeout 0 (默认是直接断开连接)

         修改为:

timeout180(超时时间为180秒)


3.3 内存设置

         一般最大内存设置为内存的60%,从而让内存不会爆满,导致OOM,并且修改内存分配策略,如下所示:

# maxmemory <bytes>

# maxmemory-policy volatile-lru根据LRU算法来移除过期的key

         修改为如下:

maxmemory612482000

maxmemory-policynoeviction表示永不过期


3.4 设置日志文件

         设置日志文件路径如下:

logfile/opt/redis/redis.log


3.5 常用命令使用

         基本的使用命令如下所示:

[redisuser@RHANCL redis]$ ./bin/redis-cli 进入客户端

127.0.0.1:6379> set name kel 设置键value的字符串

OK

127.0.0.1:6379> expire name 1000设置过期时间

(integer) 1

127.0.0.1:6379> dbsize查看键的数量

(integer) 1

127.0.0.1:6379> flushall清空所有的数据库

OK

127.0.0.1:6379> flushdb清空当前所有的数据库

OK

127.0.0.1:6379> ping查看连接是否存活

PONG

127.0.0.1:6379> shutdown存储数据到磁盘并关闭数据库

not connected>quit


3.6 内存设置错误出错

         当内存大小设置错误的时候,会出现如下错误:

OOM command not allowedwhen used memory > 'maxmemory'

         修改配置文件中的maxmemory的值即可。