文档章节

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

l
 linux-tao
发布于 2017/08/23 22:16
字数 2450
阅读 18
收藏 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
粉丝 13
博文 662
码字总数 745476
作品 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 Cluster(集群)的搭建

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

Java同学会
06/05
0
0
redis-cluster分布式集群安装部署

一.服务器环境 1.1环境说明 此次演示是3台内网机器,开启6个redis单实例。其中每台机器上按照不同的端redis服务端口开启2个redis实例 环境:机器的内网IP地址: 3台服务器事前先关闭iptable...

wjw555
07/07
0
0
redis 一二事 - 搭建集群缓存服务器

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

风间影月
2016/04/28
0
0
Redis总结(三)Redis 的主从复制

  接着上一篇,前面两篇我总结了《Redis总结(一)Redis安装》和《Redis总结(二)C#中如何使用redis》 所以这一篇,会讲讲Redis 的主从复制以及C#中如何调用。   Redis跟MySQL一样,拥有...

章为忠
2015/11/20
0
0

没有更多内容

加载失败,请刷新页面

加载更多

OSX | SafariBookmarksSyncAgent意外退出解决方法

1. 启动系统, 按住⌘-R不松手2. 在实用工具(Utilities)下打开终端,输入csrutil disable, 然后回车; 你就看到提示系统完整性保护(SIP: System Integrity Protection)已禁用3. 输入reboot回车...

云迹
今天
4
0
面向对象类之间的关系

面向对象类之间的关系:is-a、has-a、use-a is-a关系也叫继承或泛化,比如大雁和鸟类之间的关系就是继承。 has-a关系称为关联关系,例如企鹅在气候寒冷的地方生活,“企鹅”和“气候”就是关...

gackey
今天
4
0
读书(附电子书)|小狗钱钱之白色的拉布拉多

关注公众号,在公众号中回复“小狗钱钱”可免费获得电子书。 一、背景 之前写了一篇文章 《小狗钱钱》 理财小白应该读的一本书,那时候我才看那本书,现在看了一大半了,发现这本书确实不错,...

tiankonguse
今天
4
0
Permissions 0777 for ‘***’ are too open

异常显示: @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @ WARNING: UNPROTECTED PRIVATE KEY FILE! @ @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ ......

李玉长
今天
5
0
区块链10年了,还未落地,它失败了吗?

导读 几乎每个人,甚至是对通证持怀疑态度的人,都对区块链的技术有积极的看法,因为它有可能改变世界。然而,区块链技术问世已经10年了,我们仍然没有真正的用上区块链技术。 几乎每个人,甚...

问题终结者
今天
4
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部