目录
一、上传文件
二、安装
三、启动和关闭
1、前端启动
1、启动方式
2、启动缺点
3、启动图例
4、前端启动的关闭
2、后端启动
四、主从复制
1、拷贝刚安装好的redis文件,并从新命名
2、修改端口和奴隶于谁
3、测试
五、redis集群搭建
一、上传文件
[root@localhost ~]# cd /usr/upload
[root@localhost upload]# rz
二、安装
1、Redis是c语言开发的。安装redis需要c语言的环境。
[root@localhost upload]# yum install gcc-c++
2、解压源码
[root@localhost upload]# tar -zxvf redis-3.0.0.tar.gz
3、进入解压后的目录进行编译
[root@localhost upload]# cd /usr/upload/redis-3.0.0
[root@localhost redis-3.0.0]# make
4、安装到指定目录
[root@localhost redis-3.0.0]# make install PREFIX=/usr/local/redis
5、查看是否安装到了指定的位置
[root@localhost redis-3.0.0]# cd /usr/local
[root@localhost local]# ll
三、启动和关闭
1、前端启动
1、启动方式
直接运行bin/redis-server将以前端模式启动
[root@localhost local]# cd /usr/local/redis/bin
[root@localhost bin]# ./redis-server
2、启动缺点
ssh命令窗口关闭则redis-server程序结束,不推荐使用此方法
3、启动图例
4、前端启动的关闭
ctrl+c
2、后端启动
第一步:进入源码目录,里面有一份配置文件 redis.conf,然后将其拷贝到安装路径下
[root@localhost bin]# cp /usr/upload/redis-3.0.0/redis.conf /usr/local/redis/bin
[root@localhost bin]# ll
第二步:修改redis.conf,将daemonize由no改为yes
[root@localhost bin]# vim /usr/local/redis/bin/redis.conf
原有内容
修改过后
第三步:
启动
[root@localhost bin]# ./redis-server redis.conf
进到客户端,查看是否安装成功
[root@localhost bin]# ./redis-cli
成功
ctrl+c 是退出客户端
关闭:到redis/bin目录下执行如下命令
[root@localhost bin]# ./redis-cli shutdown
四、主从复制
1、拷贝刚安装好的redis文件,并从新命名
[root@localhost local]# cp -r redis redis_6380
拷贝成功
2、修改端口和奴隶于谁
[root@localhost local]# cd redis_6380
[root@localhost redis_6380]# cd bin
[root@localhost bin]# vim redis.conf
将原有内容
分别修改以下内容
这里的ip地址和端口就是作为主机的redis的端口
到这里主从复制就配置好了
3、测试
分别启动两个redis,先启动哪个都不影响
[root@localhost bin]# ./redis-server redis.conf
[root@localhost bin]# cd /usr/local/redis/bin
[root@localhost bin]# ./redis-server redis.conf
1.先连接主,并添加数据,在退出来,连接从,查看数据,从此结果来看,主从复制配置成功了。
2.关闭主,往从写数据,由此结果看,主关闭了从是无法往里写数据的,只能读
五、redis集群搭建
1、使用ruby脚本搭建集群,需要安装ruby
[root@localhost local]# yum install ruby
[root@localhost local]# yum install rubygems
[root@localhost local]# cd /usr/upload
[root@localhost upload]# rz #上传文件
[root@localhost upload]# gem install redis-3.0.0.gem
[root@localhost upload]# cd redis-3.0.0/src
[root@localhost src]# ll *.rb #找到以.rb结尾的文件
上传文件
搜索到以.rb结尾的文件,证明以安装成功ruby
2、搭建步骤
注意:必须删除dump.rdb和appendonly.aof文件
搭建伪分布式,需要6个redis实例放到/usr/local/redis-cluster目录下,并且运行在不同的端口7001-7006
我这里没有appendonly.aof文件,就不用理会了
[root@localhost src]# cd /usr/local/redis/bin
[root@localhost bin]# rm -rf dump.rdb
[root@localhost bin]# ll
创建redis-cluster文件夹
[root@localhost bin]# cd /usr/local
[root@localhost local]# mkdir redis-cluster
拷贝6个redis
[root@localhost local]# cp -r /usr/local/redis /usr/local/redis-cluster/redis-7001
[root@localhost local]# cp -r /usr/local/redis /usr/local/redis-cluster/redis-7002
[root@localhost local]# cp -r /usr/local/redis /usr/local/redis-cluster/redis-7003
[root@localhost local]# cp -r /usr/local/redis /usr/local/redis-cluster/redis-7004
[root@localhost local]# cp -r /usr/local/redis /usr/local/redis-cluster/redis-7005
[root@localhost local]# cp -r /usr/local/redis /usr/local/redis-cluster/redis-7006
分别修改它们的配置文件
[root@localhost local]# vim /usr/local/redis-cluster/redis-7001/bin/redis.conf
依次类推去修改它们的配置文件
创建启动集群的脚本:start-all.sh 放在/usr/java/redis-cluster目录下。
[root@localhost local]# cd redis-cluster/
[root@localhost redis-cluster]# vim start-all.sh
启动脚本
cd /usr/local/redis-cluster/redis-7001/bin
./redis-server redis.conf
cd /usr/local/redis-cluster/redis-7002/bin
./redis-server redis.conf
cd /usr/local/redis-cluster/redis-7003/bin
./redis-server redis.conf
cd /usr/local/redis-cluster/redis-7004/bin
./redis-server redis.conf
cd /usr/local/redis-cluster/redis-7005/bin
./redis-server redis.conf
cd /usr/local/redis-cluster/redis-7006/bin
./redis-server redis.conf
修改开机启动脚本权限
[root@localhost redis-cluster]# chmod 777 start-all.sh
启动
[root@localhost redis-cluster]# ./start-all.sh
查看是否启动成功
[root@localhost redis-cluster]# ps -ef | grep redis
使用ruby脚本搭建redis集群
#
create --replicas 1 意思就是说,为每个节点创建一个备份机,也就是备份,如果是不同的服务器,那么后面的端口也需要改成服务器的端口, 前面3个ip是主,后面3个ip是备份机
[root@localhost redis-cluster]# cd /usr/upload/redis-3.0.0/src
[root@localhost src]# ./redis-trib.rb create --replicas 1 192.168.238.135:7001 192.168.238.135:7002 192.168.238.135:7003 192.168.238.135:7004 192.168.238.135:7005 192.168.238.135:7006
>>> Creating cluster
Connecting to node 192.168.238.135:7001: OK
Connecting to node 192.168.238.135:7002: OK
Connecting to node 192.168.238.135:7003: OK
Connecting to node 192.168.238.135:7004: OK
Connecting to node 192.168.238.135:7005: OK
Connecting to node 192.168.238.135:7006: OK
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
192.168.238.135:7001
192.168.238.135:7002
192.168.238.135:7003
Adding replica 192.168.238.135:7004 to 192.168.238.135:7001
Adding replica 192.168.238.135:7005 to 192.168.238.135:7002
Adding replica 192.168.238.135:7006 to 192.168.238.135:7003
M: 2e48ae301e9c32b04a7d4d92e15e98e78de8c1f3 192.168.238.135:7001
slots:0-5460 (5461 slots) master
M: 8cd93a9a943b4ef851af6a03edd699a6061ace01 192.168.238.135:7002
slots:5461-10922 (5462 slots) master
M: 2935007902d83f20b1253d7f43dae32aab9744e6 192.168.238.135:7003
slots:10923-16383 (5461 slots) master
S: 74f9d9706f848471583929fc8bbde3c8e99e211b 192.168.238.135:7004
replicates 2e48ae301e9c32b04a7d4d92e15e98e78de8c1f3
S: 42cc9e25ebb19dda92591364c1df4b3a518b795b 192.168.238.135:7005
replicates 8cd93a9a943b4ef851af6a03edd699a6061ace01
S: 8b1b11d509d29659c2831e7a9f6469c060dfcd39 192.168.238.135:7006
replicates 2935007902d83f20b1253d7f43dae32aab9744e6
Can I set the above configuration? (type 'yes' to accept): yes
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join.....
>>> Performing Cluster Check (using node 192.168.238.135:7001)
M: 2e48ae301e9c32b04a7d4d92e15e98e78de8c1f3 192.168.238.135:7001
slots:0-5460 (5461 slots) master
M: 8cd93a9a943b4ef851af6a03edd699a6061ace01 192.168.238.135:7002
slots:5461-10922 (5462 slots) master
M: 2935007902d83f20b1253d7f43dae32aab9744e6 192.168.238.135:7003
slots:10923-16383 (5461 slots) master
M: 74f9d9706f848471583929fc8bbde3c8e99e211b 192.168.238.135:7004
slots: (0 slots) master
replicates 2e48ae301e9c32b04a7d4d92e15e98e78de8c1f3
M: 42cc9e25ebb19dda92591364c1df4b3a518b795b 192.168.238.135:7005
slots: (0 slots) master
replicates 8cd93a9a943b4ef851af6a03edd699a6061ace01
M: 8b1b11d509d29659c2831e7a9f6469c060dfcd39 192.168.238.135:7006
slots: (0 slots) master
replicates 2935007902d83f20b1253d7f43dae32aab9744e6
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
[root@localhost redis-cluster]#
测试
[root@localhost src]# cd /usr/local/redis-cluster/redis-7001/bin
[root@localhost bin]# ./redis-cli -c -p 7001
redis cluster命令
cluster info #打印集群的信息
cluster nodes #列出集群当前已知的所有节点(node),以及这些节点的相关信息
补充:
创建关闭集群的脚本:shutdown-all.sh,放在/usr/local/redis-cluster目录下。
cd redis-7001
./redis7001/ bin/redis-cli -p 7001 shutdown
./redis7001/ bin/redis-cli -p 7002 shutdown
./redis7001/ bin/redis-cli -p 7003 shutdown
./redis7001/ bin/redis-cli -p 7004 shutdown
./redis7001/ bin/redis-cli -p 7005 shutdown
./redis7001/ bin/redis-cli -p 7006 shutdown
[root@localhost redis-cluster]# chmod 777shutdown-all.sh
这里是将所有的redis的安装到了一台服务器中。