-
redis特点
1.1 支持内存的缓存,相当于memcached
1.2 数据类型丰富
1.3 支持集群分布式
1.4 持久化,相当于memcachedb
1.5 redis支持10W每秒的读写频率
-
memcache的特点
2.1 部署简单支持高并发
2.2 仅为内存缓存,重启服务,缓存丢失
3. redis持久化
3.1.RDB
是指在指定的时间内生成数据集的快照
每隔一段时间redis会把内存中的数据便利一下,生成一个dump.rdb的文件,存放在硬盘中,这个叫做快照,redis父进程会开辟一个子进程,这个子进程会负责rdb文件的保存的工作,父进程无需消耗磁盘的IO。但是如果服务器宕机的话,因为是按照一定的时间做的快照,所以会丢失一部分数据
在conf下生成 dump.rdb 文件
3.2.AOF
持久化记录redis服务器的操作命令,redis是默认1s执行一次fsync的命令,讲数据追加到硬盘,但是这种对磁盘的I/O消耗特别的大,但是数据一致性比较完整。
在conf下生成 appendonly.aof 文件
4.redis的安装部署
4.1 下载安装包
1
2
3
4
|
yum -y install gcc gcc++ tcl
cd /root
wget tar xf redis-3.0.6. tar .gz
|
4.2编译安装
1
2
3
4
5
|
mkdir -p /opt/redis-3 .0.6
cd /root/redis-3 .0.6
make make PREFIX= /opt/redis-3 .0.6 install
ln -s /opt/redis-3 .0.6 /opt/redis
|
4.3 拷贝配置文件
1
2
3
4
5
|
mkdir -p /opt/redis/conf
cp /root/redis-3 .2.2 /opt/redis/conf/6379 .conf
vim /opt/redis/conf/6379 .conf
daemonize yes #修改为yes 守护进程启动
pidfile /var/run/redis_6379 .pid #这个要和接下来的启动脚本一致
|
4.4 修改启动脚本默认是没有的
1
2
3
4
5
6
7
8
9
10
|
cd /root/redis-3 .2.2 /utils
cp redis_init_script /etc/init .d /redis6379
vi /etc/init .d /redis6379
# chkconfig: 2345 18 91 #加上 EXEC= /opt/redis/bin/redis-server
CLIEXEC= /opt/redis/bin/redis-cli
PIDFILE= /var/run/redis_ ${REDISPORT}.pid
CONF= "/opt/redis/conf/${REDISPORT}.conf" #注意脚本中定义的变量
chmod +x /etc/init .d /redis6379
chkconfig --add redis6379 |
4.5 增加环境变量
1
2
|
echo "PATH=/opt/redis/bin/" >> /etc/profile
source /etc/profile
|
4.6 启动redis
1
2
3
4
5
6
7
8
9
10
11
12
13
|
/etc/init .d /redis6379 start
[root@zk03 utils] # ps -ef |grep redis
root 5715 1 0 20:32 ? 00:00:00 /opt/redis/bin/redis-server 127.0.0.1:6379
root 5743 5654 0 20:43 pts /2 00:00:00 grep --color=auto redis
[root@zk03 utils] #
[root@zk03 utils] # /etc/init.d/redis6379 stop
Stopping ... Redis stopped [root@zk03 utils] # ps -ef |grep redis
root 5753 5654 0 20:46 pts /2 00:00:00 grep --color=auto redis
[root@zk03 utils] #
[root@zk03 utils] # redis-cli
127.0.0.1:6379> |
4.7 开启认证bind本机的IP:目的是远程连接redis通过iP
1
2
3
4
|
[root@zk03 conf] # vi 6379.conf
bind 192.168.56.17 requirepass 123456 重新启动发现启动脚本报错 |
4.8 修改启动脚本
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
stop)
if [ ! -f $PIDFILE ]
then
echo "$PIDFILE does not exist, process is not running"
else
PID=$( cat $PIDFILE)
echo "Stopping ..."
kill -9 ` cat /var/run/redis_ ${REDISPORT}.pid`
rm -f /var/run/redis_ ${REDISPORT}.pid
#$CLIEXEC -p $REDISPORT shutdown
#while [ -x /proc/${PID} ]
#do
# echo "Waiting for Redis to shutdown ..."
# sleep 1
#done
echo "Redis stopped"
fi
;;
停止的脚本修改一下
|
4.9 在另外一台服务器远程测试
1
2
3
4
5
6
7
8
|
[root@zk01 ~] # redis-cli -h 192.168.56.17
192.168.56.17:6379> set age 18
(error) NOAUTH Authentication required. 192.168.56.17:6379> auth 123456 OK 192.168.56.17:6379> set age 18
OK 192.168.56.17:6379>
|