Redis(Remote Dictionary Server ),即远程字典服务,是一个开源的使用ANSI C语言编写、支持网络、可基于内存亦可持久化的日志型、Key-Value数据库,并提供多种语言的API。
redis的安装
[root@server7 ~]# tar zxf redis-5.0.8.tar.gz
[root@server7 ~]# ls
anaconda-ks.cfg jar jdk-8u121-linux-x64.rpm redis-5.0.8 redis-5.0.8.tar.gz
[root@server7 ~]# cd redis-5.0.8/
[root@server7 redis-5.0.8]# ls
00-RELEASENOTES COPYING Makefile redis.conf runtest-moduleapi src
BUGS deps MANIFESTO runtest runtest-sentinel tests
CONTRIBUTING INSTALL README.md runtest-cluster sentinel.conf utils
[root@server7 redis-5.0.8]# make
cd src && make all
make[1]: Entering directory `/root/redis-5.0.8/src'
CC Makefile.dep
make[1]: Leaving directory `/root/redis-5.0.8/src'
make[1]: Entering directory `/root/redis-5.0.8/src'
rm -rf redis-server redis-sentinel redis-cli redis-benchmark redis-check-rdb redis-check-aof *.o *.gcda *.gcno *.gcov redis.info lcov-html Makefile.dep dict-benchmark
(cd ../deps && make distclean)
make[2]: Entering directory `/root/redis-5.0.8/deps'
(cd hiredis && make clean) > /dev/null || true
(cd linenoise && make clean) > /dev/null || true
(cd lua && make clean) > /dev/null || true
(cd jemalloc && [ -f Makefile ] && make distclean) > /dev/null || true
(rm -f .make-*)
make[2]: Leaving directory `/root/redis-5.0.8/deps'
(rm -f .make-*)
echo STD=-std=c99 -pedantic -DREDIS_STATIC='' >> .make-settings
echo WARN=-Wall -W -Wno-missing-field-initializers >> .make-settings
echo OPT=-O2 >> .make-settings
echo MALLOC=jemalloc >> .make-settings
echo CFLAGS= >> .make-settings
echo LDFLAGS= >> .make-settings
echo REDIS_CFLAGS= >> .make-settings
echo REDIS_LDFLAGS= >> .make-settings
echo PREV_FINAL_CFLAGS=-std=c99 -pedantic -DREDIS_STATIC='' -Wall -W -Wno-missing-field-initializers -O2 -g -ggdb -I../deps/hiredis -I../deps/linenoise -I../deps/lua/src -DUSE_JEMALLOC -I../deps/jemalloc/include >> .make-settings
echo PREV_FINAL_LDFLAGS= -g -ggdb -rdynamic >> .make-settings
(cd ../deps && make hiredis linenoise lua jemalloc)
make[2]: Entering directory `/root/redis-5.0.8/deps'
(cd hiredis && make clean) > /dev/null || true
(cd linenoise && make clean) > /dev/null || true
(cd lua && make clean) > /dev/null || true
(cd jemalloc && [ -f Makefile ] && make distclean) > /dev/null || true
(rm -f .make-*)
(echo "" > .make-cflags)
(echo "" > .make-ldflags)
MAKE hiredis
cd hiredis && make static
make[3]: Entering directory `/root/redis-5.0.8/deps/hiredis'
gcc -std=c99 -pedantic -c -O3 -fPIC -Wall -W -Wstrict-prototypes -Wwrite-strings -g -ggdb net.c
make[3]: gcc: Command not found
make[3]: *** [net.o] Error 127
make[3]: Leaving directory `/root/redis-5.0.8/deps/hiredis'
make[2]: *** [hiredis] Error 2
make[2]: Leaving directory `/root/redis-5.0.8/deps'
make[1]: [persist-settings] Error 2 (ignored)
CC adlist.o
/bin/sh: cc: command not found
make[1]: *** [adlist.o] Error 127
make[1]: Leaving directory `/root/redis-5.0.8/src'
make: *** [all] Error 2
[root@server7 redis-5.0.8]#
//安装失败 为什么了?
//按照提示 是我们的gcc版本不够,所以此时我们需要安装gcc
yum isntall gcc -y
[root@server7 redis-5.0.8]# make
cd src && make all
make[1]: Entering directory `/root/redis-5.0.8/src'
CC Makefile.dep
make[1]: Leaving directory `/root/redis-5.0.8/src'
make[1]: Entering directory `/root/redis-5.0.8/src'
CC adlist.o
In file included from adlist.c:34:0:
zmalloc.h:50:31: fatal error: jemalloc/jemalloc.h: No such file or directory
#include <jemalloc/jemalloc.h>
^
compilation terminated.
make[1]: *** [adlist.o] Error 1
make[1]: Leaving directory `/root/redis-5.0.8/src'
make: *** [all] Error 2
//仍然报错,为什么?
//此时需要把这个解压包删除,重新解压
[root@server7 ~]# rm -rf redis-5.0.8
[root@server7 ~]# tar zxf redis-5.0.8.tar.gz
[root@server7 ~]# ls
anaconda-ks.cfg jar jdk-8u121-linux-x64.rpm redis-5.0.8 redis-5.0.8.tar.gz
[root@server7 ~]# cd redis-5.0.8/
[root@server7 redis-5.0.8]# make
//此时可以安装成功
[root@server7 redis-5.0.8]# make install
cd src && make install
make[1]: Entering directory `/root/redis-5.0.8/src'
CC Makefile.dep
make[1]: Leaving directory `/root/redis-5.0.8/src'
make[1]: Entering directory `/root/redis-5.0.8/src'
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-5.0.8/src'
//安装成功
//进入 utils
[root@server7 redis-5.0.8]# cd utils/
[root@server7 utils]# ls
build-static-symbols.tcl hashtable redis_init_script.tpl
cluster_fail_time.tcl hyperloglog redis-sha1.rb
corrupt_rdb.c install_server.sh releasetools
create-cluster lru speed-regression.tcl
generate-command-help.rb redis-copy.rb whatisdoing.sh
graphs redis_init_script
[root@server7 utils]# ./install_server.sh
//没有特殊要求直接默认 Enter 即可
[root@server7 redis]# vim 6379.conf //70行bind项目改为0。0。0。0
[root@server7 redis]# /etc/init.d/redis_6379 restart
Stopping ...
Redis stopped
Starting Redis server...
[root@server7 redis]# netstat -antlp
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:6379 0.0.0.0:* LISTEN 10398/redis-server
配置完成
redis持久化:
219 save 900 1 ##900s更新到disk 一条数据改变时
220 save 300 10
221 save 60 10000 #60s 更新
700 appendonly yes
redis主从复制:
在一台主机上操作
redis-server --port 6380 --replicaof 127.0.0.1 6379
[root@server7 redis] bg
[1]+ redis-server --port 6380 --replicaof 127.0.0.1 6379 & ##打入后台
[root@server7 redis] ps ax | grep :6380
11530 pts/0 Sl 0:00 redis-server *:6380
11663 pts/0 R+ 0:00 grep --color=auto :6380
[root@server7 redis] redis-cli
127.0.0.1:6379> set name qqqq
OK
127.0.0.1:6379> quit
[root@server7 redis] redis-cli -p 6380
127.0.0.1:6380> get name
“qqqq”
127.0.0.1:6380>
#一台主机的redis主从复制
127.0.0.1:6380> set name qaz
(error) READONLY You can’t write against a read only replica.
#在6380这台从上面不可以修改
#把master给6380
127.0.0.1:6380> SLAVEOF no one
11530:M 23 Aug 2020 11:00:52.108 # Setting secondary replication ID to 60c64d8a3d18a109eabe4ed8de665cbfa0ffc05e, valid up to offset: 519. New replication ID is c76cdbbb1f0dabcb707ead7b964afaa0f8aa945b
11530:M 23 Aug 2020 11:00:52.108 # Connection with master lost.
11530:M 23 Aug 2020 11:00:52.108 * Caching the disconnected master state.
11530:M 23 Aug 2020 11:00:52.108 * Discarding previously cached master state.
11530:M 23 Aug 2020 11:00:52.108 * MASTER MODE enabled (user request from ‘id=4 addr=127.0.0.1:40414 fd=9 name= age=136 idle=0 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=34 qbuf-free=32734 obl=0 oll=0 omem=0 events=r cmd=slaveof’)
OK
127.0.0.1:6380> set name qaz
OK
127.0.0.1:6380> get name
“qaz”
多台主机的部署主从复制
快速部署另外两台主机(操作如上)
在server5上
/etc/init.d/redis_6379 rstart
[root@server2 redis]# redis-cli
127.0.0.1:6379> set name cxf
OK
127.0.0.1:6379> quit
[root@server2 redis]# hostname
server2
[root@server5 redis]# redis-cli
127.0.0.1:6379> get name
“cxf”
127.0.0.1:6379> quit
[root@server5 redis]# hostname
server5
[root@server5 redis]#
[root@server7 redis]# redis-cli
127.0.0.1:6379> get name
“cxf”
127.0.0.1:6379> quit
[root@server7 redis]# hostname
server7
多台主机redis主从复制成功
redis的sentinel分布式系统:
监控(monitoring):sentinel会不断的检查你的主服务器和从服务器是否运作正常
提醒(notification):当被监控的某个系统redis服务器出现问题时,sentinel可以通过api向管理员或者其他的程序发送通知
自动故障迁移
[root@server2 redis]# cd /root/redis-5.0.8/
[root@server2 redis-5.0.8]# ls
00-RELEASENOTES CONTRIBUTING deps Makefile README.md runtest runtest-moduleapi sentinel.conf tests
BUGS COPYING INSTALL MANIFESTO redis.conf runtest-cluster runtest-sentinel src utils
[root@server2 redis-5.0.8]# cp sentinel.conf /etc/redis/
[root@server2 redis-5.0.8]# vim /etc/redis/sentinel.conf
[root@server2 redis-5.0.8]#
[root@server2 redis]# redis-sentinel /etc/redis/sentinel.conf
6798:X 23 Aug 2020 11:40:55.916 * +slave slave 172.25.16.20:6379 172.25.16.20 6379 @ mymaster 127.25.16.2 6379 //有此显示既是成功