文档章节

单实例redis 的安装配置(下)

l
 linux-tao
发布于 2017/08/23 22:16
字数 2450
阅读 19
收藏 0
导读 Redis是一种高级key-value数据库。它跟memcached类似,不过数据可以持久化,而且支持的数据类型很丰富。在运维的工作中,缓存是一个非常重要的技术,静态文件的缓存我们有:nginx的缓存,squid的缓存等,数据库的缓存我们有redis和memcache等,甚至有些公司会选择做redis的集群。上次我们讲了redis的缓存的安装和简单的配置上,现在我们来看下redis的配置和后期的压测

设置环境变量添加redis 相关命令

Redis 的运行模式对比

Linux系统进行make后,我们其实是可以直接进入安装包的"/usr/local/src/redis-3.2.9/src"直接运行redis的,这种呢是一种非系统守护进程的模式运行的:

[root@zbbix redis-3.2.9]# ls
00-RELEASENOTES  BUGS  CONTRIBUTING  COPYING  deps  INSTALL  Makefile  MANIFESTO  README.md  redis.conf  runtest  runtest-cluster  runtest-sentinel  sentinel.conf  src  tests  utils
[root@zbbix redis-3.2.9]# pwd
/usr/local/src/redis-3.2.9
[root@zbbix redis-3.2.9]# src/redis-server 
9795:C 11 Jul 23:34:09.872 # Warning: no config file specified, using the default config. In order to specify a config file use src/redis-server /path/to/redis.conf
9795:M 11 Jul 23:34:09.875 * Increased maximum number of open files to 10032 (it was originally set to 1024).
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 3.2.9 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 9795
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

9795:M 11 Jul 23:34:09.883 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
9795:M 11 Jul 23:34:09.884 # Server started, Redis version 3.2.9
9795:M 11 Jul 23:34:09.884 # 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.
9795:M 11 Jul 23:34:09.891 # 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.
9795:M 11 Jul 23:34:09.891 * The server is now ready to accept connections on port 6379




^C9795:signal-handler (1499787252) Received SIGINT scheduling shutdown...
9795:M 11 Jul 23:34:12.419 # User requested shutdown...
9795:M 11 Jul 23:34:12.419 * Saving the final RDB snapshot before exiting.
9795:M 11 Jul 23:34:12.481 * DB saved on disk
9795:M 11 Jul 23:34:12.481 # Redis is now ready to exit, bye bye...

我们发现这种模式下我们不能退出这个界面,当我们按ctrl+c的时候,redis服务也跟着停止了。我们尝试以后台进程的形式进行:

[root@zbbix redis-3.2.9]# src/redis-server &
[1] 9798
[root@zbbix redis-3.2.9]# 9798:C 11 Jul 23:34:17.785 # Warning: no config file specified, using the default config. In order to specify a config file use src/redis-server /path/to/redis.conf
9798:M 11 Jul 23:34:17.786 * Increased maximum number of open files to 10032 (it was originally set to 1024).
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 3.2.9 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 9798
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

9798:M 11 Jul 23:34:17.788 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
9798:M 11 Jul 23:34:17.788 # Server started, Redis version 3.2.9
9798:M 11 Jul 23:34:17.788 # 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.
9798:M 11 Jul 23:34:17.788 # 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.
9798:M 11 Jul 23:34:17.789 * DB loaded from disk: 0.001 seconds
9798:M 11 Jul 23:34:17.789 * The server is now ready to accept connections on port 6379

[root@zabbix redis-3.2.9]# 
[root@zabbix redis-3.2.9]# 
[root@zabbix redis-3.2.9]# 
[root@zabbix redis-3.2.9]# 
[root@zabbix redis-3.2.9]# 
[root@zabbix redis-3.2.9]# 
[root@zabbix redis-3.2.9]# src/redis-cli 
127.0.0.1:6379> 

127.0.0.1:6379> info [section]

127.0.0.1:6379> info

# Server

redis_version:3.2.9

redis_git_sha1:00000000

redis_git_dirty:0

redis_build_id:a2c99427c4076cd4
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

后台进程是ok的,我们可以正常操作,但是对于进程的管理我们只能通过"ps -ef |grep redis |grep -v redis |awk '{print$1}' | xargs kill -9" 来进程结束redis进程,通过"/usr/local/src/redis-3.2.9/src/src/redis-server &"来启动进程,这样并不是太方便。于是有了我们安装redis并以守护进程的方式运行。由于我们制定了我们redis的安装位置,我们的redis命令的位置也没有在我们的系统变量中,我们修改系统变量。

添加redis命令的系统变量

#echo "export PATH=$PATH:/usr/local/redis/bin >>/etc/profile              \\添加环境变量
#tail -n 1 /etc/profile                                                   \\检查系统环境变量
export 	PATH=$PATH:/usr/local/redis/bin

