知识拓展之Linux 搭建 redis 【保姆级教学】

68 阅读5分钟

知识拓展之Linux 搭建 redis 【保姆级教学】

		************************************************************************************************************************************************
							Redis 安装
		************************************************************************************************************************************************
			① 下载redis压缩包   http://redis.io/
			②  解压redis 压缩包   tar zxvf redis-5.0.3.tar.gz
			③  安装redis依赖   yum -y install gcc- c++ autoconf automake
			④  切换到解压目录预编译    cd redis-5.0.3/   make
			⑤  创建安装目录   mkdir -p /usr/local/redis				 
			⑥  安装redis到指定目录   make PREFIX=/usr/local/redis install
				不使用:make install (make install默认安装到/usr/local/bin目录下)
				使用:如果需要指定安装路径,需要添加PREFIX路径

		************************************************************************************************************************************************
							Redis 启动
		************************************************************************************************************************************************				
			Redis-cli :客户端
			Redis-server:服务端
			①  切换到安装的默认路径   cd /usr/local/redis/bin
			②  执行服务端启动命令   ./redis-server
				默认为前台启动,修改为后台启动
				1.  复制redis.conf至安装路径下   cp redis.conf /usr/loca/redis/bin/
				2.  修改安装路径下的redis.conf,将daemonize修改为 yes
				3.  启动时指定配置文件即可   ./redis-server ./redis.conf

		************************************************************************************************************************************************
						RedisDesktopManager 访问 redis
		************************************************************************************************************************************************
			
			① 修改配置文件 redis.conf,注释掉bind 127.0.0.1,可以使所有的ip访问redis,若是想指定多个ip访问,但并不是全部的ip访问,可以bind设置。
			② 关闭保护模式,修改为no   protected-mode no
			③ 添加访问认证,requirepass root (原先为注释状态,取消注释。root为redis的访问密码)
			④ 关闭 vmware 防火墙
			⑤ 使用 ps -ef | grep redis 查看redis进程,使用kill -9 xxxx杀死redis进程,重启redis  
			⑥ database 16 (数据库默认为16)
			
			
		************************************************************************************************************************************************
						Redis-cli 访问 redis
		************************************************************************************************************************************************
			-h :指定ip
			-p :指定端口号
			-a :用于指定认证密码
			① 连接redis-cli   ./redis-cli -p 6379 -a root
			② PING 返回 PONG 测试连接成功
			③ 指定database   select 3
			
			
		************************************************************************************************************************************************
						Redis 搭建 主从复用
		************************************************************************************************************************************************	
			Redis支持主从复用,数据可以从服务器向任意数量的从服务器上同步,同步使用的是发布/订阅机制,MasterSlave的模式,从Slave向Master
		发起SYNC命令。

			读写分离:
				① 创建三个目录(数据文件、日志文件、配置文件)								
					mkdir -p /opt/redis/data
					mkdir -p /opt/redis/log
					mkdir -p /opt/redis/conf	
				② 复制redis.conf 至 /opt/redis/conf目录下  cp redis.conf /opt/redis/redis-common.conf
				③ 修改redis-common.conf公共配置文件
					注释掉  bind 127.0.0.1			
					关闭保护模式  protected-mode 修改为 no
					注释公共配置端口  port 6379
					修改为后台启动  daemonize yes
					注释进程编号记录文件  pidfile /var/run/redis_6379.pid
					注释公共配置日志文件  logfile ""
					注释公共配置数据文件、修改数据文件路径 
							#dbfilename dump.rdb
							dir /opt/redis/data 
							在默认情况下,Redis 将数据库快照保存在名字为 dump.rdb 的二进制文件中。当然,这里可以通过修改
						redis.conf 配置文件来对数据存储条件进行定义,规定在“ N 秒内数据集至少有 M 个改动”这一条件被满足时,自动
						保存一次数据集。也可以通过调用save 或bgsave ,手动让Redis进行数据集保存操作dbfilename和dir组合使用,
						dbfilename找dir路径生成数据文件
					添加从服务器访问主服务器认证  masterauth root
					添加访问认证  requirepass root
					注释公共配置追加文件
							appendonly no
							appendfilename "appendonly"
							appendfilename 和 dir 组合使用,找dir(/opt/redis/data)路径生成数据文件  
							根据需求配置是否打开追加文件选项
							appendonly yes -> 每当redis执行一个改变数据集的命令时(比如set) 这个命令就会追加到AOF文件的末尾,这样
						的话当redis重启时,程序就可以通过重新执行AOF文件中的命令来达到重建数据集的目的。
					从服务器默认是只读不允许写操作(不用修改)  slave-read-only yes
					添加三个服务的私有配置文件
							touch 或者 vi 都可以创建空白文件
							touch 直接创建空白文件,vi创建并且进入编辑模式,:wq创建成功,否则不创建。 	
							touch redis-6379.conf、touch redis-6380.conf、touch redis-6381.conf
						
							redis-6379.conf	
								#引用公共配置
								include /opt/redis/conf/redis-common.conf 
								#进程编号记录文件
								pidfile /var/run/redis-6379.pid
								#进程端口号
								port 6379 
								#日志记录文件
								logfile "/opt/redis/log/redis-6379.log" 
								#数据记录文件
								dbfilename dump-6379.rdb 
								#追加文件名称
								appendfilename "appendonly-6379.aof" 
								#下面的配置无需在6379里配置
								#备份服务器从属于6379推荐配置配局域网IP 
								slaveof 192.168.10.100 6379 
							复制redis-6379.conf的内容至redis-6380.conf,redis-6381.conf并且修改其内容,将6379替换即可。
					运行三个redis进程
						/usr/local/redis/bin/redis-server /opt/redis/conf/redis-6379.conf
						/usr/local/redis/bin/redis-server /opt/redis/conf/redis-6380.conf
						/usr/local/redis/bin/redis-server /opt/redis/conf/redis-6381.conf
						
						ps -ef | grep redis  查看redis进程
					
			主备切换:
				主从节点redis.conf 配置 (参照读写分离的相应配置)
				修改sentinel-common.conf哨兵公共配置文件
					从redis解压目录下复制sentinel.conf至/opt/conf/sentinel-common.conf 
							(cp sentinel.conf /opt/redis/conf/sentinel-common.conf)
					注释掉哨兵监听进程端口号  port 26379
							指示一个sentinel去监听一个名为master的主服务器,这个服务器的地址为127.0.0.1,端口号为6379,而将这个
						主服务器判断为失效至少需要一个(一般设置为两个)。sentinel同意(只要同意sentinel的数量不达标,自动故障迁移就不会执行)
							这个要配局域网ip,否则远程连接不上。 sentinel monitor mymaster 192.168.10.100 6379 2
					设置master和slaves的密码  sentinel auth-pass mymaster root
					Sentinel 认为服务器已经断线所需的毫秒数  sentinel down-after-milliseconds mymaster 10000
							若sentinel在该配置内未能完成failover操作(即故障时master/slave自动切换),则认为本次failover失败。
					关闭保护模式,修改为no  protected-mode no
					修改为后台启动  daemonized yes
				添加三个哨兵的私有配置文件	
					touch 或者 vi 都可以创建空白文件
					touch 直接创建空白文件, vi 创建并且进入编辑模式, :wq 创建成功,否则不创建
					touch sentinel-26379.conf、touch sentinel-26380.conf、touch sentinel-26381.conf

					sentinel-26379.conf
						#引用公共配置
						include /opt/redis/conf/sentinel-common.conf 
						#进程端口号
						port 26379 
						#进程编号记录文件
						pidfile /var/run/sentinel-26379.pid 
						#日志记录文件(为了方便查看日志,先注释掉,搭好环境后再打开) 
						logfile "/opt/redis/log/sentinel-26379.log"	
					复制 sentinel-26379.conf 的内容至 sentinel-26380.conf , sentinel-26381.conf 并且修改其内容,将26379替换即可。

			启动测试 
				启动三个redis 服务
					/usr/local/redis/bin/redis-server /opt/redis/conf/redis-6379.conf 
					/usr/local/redis/bin/redis-server /opt/redis/conf/redis-6380.conf 
					/usr/local/redis/bin/redis-server /opt/redis/conf/redis-6381.conf	
				启动三个哨兵服务
					/usr/local/redis/bin/redis-sentinel /opt/redis/conf/sentinel-26379.conf 
					/usr/local/redis/bin/redis-sentinel /opt/redis/conf/sentinel-26380.conf 
					/usr/local/redis/bin/redis-sentinel /opt/redis/conf/sentinel-26381.conf 
				查看主从状态
					/usr/local/redis/bin/redis-cli -p 6379 -a root  ->  info replication