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 ##用于集群管理
|