设为首页 收藏本站
查看: 716|回复: 0

[经验分享] redis 部署

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2015-10-8 08:31:42 | 显示全部楼层 |阅读模式
redis 介绍部署
官网:http://www.redis.io/
      http://www.redis.cn/
  http://blog.nosqlfan.com/

1、redis 简介

   Remote Dictionary Server(Redis)是一个基于key-value键值对的持久化数据库存储系统,redis和大名鼎鼎的memcached缓存服务很像,但是redis支持的数据类型更加丰富,包括string(字符串)、list(链表)、set(集合)和zset(有序集合)等。
   这些数据类型都是支持push/php、add/remove及取交集、并集和差集以及更加丰富的操作,而且这些操作都是原子性的,在此基础上,redis还支持各种不同的排序,与memcached缓存服务一样,为了保证效率,数据都是缓存在内存中提供服务,和memcached不同的是,redis持久化缓存服务还会周期性把更新的数据写到磁盘已经把修改的操作记录追加到文件里记录,比memcached更有优势的是,redis还支持主从同步,这点很类似关系型数据库mysql。

   redis的出现在一定程度上弥补了memcached这类key-value内存缓存服务不足,在部分场合上可以对关系型数据库起到很好的补充作用。

2、redis优点
性能很高:redis能支持超过10万每秒的读写评论
丰富的数据类型:redis支持二进制的strings,lists,hashes,set已经ordered set等数据类型操作
原子:redis的所有操作都是原子性的,同时redis还支持对几个操作全并后的原子性操作
丰富的特性:
主从复制:redis支持异机主从复制