重新加载环境变量:
#source /etc/profile                                            
检查redis命令---
# redis-
redis-benchmark  redis-check-aof  redis-check-rdb  redis-cli        redis-sentinel   redis-server  
######reidis 环境变量添加完成

redis 命令含义

redis-server:Redis服务器的daemon启动程序
redis-cli:Redis命令行操作工具。也可以用telnet根据其纯文本协议来操作
redis-benchmark:Redis性能测试工具,测试Redis在当前系统下的读写性能
redis-check-aof:数据修复
redis-check-dump:检查导出工具

Redis 启动

[root@zabbix etc]# service redis start
Starting Redis server...
[root@zabbix etc]# lsof -i:6379
COMMAND    PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
redis-ser 9625 root    4u  IPv4  49878      0t0  TCP *:6379 (LISTEN)
[root@zabbix etc]# 
[root@zabbix etc]# 
[root@zabbix etc]# ps -ef |grep redis
root       9625      1  0 23:27 ?        00:00:00 /usr/local/redis/bin/redis-server 0.0.0.0:6379                   
root       9630   4171  0 23:28 pts/1    00:00:00 grep redis

[root@zbbix redis-3.2.9]# src/redis-cli 
127.0.0.1:6379> 

127.0.0.1:6379> info [section]

127.0.0.1:6379> info

# Server

redis_version:3.2.9

redis_git_sha1:00000000

redis_git_dirty:0

redis_build_id:a2c99427c4076cd4
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
PS:如果启用了密码验证,在cli中需要先添加auth mima 然后才能操作命令

检查redis服务启动

ps -ef |grep redis
lsof -i:6379
netstat -tanlp |grep 6379

查看redis 的系统日志:

tail -f /usr/local/redis/redis.log
9766:M 11 Jul 23:32:38.320 # 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.
9766:M 11 Jul 23:32:38.320 # 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.
9766:M 11 Jul 23:32:38.321 * DB loaded from disk: 0.000 seconds
9766:M 11 Jul 23:32:38.321 * The server is now ready to accept connections on port 6379
9766:M 11 Jul 23:32:38.321 - 0 clients connected (0 slaves), 759680 bytes in use
9766:M 11 Jul 23:32:43.363 - 0 clients connected (0 slaves), 759680 bytes in use
XXXXX

redis 服务器内核调优:

法一:
a) echo vm.overcommit_memory=1 >> /etc/sysctl.conf

法二:
b) sysctl vm.overcommit_memory=1 或执行
echo vm.overcommit_memory=1 >>/proc/sys/vm/overcommit_memory

内核vm.overcommit_memory数字含义:

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

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

2,表示内核允许分配超过所有物理内存和交换空间总和的内存

reids系统压力测试工具

redis 自带有自己的压力测试工具,在redis上生产环境的时候一定要确定redis的性能。下面是使用redis-benchmark的实例(来着互联网):

Redis-benchmark使用实例:

1、redis-benchmark -h 192.168.1.201 -p 6379 -c 100 -n 100000
100个并发连接,100000个请求,检测host为localhost 端口为6379的redis服务器性能

2、redis-benchmark -h 192.168.1.201 -p 6379 -q -d 100
测试存取大小为100字节的数据包的性能

3、redis-benchmark -t set,lpush -n 100000 -q
只测试某些操作的性能

4、redis-benchmark -n 100000 -q script load "redis.call('set','foo','bar')"
只测试某些数值存取的性能

root@iZwz94wvtlc4lisg3mgkweZ:/usr/local/redis-3.2.9/utils# redis-benchmark -c 100 -n 100000
====== PING_INLINE ======
  100000 requests completed in 1.29 seconds
  100 parallel clients
  3 bytes payload
  keep alive: 1

80.86% <= 1 milliseconds
99.83% <= 2 milliseconds
99.95% <= 3 milliseconds
100.00% <= 3 milliseconds
77639.75 requests per second

====== PING_BULK ======
  100000 requests completed in 1.33 seconds
  100 parallel clients
  3 bytes payload
  keep alive: 1

79.37% <= 1 milliseconds
99.84% <= 2 milliseconds
99.93% <= 3 milliseconds
100.00% <= 3 milliseconds
75414.78 requests per second

====== SET ======
  100000 requests completed in 1.28 seconds
  100 parallel clients
  3 bytes payload
  keep alive: 1

81.20% <= 1 milliseconds
99.85% <= 2 milliseconds
99.92% <= 3 milliseconds
100.00% <= 3 milliseconds
77942.32 requests per second

====== GET ======
  100000 requests completed in 1.29 seconds
  100 parallel clients
  3 bytes payload
  keep alive: 1

80.94% <= 1 milliseconds
99.90% <= 2 milliseconds
99.99% <= 3 milliseconds
100.00% <= 3 milliseconds
77459.34 requests per second

====== INCR ======
  100000 requests completed in 1.28 seconds
  100 parallel clients
  3 bytes payload
  keep alive: 1

