虚拟机规划
192.168.4.1 redis1 6351
192.168.4.2 redis2 6352
192.168.4.3 redis3 6353
192.168.4.4 redis4 6354
192.168.4.5 redis5 6355
192.168.4.6 redis6 6356
192.168.4.7 redis7 6357
192.168.4.8 lnmp
1) 搭建redis服务器
在真机上将redis传到虚拟机
[root@room9pc01 ~]# scp '/root/桌面/10.nosql/redis-4.0.8.tar.gz' 192.168.4.1:/root
[root@room9pc01 ~]# scp '/root/桌面/10.nosql/lnmp_soft.tar.gz' '/root/桌面/10.nosql/php-redis-2.2.4.tar.gz' '/root/桌面/10.nosql/php-devel-5.4.16-42.el7.x86_64.rpm' 192.168.4.1:/root
[root@redis1 ~]# tar -xf redis-4.0.8.tar.gz
[root@redis1 ~]# cd redis-4.0.8/
安装依赖包
[root@redis1 redis-4.0.8]# yum -y install gcc gcc-c++ make
编译安装
[root@redis1 redis-4.0.8]# make ; make install
[root@redis1 redis-4.0.8]# cd utils/
[root@redis1 utils]# ./install_server.sh
Welcome to the redis service installer
This script will help you easily set up a running redis server
Please select the redis port for this instance: [6379]
Selecting default: 6379
Please select the redis config file name [/etc/redis/6379.conf]
Selected default - /etc/redis/6379.conf
Please select the redis log file name [/var/log/redis_6379.log]
Selected default - /var/log/redis_6379.log
Please select the data directory for this instance [/var/lib/redis/6379]
Selected default - /var/lib/redis/6379
Please select the redis executable path [/usr/local/bin/redis-server]
Selected config:
Port : 6379
Config file : /etc/redis/6379.conf
Log file : /var/log/redis_6379.log
Data dir : /var/lib/redis/6379
Executable : /usr/local/bin/redis-server
Cli Executable : /usr/local/bin/redis-cli
Is this ok? Then press ENTER to go on or Ctrl-C to abort.
Copied /tmp/6379.conf => /etc/init.d/redis_6379
Installing service...
Successfully added to chkconfig!
Successfully added to runlevels 345!
Starting Redis server...
Installation successful!
[root@redis1 utils]# /etc/init.d/redis_6379 status #查看状态
Redis is running (5106)
[root@redis1 utils]# ss -antulp | grep :6379 #查看监听端口
tcp LISTEN 0 128 127.0.0.1:6379 *:* users:(("redis-server",pid=5106,fd=6))
[root@redis1 utils]# ps -C redis-server #查看服务
PID TTY TIME CMD
5106 ? 00:00:00 redis-server
[root@redis1 utils]# /etc/init.d/redis_6379 stop #关闭服务
Stopping ...
Waiting for Redis to shutdown ...
Redis stopped
[root@redis1 utils]# /etc/init.d/redis_6379 status
cat: /var/run/redis_6379.pid: 没有那个文件或目录
Redis is running ()
[root@redis1 utils]# /etc/init.d/redis_6379 start #启动服务
Starting Redis server...
[root@redis1 utils]# redis-cli #连接,测试是否正常
127.0.0.1:6379> ping
PONG
127.0.0.1:6379> set test 123 #设置变量test,值123
OK
127.0.0.1:6379> get test #获取变量test的值
"123"
127.0.0.1:6379> set k1 v1
OK
127.0.0.1:6379> get k1
"v1"
127.0.0.1:6379> del k1 #删除变量
(integer) 1
127.0.0.1:6379> get k1
(nil)
127.0.0.1:6379> keys * #获取所有的变量
1) "test"
127.0.0.1:6379> set k2 v1
OK
127.0.0.1:6379> type k2 #查看类型
string
127.0.0.1:6379> move k2 1 #移动k2到1库
(integer) 1
127.0.0.1:6379> select 1 #切换到1库
OK
127.0.0.1:6379[1]> keys * #获取所有的变量
1) "k2"
127.0.0.1:6379[1]> expire k2 10 #设置k2的有效时间
(integer) 1
127.0.0.1:6379[1]> ttl k2 #查看k2的生存时间,会不断的减少,直到-2
(integer) 3
127.0.0.1:6379[1]> flushall #删除所有的变量
OK
127.0.0.1:6379[1]> save #保存所有的变量
OK
127.0.0.1:6379[1]> shutdown #关闭redis服务
2) 修改Redis服务运行参数
[root@redis1 utils]# cp /etc/redis/6379.conf /root/6379.conf
[root@redis1 utils]# /etc/init.d/redis_6379 stop
Stopping ...
Redis stopped
[root@redis1 utils]# /etc/init.d/redis_6379 status
cat: /var/run/redis_6379.pid: 没有那个文件或目录
Redis is running ()
[root@redis1 utils]# vim /etc/redis/6379.conf
...
bind 192.168.4.51 //设置服务使用的ip
port 6351 //更改端口号
requirepass 123456 //设置密码
[root@redis1 utils]# /etc/init.d/redis_6379 start
Starting Redis server...
[root@redis1 utils]# ss -antulp | grep 6351
tcp LISTEN 0 128 192.168.4.1:6351 *:* users:(("redis-server",pid=8083,fd=6))
[root@redis1 utils]# redis-cli -h 192.168.4.1 -p 6351 #连接时需要加上端口
192.168.4.1:6351> ping
(error) NOAUTH Authentication required.
192.168.4.1:6351> auth 123456 #输入密码才能操作,否则报错
OK
192.168.4.1:6351> ping
PONG
[root@redis1 utils]# redis-cli -h 192.168.4.1 -p 6351 -a 123456 #连接时直接输入密码
192.168.4.1:6351> ping
PONG
停止服务时不能使用默认的方式停止,否则会报错
[root@redis1 utils]# redis-cli -h 192.168.4.1 -p 6351 -a 123456 shutdown
[root@redis1 utils]# ss -antulp | grep 6351
3) 部署LNMP+Redis
[root@lnmp ~]# yum -y install php-cli
[root@lnmp ~]# which php
/usr/bin/ph
[root@lnmp ~]# php -m
[root@lnmp ~]# php -m | grep -i redis #查找连接redis的功能模块(此处没有)
[root@lnmp ~]# tar -xf php-redis-2.2.4.tar.gz
[root@lnmp ~]# cd phpredis-2.2.4/
[root@lnmp phpredis-2.2.4]# which phpize
/usr/bin/phpize
[root@lnmp phpredis-2.2.4]# phpize
Can't find PHP headers in /usr/include/php
The php-devel package is required for use of this command.
[root@lnmp phpredis-2.2.4]# yum -y install autoconf automake pcre-devel
[root@lnmp ~]# rpm -ivh php-devel-5.4.16-42.el7.x86_64.rpm
[root@lnmp ~]# cd phpredis-2.2.4/
[root@lnmp phpredis-2.2.4]# phpize #生成一个php的文件
[root@lnmp phpredis-2.2.4]# find / -name php-config
/usr/bin/php-config
[root@lnmp phpredis-2.2.4]# yum -y install gcc gcc-c++ make
[root@lnmp phpredis-2.2.4]# ./configure \ #指定模块编译路径
> --with-php-config=/usr/bin/php-config
[root@lnmp phpredis-2.2.4]# make && make install
Installing shared extensions: /usr/lib64/php/modules/ #模块文件存放的路径
[root@lnmp phpredis-2.2.4]# ls /usr/lib64/php/modules/
curl.so fileinfo.so json.so phar.so redis.so zip.so
[root@lnmp phpredis-2.2.4]# vim /etc/php.ini
728 extension_dir = "/usr/lib64/php/modules/"
729 ; On windows:
730 extension = "redis.so"
[root@lnmp phpredis-2.2.4]# php -m | grep -i redis
redis
安装nginx
[root@lnmp ~]# cd lnmp_soft/
[root@lnmp lnmp_soft]# tar -xf nginx-1.12.2.tar.gz
[root@lnmp lnmp_soft]# cd nginx-1.12.2/
[root@lnmp nginx-1.12.2]# yum -y install gcc pcre-devel openssl-devel
[root@lnmp nginx-1.12.2]# useradd -s /sbin/nologin nginx
[root@lnmp nginx-1.12.2]# ./configure \
> --user=nginx \
> --group=nginx \
> --with-http_ssl_module
[root@lnmp nginx-1.12.2]# make && make install
[root@lnmp nginx-1.12.2]# ln -s /usr/local/nginx/sbin/nginx /sbin/
[root@lnmp nginx-1.12.2]# cd /usr/local/nginx/html
[root@lnmp html]# echo aa > text.html
[root@lnmp html]# yum -y install mariadb mariadb-server mariadb-devel php php-mysql
[root@lnmp ~]# cd lnmp_soft/
[root@lnmp lnmp_soft]# rpm -ivh php-fpm-5.4.16-42.el7.x86_64.rpm
[root@lnmp lnmp_soft]# cd /usr/local/nginx/html
[root@lnmp html]# vim test.php
<?php
$i=33;
$j=44;
if($i<$j){
echo "oK";
}
else{
echo "error";
}
#echo $i;
?>
[root@lnmp html]# php test.php #测试
oK
[root@lnmp html]# systemctl restart mariadb
[root@lnmp html]# systemctl restart php-fpm
[root@lnmp html]# vim /usr/local/nginx/conf/nginx.conf
...
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
#fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
include fastcgi.conf;
}
...
[root@lnmp html]# nginx
[root@lnmp html]# ss -antulp | grep 80
tcp LISTEN 0 128 *:80 *:* users:(("nginx",pid=8140,fd=6),("nginx",pid=8139,fd=6))
[root@room9pc01 ~]# firefox 192.168.4.8/text.html
[root@room9pc01 ~]# firefox 192.168.4.8/text.php
连接redis测试
[root@lnmp html]# vim lkredis.php
<?php
$redis = new redis();
$redis->connect('192.168.4.1',6351);
$redis ->auth("123456");
$redis->set("redistest","666666");
echo $redis->get("redistest");
?>
在redis上启动服务
[root@redis1 ~]# /etc/init.d/redis_6379 start
Starting Redis server...
[root@redis1 ~]# ss -antulp | grep 6351
tcp LISTEN 0 128 192.168.4.1:6351 *:* users:(("redis-server",pid=8811,fd=6))
[root@lnmp html]# php lkredis.php #测试
666666
[root@lnmp html]# firefox http://192.168.4.8/lkredis.php
在redis1上面查看,有数据存入
[root@redis1 ~]# redis-cli -h 192.168.4.1 -p 6351 -a 123456
192.168.4.1:6351> ping
PONG
192.168.4.1:6351> keys *
1) "redistest"
192.168.4.1:6351> get redistest
"666666"
4) redis集群
部署redis集群
真机上传包
[root@room9pc01 ~]# for i in {1..6}
> do
> scp '/root/桌面/10.nosql/redis-4.0.8.tar.gz' \
> 192.168.4.$i:/root/
> done
安装redis服务器,以redis2为例(全部的6台)
[root@redis2 ~]# yum -y install gcc gcc-c++ make
[root@redis2 ~]# tar -xf redis-4.0.8.tar.gz
[root@redis2 ~]# cd redis-4.0.8/
[root@redis2 redis-4.0.8]# ./utils/install_server.sh
[root@redis2 redis-4.0.8]# ss -antulp | grep 6379
tcp LISTEN 0 128 127.0.0.1:6379 *:* users:(("redis-server",pid=8967,fd=6))
[root@redis2 redis-4.0.8]# /etc/init.d/redis_6379 stop
Stopping ...
Redis stopped
[root@redis2 redis-4.0.8]# ss -antulp | grep 6379
[root@redis2 redis-4.0.8]# vim /etc/redis/6379.conf
...
bind 192.168.4.2 //修改ip
port 6352 //不允许相同,只指定物理接口的ip
daemonize yes //以守护进程方式运行
pidfile /var/run/redis_6352.pid
cluster-enabled yes //是否启用集群,前提是以守护进程方式运行
cluster-config-file nodes-6352.conf
//存储集群信息的配置文件,自动生成,不允许相同
cluster-node-timeout 5000 //集群节点通信超时时间
...
[root@redis2 redis-4.0.8]# /etc/init.d/redis_6379 start
Starting Redis server...
[root@redis2 redis-4.0.8]# ss -antulp | grep 6352
tcp LISTEN 0 128 192.168.4.2:6352 *:* users:(("redis-server",pid=9046,fd=6))
tcp LISTEN 0 128 192.168.4.2:16352 *:* users:(("redis-server",pid=9046,fd=8))
[root@redis2 redis-4.0.8]# ps -C redis-server
PID TTY TIME CMD
9046 ? 00:00:00 redis-server
[root@redis2 redis-4.0.8]# getenforce
Permissive
[root@redis2 redis-4.0.8]# systemctl disable firewalld #关闭防火墙
[root@redis2 redis-4.0.8]# redis-cli -h 192.168.4.2 -p 6352
192.168.4.2:6352> ping
PONG
192.168.4.2:6352> cluster info
cluster_state:fail
cluster_slots_assigned:0
cluster_slots_ok:0
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:1
cluster_size:0
cluster_current_epoch:0
cluster_my_epoch:0
cluster_stats_messages_sent:0
cluster_stats_messages_received:0
192.168.4.2:6352> cluster nodes
673dba4b1ab36bc1c6361957df2277d5fad689e4 :6352@16352 myself,master - 0 0 0 connected
创建集群(在任意一台上执行创建集群的脚本都可以)
真机上传需要的安装的包
[root@room9pc01 ~]# scp '/root/桌面/10.nosql/redis-cluster/ruby-devel-2.0.0.648-30.el7.x86_64.rpm' '/root/桌面/10.nosql/redis-cluster/redis-3.2.1.gem' 192.168.4.1:/root/
部署ruby脚本运行环境
[root@redis1 ~]# yum -y install rubygems ruby
[root@redis1 ~]# rpm -ivh --nodeps ruby-devel-2.0.0.648-30.el7.x86_64.rpm
[root@redis1 ~]# which gem
/usr/bin/gem
[root@redis1 ~]# gem install redis
Successfully installed redis-3.2.1
Parsing documentation for redis-3.2.1
Installing ri documentation for redis-3.2.1
1 gem installed
生成创建集群的脚本
[root@redis1 ~]# cd redis-4.0.8/
[root@redis1 redis-4.0.8]# cd src
[root@redis1 src]# cp redis-trib.rb /usr/local/bin/
[root@redis1 src]# ll /usr/local/bin/redis-trib.rb
-rwxr-xr-x. 1 root root 65991 11月 10 17:39 /usr/local/bin/redis-trib.rb
创建集群
[root@redis1 src]# redis-trib.rb create --replicas 1 \
> 192.168.4.1:6351 192.168.4.2:6352 \
> 192.168.4.3:6353 192.168.4.4:6354 \
> 192.168.4.5:6355 192.168.4.6:6356
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
192.168.4.1:6351
192.168.4.2:6352
192.168.4.3:6353
Adding replica 192.168.4.5:6355 to 192.168.4.1:6351
Adding replica 192.168.4.6:6356 to 192.168.4.2:6352
Adding replica 192.168.4.4:6354 to 192.168.4.3:6353
M: d1333b5fa89e3d3ee9be66043654532dde1ecd62 192.168.4.1:6351
slots:0-5460 (5461 slots) master
M: 673dba4b1ab36bc1c6361957df2277d5fad689e4 192.168.4.2:6352
slots:5461-10922 (5462 slots) master
M: c6730921b4bffe5c6747484122670176aa5763ff 192.168.4.3:6353
slots:10923-16383 (5461 slots) master
S: 5345ef3bcc52403a3d582fee15fa8a20a5746b03 192.168.4.4:6354
replicates c6730921b4bffe5c6747484122670176aa5763ff
S: edec71b5f0dbf358217fbfbac8fd1c9aa5588443 192.168.4.5:6355
replicates d1333b5fa89e3d3ee9be66043654532dde1ecd62
S: eb9ad1cfcb64e2106b71b434ef0ded0a69911cf5 192.168.4.6:6356
replicates 673dba4b1ab36bc1c6361957df2277d5fad689e4
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.4.1:6351)
M: d1333b5fa89e3d3ee9be66043654532dde1ecd62 192.168.4.1:6351
slots:0-5460 (5461 slots) master
1 additional replica(s)
S: edec71b5f0dbf358217fbfbac8fd1c9aa5588443 192.168.4.5:6355
slots: (0 slots) slave
replicates d1333b5fa89e3d3ee9be66043654532dde1ecd62
M: 673dba4b1ab36bc1c6361957df2277d5fad689e4 192.168.4.2:6352
slots:5461-10922 (5462 slots) master
1 additional replica(s)
S: 5345ef3bcc52403a3d582fee15fa8a20a5746b03 192.168.4.4:6354
slots: (0 slots) slave
replicates c6730921b4bffe5c6747484122670176aa5763ff
M: c6730921b4bffe5c6747484122670176aa5763ff 192.168.4.3:6353
slots:10923-16383 (5461 slots) master
1 additional replica(s)
S: eb9ad1cfcb64e2106b71b434ef0ded0a69911cf5 192.168.4.6:6356
slots: (0 slots) slave
replicates 673dba4b1ab36bc1c6361957df2277d5fad689e4
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
查看集群信息,任意一台主机访问本机的redis服务查看即可
cluster info 查看集群信息
cluster nodes 查看集群节点信息
[root@redis2 ~]# redis-cli -h 192.168.4.2 -p 6352
192.168.4.2:6352> ping
PONG
192.168.4.2:6352> cluster info #集群信息
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6
cluster_size:3
cluster_current_epoch:6
cluster_my_epoch:2
cluster_stats_messages_ping_sent:378
cluster_stats_messages_pong_sent:328
cluster_stats_messages_meet_sent:3
cluster_stats_messages_sent:709
cluster_stats_messages_ping_received:325
cluster_stats_messages_pong_received:381
cluster_stats_messages_meet_received:3
cluster_stats_messages_received:709
192.168.4.2:6352> cluster nodes #集群节点信息
673dba4b1ab36bc1c6361957df2277d5fad689e4 192.168.4.2:6352@16352 myself,master - 0 1541911289000 2 connected 5461-10922
d1333b5fa89e3d3ee9be66043654532dde1ecd62 192.168.4.1:6351@16351 master - 0 1541911290217 1 connected 0-5460
c6730921b4bffe5c6747484122670176aa5763ff 192.168.4.3:6353@16353 master - 0 1541911288512 3 connected 10923-16383
eb9ad1cfcb64e2106b71b434ef0ded0a69911cf5 192.168.4.6:6356@16356 slave 673dba4b1ab36bc1c6361957df2277d5fad689e4 0 1541911289716 6 connected
5345ef3bcc52403a3d582fee15fa8a20a5746b03 192.168.4.4:6354@16354 slave c6730921b4bffe5c6747484122670176aa5763ff 0 1541911289214 4 connected
edec71b5f0dbf358217fbfbac8fd1c9aa5588443 192.168.4.5:6355@16355 slave d1333b5fa89e3d3ee9be66043654532dde1ecd62 0 1541911289014 5 connected
测试集群
[root@redis1 src]# redis-cli -c -h 192.168.4.1 -p 6351
192.168.4.1:6351> set name tom
-> Redirected to slot [5798] located at 192.168.4.2:6352
OK
192.168.4.2:6352> get name
"tom"
192.168.4.2:6352> set age 25
-> Redirected to slot [741] located at 192.168.4.1:6351
OK
192.168.4.1:6351> get age
"25"
192.168.4.1:6351> set say hello
-> Redirected to slot [5885] located at 192.168.4.2:6352
OK
192.168.4.2:6352> set pay 2536
-> Redirected to slot [4013] located at 192.168.4.1:6351
OK
192.168.4.1:6351> get pay
"2536"
集群节点选举策略(三主,三从)
[root@redis1 src]# redis-cli -c -h 192.168.4.1 -p 6351
192.168.4.1:6351> cluster nodes
edec71b5f0dbf358217fbfbac8fd1c9aa5588443 192.168.4.5:6355@16355 slave d1333b5fa89e3d3ee9be66043654532dde1ecd62 0 1541911824106 5 connected
673dba4b1ab36bc1c6361957df2277d5fad689e4 192.168.4.2:6352@16352 master - 0 1541911823000 2 connected 5461-10922
5345ef3bcc52403a3d582fee15fa8a20a5746b03 192.168.4.4:6354@16354 slave c6730921b4bffe5c6747484122670176aa5763ff 0 1541911824105 4 connected
c6730921b4bffe5c6747484122670176aa5763ff 192.168.4.3:6353@16353 master - 0 1541911823605 3 connected 10923-16383
eb9ad1cfcb64e2106b71b434ef0ded0a69911cf5 192.168.4.6:6356@16356 slave 673dba4b1ab36bc1c6361957df2277d5fad689e4 0 1541911822601 6 connected
d1333b5fa89e3d3ee9be66043654532dde1ecd62 192.168.4.1:6351@16351 myself,master - 0 1541911822000 1 connected 0-5460
由上可以看出4.1是4.5的主
由上可以看出4.2是4.6的主
由上可以看出4.3是4.4的主
先停掉4.3服务,再开启
[root@redis3 ~]# ss -antulp | grep 6353
tcp LISTEN 0 128 192.168.4.3:6353 *:* users:(("redis-server",pid=1026,fd=6))
tcp LISTEN 0 128 192.168.4.3:16353 *:* users:(("redis-server",pid=1026,fd=8))
[root@redis3 ~]# redis-cli -h 192.168.4.3 -p 6353 shutdown
[root@redis3 ~]# ss -antulp | grep 6353
[root@redis3 ~]# ps -C redis-server
PID TTY TIME CMD
[root@redis3 ~]# /etc/init.d/redis_6379 start
Starting Redis server...
[root@redis3 ~]# ss -antulp | grep 6353
tcp LISTEN 0 128 192.168.4.3:6353 *:* users:(("redis-server",pid=1728,fd=6))
tcp LISTEN 0 128 192.168.4.3:16353 *:* users:(("redis-server",pid=1728,fd=8))
[root@redis3 ~]# ps -C redis-server
PID TTY TIME CMD
1728 ? 00:00:00 redis-server
再次查看主从信息
192.168.4.2:6352> cluster nodes
673dba4b1ab36bc1c6361957df2277d5fad689e4 192.168.4.2:6352@16352 myself,master - 0 1541912316000 2 connected 5461-10922
d1333b5fa89e3d3ee9be66043654532dde1ecd62 192.168.4.1:6351@16351 master - 0 1541912317026 1 connected 0-5460
c6730921b4bffe5c6747484122670176aa5763ff 192.168.4.3:6353@16353 slave 5345ef3bcc52403a3d582fee15fa8a20a5746b03 0 1541912316524 7 connected
eb9ad1cfcb64e2106b71b434ef0ded0a69911cf5 192.168.4.6:6356@16356 slave 673dba4b1ab36bc1c6361957df2277d5fad689e4 0 1541912315520 6 connected
5345ef3bcc52403a3d582fee15fa8a20a5746b03 192.168.4.4:6354@16354 master - 0 1541912317000 7 connected 10923-16383
edec71b5f0dbf358217fbfbac8fd1c9aa5588443 192.168.4.5:6355@16355 slave d1333b5fa89e3d3ee9be66043654532dde1ecd62 0 1541912315520 5 connected
由上可以看出,4.4成为了4.3的主
管理redis集群
添加一台主机redis7
部署redis
在redis1上把redis7添加到集群
[root@redis1 ~]# redis-trib.rb add-node 192.168.4.7:6357 192.168.4.1:6351
>>> Adding node 192.168.4.7:6357 to cluster 192.168.4.1:6351
>>> Performing Cluster Check (using node 192.168.4.1:6351)
M: d1333b5fa89e3d3ee9be66043654532dde1ecd62 192.168.4.1:6351
slots:0-5460 (5461 slots) master
1 additional replica(s)
S: edec71b5f0dbf358217fbfbac8fd1c9aa5588443 192.168.4.5:6355
slots: (0 slots) slave
replicates d1333b5fa89e3d3ee9be66043654532dde1ecd62
M: 673dba4b1ab36bc1c6361957df2277d5fad689e4 192.168.4.2:6352
slots:5461-10922 (5462 slots) master
1 additional replica(s)
M: 5345ef3bcc52403a3d582fee15fa8a20a5746b03 192.168.4.4:6354
slots:10923-16383 (5461 slots) master
1 additional replica(s)
S: c6730921b4bffe5c6747484122670176aa5763ff 192.168.4.3:6353
slots: (0 slots) slave
replicates 5345ef3bcc52403a3d582fee15fa8a20a5746b03
S: eb9ad1cfcb64e2106b71b434ef0ded0a69911cf5 192.168.4.6:6356
slots: (0 slots) slave
replicates 673dba4b1ab36bc1c6361957df2277d5fad689e4
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
>>> Send CLUSTER MEET to node 192.168.4.7:6357 to make it join the cluster.
[OK] New node added correctly.
[root@redis1 ~]# redis-trib.rb check 192.168.4.7:6357 #检查状态
>>> Performing Cluster Check (using node 192.168.4.7:6357)
M: 1cb7ab37b5114b14c6fdb459a319407b4924dd05 192.168.4.7:6357
slots: (0 slots) master
0 additional replica(s)
S: eb9ad1cfcb64e2106b71b434ef0ded0a69911cf5 192.168.4.6:6356
slots: (0 slots) slave
replicates 673dba4b1ab36bc1c6361957df2277d5fad689e4
S: c6730921b4bffe5c6747484122670176aa5763ff 192.168.4.3:6353
slots: (0 slots) slave
replicates 5345ef3bcc52403a3d582fee15fa8a20a5746b03
M: 673dba4b1ab36bc1c6361957df2277d5fad689e4 192.168.4.2:6352
slots:5461-10922 (5462 slots) master
1 additional replica(s)
M: d1333b5fa89e3d3ee9be66043654532dde1ecd62 192.168.4.1:6351
slots:0-5460 (5461 slots) master
1 additional replica(s)
S: edec71b5f0dbf358217fbfbac8fd1c9aa5588443 192.168.4.5:6355
slots: (0 slots) slave
replicates d1333b5fa89e3d3ee9be66043654532dde1ecd62
M: 5345ef3bcc52403a3d582fee15fa8a20a5746b03 192.168.4.4:6354
slots:10923-16383 (5461 slots) master
1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
手动对集群进行分片迁移
reshard 重新分配hash槽
[root@redis1 ~]# redis-trib.rb reshard 192.168.4.7:6357
...
#拿出多少个hash 槽给主机192.168.4.7
How many slots do you want to move (from 1 to 16384)? 4096
#主机192.168.4.7的id值
What is the receiving node ID? 1cb7ab37b5114b14c6fdb459a319407b4924dd05
#从当前所有的主里面获取hash槽
Source node #1:all
Do you want to proceed with the proposed reshard plan (yes/no)?yes
...
[root@redis1 ~]# redis-trib.rb check 192.168.4.7:6357
>>> Performing Cluster Check (using node 192.168.4.7:6357)
M: 1cb7ab37b5114b14c6fdb459a319407b4924dd05 192.168.4.7:6357
slots:0-1364,5461-6826,10923-12287 (4096 slots) master #有4096个hash slot
0 additional replica(s)
删除master角色的主机
先删除主机占用的hash槽
[root@redis1 ~]# redis-trib.rb reshard 192.168.4.7:6357
.....
#移除hash槽的个数
How many slots do you want to move (from 1 to 16384)? 4096
#要移动给谁的id即目标主机(这里可以随机写一个master的ID)
What is the receiving node ID? 673dba4b1ab36bc1c6361957df2277d5fad689e4
#从谁那移动即源主机(这里写4.58的ID)
Source node #1:1cb7ab37b5114b14c6fdb459a319407b4924dd05
#设置完毕
Source node #2:done
Do you want to proceed with the proposed reshard plan (yes/no)? yes
.......
[root@redis1 ~]# redis-trib.rb check 192.168.4.7:6357
>>> Performing Cluster Check (using node 192.168.4.7:6357)
M: 1cb7ab37b5114b14c6fdb459a319407b4924dd05 192.168.4.7:6357
slots: (0 slots) master
0 additional replica(s)
删除集群主机4.7(删除之后redis服务自动关闭)
[root@redis1 ~]# redis-trib.rb del-node 192.168.4.7:6357 \
> 1cb7ab37b5114b14c6fdb459a319407b4924dd05
>>> Removing node 1cb7ab37b5114b14c6fdb459a319407b4924dd05 from cluster 192.168.4.7:6357
>>> Sending CLUSTER FORGET messages to the cluster...
>>> SHUTDOWN the node.
添加从节点主机,随机添加
清除原来的信息,否则在redis1上添加从节点会报错
[root@redis7 ~]# redis-cli -h 192.168.4.7 -p 6357
192.168.4.7:6357> flushdb
OK
[root@redis7 redis-4.0.8]# redis-cli -h 192.168.4.7 -p 6357 shutdown
[root@redis7 redis-4.0.8]# rm -rf /var/lib/redis/6379/nodes-6357.conf
[root@redis7 redis-4.0.8]# /etc/init.d/redis_6379 start
[root@redis7 redis-4.0.8]# ss -natulp | grep 6357
添加
[root@redis1 ~]# redis-trib.rb add-node --slave 192.168.4.7:6357 192.168.4.1:6351
>>> Adding node 192.168.4.7:6357 to cluster 192.168.4.1:6351
>>> Performing Cluster Check (using node 192.168.4.1:6351)
M: d1333b5fa89e3d3ee9be66043654532dde1ecd62 192.168.4.1:6351
slots:1365-5460 (4096 slots) master
1 additional replica(s)
S: edec71b5f0dbf358217fbfbac8fd1c9aa5588443 192.168.4.5:6355
slots: (0 slots) slave
replicates d1333b5fa89e3d3ee9be66043654532dde1ecd62
M: 673dba4b1ab36bc1c6361957df2277d5fad689e4 192.168.4.2:6352
slots:0-1364,5461-12287 (8192 slots) master
1 additional replica(s)
M: 5345ef3bcc52403a3d582fee15fa8a20a5746b03 192.168.4.4:6354
slots:12288-16383 (4096 slots) master
1 additional replica(s)
S: c6730921b4bffe5c6747484122670176aa5763ff 192.168.4.3:6353
slots: (0 slots) slave
replicates 5345ef3bcc52403a3d582fee15fa8a20a5746b03
S: eb9ad1cfcb64e2106b71b434ef0ded0a69911cf5 192.168.4.6:6356
slots: (0 slots) slave
replicates 673dba4b1ab36bc1c6361957df2277d5fad689e4
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
Automatically selected master 192.168.4.1:6351
>>> Send CLUSTER MEET to node 192.168.4.7:6357 to make it join the cluster.
Waiting for the cluster to join.
>>> Configure node as replica of 192.168.4.1:6351.
[OK] New node added correctly.
移除从节点,从节点主机没有槽位范围,直接移除即可
[root@redis1 ~]# redis-trib.rb del-node 192.168.4.7:6357 \
> f9084dae7687bea552bb88055ca1adf24bae46df
>>> Removing node f9084dae7687bea552bb88055ca1adf24bae46df from cluster 192.168.4.7:6357
>>> Sending CLUSTER FORGET messages to the cluster...
>>> SHUTDOWN the node.
配置redis主从复制
4.1为主,4.2为从
还原4.1和4.2
[root@redis1 ~]# redis-cli -c -h 192.168.4.1 -p 6351 shutdown
[root@redis1 ~]# ss -antulp | grep 6351
[root@redis1 ~]# vim /etc/redis/6379.conf
bind 192.168.4.1
port 6379
# cluster-enabled yes #关闭集群
# cluster-config-file nodes-6351.conf #关闭集群信息的配置文件
[root@redis1 ~]# /etc/init.d/redis_6379 start
Starting Redis server...
[root@redis1 ~]# ss -antulp | grep 6379
tcp LISTEN 0 128 192.168.4.1:6379 *:* users:(("redis-server",pid=8602,fd=6))
[root@redis1 ~]# redis-cli -h 192.168.4.1
192.168.4.1:6379> info replication #查看主从配置信息
# Replication
role:master #默认是master 服务器
connected_slaves:0
master_replid:43b4754fe2310e5704004617ca190f9003206d95
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:0
second_repl_offset:-1
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
[root@redis2 ~]# redis-cli -c -h 192.168.4.2 -p 6352 shutdown
[root@redis2 ~]# ss -antulp | grep 6352
[root@redis2 ~]# vim /etc/redis/6379.conf
bind 192.168.4.2
port 6379
# cluster-enabled yes #关闭集群
# cluster-config-file nodes-6352.conf #关闭集群信息的配置文件
[root@redis2 ~]# /etc/init.d/redis_6379 start
Starting Redis server...
[root@redis2 ~]# ss -antulp | grep 6379
tcp LISTEN 0 128 192.168.4.2:6379 *:* users:(("redis-server",pid=3565,fd=6))
[root@redis2 ~]# redis-cli -h 192.168.4.2
192.168.4.2:6379> info replication
# Replication
role:master
connected_slaves:0
master_replid:4bc2d4da27307dceb66a5602f54e16c3c9477ddf
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:0
second_repl_offset:-1
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
配置从库192.168.4.2
192.168.4.2:6379> slaveof 192.168.4.1 6379 #将4.2配置为4.1的从库
OK
在从库再次查看信息
192.168.4.2:6379> info replication
# Replication
role:slave
master_host:192.168.4.1
master_port:6379
master_link_status:up
master_last_io_seconds_ago:8
master_sync_in_progress:0
slave_repl_offset:126
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:c9b9e2fb4ae04bdfaea621943fab736680688446
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:126
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:126
在主库4.1查看
192.168.4.1:6379> info replication
# Replication
role:master
connected_slaves:1
slave0:ip=192.168.4.2,port=6379,state=online,offset=196,lag=0
master_replid:c9b9e2fb4ae04bdfaea621943fab736680688446
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:196
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:196
主库宕机后,手动将从库设置为主库
[root@redis1 ~]# redis-cli -h 192.168.4.1 shutdown
[root@redis1 ~]# ss -antulp | grep 6379
192.168.4.2:6379> slaveof no one #手动设置为主库
OK
192.168.4.2:6379> info replication
# Replication
role:master
connected_slaves:0
master_replid:2e3bfc3cc16972071937d0646346451434afeb88
master_replid2:c9b9e2fb4ae04bdfaea621943fab736680688446
master_repl_offset:308
second_repl_offset:309
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:308
哨兵模式
主库宕机后,从库自动升级为主库
在slave主机编辑sentinel.conf文件
在slave主机运行哨兵程序
[root@redis1 ~]# /etc/init.d/redis_6379 start
[root@redis1 ~]# ss -antulp | grep 6379
tcp LISTEN 0 128 192.168.4.1:6379 *:* users:(("redis-server",pid=8736,fd=6))
192.168.4.2:6379> slaveof 192.168.4.1 6379
OK
192.168.4.2:6379> info replication
# Replication
role:slave
master_host:192.168.4.1
master_port:6379
master_link_status:up
master_last_io_seconds_ago:5
master_sync_in_progress:0
slave_repl_offset:0
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:f9caad197d44a4a51a49fa912dea05692594400b
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:0
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:0
[root@redis1 ~]# redis-cli -h 192.168.4.1
192.168.4.1:6379> info replication
# Replication
role:master
connected_slaves:1
slave0:ip=192.168.4.2,port=6379,state=online,offset=70,lag=1
master_replid:f9caad197d44a4a51a49fa912dea05692594400b
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:70
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:70
[root@redis2 ~]# vim /etc/sentinel.conf
sentinel monitor redisA 192.168.4.51 6379 1
#关键字 关键字 主机名自定义 ip 端口 票数
#sentinel auth-pass redis51 密码 //连接主库密码,若主库有密码加上这一行
[root@redis2 ~]# redis-sentinel /etc/sentinel.conf
将主即4.1关闭redis服务查看上面命令的显示信息
3709:X 11 Nov 15:57:52.087 # +sdown master redis1 192.168.4.1 6379
3709:X 11 Nov 15:57:52.087 # +odown master redis1 192.168.4.1 6379 #quorum 1/1
3709:X 11 Nov 15:57:52.087 # +new-epoch 1
3709:X 11 Nov 15:57:52.087 # +try-failover master redis1 192.168.4.1 6379
3709:X 11 Nov 15:57:52.126 # +vote-for-leader 30d5b295599bdad1e6c5d684e28dc893206deaf5 1
3709:X 11 Nov 15:57:52.126 # +elected-leader master redis1 192.168.4.1 6379
3709:X 11 Nov 15:57:52.126 # +failover-state-select-slave master redis1 192.168.4.1 6379
3709:X 11 Nov 15:57:52.203 # +selected-slave slave 192.168.4.2:6379 192.168.4.2 6379 @ redis1 192.168.4.1 6379
3709:X 11 Nov 15:57:52.203 * +failover-state-send-slaveof-noone slave 192.168.4.2:6379 192.168.4.2 6379 @ redis1 192.168.4.1 6379
3709:X 11 Nov 15:57:52.287 * +failover-state-wait-promotion slave 192.168.4.2:6379 192.168.4.2 6379 @ redis1 192.168.4.1 6379
3709:X 11 Nov 15:57:53.277 # +promoted-slave slave 192.168.4.2:6379 192.168.4.2 6379 @ redis1 192.168.4.1 6379
3709:X 11 Nov 15:57:53.277 # +failover-state-reconf-slaves master redis1 192.168.4.1 6379
3709:X 11 Nov 15:57:53.354 # +failover-end master redis1 192.168.4.1 6379
3709:X 11 Nov 15:57:53.355 # +switch-master redis1 192.168.4.1 6379 192.168.4.2 6379
3709:X 11 Nov 15:57:53.355 * +slave slave 192.168.4.1:6379 192.168.4.1 6379 @ redis1 192.168.4.2 6379
3709:X 11 Nov 15:58:23.388 # +sdown slave 192.168.4.1:6379 192.168.4.1 6379 @ redis1 192.168.4.2 6379
启动4.1进行查看
[root@redis1 ~]# /etc/init.d/redis_6379 start
Starting Redis server...
[root@redis1 ~]# ss -antulp | grep 6379
tcp LISTEN 0 128 192.168.4.1:6379 *:* users:(("redis-server",pid=8841,fd=6))
[root@redis1 ~]# redis-cli -h 192.168.4.1
192.168.4.1:6379> info replication
# Replication
role:slave
master_host:192.168.4.2
master_port:6379
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_repl_offset:17839
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:78db61939af082a46c353edb1ec0d40b41b9e9be
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:17839
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:16020
repl_backlog_histlen:1820
配置带验证的主从复制
关闭4.1和4.2,启动之后用info replication查看,各自为主
主库设置密码,在1上面操作
[root@redis1 ~]# redis-cli -h 192.168.4.1 shutdown
[root@redis1 ~]# vim /etc/redis/6379.conf
requirepass 123456
[root@redis1 ~]# /etc/init.d/redis_6379 start
Starting Redis server...
[root@redis1 ~]# redis-cli -h 192.168.4.1 -a 123456
192.168.4.1:6379> ping
PONG
配置从库主机
[root@redis2 ~]# redis-cli -h 192.168.4.2 shutdown
[root@redis2 ~]# vim /etc/redis/6379.conf
slaveof 192.168.4.1 6379
masterauth 123456
[root@redis2 ~]# /etc/init.d/redis_6379 start
Starting Redis server...
使用RDB文件恢复数据
数据持久化方式之一
在指定时间间隔内,将内存中的数据集快照写入硬盘
术语叫Snapshot快照
恢复时,将快照文件直接读到内存里
相关配置参数
文件名
dbfilename “dump.rdb” 文件名
save “” 禁用RDB
数据从内存保存到硬盘的频率
save 900 1 900秒内且有1次修改
save 300 10 300秒内且有10次修改
save 60 10000 60秒内且有10000修改
[root@redis1 ~]# redis-cli -h 192.168.4.1 -a 123456 shutdown
[root@redis1 ~]# vim /etc/redis/6379.conf
dbfilename dump.rdb
# save "" //启用RDB,去掉#号为禁用RDB
save 120 10 //120秒内且有1次修改(满足三个条件中的任意一个都会保存)
save 300 10
save 60 10000
进入redis中,写入数据,并查看
备份数据
关闭redis服务,进入/var/lib/redis/6379/,备份dump.rdb为dump.rdb.bak
删除数据
rm -rf dump.rdb
启动服务,并在redis内查看是否有数据
/etc/init.d/redis_6379 start
恢复数据
关闭redis
将备份还原
mv dump.rdb.bak dump.rdb
启动redis服务,并查看是否有数据
RDB优点:
高性能的持久化实现:创建一个子进程来执行持久化,先将数据写入临时文件,持久化过程结束后,再用这个临时文件替换上次持久化好的文件;过程中主进程不做任何IO操作
比较适合大规模数据恢复,且对数据完整性要求不是非常高的场合
RDB的缺点:
意外宕机时,最后一次持久化的数据会丢失
使用AOF文件恢复数据
1)AOF介绍
只做追加操作的文件,Append Only File
记录redis服务所有写操作
不断的将新的写操作,追加到文件的末尾
使用cat命令可以查看文件内容
2)参数配置
文件名
appendfilename "appendonly.aof" 指定文件名
appendonly yes 启用aof ,默认no
AOF文件记录写操作的方式
appendfsync always 有新写操作立即记录
appendfsync everysec 每秒记录一次
appendfsync no 从不记录
操作参考上面
修复AOF文件,把文件恢复到最后一次的正确操作
[root@redis1 6379]# vim appendonly.aof
*2 //可以把这一行删除
$6
SELECT
$1
0
*3
$3
set
$2
v1
$2
a1
*3
$3
...
[root@redisA 6379]# redis-check-aof --fix appendonly.aof //恢复文件
0x 0: Expected prefix '*', got: '$'
AOF analyzed: size=311, ok_up_to=0, diff=311
This will shrink the AOF from 311 bytes, with 311 bytes, to 0 bytes
Continue? [y/N]: y
Successfully truncated AOF
RDB优点:
可以灵活的设置同步持久化appendfsync always或异步持久化appendfsync verysec
宕机时,仅可能丢失1秒的数据
RDB的缺点:
AOF文件的体积通常会大于RDB文件的体积
执行fsync策略时的速度可能会比RDB慢