REmote DIctionary Server(Redis) 是一个由Salvatore Sanfilippo写的key-value存储系统。也可以叫做非关系型数据库。多用做缓存。
Redis是一个开源的使用ANSI C语言编写、遵守BSD协议、支持网络、可基于内存亦可持久化的日志型、Key-Value数据库,并提供多种语言的API。
它通常被称为数据结构服务器,因为值(value)可以是 字符串(String), 哈希(Hash), 列表(list), 集合(sets) 和 有序集合(sorted sets)等类型。
安装redis:
下载在官方下载就ok了。
解压:
[root@redis1 ~]# tar xf redis-5.0.7.tar.gz
进入目录:
[root@redis1 ~]# cd redis-5.0.7/
[root@redis1 redis-5.0.7]# make clean
[root@redis1 redis-5.0.7]# make install
先清除,然后make install
安装,这个和正常的编译安装不同,直接make install。
然后,再进入utils目录。
用这个进行安装
[root@redis1 utils]# ./install_server.sh
Welcome to the redis service installer
This script will help you easily set up a running redis server
Please select the redis port for this instance: [6379] ##端口
Selecting default: 6379
Please select the redis config file name [/etc/redis/6379.conf] ##配置文件位置和名称
Selected default - /etc/redis/6379.conf
Please select the redis log file name [/var/log/redis_6379.log] ##日志
Selected default - /var/log/redis_6379.log
Please select the data directory for this instance [/var/lib/redis/6379] ##数据存放位置
Selected default - /var/lib/redis/6379
Please select the redis executable path [/usr/local/bin/redis-server]
Selected config:
Port : 6379
Config file : /etc/redis/6379.conf
Log file : /var/log/redis_6379.log
Data dir : /var/lib/redis/6379
Executable : /usr/local/bin/redis-server
Cli Executable : /usr/local/bin/redis-cli
Is this ok? Then press ENTER to go on or Ctrl-C to abort.
Copied /tmp/6379.conf => /etc/init.d/redis_6379 ##服务的启动方式
Installing service...
Successfully added to chkconfig!
Successfully added to runlevels 345!
Starting Redis server...
Installation successful!
遇到的选项直接默认就好了。直接回车继续。
修改redis的配置文件:
[root@redis1 redis]# vim 6379.conf
70 bind 172.16.12.134 ##本机IP
264 dir /var/lib/redis/6379 ##数据文件存储的位置这个其实没改
295 masterauth 000000 ##master的验证密码
510 requirepass 000000 ##redis的访问密码,可以复杂一点
702 appendonly yes ##开启AOF模式
754 no-appendfsync-on-rewrite yes ##这个是为了防止另一个进程被阻止。
行数根据版本的不同可能会改变。修改的项就是这些。
Redis可以实现数据的持久化存储,即将数据保存到磁盘上。
Redis的持久化存储提供两种方式:RDB与AOF。RDB是默认配置。AOF需要手动开启。
现在Redis的配置中默认是关闭AOF模式的。
如果要开启AOF模式,修改Redis的配置文件6379.conf。
关于这部分配置文件的解释:https://blog.youkuaiyun.com/n_u_l_l_/article/details/103807064
修改哨兵模式的配置文件:
哨兵是一个独立的进程,作为进程,它会独立运行。其原理是哨兵通过发送命令,等待Redis服务器响应,从而监控运行的多个Redis实例。
[root@redis1 redis-5.0.7]# cp sentinel.conf /etc/redis/
[root@redis1 utils]# vim /etc/redis/sentinel.conf
84 sentinel monitor master 172.16.12.51 6379 1 ##监控masterIP,集群存活的slave的最少数量是1个
86 sentinel auth-pass master 000000 ##写入master的验证密码
113 sentinel down-after-milliseconds master 30000 ##master被哨兵认定为失效的时间间隔,ms
121 sentinel parallel-syncs master 1 ##允许1个slave指向新的master
146 sentinel failover-timeout master 180000 ##失效切换时间
失效切换时间就是两次故障切换的间隔,没到时间就算故障也是不会切换的。
将启动文件放进bin下/root/redis-5.0.7/src
[root@redis1 src]# cp redis-sentinel /bin/
[root@redis1 /etc/redis]# scp sentinel.conf 172.16.12.135:/etc/redis/
[root@redis1 /etc/redis]# scp sentinel.conf 172.16.12.136:/etc/redis/
[root@redis1 /etc/redis]# scp 6379.conf 172.16.12.135:/etc/redis/
[root@redis1 /etc/redis]# scp 6379.conf 172.16.12.136:/etc/redis/
[root@redis1 src]# scp /bin/redis-sentinel 172.16.12.135:/bin/
[root@redis1 src]# scp /bin/redis-sentinel 172.16.12.136:/bin/
redis2 3修改配置文件:
[root@redis2 /etc/redis]# vim 6379.conf
71 bind 172.16.12.135
288 replicaof 172.16.12.134 6379
[root@redis3 /etc/redis]# vim 6379.conf
71 bind 172.16.12.136
288 replicaof 172.16.12.134 6379
启动服务:
[root@redis1 ~]# /etc/init.d/redis_6379 restart
[root@redis2 ~]# /etc/init.d/redis_6379 restart
[root@redis3 ~]# /etc/init.d/redis_6379 restart
启动哨兵:
master --> slave1 --> slave2
[root@redis1 ~]# /bin/redis-sentinel /etc/redis/sentinel.conf &
[root@redis2 ~]# /bin/redis-sentinel /etc/redis/sentinel.conf &
[root@redis3 ~]# /bin/redis-sentinel /etc/redis/sentinel.conf &
[root@redis1 ~]# echo "/bin/redis-sentinel /etc/redis/sentinel.conf &" >> /etc/rc.local
[root@redis2 ~]# echo "/bin/redis-sentinel /etc/redis/sentinel.conf &" >> /etc/rc.local
[root@redis3 ~]# echo "/bin/redis-sentinel /etc/redis/sentinel.conf &" >> /etc/rc.local
[root@redis1 ~]# chmod +x /etc/rc.d/rc.local
[root@redis2 ~]# chmod +x /etc/rc.d/rc.local
[root@redis3 ~]# chmod +x /etc/rc.d/rc.local
[root@redis1 redis]# /bin/redis-sentinel /etc/redis/sentinel.conf &
[root@localhost redis]# 9830:X 02 Jan 2020 23:35:03.778 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
9830:X 02 Jan 2020 23:35:03.778 # Redis version=5.0.7, bits=64, commit=00000000, modified=0, pid=9830, just started
9830:X 02 Jan 2020 23:35:03.778 # Configuration loaded
9830:X 02 Jan 2020 23:35:03.779 * Increased maximum number of open files to 10032 (it was originally set to 1024).
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 5.0.7 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in sentinel mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 26379
| `-._ `._ / _.-' | PID: 9830
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
9830:X 02 Jan 2020 23:35:03.786 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
9830:X 02 Jan 2020 23:35:03.791 # Sentinel ID is 5fc384cf5377a4acf1d9723448fc64e1d14e7d4a
9830:X 02 Jan 2020 23:35:03.791 # +monitor master master 172.16.12.134 6379 quorum 1
能看到这个东西就是启动了:
[root@localhost redis]# redis-cli -h 172.16.12.134 -p 6379
连接redis输入密码
172.16.12.134:6379> auth 000000
OK
172.16.12.134:6379> info replication
# Replication
role:master ##这个是主节点
connected_slaves:2 ##2个从节点
slave0:ip=172.16.12.135,port=6379,state=online,offset=148900,lag=0
slave1:ip=172.16.12.136,port=6379,state=online,offset=148626,lag=1
master_repl_offset:148900
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:148899
redis的基本就是一个key对应着1个values。
172.16.12.134:6379> set animals apple
OK
172.16.12.134:6379> get animals
"apple"
animals就是key,apple就是值values。
然后可以关掉主节点,这样就会发生切换,2个从节点中的某个就会 变成主节点。就算这个主节点再次启动,它也变成从节点了。