湖南白癜风医院 http://baidianfeng.39.net/a_yqyy/140104/4322074.html
最近比较闲,买了个服务器打算自己玩玩,把平常工作比较多接触的技术和框架自己搭建下,这也算是一种成长吧。
我们平常工作肯定离不开Redis的使用,我们公司的Redis使用起来很简单并且没有做主从复制,更别提哨兵模式的Redis集群了。这篇文章记录了Redis主从复制架构的搭建过程,下篇文章向大家分享哨兵模式下的Redis集群搭建。
一、环境准备 1、CentOS8.2 2、redis-6.0.10二、搭建过程1、安装Redis单机版1、安装Redis#在/home目录下创建redis文件夹,将redis-6.0.10.tar.gz文件放置在/home/redis目录下[root
iZbp11wo2zhejrg49h4tbwZredis]#pwd/home/redis[rootiZbp11wo2zhejrg49h4tbwZredis]#lltotal-rw-r--r--1rootrootJan:35redis-6.0.10.tar.gz#解压[rootiZbp11wo2zhejrg49h4tbwZredis]#tar-zxvfredis-6.0.10.tar.gz[rootiZbp11wo2zhejrg49h4tbwZredis]#cdredis-6.0.10/[rootiZbp11wo2zhejrg49h4tbwZredis-6.0.10]#lltotal-rw-rw-r--1rootrootJan:-RELEASENOTES-rw-rw-r--1rootroot51Jan:25BUGS-rw-rw-r--1rootrootJan:25CONTRIBUTING-rw-rw-r--1rootrootJan:25COPYINGdrwxrwxr-x6rootrootJan:25deps-rw-rw-r--1rootroot11Jan:25INSTALL-rw-rw-r--1rootrootJan:25Makefile-rw-rw-r--1rootrootJan:25MANIFESTO-rw-rw-r--1rootrootJan:25README.md-rw-rw-r--1rootrootJan:25redis.conf-rwxrwxr-x1rootrootJan:25runtest-rwxrwxr-x1rootrootJan:25runtest-cluster-rwxrwxr-x1rootrootJan:25runtest-moduleapi-rwxrwxr-x1rootrootJan:25runtest-sentinel-rw-rw-r--1rootrootJan:25sentinel.confdrwxrwxr-x3rootrootJan:25srcdrwxrwxr-x11rootrootJan:25tests-rw-rw-r--1rootrootJan:25TLS.mddrwxrwxr-x9rootrootJan:25utils#编译[rootiZbp11wo2zhejrg49h4tbwZredis-6.0.10]#make#安装[rootiZbp11wo2zhejrg49h4tbwZredis-6.0.10]#makeinstall#也可以直接makemakeinstall一步到位#安装完成后默认在/usr/local/bin目录下多出以下文件[rootiZbp11wo2zhejrg49h4tbwZbin]#pwd/usr/local/bin[rootiZbp11wo2zhejrg49h4tbwZbin]#lltotal-rwxr-xr-x1rootrootSep:30chardetect-rwxr-xr-x1rootrootSep:30cloud-id-rwxr-xr-x1rootrootSep:30cloud-init-rwxr-xr-x1rootrootSep:30cloud-init-per-rwxr-xr-x1rootrootSep:30easy_install-rwxr-xr-x1rootrootSep:30easy_install-3.6-rwxr-xr-x1rootrootSep:30easy_install-3.8-rwxr-xr-x1rootrootSep:30jsondiff-rwxr-xr-x1rootrootSep:30jsonpatch-rwxr-xr-x1rootrootSep:30jsonpointer-rwxr-xr-x1rootrootSep:30jsonschema-rwxr-xr-x1rootrootMar:48redis-benchmark-rwxr-xr-x1rootrootMar:48redis-check-aof-rwxr-xr-x1rootrootMar:48redis-check-rdb-rwxr-xr-x1rootrootMar:48redis-clilrwxrwxrwx1rootroot12Mar:48redis-sentinel-redis-server-rwxr-xr-x1rootrootMar:48redis-server-------------------------------------------------------------------#redis-cliRedis客户端启动#redis-serverRedis服务端启动#redis-sentinelRedis哨兵启动#redis-benchmarkRedis压力测试工具#redis-check-aof服务器可能在程序正在对AOF文件进行写入时停机,如果停机造成了AOF文件出错(corrupt),那么Redis在重启时会拒绝载入这个AOF文件,从而确保数据的一致性不会被破坏。这时候可以使用可以先使用Redis附带的`redis-check-aof`程序,对原来的AOF文件进行修复,进而再启动redis#redis-check-rdb与redis-check-aof同理#至此安装完成-------------------------------------------------------------------2、修改配置文件[root
iZbp11wo2zhejrg49h4tbwZredis-6.0.10]#cd/home/redis/redis-6.0.10/#复制redis-default.conf[rootiZbp11wo2zhejrg49h4tbwZredis-6.0.10]#cpredis.confredis-def.conf[rootiZbp11wo2zhejrg49h4tbwZredis-6.0.10]#vimredis-def.conf-----------------------------#只修改以下配置就行#Redis默认端口号port#设置为后台进程方式启动daemonizeyes#指定Redis只接收来自于该IP地址的请求,如果不进行设置,那么将处理所有请求#默认情况下,如果没有指定“绑定”配置指令,Redis会监听用于从主机上所有可用的网络接口连接。#可以只监听一个或多个选择的接口“bind”配置指令,后面跟着一个或多个IP地址。#例如:绑定..1..0.0.1#绑定.0.0.1::1#只能接收到bind后面的ip地址发送的Redis请求bind0.0.0.0#保护模式是一层安全保护,就是只有可以访问redis,其他任何都不可以访问redis#这个安全层开启必须满足三个条件,不然安全层处于关闭状态:#1、protected-modeyes(处于开启)#2、没有bind指令#3、没有设置密码#这时redis的保护机制就会开启。开启之后,只有本机才可以访问redis。如果上面三个条件任何一个不满足,就不会开启保护机制。protected-modeno-----------------------------3、启动Redis#启动Redis[root
iZbp11wo2zhejrg49h4tbwZredis-6.0.10]#/usr/local/bin/redis-server/home/redis/redis-6.0.10/redis-def.conf#连接Redis客户端[rootiZbp11wo2zhejrg49h4tbwZredis]#/usr/local/bin/redis-cli-p[rootiZbp11wo2zhejrg49h4tbwZredis-6.0.10]#/usr/local/bin/redis-server/home/redis/redis-6.0.10/redis-def.conf[rootiZbp11wo2zhejrg49h4tbwZredis-6.0.10]#/usr/local/bin/redis-cli-p.0.0.1:setnametestOK.0.0.1:getname"test"至此,Redis单机版安装成功,这是最简单版本的Redis单机版搭建。
其他常用基础配置可视情况使用:
#设置Redis密码requirepassSaligiaRedis#开启aof持久化策略appendonlyyes#aof持久化文件存放文件appendfilename"appendonly.aof"
设置Redis密码后连接Redis客户端:
#关闭Redis[root
iZbp11wo2zhejrg49h4tbwZredis-6.0.10]#ps-efgrepredisroot:20?00:00:00/usr/local/bin/redis-server.0.0.1:root7:20pts/:00:00grep--color=autoredis[root
iZbp11wo2zhejrg49h4tbwZredis-6.0.10]#kill#启动Redis[rootiZbp11wo2zhejrg49h4tbwZredis-6.0.10]#/usr/local/bin/redis-server/home/redis/redis-6.0.10/redis-def.conf[rootiZbp11wo2zhejrg49h4tbwZredis-6.0.10]#/usr/local/bin/redis-cli-p.0.0.1:getname(error)NOAUTHAuthenticationrequired..0.0.1:authSaligiaRedisOK.0.0.1:getname"test".0.0.1:[rootiZbp11wo2zhejrg49h4tbwZredis-6.0.10]#/usr/local/bin/redis-cli-p-aSaligiaRedisWarning:Usingapasswordwith-aor-uoptiononthe