【安装redis单实例实战】

yum -y install gcc gcc-c++    #安装编译工具
cd /opt
wget -c http://download.redis.io/releases/redis-3.0.5.tar.gz   #下载包
tar xf redis-3.0.5.tar.gz     #解压
cd redis-3.0.5
make MALLOC=libc  #编译
make PREFIX=/usr/local/redis install  #安装
echo 'PATH=/usr/local/redis/bin:$PATH' >> /etc/profile    #配置环境变量
source /etc/profile #或 . /etc/profile
mkdir /usr/local/redis/conf  #创建目录
cp /opt/redis-3.0.5/redis.conf /usr/local/redis/conf/   #拷贝配置文件
sed -i "46s#/var/run/redis.pid#/usr/local/redis/conf/redis.pid#" /usr/local/redis/conf/redis.conf
sed -i "192s#./#/usr/local/redis/conf/#" /usr/local/redis/conf/redis.conf
sed -i 509s/no/yes/ /usr/local/redis/conf/redis.conf
nohup redis-server /usr/local/redis/conf/redis.conf &> /dev/null &
echo vm.overcommit_memory = 1 >> /etc/sysctl.conf
sysctl -p
lsof -i:6379  #查看端口有没有起来
#redis-cli shutdown



【redis多实例实战】

mkdir -p /usr/local/redis/6380/conf
cp /usr/local/redis/conf/redis.conf /usr/local/redis/6380/conf/
sed -i 50s/6379/6380/ /usr/local/redis/6380/conf/redis.conf
sed -i "46s#/var/run/redis.pid#/usr/local/redis/6380/conf/redis.pid#" /usr/local/redis/6380/conf/redis.conf
sed -i "192s#./#/usr/local/redis/6380/conf/#" /usr/local/redis/6380/conf/redis.conf
sed -i 509s/no/yes/ /usr/local/redis/6380/conf/redis.conf
redis-server /usr/local/redis/6380/conf/redis.conf &
mkdir -p /usr/local/redis/6381/conf
cp /usr/local/redis/conf/redis.conf /usr/local/redis/6381/conf/
sed -i 50s/6379/6381/ /usr/local/redis/6381/conf/redis.conf
sed -i "46s#/var/run/redis.pid#/usr/local/redis/6381/conf/redis.pid#" /usr/local/redis/6381/conf/redis.conf
sed -i "192s#./#/usr/local/redis/6381/conf/#" /usr/local/redis/6381/conf/redis.conf    #更改rdb文件存放目录
sed -i 509s/no/yes/ /usr/local/redis/6381/conf/redis.conf    #开启aof日志文件
redis-server /usr/local/redis/6380/conf/redis.conf &
# redis-cli -p 6381 
> set a b
> save
> quit
# ls /usr/local/redis/6381/conf  #查看生成的dump.rdb文件
# redis-cli -p 6381 shutdown #关闭服务



【主从同步实战】(主跟从可以在相同服务器,同服务器的时候必须至少要有两个实例)

主:6379  从:6380   -》同一台服务器,不同实例

1)从修改配置文件

sed -i "210a slaveof 192.168.1.103 6379" /usr/local/redis/6380/conf/redis.conf  #此处不写主的IP写主的域名也可以
sed -i "217a masterauth wsyht" /usr/local/redis/6380/conf/redis.conf #master没密码可以不做这一步
redis-cli -p 6380 shutdown
redis-server /usr/local/redis/6380/conf/redis.conf &


2)建库验证查看,主操作

[root@localhost conf]# redis-cli -p 6379 -a wsyht
127.0.0.1:6379> set name wsyht
OK
127.0.0.1:6379> get name
"wsyht"
127.0.0.1:6379> select 1
OK
127.0.0.1:6379[1]> set name peter
OK
127.0.0.1:6379[1]>info   #查看所有信息包括同步
127.0.0.1:6380>info replication  #只查看他的同步信息


3)从查看库和状态信息,monitor监控每十秒拼一次

[root@localhost ~]# redis-cli -p 6380 
127.0.0.1:6380> monitor
OK
1446980247.929617 [0 192.168.1.103:6379] "PING"
1446980257.981266 [0 192.168.1.103:6379] "PING"
1446980268.034426 [0 192.168.1.103:6379] "PING"
1446980273.602672 [0 192.168.1.103:6379] "SELECT" "0"
1446980273.602687 [0 192.168.1.103:6379] "set" "name" "wsyht"
1446980278.089068 [0 192.168.1.103:6379] "PING"
1446980288.140236 [0 192.168.1.103:6379] "PING"
1446980294.722701 [0 192.168.1.103:6379] "SELECT" "1"
1446980294.722721 [1 192.168.1.103:6379] "set" "name" "peter"
1446980298.190898 [1 192.168.1.103:6379] "PING"
^C
[root@localhost ~]# redis-cli -p 6380 
127.0.0.1:6380> get name  #查看同步过来的库
"wsyht"
127.0.0.1:6380>info  #查看所有信息包括同步
127.0.0.1:6380>info replication  #只查看他的同步信息