IP | Hostname |
---|---|
192.168.88.40 | redis-master |
192.168.88.50 | redis-slave |
防火墙和selinux需关闭,不然会影响主从间的连接。
1. 下载并解压 (主从)
# 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 ##安装依赖包
2. 编译并安装 (主从)
# 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/ ##方便直接执行命令
3. 建立并修改配置文件
主从都需操作
# 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
4. 启动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
4. 验证
主服务器:
[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>
将Redis注册为系统服务
在/etc/init.d目录下添加Redis服务的启动,暂停和重启脚本:
vi /etc/init.d/redis
脚本内容如下:
#!/bin/sh
#
# redis - this script starts and stops the redis-server daemon
#
# chkconfig: - 85 15
# description: Redis is a persistent key-value database
# processname: redis-server
# config: /usr/local/redis/bin/redis-server
# config: /etc/redis.conf
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "KaTeX parse error: Expected 'EOF', got '&' at position 22: …KING" = "no" ] &̲& exit 0 redi…(basename KaTeX parse error: Expected 'EOF', got '&' at position 73: …config/redis ] &̲& . /etc/syscon…redis ] || exit 5
[ -f
R
E
D
I
S
C
O
N
F
F
I
L
E
]
∣
∣
e
x
i
t
6
e
c
h
o
−
n
REDIS_CONF_FILE ] || exit 6 echo -n
REDISCONFFILE ] ∣∣ exit 6 echo −n "Starting
p
r
o
g
:
"
d
a
e
m
o
n
prog: " daemon
prog: " daemon redis
R
E
D
I
S
C
O
N
F
F
I
L
E
r
e
t
v
a
l
=
REDIS_CONF_FILE retval=
REDISCONFFILE retval=?
echo
[ KaTeX parse error: Expected 'EOF', got '&' at position 16: retval -eq 0 ] &̲& touch lockfile
return KaTeX parse error: Expected 'EOF', got '}' at position 10: retval }̲ stop() { …"Stopping
p
r
o
g
:
"
k
i
l
l
p
r
o
c
prog: " killproc
prog: " killproc prog -QUIT
retval=
?
e
c
h
o
[
? echo [
? echo [ retval -eq 0 ] && rm -f
l
o
c
k
f
i
l
e
r
e
t
u
r
n
lockfile return
lockfile return retval
}
restart() {
stop
start
}
reload() {
echo -n
"
R
e
l
o
a
d
i
n
g
"Reloading
"Reloading prog: "
killproc
r
e
d
i
s
−
H
U
P
R
E
T
V
A
L
=
redis -HUP RETVAL=
redis −HUP RETVAL=?
echo
}
force_reload() {
restart
}
rh_status() {
status $prog
}
rh_status_q() {
rh_status >/dev/null 2>&1
}
case “$1” in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit 7
1
;
;
f
o
r
c
e
−
r
e
l
o
a
d
)
f
o
r
c
e
r
e
l
o
a
d
;
;
s
t
a
t
u
s
)
r
h
s
t
a
t
u
s
;
;
c
o
n
d
r
e
s
t
a
r
t
∣
t
r
y
−
r
e
s
t
a
r
t
)
r
h
s
t
a
t
u
s
q
∣
∣
e
x
i
t
0
;
;
∗
)
e
c
h
o
1 ;; force-reload) force_reload ;; status) rh_status ;; condrestart|try-restart) rh_status_q || exit 0 ;; *) echo
1 ;; force−reload) forcereload ;; status) rhstatus ;; condrestart∣try−restart) rhstatusq ∣∣ exit 0 ;; ∗) echo “Usage: $0 {start|stop|status|restart|condrestart|try-restart| reload|orce-reload}”
exit 2
esac