redis 服务器安装
进入下载目录
cd /usr/local/
下载软件包
wget http://download.redis.io/releases/redis-3.2.4.tar.gz
解压缩
tar zxvf redis-3.2.4.tar.gz && cd redis-3.2.4
编译
make && make test
cd /usr/local/redis-3.2.4 && make install
创建redis配置文件存放目录
mkdir -p /usr/local/redis-6379/conf
cp /usr/local/redis-3.2.4/redis.conf /usr/local/redis-6379/conf
创建可执行程序的目录
mkdir -p /usr/local/redis-6379/sbin
cd /usr/local/redis-3.2.4/src/
cp redis-cli redis-sentinel mkreleasehdr.sh redis-benchmark redis-check-aof redis-check-rdb redis-server redis-trib.rb /usr/local/redis-6379/sbin
编写启动脚本
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
#!/bin/bash
source
/etc/profile
REDISPORT=6379
EXEC=
/usr/local/redis-6379/sbin/redis-server
REDIS_CLI=
/usr/local/redis-6379/sbin/redis-cli
PIDFILE=
/var/run/redis_6379
.pid
CONF=
" /usr/local/redis-6379/conf/redis.conf "
AUTH=
"1234"
BIND_IP=
'127.0.0.1'
case
"$1"
in
start)
if
[ -f $PIDFILE ]
then
echo
"$PIDFILE is exists"
exit
1
else
echo
"start redis server"
$EXEC $CONF &
fi
if
[
"$?"
=
"0"
]
then
echo
"Redis is running...."
fi
;;
stop)
if
[ ! -f $PIDFILE ]
then
echo
"$PIDFILE exists,process is not running "
exit
1
else
PID=$(
cat
$PIDFILE )
echo
"stoping Redis ...."
$REDIS_CLI -h $BIND_IP -a $AUTH -p $REDISPORT SHUTDOWN
sleep
3
if
[ ! -f $PIDFILE ]
then
echo
“stop Redis success ”
fi
*)
echo
"Usage:redis_control.sh{start|stop}"
exit
1
esac
|
登录
./redis-cli -p 6379
模拟插入数据
127.0.0.1:6379> set test hello
OK
查看
127.0.0.1:6379> get test
"hello"
redis 主从搭建
1、使用类似的方法搭建redis 从服务器、设置从服务器的端口号为 6380
2、以6379端口号的redis 服务器为主服务器,6380 服务器为从服务器
3、修改redis 从服务器的配置
在从服务器上添加如下配置
# slaveof <masterip> <masterport>
slaveof 127.0.0.1 6379
masterauth 1234 ##备注如果master 服务器有密码的话需要加上
测试主从
查看master 上的信息
1
2
3
4
5
6
7
8
9
|
# Replication
role:master
connected_slaves:1
slave0:ip=127.0.0.1,port=6380,state=online,offset=4321,lag=1
master_repl_offset:4321
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:4320
|
查看 slave 上的信息
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
# Replication
role:slave
master_host:127.0.0.1
master_port:6379
master_link_status:up
master_last_io_seconds_ago:7
master_sync_in_progress:0
slave_repl_offset:4601
slave_priority:100
slave_read_only:1
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
|
测试数据是否正常
master
127.0.0.1:6379> set test1 hi
OK
127.0.0.1:6379> get test1
"hi"
slave
127.0.0.1:6380> get test1
"hi"
测试正常,主从搭建完成
本文转自 水滴石川1 51CTO博客,原文链接:http://blog.51cto.com/sdsca/1911171,如需转载请自行联系原作者