使用redis做缓存
版本说明
本教程使用redis3.0版本。3.0版本主要增加了redis集群功能。
安装的前提条件:
需要安装gcc:yuminstall gcc-c++
1、下载redis的源码包。
2、把源码包上传到linux服务器
3、解压源码包
tar -zxvf redis-3.0.0.tar.gz
4、Make
5、Make install
[root@bogon redis-3.0.0]# make installPREFIX=/usr/local/redis
1.1 启动redis
1、前端启动模式
/usr/local/redis/bin/redis-server
默认是前端启动模式,端口是6379
2、后端启动
1)从redis的源码目录中复制redis.conf到redis的安装目录。
cp redis.conf/usr/local/redis/bin/
2)修改配置文件
3)[root@bogonbin]# ./redis-server redis.conf
1.2 Redis常用命令
执行./redis-cli后进入下面命令操作:
127.0.0.1:6379> set a 10
OK
127.0.0.1:6379> get a
"10"
1.1 搭建集群需要的环境
搭建集群需要使用到官方提供的ruby脚本。
需要安装ruby的环境。
安装ruby
yum install ruby
yum install rubygems
redis集群管理工具redis-trib.rb
[root@bogon ~]# cd redis-3.0.0
[root@bogon redis-3.0.0]# cd src
[root@bogon src]# ll *.rb
-rwxrwxr-x. 1 root root 48141 Apr 1 07:01 redis-trib.rb
[root@bogon src]#
脚本需要的ruby包:
需要上传到linux服务。
安装ruby的包:
gem install redis-3.0.0.gem
[root@bogon ~]# gem install redis-3.0.0.gem
Successfully installed redis-3.0.0
1 gem installed
Installing ri documentation forredis-3.0.0...
Installing RDoc documentation forredis-3.0.0...
1.2 集群的搭建
删除复制过来的快照文件dump.rdb
第一步:创建6个redis实例,端口号从7001~7006
第二步:修改redis的配置文件
1、修改端口号
2、打开cluster-enable前面的注释。
第三步:把创建集群的ruby脚本复制到redis-cluster目录下。
[root@localhost redis-cluster]# cd/home/ftpuser/redis-3.0.0/src
[root@localhost src]# cp *.rb/usr/local/redis-cluster
第四步:启动6个redis实例
[root@localhost redis-cluster]# vimstartall.sh
cd redis01
./redis-serverredis.conf
cd ..
cd redis02
./redis-serverredis.conf
cd ..
[root@localhost redis-cluster]# chmod +xstartall.sh
[root@localhost redis-cluster]#./startall.sh
[root@localhost redis-cluster]# ps aux|grep redis
第五步:创建集群。需要将ip地址改为当前的ip
./redis-trib.rb create --replicas 1 192.168.25.153:7001 192.168.25.153:7002 192.168.25.153:7003 192.168.25.153:7004 192.168.25.153:7005 192.168.25.153:7006 |
[root@bogon redis-cluster]# ./redis-trib.rbcreate --replicas 1 192.168.25.153:7001 192.168.25.153:7002 192.168.25.153:7003192.168.25.153:7004 192.168.25.153:7005 192.168.25.153:7006
Can I set the above configuration? (type'yes' to accept): yes
>>> Nodes configuration updated
>>> Assign a different configepoch to each node
>>> Sending CLUSTER MEET messagesto join the cluster
Waiting for the cluster to join.....
>>> Performing Cluster Check(using node 192.168.25.153:7001)
[OK] All nodes agree about slotsconfiguration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
[root@bogon redis-cluster]#
1.3 测试集群
[root@bogon redis-cluster]# redis01/redis-cli-h 192.168.25.153 -p 7001 -c
[root@bogon redis-cluster]# redis01/redis-cli-h 192.168.25.153 -p 7001
192.168.25.153:7001> set a 100
(error) MOVED 15495 192.168.25.153:7003
192.168.25.153:7001>
[root@bogon redis-cluster]# redis01/redis-cli-h 192.168.25.153 -p 7001 -c
192.168.25.153:7001> set a 100
-> Redirected to slot [15495] located at192.168.25.153:7003
OK
192.168.25.153:7003>
1.4 关闭redis
单机打开:[root@localhostredis]# bin/redis-cli
关闭:redis01/redis-cli-p 7001 shutdown
vim shutdown.sh
redis01/redis-cli -p 7001 shutdown
redis02/redis-cli -p 7002 shutdown
[root@localhost redis-cluster]# chmod +xshutdown.sh
[root@localhost redis-cluster]#./shutdown.sh
1.5 Redis客户端
1.5.1 Redis-cli
自带客户端。使用最多的。
1.5.2 图形化界面客户端
只支持单机版,不支持集群。
1.5.3 Jedis客户端
1.5.3.1 单机版
启动redis:[root@localhost bin]#./redis-server redis.conf
关闭redis:[root@localhost bin]# ./redis-cli-p 6379 shutdown
默认16个数据库,切换库 select “序号”
clusterdown the cluster is down:
原因:ifconfig发现ip地址变了需要执行
./redis-trib.rb create --replicas 1 192.168.25.153:7001 192.168.25.153:7002 192.168.25.153:7003 192.168.25.153:7004 192.168.25.153:7005 192.168.25.153:7006 |