单机版配置
1. 下载
wget http://download.redis.io/releases/redis-4.0.2.tar.gz
2. 解压
tar xzf redis-4.0.2.tar.gz -C /opt/app
3. 编译
cd /opt/app/redis-4.0.2/src -> make -j4 (需要安装 gcc yum install gcc 否则会报错)
make MALLOC=libc // jemalloc.h: No such file or directory 报错可以使用此编译
-> make install //默认安装在当前目录下
make PREFIX=/usr/local/redis install //安装到/usr/local/redis 目录下有个bin目录
4. cd ../utils -> cp redis_init_script /etc/rc.d/init.d/redis
5. 配置redis文件
#!/bin/sh
#chkconfig: 2345 80 90 //add this
# Simple Redis init.d script conceived to work on Linux systems
---------------------------------------------------------------
EXEC=/usr/local/redis/bin/redis-server //修改为真实的目录下 redis-server 的目录
CLIEXEC=/usr/local/redis/bin/redis-cli //redis-cli 的目录
PIDFILE=/usr/local/redis/pid/redis_${REDISPORT}.pid //指定目录
CONF="/usr/local/redis/redis.conf" //指定配置目录
6. cd /opt/app/redis-4.0.2 -> cp redis.conf /usr/local/redis/
7. vi redis.conf
1) # IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES
# JUST COMMENT THE FOLLOWING LINE.
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bind 0.0.0.0 //绑定端口号, 小心安全攻击,必须加上权限密码requirepass. 进入用AUTH password.
2) # By default Redis does not run as a daemon. Use 'yes' if you need it.
# Note that Redis will write a pid file in /var/run/redis.pid when daemonized.
daem onize yes //允许后台运行
# requirepass 123456 //设置进入密码
8. chkconfig --add redis //开机启动
chkconfig --list
9. cd /usr/local/redis/bin -> ./redis-cli
./redis-cli -h 192.168.161.150 -p 6379 -a password //-h IP -h host
10. 加入到环境中
vi /etc/profile -> export PATH="$PATH:/usr/local/redis/bin" -> source
11. 启动
./redis-server ../redis.conf
12. 使用客户端
./redis_cli
redis-cli -h xxx -p 6379 -a xxx
单机版配置2
1. 下载
wget http://download.redis.io/releases/redis-4.0.2.tar.gz
2. 解压
tar xzf redis-4.0.2.tar.gz -C /opt/app
3. 编译
cd /opt/app/redis-4.0.2/src -> make -j4 (需要安装 gcc yum install gcc 否则会报错)
make MALLOC=libc // jemalloc.h: No such file or directory 报错可以使用此编译
-> make install //默认安装在当前目录下
4. vi redis.conf
1) # IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES
# JUST COMMENT THE FOLLOWING LINE.
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bind 0.0.0.0 //绑定端口号
2) # By default Redis does not run as a daemon. Use 'yes' if you need it.
# Note that Redis will write a pid file in /var/run/redis.pid when daemonized.
daem onize yes //允许后台运行
requirepass 123456 //设置进入密码
5. cd utils -> ./install_server.sh ->
cd /etc/rc.d/init.d/ //开机时启动, 如果存在表示可以开机启动.
cd /etc/init.d/ //脚本启动 /etc/init.d/redis-6739 start/stop/restart
6. 使用客户端
./redis_cli
redis-cli -h xxx -p 6379 -a xxx
集群配置之主从配置
主从配置: 一台host作为 master, 其他host作为slave. 即使master死亡, slave也不会编程master.
一般 master服务器需要采用 2 * n + 1 的的策略进行配置, 即需要主机的数量有 (2 * n + 1) *2.
这是由于基于选举机制, 当 master 主机有一半以上的同时同意某一要求时,可以更好的发挥选举机制. 如果是偶数, 那么就会有一种极端环境.
同时在底层实现中, 也是基于该选举方式来处理的.
1. 下载
wget http://download.redis.io/releases/redis-4.0.2.tar.gz
2. 解压
tar xzf redis-4.0.2.tar.gz -C /opt/app
3. 编译
cd /opt/app/redis-4.0.2/src -> make -j4 (需要安装 gcc yum install gcc 否则会报错)
make MALLOC=libc // jemalloc.h: No such file or directory 报错可以使用此编译
-> make install //默认安装在当前目录下
4. cd /opt/app/redis-4.0.2/ ->
mkdir cluster-test ->
mkdir 7000 7001 7002 7003 7004 7005 ->
cd /opt/app/redis-4.0.2/ ->
cp redis.conf cluster-test/7000 //复制redis.conf配置文件复制到 7000. 7001, 7002,7003,7004,7005 亦是如此.
5. vi cluster-test/7000/redis.conf //7001, 7002,7003,7004,7005 亦是如此.
daemonize yes //redis后台运行
pidfile /var/run/redis_7000.pid //pidfile文件对应7000,7001,7002,7003, 7004, 7005
port 7000 //端口7000, 7001,7002, 7003, 7004, 7005
cluster-enabled yes //开启集群
cluster-config-file nodes_7000.conf //集群的配置 配置文件首次启动自动生成 7000,7001,7002, 7003, 7004, 7005.该文件会生成在数据持久化文件目录下
cluster-node-timeout 5000
appendonly yes //aof日志开启 有需要就开启,它会每次写操作都记录一条日志
6. cd src/
./redis-server ../cluster-test/7000/redis.conf //7001, 7002,7003,7004,7005 亦是如此.
ps -ef |grep redis || ps aux |grep redis
root 6733 1 0 10:12 ? 00:00:04 ./src/redis-server 0.0.0.0:7001 [cluster]
root 6747 1 0 10:12 ? 00:00:04 ./src/redis-server 0.0.0.0:7004 [cluster]
root 6752 1 0 10:12 ? 00:00:04 ./src/redis-server 0.0.0.0:7003 [cluster]
root 6757 1 0 10:12 ? 00:00:04 ./src/redis-server 0.0.0.0:7005 [cluster]
root 7435 1 0 10:36 ? 00:00:00 ./redis-server 0.0.0.0:7000 [cluster]
root 7451 1 0 10:37 ? 00:00:00 ./redis-server 0.0.0.0:7002 [cluster]
当前已经在单机上开启了六个 redis 实例. 此时可以针对六个实例搭建集群了
7. 安装ruby环境, 将为 redis-trib.rb 运行做准备
wget https:// cache.ruby-lang.org/pub/ruby/2.3/ruby-2.3.1.tar.gz
tar xvf ruby-2.3.1.tar.gz -C /opt/app
cd ruby-2.3.1 -> ./configure -prefix=/usr/local/ruby
make; make install //当前目录编译安装. 被指定到 /usr/bin/ruby
cd /usr -> cp bin/ruby /usr/local/bin cp bin/gem /usr/local/bin
8. 下载rubygen redis依赖
wget http:// rubygems.org/downloads/redis-3.3.0.gem
gem install -lredis-3.3.0.gem //安装依赖
gem list check redis gem //验证
9. 执行创建集群的脚本文件
./redis-trib.rb create --replicas 1 192.168.47.188:7000 192.168.47.188:7001 192.168.47.188:7002 192.168.47.188:7003 192.168.47.188:7004 192.168.47.188:7005
./redis-trib.rb check 192.168.47.188:7000 //验证
注:ERR Slot 0 is already busy, 这是可能创建集群的脚本文件时会报的错误, 这是由于可能某些key没有被清楚干净, 进入redis-cli: flushall, cluster reset, 重置数据库.清空所有数据.
10. 打开client, 也就是说在执行上述操作时, redis一直是在后台运行的.
./redis-cli -c -p 7000 //7001, 7002,7003,7004,7005 亦是如此; -c 集群
11. 验证
进入某一个master -> set key1 val1 -> 去slave去查看.
---------------------------------------------------------------------------------------------------------------------------------------------------------------------
设置密码之后如果需要使用redis-trib.rb的各种命令
如:./redis-trib.rb check 127.0.0.1:7000,则会报错ERR] Sorry, can’t connect to node 127.0.0.1:7000
解决办法:vim /usr/local/rvm/gems/ruby-2.3.3/gems/redis-4.0.0/lib/redis/client.rb,然后修改passord
class Client
DEFAULTS = {
:url => lambda { ENV["REDIS_URL"] },
:scheme => "redis",
:host => "127.0.0.1",
:port => 6379,
:path => nil,
:timeout => 5.0,
:password => "passwd123", # 这里设置成 master 的password
:db => 0,
:driver => nil,
:id => nil,
:tcp_keepalive => 0,
:reconnect_attempts => 1,
:inherit_socket => false
}
master - slave 复制配置
master - slave 复制配置 (将 read 操作将给 slave)
master: 8000
关闭rdb快照
#save 900 1 注释掉
#save 300 10 注释掉
#save 60 10000 注释掉
开启 aof
appendonly yes
关闭只读
slave-read-only no
slave 8001 8002
声明 slave-of
# slaveof <masterip> <masterport>
slaveof 127.0.0.1 8000
配置密码[如果 master 有密码] masterauth abc
slave 打开 rdb 快照功能
save 900 1
save 300 10
save 60 10000
配置只读
slave-read-only yes
开启 aof 模式
appendonly yes
将 master, slave 的 logfile 打开.
logfile "/opt/app/redis-4.0.2/lineNetwork/8000/8000.log"
先启动 master, 然后再启动 slave.
只有 master 可写, slave 只读.
哨兵 sentinel 模式
哨兵模式, 当 master 死亡, slave将接替 master的工作.
1. 先完成主从复制配置的配置
2. vi sentinel.conf
sentinel monitor mymaster 127.0.0.1 8000 1 //master对象
# master 被当前 sentinel 实例认定为“失效”的间隔时间
# 如果当前 sentinel 与 master 直接的通讯中,在指定时间内没有响应或者响应错误代码,那么
# 当前sentinel就认为master失效(SDOWN,“主观”失效)
# <mastername> <millseconds>
# 默认为30秒, 这里设置为 60s.
sentinel down-after-milliseconds mymaster 6000 //时间
sentinel failover-timeout mymaster 6000 //时间
sentinel config-epoch mymaster 175 //时间
sentinel leader-epoch mymaster 175 //时间
sentinel known-slave mymaster 127.0.0.1 8000 //master对象
3. 启动. 首先启动 redis. 在启动sentinel
./src/redis-server sentinel.conf --sentinel
4. 验证. kill master , 查看是否存在 slave 切换到 master.
Master->Slave->Sentinel,要确保按照这个顺序依次启动