redis安装
- 首先安装gcc:sudo apt-get install gcc
- 下载安装redis:
wget http://download.redis.io/releases/redis-4.0.8.tar.gz
tar xzf redis-4.0.8.tar.gz
cd redis-4.0.8
make
sudo make install
模拟运行多机环境:
启动多个进程,运行在不同的端口上就行
新建目录结构:新建多个目录存放运行配置文件,复制redis-4.0.8到相应目录
目录结构:
/redis-4.0.8
/src
redis-server
redis-cli
/6666/redis.conf
/7777/redis.conf
修改redis.conf
./6666/redis.conf修改: 作为master
1. port 6666
2. bind 192.168.227.144
3. dir ./
4. protected-mode no
5. daemonize yes
6. pidfile /var/run/redis_6666.pid
./7777/redis.conf修改: 作为slave
1. port 7777
2. bind 192.168.227.144
3. slaveof 192.168.227.144 6666
4. # master的密码 没有就不填这个
# masterauth 123456
5. protected-mode no
6. daemonize yes
7. pidfile /var/run/redis_7777.pid
8. dir ./
测试主从复制
登录6666的redis:
./redis-4.0.8/src/redis-cli -h localhost -p 6666
输入 info:输出role为master
# Replication
role:master
connected_slaves:1
slave0:ip=127.0.0.1,port=7777,state=online,offset=580,lag=0
master可以正常的get和set
登录7777的redis:
./redis-4.0.8/src/redis-cli -h localhost -p 7777
输入 info:输出role为slave
# Replication
role:slave
master_host:localhost
master_port:6666
master_link_status:up
master_last_io_seconds_ago:5
master_sync_in_progress:0
set操作报错:
localhost:7777> set username username
(error) READONLY You can't write against a read only slave.
sentinel 自动主从切换
修改./redis-4.0.8/sentinel.conf
1. protected-mode no
2. 修改sentinel monitor mymaster 192.168.227.144 6666 1
3. 修改心跳时间:sentinel down-after-milliseconds mymaster 3000
启动sentinel:
./redis-4.0.8/src/redis-server ./redis-4.0.8/sentinel.conf --sentinel &
1819:X 08 Aug 09:44:45.277 # Redis version=4.0.8, bits=64, commit=00000000, modified=0, pid=1819, just started
1819:X 08 Aug 09:44:45.277 # Configuration loaded
1819:X 08 Aug 09:44:45.278 * Increased maximum number of open files to 10032 (it was originally set to 1024).
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 4.0.8 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in sentinel mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 26379
| `-._ `._ / _.-' | PID: 1819
`-._ `-._ `-./ _.-' _.-'
|`-._`