81.77% <= 1 milliseconds
99.79% <= 2 milliseconds
99.92% <= 3 milliseconds
99.97% <= 4 milliseconds
100.00% <= 4 milliseconds
78308.54 requests per second
xxxxxxxxxxxxxxxx

我们看到70000 多的峰值    5s左右

检查redis重启动脚本

经过以上的操作我们的redis服务应该已经配置差不多了,但是我们要有一个检测机制来确定redis服务的正常运行。在这里呢我们写了一个脚本来检测我们的redis服务,如果服务出现问题,我们则重启redis服务。(不建议,建议使用监控系统了预警redis服务!!!)

检测脚本内容

cat check-redis-restart.sh

#!/bin/sh
#this is check redis and restart redis 

lsof -i:6379 |grep LISTEN >> /tmp/reids-status.log

if [ $? -eq 1 ]; then 
	echo "`date` redis is down,restart it " >> /usr/local/redis/redis-check-error.log
	rm -rf /var/run/redis.pid >> /usr/local/redis/redis-check-error.log
	/etc/init.d/redis stop >> /usr/local/redis/redis-check-error.log
	/etc/init.d/redis start >> /usr/local/redis/redis-check-error.log
fi

将脚本添加到crontab任务中1分钟执行一次

crontab -e
* * * * * cd /usr/local/redis/bin/ && ./check-redis-restart.sh >> /usr/local/redis/edis-check-error.log 2>&1

重启crontab服务:

service cron restart
chkconfig cron on

检查重启redis日志:

tail -f /tmp/reids-status.log
tail -f /usr/local/redis/redis-check-error.log

本文原创地址:http://www.linuxprobe.com/redis-install-conf2.html

© 著作权归作者所有

共有 人打赏支持
l
粉丝 17
博文 756
码字总数 851228
作品 0
深圳
运维
私信 提问
redis 一二事 - 搭建集群缓存服务器

在如今并发的环境下,对大数据量的查询采用缓存是最好不过的了,本文使用redis搭建集群 (个人喜欢redis,对memcache不感冒) redis是3.0后增加的集群功能,非常强大 集群中应该至少有三个节...

风间影月
2016/04/28
0
0
Redis Cluster(集群)的搭建

Redis Cluster(集群)的搭建 一、Redis的下载、安装、启动(单实例) 我们统一将Redis安装在/opt目录下,执行命令如下: 安装完成,下面我们启动Redis服务: Redis启动成功,将采用Redis的默...

Java同学会
06/05
0
0
初学乍练redis:使用redis-migrate-tool做redis在线数据迁移

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wzy0623/article/details/84138537 目录 一、问题提出 二、关于redis-migrate-tool 三、实验 1. 建议集群red...

wzy0623
11/16
0
0
linux--redis的安装和配置和开启多个端口

在workerman开发过程中需要安装redis来存储用户ip、端口等信息 首先UBUNTU中安装redis: apt-update //更新apt包源apt-get install redis-server //安装redis-server 安装完毕后可以直接启动...

李有常
2015/09/29
5.4K
0
Redis探索之旅(4)- 在Linux环境下搭建单实例Redis服务实例

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/andamajing/article/details/52425098 在前面的两篇文章 《Redis探索之旅(2)- 在Windows下搭建单实例Redis...

DreamMakers
2016/09/03
0
0

没有更多内容

加载失败,请刷新页面

加载更多

docker搞个wordpress

1.先把wordpress的镜像下载下来 docker pull wordpress 2.下载mysql docker pull mysql:lastest 3.启动mysql docker run --name blog -e root -d mysql:5.7 docker run --name some-mysql -e......

无极之岚
6分钟前
0
0
【宇润日常疯测-005】PHP 中的 clone 和 new 性能比较

clone和new本不应该放在一起比较,它们的作用是不同的。但可能有一些场景下,可以用clone也可以用new,那么这时候我们选哪个呢? 我编写了两个测试,第一个是声明一个空类,第二个是带构造方...

宇润
7分钟前
0
1
点击按钮弹出类似IOS 底部 dialog

implementation 'com.baoyz.actionsheet:library:1.1.7' 然后设置按钮点击监听,,调用下列代码即可 ActionSheet.createBuilder(this, getSupportFragmentManager()) ......

lanyu96
10分钟前
1
0
专访阿里云专有云马劲,一个理性的理想主义者

“我的故事都是和团队技术相关的,自己还真没有什么引人入胜的故事。”当马劲被问到能不能多分享些个人经历故事时他笑着说,我们就干脆怀着好奇聊了聊他和阿里云专有云一路走来的故事。 马劲...

阿里云官方博客
42分钟前
1
0
java环形缓冲区

import java.util.ArrayList;import java.util.List;/** * * 环形缓冲区<br/> * 一. 写数据:<br/> * 1. push: 当数据已写满时返回false,否则可以正常写入返回true<br/>......

whoisliang
今天
2
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部