IP | Hostname |
---|---|
192.168.88.40 | redis-master |
192.168.88.50 | redis-slave |
- 下载并解压 (主从)
# wget http://download.redis.io/releases/redis-5.0.2.tar.gz
# tar -zxvf redis-5.0.2.tar.gz -C /usr/local/src ##解压到/usr/local/src目录下
# yum install -y gcc-c++ tcl ##安装依赖包`在这里插入代码片
- 编译并安装 (主从)
# cd /usr/local/src/redis-5.0.2/
# make install PREFIX=/usr/local/redis ##编译安装
# cd /usr/local/redis/bin/
# cp redis-cli redis-server /usr/sbin/ ##方便直接执行命令
- 建立并修改配置文件
主从都要操作
# cp /usr/local/src/redis-5.0.2/redis.conf /usr/local/redis/
主redis服务器的redis.conf配置:
# vi /usr/local/redis/redis.conf ##修改以下配置
# daemonize no 改为 daemonize yes
# bind 127.0.0.1 改为 bind 0.0.0.0
# logfile "" 改为 logfile /var/log/redis_6379.log
# # requirepass foobared 改为 requirepass 123456
从redis服务器的redis.conf配置:
# vi /usr/local/redis/redis.conf ##修改以下配置
# daemonize no 改为 daemonize yes
# bind 127.0.0.1 改为 bind 0.0.0.0
# logfile "" 改为 logfile /var/log/redis_6379.log
# # requirepass foobared 改为 requirepass 123456
# masterauth <master-password> 改为 masterauth 123456
# 添加 slaveof 192.168.88.40 6379
- 启动Redis服务 (主从)
# redis-server /usr/local/redis/redis.conf
# netstat -lnt ##查看6379端口开启
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:6379 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
tcp6 0 0 ::1:25 :::* LISTEN
tcp6 0 0 :::22 :::* LISTEN
- 验证
主:
[root@redis-master ~]# redis-cli -h 127.0.0.1 -p 6379 -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6379> INFO replication ##查看主/从复制信息
# Replication
role:master ##角色为master
connected_slaves:1 ##显示一台从服务器连接
slave0:ip=192.168.88.50,port=6379,state=online,offset=112,lag=1
master_replid:93a21063ffd0678eb7e866de88c976b47ce7c433
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:112
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:112
127.0.0.1:6379> set myblog YouMeek.com ##set某个值
OK
从:
[root@redis-slave ~]# redis-cli -h 127.0.0.1 -p 6379 -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6379> INFO replication ##查看主/从复制信息
# Replication
role:slave ##角色为slave
master_host:192.168.88.40 ##master主机ip
master_port:6379
master_link_status:up
master_last_io_seconds_ago:2
master_sync_in_progress:0
slave_repl_offset:56
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:93a21063ffd0678eb7e866de88c976b47ce7c433
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:56
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:56
127.0.0.1:6379> get myblog ##get刚刚master设置的值
"YouMeek.com"
127.0.0.1:6379>