下载解压编译
$ wget http://download.redis.io/releases/redis-3.2.8.tar.gz $ tar -zxvf redis-3.2.8.tar.gz -C /usr/local $ cd /usr/local/redis-3.2.8 $ make
$ make install #将这些可执行程序复制到/usr/local/bin目录中以便以后执行程序时可以不用输入完整的路径
配置
复制默认配置到指定目录
cp redis.conf /usr/local/etc/
按需要修改参数
daemonize yes #---默认值no,该参数用于定制redis服务是否以守护模式运行。---
远程连接时需要修改以下参数
# bind
127.0
.
0.1
#绑定ip,默认是本机所有网络设备;
protected-mode no #保护模式[无密码模式设置为no]
requirepass test #设置密码
启动
带配置启动
/usr/local/bin/redis-server /usr/local/etc/redis.conf
######查看进程
ps -ef | grep redis
开机启动,执行自带脚本
bash utils/install_server.sh
连接
/usr/bin/redis-cli
开户主从
复制配置文件,修改端口号,同时配置主库,然后启动服务即可。
可使用自带脚本,输入端口号,自动生成。然后配置主从。
sunil@ubuntu:/usr/local/redis-3.2.8$ sudo bash 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] 6380 Please select the redis config file name [/etc/redis/6380.conf] /usr/local/etc/redis_6380.conf Please select the redis log file name [/var/log/redis_6380.log] Selected default - /var/log/redis_6380.log Please select the data directory for this instance [/var/lib/redis/6380] Selected default - /var/lib/redis/6380 Please select the redis executable path [/usr/local/bin/redis-server] Selected config: Port : 6380 Config file : /usr/local/etc/redis_6380.conf Log file : /var/log/redis_6380.log Data dir : /var/lib/redis/6380 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/6380.conf => /etc/init.d/redis_6380 Installing service... Success! Starting Redis server... Installation successful!
主从配置
root@ubuntu:/usr/local/etc# nano redis_6380.conf
slaveof 127.0.0.1 6379
查看
sunil@ubuntu:~$ redis-cli -p 6379 127.0.0.1:6379> info replication # Replication role:master connected_slaves:2 slave0:ip=127.0.0.1,port=6381,state=online,offset=85,lag=1 slave1:ip=127.0.0.1,port=6380,state=online,offset=85,lag=1 master_repl_offset:85 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:2 repl_backlog_histlen:84
sentinel哨兵模式
$ sudo cp /usr/local/redis-3.2.8/sentinel.conf /usr/local/etc/
修改配置
nano /usr/local/etc/sentinel.conf
#以守护进程启动 daemonize yes #关闭保护模式 否则远程超连接失败 protected-mode no #sentinel默认端口 port 26379 #工作目录 dir "/tmp" #监控的master,最后的1表示1个sentinel认为master短线就干预,进行主从切换。192.168.13.169为本机对外IP sentinel monitor mymaster 192.168.13.169 6379 1 #master30000毫秒无响应,sentinel就认为它已经断线 sentinel down-after-milliseconds mymaster 30000 #在执行故障转移时, 最多可以有多少个slave同时对新的master进行同步, 这个数字越小, 完成故障转移所需的时间就越长,但越大就意味着越多的slave因为复制而不可用。可以通过将这个值设为 1 来保证每次只有一个slave处于不能处理命令请求的状态。 sentinel parallel-syncs mymaster 1 #故障转移的毫秒数 sentinel failover-timeout mymaster 180000 #日志文件 logfile /var/log/sentinel.log
启动
$ sudo redis-sentinel /usr/local/etc/sentinel.conf
$ ps -ef | grep redis root 1126 1 0 18:20 ? 00:00:10 /usr/local/bin/redis-server *:6381 root 1127 1 0 18:20 ? 00:00:10 /usr/local/bin/redis-server *:6380 root 1650 1 0 19:18 ? 00:00:01 redis-server *:6379 root 1668 1 0 19:28 ? 00:00:00 redis-sentinel *:26379 [sentinel] sunil 1672 1431 0 19:28 pts/8 00:00:00 grep --color=auto redis
关闭
$ redis-cli -p 26379 127.0.0.1:26379> shutdown not connected> exit
常用命令
参考文档:
http://www.tianshouzhi.com/api/tutorials/redis/174
http://www.cnblogs.com/wenanry/archive/2012/02/26/2368398.html
https://github.com/judasn/Linux-Tutorial/blob/master/Redis-Install-And-Settings.md