3、数据类型:
   string 字符串类型
   list(链表)
   set(集合)
   zset(有序集合
   hash 哈希

4、redis的应用场景
   传统的mysql+memcached的网站架构遇到的问题
   mysql数据库实际上是合适进行海量数据存储的,加上通过memcached将热点数据存放到内存cache里,达到加速数据访问的目的,绝大部分公司都曾经使用过这样的架构,但随着业务数量的不断增加,和访问量的持续增加,很多问题就会暴露出来
a、需要不断对mysql进行拆库拆表,memcached也需要不断跟着扩容,扩容和维护需要大量开发和运维时间
b、memcached和mysql数据库数据一致性问题也是个大难题
c、memcached数据命中率低或宕机,会导致大量的访问直接穿透数据库,导致mysql数据库无法支撑访问
d、跨机房cache同步一致性问题

所以就有了下面redis的应用场景:
a、redis最佳试用场景是全部数据in-memory
b、redis更多场合作为memcached的代替品来使用
c、当需要除了key-value之外的更多数据类型支撑时,使用redis更加合适
d、支持持久化
e、需要负债均衡的场景

##################################################################################
##################################################################################
##################################################################################
##################################################################################
5、redis数据库部署
#更新yum 源
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo
yum install tree -y

grep keepcache /etc/yum.conf
sed -i 's/keepcache=0/keepcache=1/g' /etc/yum.conf
grep keepcache /etc/yum.conf

#关闭selinux:
setenforce 0         #临时生效
sed -i 's#SELINUX=enforcing#SELINUX=disabled#g' /etc/selinux/config  #永久生效
setenforce 0   
sed -i 's#SELINUX=enforcing#SELINUX=disabled#g' /etc/selinux/config  

#关闭防火墙
/etc/init.d/iptables stop
chkconfig iptables off
chkconfig --list iptables

#时间同步
/usr/sbin/ntpdate pool.ntp.org
echo '#time sync by gao at 2010-2-1'>>/var/spool/cron/root
echo '*/10 * * * * /usr/sbin/ntpdate pool.ntp.org >/dev/null 2>&1'>>/var/spool/cron/root
crontab -l

#修改主机名
hostname MASTER
cp /etc/hosts /etc/hosts.bak
cp /etc/sysconfig/network /etc/sysconfig/network.bak
sed -i '10.0.0.3 MASTER' /etc/hosts
sed -i '10.0.0.4 Slave' /etc/hosts
sed -i -e '/HOSTNAME=/d' -e '1aHOSTNAME=MASTER' /etc/sysconfig/network
logout
############
hostname Slave
cp /etc/hosts /etc/hosts.bak
cp /etc/sysconfig/network /etc/sysconfig/network.bak
sed -i '10.0.0.3 MASTER' /etc/hosts
sed -i '10.0.0.4Slave' /etc/hosts
sed -i -e '/HOSTNAME=/d' -e '1aHOSTNAME=Slave' /etc/sysconfig/network
logout
#########################################################################
#########################################################################
开始部署
redis部署环境
系统 centos 6.5
master:10.0.0.3
slave:10.0.0.4

开始部署
###################################
获取redis安装包
mkdir -p /home/lvnian/tools
cd /home/lvnian/tools/
#wget https://codeload.github.com/antirez/redis/tar.gz/2.8.22
rz -y                     ##上传redis安装包2.8.22
======
[iyunv@M_Redis tools]# ls
2.8.22
[iyunv@M_Redis tools]#
#####################################
安装redis

tat xf 2.8.22
cd redis-2.8.22
#make MALLOC=jemalloc  #或者直接make也可以,这次是make安装,不加参数
make
mkdir -p /application/
make PREFIX=/application/redis-2.8.22 install
ln -s /application/redis-2.8.22/ /application/redis
ll /application/

上面步骤如果不出错,那就安装成功了
===================
###设置redis环境变量

echo " export PATH=/application/redis/bin/:$PATH" >>/etc/profile
tail -1 /etc/profile
. /etc/profile
which redis-server
===================
[iyunv@M_Redis ~]# echo " export PATH=/application/redis/bin/:$PATH" >>/etc/profile
[iyunv@M_Redis ~]# tail -1 /etc/profile
export PATH=/application/redis/bin/:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
[iyunv@M_Redis ~]# . /etc/profile
[iyunv@M_Redis ~]# which redis-server
/application/redis/bin/redis-server
[iyunv@M_Redis ~]#
===================
###配置启动redis
cd /application/redis
mkdir -p /application/redis/conf
mkdir -p /application/redis/logs
pwd
cd /application/redis/conf
cp /home/lvnian/tools/redis-2.8.22/redis.conf /application/redis/conf
=====================
[iyunv@M_Redis ~]# cd /application/redis
[iyunv@M_Redis redis]# mkdir -p /application/redis/conf
[iyunv@M_Redis redis]# mkdir -p /application/redis/logs
[iyunv@M_Redis redis]# pwd
/application/redis
[iyunv@M_Redis redis]# cd /application/redis/conf
[iyunv@M_Redis conf]# cp /home/lvnian/tools/redis-2.8.22/redis.conf /application/redis/conf
[iyunv@M_Redis conf]# ls
redis.conf
[iyunv@M_Redis conf]#


启动redis
redis-server /application/redis/conf/redis.conf &
netstat -lntup |grep redis
警告解决:
sysctl vm.overcommit_memory=1                         ######不是必须操作,是运行内存不足时候,继续使用内存,防止数据丢掉的优化来的
echo "vm.overcommit_memory=1" >>/etc/sysctl.conf
echo "net.core.somaxconn = 512 " >>/etc/sysctl.conf
#sysctl vm.overcommit_memory=1      
#echo 512 > /proc/sys/net/core/somaxconn
sysctl -p
lsof -i :6379
pkill redis
redis-server /application/redis/conf/redis.conf &
netstat -lntup |grep redis

================================
[iyunv@M_Redis conf]# redis-server /application/redis/conf/redis.conf &
[1] 4590
[iyunv@M_Redis conf]# [4590] 04 Oct 13:13:19.040 * Increased maximum number of open files to 10032 (it was originally set to 1024).
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 2.8.22 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                   
(    '      ,       .-`  | `,    )     Running in stand alone mode
|`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
|    `-._   `._    /     _.-'    |     PID: 4590
  `-._    `-._  `-./  _.-'    _.-'                                   
|`-._`-._    `-.__.-'    _.-'_.-'|                                 
|    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
|`-._`-._    `-.__.-'    _.-'_.-'|                                 
|    `-._`-._        _.-'_.-'    |                                 
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                          
              `-.__.-'                                               

[4590] 04 Oct 13:13:19.043 # Server started, Redis version 2.8.22
[4590] 04 Oct 13:13:19.044 # 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.
[4590] 04 Oct 13:13:19.046 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
[4590] 04 Oct 13:13:19.046 * The server is now ready to accept connections on port 6379
[iyunv@M_Redis conf]# netstat -lntup|grep redis
tcp        0      0 0.0.0.0:6379                0.0.0.0:*                   LISTEN      4590/redis-server *
tcp        0      0 :::6379                     :::*                        LISTEN      4590/redis-server *
[iyunv@M_Redis conf]#

到这来就已经部署成功了,但是看上面的警告提示:
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.
WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
我们操作下面命令就可以解决这个警告:
永久生效:
[iyunv@M_Redis conf]#echo "vm.overcommit_memory=1" >>/etc/sysctl.conf
[iyunv@M_Redis conf]#echo "net.core.somaxconn = 512 " >>/etc/sysctl.conf
[iyunv@M_Redis conf]# sysctl -p
或者临时生效:
sysctl vm.overcommit_memory=1      
echo 512 > /proc/sys/net/core/somaxconn

#############################################
[iyunv@M_Redis conf]# sysctl -p
net.ipv4.ip_forward = 0
net.ipv4.conf.default.rp_filter = 1
net.ipv4.conf.default.accept_source_route = 0
kernel.sysrq = 0
kernel.core_uses_pid = 1
net.ipv4.tcp_syncookies = 1
error: "net.bridge.bridge-nf-call-ip6tables" is an unknown key
error: "net.bridge.bridge-nf-call-iptables" is an unknown key
error: "net.bridge.bridge-nf-call-arptables" is an unknown key
kernel.msgmnb = 65536
kernel.msgmax = 65536
kernel.shmmax = 68719476736
kernel.shmall = 4294967296
vm.overcommit_memory = 1
net.core.somaxconn = 512
[iyunv@M_Redis conf]#
#################################
[iyunv@M_Redis conf]# redis-server /application/redis/conf/redis.conf &
[1] 6097
[iyunv@M_Redis conf]# [6097] 04 Oct 13:29:01.008 * Increased maximum number of open files to 10032 (it was originally set to 1024).
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 2.8.22 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                   
(    '      ,       .-`  | `,    )     Running in stand alone mode
|`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
|    `-._   `._    /     _.-'    |     PID: 6097
  `-._    `-._  `-./  _.-'    _.-'                                   
|`-._`-._    `-.__.-'    _.-'_.-'|                                 
|    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
|`-._`-._    `-.__.-'    _.-'_.-'|                                 
|    `-._`-._        _.-'_.-'    |                                 
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                          
              `-.__.-'                                               

[6097] 04 Oct 13:29:01.019 # Server started, Redis version 2.8.22
[6097] 04 Oct 13:29:01.021 * DB loaded from disk: 0.000 seconds
[6097] 04 Oct 13:29:01.021 * The server is now ready to accept connections on port 6379
#################################
[iyunv@M_Redis conf]# netstat -lntup|grep redis
tcp        0      0 0.0.0.0:6379                0.0.0.0:*                   LISTEN      6097/redis-server *
tcp        0      0 :::6379                     :::*                        LISTEN      6097/redis-server *
[iyunv@M_Redis conf]# lsof -i :6379
COMMAND    PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
redis-ser 6097 root    4u  IPv6  19615      0t0  TCP *:6379 (LISTEN)
redis-ser 6097 root    5u  IPv4  19617      0t0  TCP *:6379 (LISTEN)
[iyunv@M_Redis conf]#


部署成功
##########################
redis 的启动和关闭命令:
启动
redis-server /application/redis/conf/redis.conf &

关闭:
redis-cli shutdown
##############################################################################
##############################################################################
##############################################################################
redis 命令解析
[iyunv@M_Redis conf]# ls /application/redis/bin/ -l
total 15240
-rwxr-xr-x. 1 root root 4586299 Oct  4 13:07 redis-benchmark
-rwxr-xr-x. 1 root root   22185 Oct  4 13:07 redis-check-aof
-rwxr-xr-x. 1 root root   45403 Oct  4 13:07 redis-check-dump
-rwxr-xr-x. 1 root root 4688037 Oct  4 13:07 redis-cli
lrwxrwxrwx. 1 root root      12 Oct  4 13:07 redis-sentinel -> redis-server
-rwxr-xr-x. 1 root root 6254141 Oct  4 13:07 redis-server
[iyunv@M_Redis conf]#

./redis-benchmark ##用于进行redis性能测试的工具
./redis-check-dump ##用于修复出问题的dump.rdb文件,本地数据库检查
./redis-cli ##redis的客户端
./redis-server ##redis的服务端
./redis-check-aof ##用于修复出问题的AOF文件,更新日志检查
./redis-sentinel ##用于集群管理


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-124095-1-1.html 上篇帖子: redis-cli的三种使用方法 下篇帖子: spring整合redis集群
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表