多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
Linux 下安装 本教程使用的版本为 4.0.12,下载并安装: Redis是一个开源、支持网络、基于内存、键值对存储数据库,使用ANSI C编写。所以在搭建Redis服务器时需要C语言的编译环境gcc或g++。 # 1. **环境确认** 首先确认系统中是否存在C语言的编译环境,终端运行如下命令:     #gcc –version 或     #g++ --version 如果出现如下字样则表示系统中存在C语言的编译环境,不需要安装。     gcc (GCC) 4.1.2 20080704 (Red Hat 4.1.2-44)     Copyright (C) 2006 Free Software Foundation, Inc.     This is free software; see the source for copying conditions. There is NO     warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE 或     g++ (GCC) 4.1.2 20080704 (Red Hat 4.1.2-44)     Copyright (C) 2006 Free Software Foundation, Inc.     This is free software; see the source for copying conditions. There is NO     warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 如果没有,就需要在系统中安装gcc、g++。 在有网络的环境下 执行 yum install gcc-c++ # 2. **redis安装** 将redis-4.0.12.tar.gz解压,然后安装。步骤及相关命令如下:     #tar –zxvf redis-4.0.12.tar.gz     #cd redis-4.0.12     #make #make install 出现 ![](https://box.kancloud.cn/e15c68d898e56aca457e48745f453215_364x51.png) 基本安装成功了 使用Redis启动脚本设置开机自启动 # 3.**启动脚本** 在生产环境中使用启动脚本方式启动redis服务。启动脚本redis_init_script 位于位于Redis的 /utils/ 目录下。 ``` #redis用监听的端口名作为配置文件命名 #redis服务器监听的端口 REDISPORT=6379 #服务端所处位置,在make install后默认存放与/usr/local/bin/redis-server,如果未 make install则需要修改该路径,下同。 EXEC=/usr/local/bin/redis-server #客户端位置 CLIEXEC=/usr/local/bin/redis-cli #Redis的PID文件位置 PIDFILE=/var/run/redis_${REDISPORT}.pid #配置文件位置,需要修改 CONF="/etc/redis/${REDISPORT}.conf" ``` 1. 根据启动脚本要求,将修改好的配置文件以端口为名复制一份到指定目录。需使用root用户。 `mkdir /etc/redis` `cp redis.conf /etc/redis/6379.conf ` 修改/etc/redis/6379.conf ``` #让redis以daemon进程运行 大概36行 daemonize yes #设置redis的pid文件位置 大概158行 pidfile /var/run/redis_6379.pid #设置redis的监听端口号 大概92行 port 6379 #设置持久化文件的存储位置 设置时候到具体目录新建相应文件夹 大概263行 dir /var/redis/6379 #密码设置 大概500行 requirepass xxxxxx #设置过期通知 默认为 notify-keyspace-events "" 大概1042行 notify-keyspace-events Ex ``` 2. 将启动脚本复制到/etc/init.d目录下,本例将启动脚本命名为redisd(通常都以d结尾表示是后台自启动服务)。 `cp redis_init_script /etc/init.d/redisd ` 3. 设置为开机自启动 此处直接配置开启自启动 chkconfig redisd on 将报错误: service redisd does not support chkconfig 在启动脚本开头添加如下两行注释以修改其运行级别: ``` #!/bin/sh # chkconfig: 2345 90 10 # description: Redis is a persistent key-value database # ``` ``` #stop的时候加上密码 -a xxxxxx $CLIEXEC -a xxxxxx -p $REDISPORT shutdown ``` 再设置即可成功。 #设置为开机自启动服务器 chkconfig redisd on #打开服务 service redisd start #关闭服务 service redisd stop # 3. **redis主从配置** 准备三个redis服务,我是阿里云的单机(10.17.22.33),所以就创建了3个文件夹master,slave1,slave2 修改配置文件(redis.conf): master ``` port 6379 bind 0.0.0.0 protected-mode no masterauth XXXXXX requirepass XXXXX ``` slave1修改配置: ``` port 6380 bind 0.0.0.0 protected-mode no slaveof 10.17.22.33 6379 masterauth XXXXXX requirepass XXXXX ``` slave2修改配置: ``` port 6381 bind 0.0.0.0 protected-mode no slaveof 10.17.22.33 6379 masterauth XXXXXX requirepass XXXXX ``` 分别启动三个redis ![](https://box.kancloud.cn/db8f815f1ff0f7480cca4a8883988512_655x52.png) 设置sentinel.conf配置文件 ``` bind 0.0.0.0 protected-mode no port 26380 sentinel monitor mymaster 10.17.22.33 6379 1 sentinel auth-pass mymaster Aa123456 daemonize yes logfile "/log/redis/sentinel.log" ``` redis-server sentinel.conf 启动哨兵 ![](https://box.kancloud.cn/7ea07c3e82b7858984dd8eedb73b3231_902x68.png) 启动成功 在安装过程中遇到的问题,哨兵同步不成功,修改阿里云安全组放行6379/6381端口