1,安装单机版Redis
centos7可以yum直接安装,也可以通过源码包安装,以下仅展示源码编译安装
安装依赖
[root@localhost]# yum install gcc -y
下载redis4.0
[root@localhost]# wget http://download.redis.io/releases/redis-4.0.10.tar.gz
[root@localhost]# tar -xvf redis-4.0.10.tar.gz
编译文件
[root@localhost]# cd redis-4.0.10 && make && make install
Hint: It's a good idea to run 'make test' ;)
INSTALL install
INSTALL install
INSTALL install
INSTALL install
INSTALL install
make[1]: Leaving directory `/root/redis-4.0.10/src'
出现以上等相关信息编译完成
安装完成后在bin目录下有几个可执行文件
redis-benchmark ---性能测试工具
redis-check-aof ---AOF文件修复工具
redis-check-rdb ---RDB文件检测工具(快照持久化文件)
redis-cli ---命令行客户端
redis-sentinel -> redis-server ---redis哨兵
redis-server ---redis服务器启动命令
配置环境变量
[root@localhost]# vim /etc/profile
添加
export REDIS_HOME=/root/redis-4.0.10
export PATH=$REDIS_HOME/bin:$PATH
[root@localhost]# source /etc/profile
[root@localhost]# cd redis-4.0.10/src
[root@localhost]# ./redis-server
8522:C 29 May 07:45:03.246 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
48522:C 29 May 07:45:03.247 # Redis version=4.0.10, bits=64, commit=00000000, modified=0, pid=48522, just started
48522:C 29 May 07:45:03.247 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
48522:M 29 May 07:45:03.248 * Increased maximum number of open files to 10032 (it was originally set to 1024).
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 4.0.10 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
| `-._ `._ / _.-' | PID: 48522
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
48522:M 29 May 07:45:03.250 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
48522:M 29 May 07:45:03.250 # Server initialized
48522:M 29 May 07:45:03.251 # 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.
48522:M 29 May 07:45:03.251 # 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.
48522:M 29 May 07:45:03.251 * Ready to accept connections
启动成功
修改redis配置文件后台启动
修改redis.conf 将daemonize no 改为 daemonize yes;
绑定ip由本地ip换成真机ip,方便对外访问
单机启动方式这里我就不多说了注意有一下几点需要注意
1.启动时指定配置文件
2.停止时不要强制kill 可能会导致数据丢失 正确关闭方式是shutdown
[root@localhost]# redis-cli -h 192.168.100.36 -p 6379 shutdown
可以安装服务脚本
[root@localhost]# cd redis-4.0.10/utils
[root@localhost]# ./install_server.sh
2,Redis集群安装
官网文档(Redis集群规范) http://redis.io/topics/cluster-spec
链接的翻译文档 http://redisdoc.com/topic/cluster-spec.html#cluster-spec
在redis3.0之前的版本需借助哨兵模式去搭建集群,哨兵对外接口,也可以是多个实现高可用,3.0之后redis官方变有了自己的集群
redis集群最小规划是三个master 此处测试为三主三从
创建节点目录
[root@localhost]# mkdir /opt/redis-cluster
[root@localhost]# cd /opt/redis-cluster
[root@localhost redis-cluster]# mkdir 7001 7002 7003 7004 7005 7006
创建配置文件
在每个节点目录中创建一个文件redis.conf,从7001到7006.作为配置文件的模板,以下是最小的Redis群集配置文件,每个节点实例配置稍作修改
[root@localhost redis-cluster]# cd 7001
[root@localhost 7001]# vim redis.conf
port 7001 #端口7001,7002,7003,7004,7005,7006
cluster-enabled yes #开启集群
cluster-config-file nodes.conf #集群配置信息,开启集群后自动生成
cluster-node-timeout 5000 #请求超时时长
appendonly yes #开启aof日志,它会每次写操作都记录一条日志
daemonize yes #redis后台运行
bind 192.168.100.36 #默认ip为127.0.0.1 需要改为其他节点机器可访问的ip 否则创建集群时无法访问对应的端口,无法创建集群
pidfile /opt/module/redis-Cluster/7001/redis_7001.pid #pidfile文件对应7001,7002,7003,7004,7005,7006
dir /opt/module/redis-Cluster/7001/ #数据文件存放位置对应7001,7002,7003,7004,7005,7006
接下来依次启动
[root@localhost redis-cluster]# redis-server 7001/redis.conf
[root@localhost redis-cluster]# redis-server 7002/redis.conf
[root@localhost redis-cluster]# redis-server 7003/redis.conf
[root@localhost redis-cluster]# redis-server 7004/redis.conf
[root@localhost redis-cluster]# redis-server 7005/redis.conf
[root@localhost redis-cluster]# redis-server 7006/redis.conf
启动之后可查看redis进程
[root@localhost redis-4.0]# ps -ef | grep redis
root 5744 1 0 May28 ? 00:00:53 redis-server 192.168.100.36:7001 [cluster]
root 5746 1 0 May28 ? 00:00:53 redis-server 192.168.100.36:7002 [cluster]
root 5755 1 0 May28 ? 00:00:53 redis-server 192.168.100.36:7003 [cluster]
root 5760 1 0 May28 ? 00:00:53 redis-server 192.168.100.36:7004 [cluster]
root 5765 1 0 May28 ? 00:00:53 redis-server 192.168.100.36:7005 [cluster]
root 5770 1 0 May28 ? 00:00:53 redis-server 192.168.100.36:7006 [cluster]
创建集群 redis安装目录src中redis-trib.rb完成集群创建,redis-trib.rb命令需要安装gem redis模块才能运行,gem redis需要Ruby环境
[root@localhost redis-cluster]# yum install ruby rubygems -y
[root@localhost redis-cluster]# gem install redis
这里遇到问题可以移步至ruby安装
创建新集群命令:命令create,选项–replicas 1意味着我们希望每个创建的主服务器都有一个从服 其他参数是我要用于创建新集群的实例的地址列表。
[root@localhost src]# ./redis-trib.rb create --replicas 1 192.168.100.36:7001 192.168.100.36:7002 192.168.100.36:7003 192.168.100.36:7004 192.168.100.36:7005 192.168.100.36:7006
如无报错证明创建成功
集群测试
[root@localhost redis-custer]# redis-cli -c -h 192.168.100.36 -p 7001
192.168.100.36:7001> set foo bar
-> Redirected to slot [12182] located at 192.168.100.36:7002
OK
192.168.100.36:7002> get foo
"bar"
192.168.100.36:7002> keys *
1) "foo"
192.168.100.36:7002> exit
[root@localhost redis-cluster]# redis-cli -c -h 192.168.100.36 -p 7001
192.168.100.36:7001> keys *
(empty list or set)
192.168.100.36:7001> get foo
-> Redirected to slot [12182] located at 192.168.100.36:7002
"bar"
192.168.100.36:7002>