关系数据库管理系统
Relational Database Management System
数据按照预先设置的组织结构,存储在物理存储介质上。
数据之间可以做关联操作
主流的RDBMS软件
Oracle
DB2
WS-sqlserver
MySQ
NoSQL(NoSQL = Not Only SQL )
意思是"不仅仅是SQL“
泛指非关系型数据库
不需要预先定义数据存储结构
表的每条记录都可以有不同的类型和结构
主流软件
Redis
MongoDB
Memcached
CouchDB
Neo4j
FlockDB
Redis
Remode DIctionary Server(远程字典服务器)
使用C语言编写的,遵守BSD的开源软件
是一款高性能的(Key/Values)分布式内存数据库
并支持数据持久化的NoSQL数据库服务软件
中文网站www.redis.cn
Redis特点:
支持数据持久化,可以把内存里数据保存到硬盘中
不仅仅支持key/values类型的数据,同时还支持list hash set zset 类型
支持master-salve 模式数据备份
+++++++++++++搭建redis服务器
# tar -xzf redis-4.0.8.tar.gz
# cd redis-4.0.8
#make
#make install
tar -zxf redis-4.0.8.tar.gz
cd redis-4.0.8/
rpm -q gcc gcc-c++ || yum -y install gcc gcc-c++
make && make install
初始化配置
[root@localhost redis-4.0.8]# ./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 //命令行连接redis服务命令
Is this ok? Then press ENTER to go on or Ctrl-C to abort. //回车完成配置
Copied /tmp/6379.conf => /etc/init.d/redis_6379 //服务启动脚本
启动redis服务(软件安装完成后服务自动运行)
[root@localhost redis-4.0.8]# /etc/init.d/redis_6379 status
Redis is running (5341)
[root@localhost redis-4.0.8]# netstat -utnlp | grep :6379
tcp 0 0 127.0.0.1:6379 0.0.0.0:* LISTEN 5341/redis-server 1
[root@localhost redis-4.0.8]#
[root@localhost redis-4.0.8]# ps -C redis-server
PID TTY TIME CMD
5341 ? 00:00:00 redis-server
[root@localhost redis-4.0.
Set keyname keyvalue //存储
get keyname //获取
Select 数据库编号0-15 //切换库
Keys * //打印所以变量
Keys a? //打印指定变量
Exists keyname //测试是否存在
ttl keyname //查看生存时间
type keyname //查看类型
连接本机redis服务
[root@localhost redis-4.0.8]# redis-cli
127.0.0.1:6379> ping
PONG
127.0.0.1:6379> set k1 v1
OK
127.0.0.1:6379> get k1
"v1"
127.0.0.1:6379> SHUTDOWN
not connected> exit
[root@localhost redis-4.0.8]#
--------配置文件解析(1)
计量单位
port 6379 //端口
bind 127.0.0.1 //IP地址
tcp-backlog 511 //tcp连接总数
timeout 0 //连接超时时间
tcp-keepalive 300 //长连接时间
daemonize yes //守护进程方式运行
databases 16 //数据库个数
logfile /var/log/redis_6379.log //pid文件
maxclients 10000 //并发连接数量
dir /var/lib/redis/6379 //数据库目录
93 port 6379
70 bind 127.0.0.1
102 tcp-backlog 511
114 timeout 0
131 tcp-keepalive 300
137 daemonize yes
日志级别
163 # debug (a lot of information, useful for development/testing)
164 # verbose (many rarely useful info, but not a mess like the debug level)
165 # notice (moderately verbose, what you want in production probably)
166 # warning (only very important / critical messages are logged)
167 loglevel notice
187 databases 16
172 logfile /var/log/redis_6379.log
533 # maxclients 10000
264 dir /var/lib/redis/6379
# requirepass foobared 设置登陆密码 设置密码后的登陆方式AUTH <PASSWORD>
设置密码
grep -n requirepass /etc/redis/6379.conf
501:requirepass 123456
[root@localhost redis-4.0.8]# redis-cli
127.0.0.1:6379> ping
(error) NOAUTH Authentication required.
127.0.0.1:6379> auth 123456 //输入密码
OK
127.0.0.1:6379> ping
PONG
127.0.0.1:6379>
MEMORY MANAGEMENT 内存管理
560 # maxmemory <bytes>
内存清除策略
565 # volatile-lru -> Evict using approximated LRU among the keys with an expire set. 最近最少使用 (针对设置了过期时间的key)
566 # allkeys-lru -> Evict any key using approximated LRU. 删除最少使用的key
569 # volatile-random -> Remove a random key among the ones with an expire set. 在设置了过期的key里随机移除
570 # allkeys-random -> Remove a random key, any key. 随机移除key
571 # volatile-ttl -> Remove the key with the nearest expire time (minor TTL) 移除最近过期的key
572 # noeviction -> Don't evict anything, just return an error on write operations. 不删除 写满时报错
591 # maxmemory-policy noeviction 定义使用的策略
602 # maxmemory-samples 5 选取模板数据的个数 (针对lru 和 ttl 策略)
修改密码后如何调用/etc/init.d/redis_6379 脚本停止服务
给主机53的redis服务设置连接密码 密码是123456
vim /etc/redis/6379.conf
501 requirepass 123456
:wq
/etc/init.d/redis_6379 stop
/etc/init.d/redis_6379 start
连接带认证redis服务
[root@db53 ~]# redis-cli
127.0.0.1:6379> ping
(error) NOAUTH Authentication required.
127.0.0.1:6379> auth 123456
OK
127.0.0.1:6379> ping
PONG
127.0.0.1:6379> quit
[root@db53 ~]# redis-cli -a 123456
127.0.0.1:6379> ping
PONG
127.0.0.1:6379>
]# /etc/init.d/redis_6379 stop
[root@db53 ~]# redis-cli -p 6379 -a 123456 shutdown
]# /etc/init.d/redis_6379 start
------设置连接密码后如何使用脚本停止redis服务
vim /etc/init.d/redis_6379
43 $CLIEXEC -p $REDISPORT -a 123456 shutdown
:wq
]# /etc/init.d/redis_6379 stop
++++++++++++++++++++++++++++++
]#yum -y install pcre-devel zlib-devel
]#tar -zxf nginx-1.12.2.tar.gz
]#cd nginx-1.12.2/
]# ./configure --prefix=/usr/local/nginx
make&&make install
[root@db54 nginx-1.12.2]# ls /usr/local/nginx/
conf html logs sbin
./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre=<path> option.
./configure: error: the HTTP gzip module requires the zlib library.
You can either disable the module by using --without-http_gzip_module
option, or install the zlib library into the system, or build the zlib library
statically from the source with nginx by using --with-zlib=<path> option.
[root@db54 nginx-1.12.2]# /usr/local/nginx/sbin/nginx
[root@db54 nginx-1.12.2]# netstat -utnlp | grep :80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 23909/nginx: master
[root@db54 nginx-1.12.2]#
[root@db54 nginx-1.12.2]# ps -C nginx
PID TTY TIME CMD
23909 ? 00:00:00 nginx
23910 ? 00:00:00 nginx
[root@db54 nginx-1.12.2]#
108 yum -y install php-common
109 rpm -q php-common
110 rpm -ivh php-fpm-5.4.16-42.el7.x86_64.rpm
111 systemctl status php-fpm
112 systemctl enable php-fpm
113 systemctl start php-fpm
114 netstat -utnlp | grep :9000
[root@db54 lnmp]# systemctl start php-fpm
[root@db54 lnmp]# netstat -utnlp | grep :9000
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 24312/php-fpm:
mast
[root@db54 lnmp]#
]# 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 $document_root/$fastcgi_script_name;
include fastcgi_params;
}
:wq
[root@db54 lnmp]# /usr/local/nginx/sbin/nginx -s stop
[root@db54 lnmp]#
[root@db54 lnmp]#
[root@db54 lnmp]# /usr/local/nginx/sbin/nginx
[root@db54 lnmp]# vim /usr/local/nginx/html/test.php
<?php
phpinfo();
?>
[root@db54 lnmp]#
13 yum -y install autoconf automake
14 rpm -ivh php-devel-5.4.16-42.el7.x86_64.rpm
1 tar -zxf php-redis-2.2.4.tar.gz
2 cd phpredis-2.2.4/
]# cd phpredis-2.2.4
[root@db54 phpredis-2.2.4]# phpize
Configuring for:
PHP Api Version: 20100412
Zend Module Api No: 20100525
Zend Extension Api No: 220100525
[root@db54 phpredis-2.2.4]#
]# ll /usr/bin/php-config
]# ./configure --with-php-config=/usr/bin/php-config
]# make
]# make install
[root@db54 phpredis-2.2.4]# make install
Installing shared extensions: /usr/lib64/php/modules/
[root@db54 phpredis-2.2.4]#
[root@db54 phpredis-2.2.4]#
[root@db54 phpredis-2.2.4]# ls /usr/lib64/php/modules/
curl.so fileinfo.so json.so phar.so redis.so zip.so
[root@db54 phpredis-2.2.4]#
[root@db54 phpredis-2.2.4]# vim /etc/php.ini
728 extension_dir = "/usr/lib64/php/modules/"
729 ; On windows:
730 extension = "redis.so"
:wq
[root@db54 phpredis-2.2.4]# php -m | grep -i redis
redis
]# systemctl stop php-fpm
]# systemctl start php-fpm
+++++++++++运行Redis服务
45 tar -zxf redis-4.0.8.tar.gz
46 cd redis-4.0.8/
48 make
49 make install
51 ./utils/install_server.sh
[root@db54 redis-4.0.8]# /etc/init.d/redis_6379 status
Redis is running (32621)
[root@db54 redis-4.0.8]#
[root@db54 redis-4.0.8]# redis-cli
127.0.0.1:6379> keys *
(empty list or set)
127.0.0.1:6379> quit
[root@db54 redis-4.0.8]#
vim /usr/local/nginx/html/lkredis.php
<?php
$redis = new redis();
$redis->connect('127.0.0.1', 6379);
$redis->set('redistest','666666');
echo $redis->get('redistest');
?>
:wq
254]# http://192.168.4.54/lkredis.php
[root@db54 redis-4.0.8]# redis-cli
127.0.0.1:6379> keys *
1) "redistest"
127.0.0.1:6379> get redistest
"666666"
127.0.0.1:6379>
redis-cli -h 192.168.4.71 -p 6371 shutdown
/etc/init.d/redis_6379 start
vim /etc/redis/6379.conf
$CLIEXEC -h 192.168.4.72 -p 6372 shutdown
netstat -utnlp | grep redis
准备6台redis服务器,具体要求如下:
ip地址 端口 etho 日志文件名
192.168.4.51 6051 都可以接收连接请求 redis51.log
192.168.4.52 6052 都可以接收连接请求 redis52.log
192.168.4.53 6053 都可以接收连接请求 redis53.log
192.168.4.54 6054 都可以接收连接请求 redis54.log
192.168.4.55 6055 都可以接收连接请求 redis55.log
192.168.4.56 6056 都可以接收连接请求 redis56.log
环境准备
1 在6台redis服务器上运行服务,按照如下要求修改配置文件后,重启redis服务。
]#vim /etc/redis/6379.conf
bind 192.168.4.56 #只指定物理接口ip地址
port 6056 #不允许相同
daemonize yes
cluster-enabled yes
pidfile /var/run/redis_56.pid #不允许相同
cluster-config-file nodes-6056.conf #不允许相同
cluster-node-timeout 5000 #集群节点之间通信超时时间单位秒
:wq
停止服务
]# redis-cli -h 192.168.4.56 -p 6056 [ -a 密码 ] shutdown
启动服务
]# /etc/init.d/redis_6379 start
连接服务
[root@host56 ~]# redis-cli -h 192.168.4.56 -p 6056
装包: 在执行创建集群命令的主机安装ruby软件包(192.168.4.51)
安装解释ruby代码的软件包
[root@host51 ~]# yum -y install ruby rubygems
[root@host51 ~]# rpm -q rubygems ruby
rubygems-2.0.14.1-30.el7.noarch
ruby-2.0.0.648-30.el7.x86_64
[root@host51 ~]#
[root@host51 redis-cluster]# rpm -ivh --nodeps ruby-devel-2.0.0.648-30.el7.x86_64.rpm
安装ruby连接redis 接口程序 gem
[root@host51 redis-cluster]# which gem
/usr/bin/gem
[root@host51 redis-cluster]# gem install redis-3.2.1.gem
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@host51 redis-cluster]#
生成创建集群的脚本redis-trib.rb
[root@host51 src]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@host51 src]# cd
[root@host51 ~]# cp redis-4.0.8/src/redis-trib.rb /usr/local/bin/
创建集群:
redis-trib.rb create --replicas 1 192.168.4.1:6301 192.168.4.2:6302 192.168.4.4:6304 192.168.4.5:6305 192.168.4.3:6303 192.168.4.6:6306
[ERR] Node 192.168.4.54:6054 is not empty. Either the node already knows other nodes (check with
CLUSTER NODES) or contains some key in database 0.
[root@db54 ~]# redis-cli -h 192.168.4.54 -p 6054
192.168.4.54:6054> flushallcd
OK
192.168.4.54:6054> keys *
(empty list or set)
192.168.4.54:6054> flushall
OK
192.168.4.54:6054> save
OK
192.168.4.54:6054> quit
[root@db54 ~]#
槽位个数是16384
范围0-16383
[root@host51 ~]# redis-trib.rb create --replicas 1 192.168.4.51:6051 192.168.4.52:6052
192.168.4.53:6053 192.168.4.54:6054 192.168.4.55:6055 192.168.4.56:6056 #创建集群
--replicas 1 表示给master 配置1个slave
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
192.168.4.51:6051
192.168.4.52:6052
192.168.4.53:6053
Adding replica 192.168.4.55:6055 to 192.168.4.51:6051
Adding replica 192.168.4.56:6056 to 192.168.4.52:6052
Adding replica 192.168.4.54:6054 to 192.168.4.53:6053
M: 0ec903c572270a90f3b140fba31aac15aaf5336b 192.168.4.51:6051
slots:0-5460 (5461 slots) master
M: 5278df7384edc9774b1a36b0b9d60a813a7424a9 192.168.4.52:6052
slots:5461-10922 (5462 slots) master
M: 6cdb4c64c48c0ee2ca35bf139660f31ca92821dc 192.168.4.53:6053
slots:10923-16383 (5461 slots) master
S: 388c33e7128fc961b381ad3b3c27c3c217912666 192.168.4.54:6054
replicates 6cdb4c64c48c0ee2ca35bf139660f31ca92821dc
S: 651f7d99965316c1b8a27a2e9b034a5b14c2be55 192.168.4.55:6055
replicates 0ec903c572270a90f3b140fba31aac15aaf5336b
S: a3af3096ee214c92a178eadf6e9299584899e62f 192.168.4.56:6056
replicates 5278df7384edc9774b1a36b0b9d60a813a7424a9
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.51:6051)
M: 0ec903c572270a90f3b140fba31aac15aaf5336b 192.168.4.51:6051
slots:0-5460 (5461 slots) master
1 additional replica(s)
M: 388c33e7128fc961b381ad3b3c27c3c217912666 192.168.4.54:6054
slots:9678 (1 slots) master
0 additional replica(s)
S: 651f7d99965316c1b8a27a2e9b034a5b14c2be55 192.168.4.55:6055
slots: (0 slots) slave
replicates 0ec903c572270a90f3b140fba31aac15aaf5336b
S: a3af3096ee214c92a178eadf6e9299584899e62f 192.168.4.56:6056
slots: (0 slots) slave
replicates 5278df7384edc9774b1a36b0b9d60a813a7424a9
M: 6cdb4c64c48c0ee2ca35bf139660f31ca92821dc 192.168.4.53:6053
slots:10923-16383 (5461 slots) master
0 additional replica(s)
M: 5278df7384edc9774b1a36b0b9d60a813a7424a9 192.168.4.52:6052
slots:5461-9677,9679-10922 (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.
[root@host51 ~]#
[root@db52 redis-4.0.8]# cat /var/lib/redis/6052/nodes-6052.conf #集群配置文件
6cdb4c64c48c0ee2ca35bf139660f31ca92821dc 192.168.4.53:6053@16053 master - 0 1526096611374
3 connected 10923-16383
0ec903c572270a90f3b140fba31aac15aaf5336b 192.168.4.51:6051@16051 master - 0 1526096612000
1 connected 0-5460
388c33e7128fc961b381ad3b3c27c3c217912666 192.168.4.54:6054@16054 master - 0 1526096611043
4 connected 9678
a3af3096ee214c92a178eadf6e9299584899e62f 192.168.4.56:6056@16056 slave
5278df7384edc9774b1a36b0b9d60a813a7424a9 0 1526096611745 6 connected
651f7d99965316c1b8a27a2e9b034a5b14c2be55 192.168.4.55:6055@16055 slave
0ec903c572270a90f3b140fba31aac15aaf5336b 0 1526096613000 5 connected
5278df7384edc9774b1a36b0b9d60a813a7424a9 192.168.4.52:6052@16052 myself,master - 0
1526096612000 2 connected 5461-9677 9679-10922
vars currentEpoch 6 lastVoteEpoch 0
[root@db52 redis-4.0.8]#
set the above configuration? (type 'yes' to accept): yes
/usr/local/share/gems/gems/redis-3.2.1/lib/redis/client.rb:113:in `call': ERR Slot 0 is already busy
(Redis::CommandError)
from /usr/local/share/gems/gems/redis-3.2.1/lib/redis.rb:2556:in `block in method_missing'
from /usr/local/share/gems/gems/redis-3.2.1/lib/redis.rb:37:in `block in synchronize'
from /usr/share/ruby/monitor.rb:211:in `mon_synchronize'
from /usr/local/share/gems/gems/redis-3.2.1/lib/redis.rb:37:in `synchronize'
from /usr/local/share/gems/gems/redis-3.2.1/lib/redis.rb:2555:in `method_missing'
from /usr/local/bin/redis-trib.rb:212:in `flush_node_config'
from /usr/local/bin/redis-trib.rb:906:in `block in flush_nodes_config'
from /usr/local/bin/redis-trib.rb:905:in `each'
from /usr/local/bin/redis-trib.rb:905:in `flush_nodes_config'
from /usr/local/bin/redis-trib.rb:1426:in `create_cluster_cmd'
from /usr/local/bin/redis-trib.rb:1830:in `<main>'
[root@mysql51 ~]#
[ERR] Node 192.168.4.53:6053 is not configured as a cluster node.
每台 redis服务 在本机登录 查看集群信息
[root@host51 ~]# redis-cli -c -h 192.168.4.51 -p 6051
-c 连接集群中的主机
[root@host51 ~]# redis-cli -c -h 192.168.4.51 -p 6051
192.168.4.51:6051> CLUSTER info
192.168.4.51:6051> CLUSTER nodes
192.168.4.51:6051> quit
192.168.4.51:6051> CLUSTER nodes
388c33e7128fc961b381ad3b3c27c3c217912666 192.168.4.54:6054@16054 master - 0 1526108837942
4 connected 9678
651f7d99965316c1b8a27a2e9b034a5b14c2be55 192.168.4.55:6055@16055 slave
0ec903c572270a90f3b140fba31aac15aaf5336b 0 1526108838444 5 connected
a3af3096ee214c92a178eadf6e9299584899e62f 192.168.4.56:6056@16056 slave
5278df7384edc9774b1a36b0b9d60a813a7424a9 0 1526108838946 6 connected
6cdb4c64c48c0ee2ca35bf139660f31ca92821dc 192.168.4.53:6053@16053 master - 0 1526108837942
3 connected 10923-16383
0ec903c572270a90f3b140fba31aac15aaf5336b 192.168.4.51:6051@16051 myself,master - 0
1526108836000 1 connected 0-5460
5278df7384edc9774b1a36b0b9d60a813a7424a9 192.168.4.52:6052@16052 master - 0
1526108839447 2 connected 5461-9677 9679-10922
192.168.4.51:6051>
192.168.4.51:6051>
192.168.4.51:6051>
192.168.4.51:6051> 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:4
cluster_current_epoch:6
cluster_my_epoch:1
cluster_stats_messages_ping_sent:26580
cluster_stats_messages_pong_sent:20656
cluster_stats_messages_update_sent:2
cluster_stats_messages_sent:47238
cluster_stats_messages_ping_received:20651
cluster_stats_messages_pong_received:26580
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:47236
192.168.4.51:6051>
测试集群:连接master库 存储数据,对应的从库 会自动同步数据
[root@host51 ~]# redis-cli -c -h master库ip地址 -p 对应的端口号
[root@host51 ~]# redis-cli -c -h 192.168.4.51 -p 6051
192.168.4.51:6051> CLUSTER nodes
192.168.4.51:6051>
[root@host51 ~]# redis-cli -c -h 192.168.4.51 -p 6051
192.168.4.51:6051> set age 99
OK
192.168.4.51:6051> get age
"99"
192.168.4.51:6051>
[root@db54 ~]# redis-cli -c -h 192.168.4.54 -p 6054
192.168.4.54:6054> get age
-> Redirected to slot [741] located at 192.168.4.51:6051
"99"
192.168.4.51:6051>
二 、管理集群
rm -rf /var/lib/redis/6379/*
/etc/init.d/redis_6379 stop
/etc/init.d/redis_6379 start
netstat -anutlp |grep redis
cat /var/lib/redis/6379/nodes
redis-trib.rb create --replicas 1 192.168.4.71:6371
192.168.4.72:6372
192.168.4.73:6373
192.168.4.74:6374
192.168.4.75:6375
192.168.4.76:6376
2.1 集群选举测试 : 停止某个主库的redi服务 对应的从会自动升级主库
2.2 向集群中添加新节点主机 192.168.4.77
1 在主机57运行redis服务(要做对应的集群配置)
装包 初始配置
修改配置文件
vim /etc/redis/6379.conf
停止服务
启动服务
查看端口号
11 yum -y install gcc
12 init 6
13 ls
14 tar -zxf redis-4.0.8.tar.gz
15 cd redis-4.0.8/
16 make
17 make install
18 ./utils/install_server.sh
19 /etc/init.d/redis_6379 status
20 /etc/init.d/redis_6379 stop
21 vim /etc/redis/6379.conf
22 /etc/init.d/redis_6379 start
23 /etc/init.d/redis_6379 status
[root@redis77 redis-4.0.8]# netstat -utnalp | grep redis-server
tcp 0 0 192.168.4.77:16077 0.0.0.0:* LISTEN 4705/redis-server 1
tcp 0 0 192.168.4.77:6077 0.0.0.0:* LISTEN 4705/redis-server 1
[root@redis77 redis-4.0.8]#
redis-trib.rb 选项
add-node 添加主机 (主机的角色是master)
check 检查集群
reshard 重新分配hash slot
2 把主机77添加到集群里
redis-trib.rb add-node 192.168.4.77:6077 192.168.4.71:6071
[root@redis71 ~]# redis-trib.rb add-node 192.168.4.77:6077 192.168.4.71:6071
>>> Adding node 192.168.4.77:6077 to cluster 192.168.4.71:6071
>>> Performing Cluster Check (using node 192.168.4.71:6071)
S: 3bfd47406253a25be9b13504ff0ecdd8524e9a9a 192.168.4.71:6071
slots: (0 slots) slave
replicates abb585e1803d756eb0b2d9a1b23790b6712ee7b2
M: cc0f13a4183eb06a771c5190319f7ac40626d04b 192.168.4.73:6073
slots:10923-16383 (5461 slots) master
1 additional replica(s)
S: 5172f3a9cbd4fe8a488a8d45bdf60ae46ac9f900 192.168.4.76:6076
slots: (0 slots) slave
replicates 9717d02408e45ad996dcdbde6401bf1319727e22
S: 30aac94cf32bd5d2446b584be5e7c6b0c07f7da0 192.168.4.74:6074
slots: (0 slots) slave
replicates cc0f13a4183eb06a771c5190319f7ac40626d04b
M: 9717d02408e45ad996dcdbde6401bf1319727e22 192.168.4.72:6072
slots:5461-10922 (5462 slots) master
1 additional replica(s)
M: abb585e1803d756eb0b2d9a1b23790b6712ee7b2 192.168.4.75:6075
slots:0-5460 (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.
>>> Send CLUSTER MEET to node 192.168.4.77:6077 to make it join the cluster.
[OK] New node added correctly.
[root@redis71 ~]#
redis-trib.rb check 192.168.4.77:6077
[root@redis71 ~]# redis-trib.rb check 192.168.4.77:6077
>>> Performing Cluster Check (using node 192.168.4.77:6077)
M: 6cd754d126998090dcbe409315d7577bcd3b5605 192.168.4.77:6077
slots: (0 slots) master
0 additional replica(s)
S: 5172f3a9cbd4fe8a488a8d45bdf60ae46ac9f900 192.168.4.76:6076
slots: (0 slots) slave
replicates 9717d02408e45ad996dcdbde6401bf1319727e22
M: abb585e1803d756eb0b2d9a1b23790b6712ee7b2 192.168.4.75:6075
slots:0-5460 (5461 slots) master
1 additional replica(s)
S: 3bfd47406253a25be9b13504ff0ecdd8524e9a9a 192.168.4.71:6071
slots: (0 slots) slave
replicates abb585e1803d756eb0b2d9a1b23790b6712ee7b2
M: cc0f13a4183eb06a771c5190319f7ac40626d04b 192.168.4.73:6073
slots:10923-16383 (5461 slots) master
1 additional replica(s)
M: 9717d02408e45ad996dcdbde6401bf1319727e22 192.168.4.72:6072
slots:5461-10922 (5462 slots) master
1 additional replica(s)
S: 30aac94cf32bd5d2446b584be5e7c6b0c07f7da0 192.168.4.74:6074
slots: (0 slots) slave
replicates cc0f13a4183eb06a771c5190319f7ac40626d04b
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
[root@redis71 ~]#
手动重新分配hash slot
]# redis-trib.rb reshard 192.168.4.77:6077
4096 拿出多少个 hash slot 给主机 192.168.4.77
主机 192.168.4.77 id 值
all 从当前所有的master里 获取hash slot 个数
检查集群主机状态信息
[root@redis71 ~]# redis-trib.rb check 192.168.4.77:6077
>>> Performing Cluster Check (using node 192.168.4.77:6077)
M: 6cd754d126998090dcbe409315d7577bcd3b5605 192.168.4.77:6077
slots:0-1364,5461-6826,10923-12287 (4096 slots) master
0 additional replica(s)
删除节点主机
1 删除master角色的节点主机
1.1 移除占用的hash槽(slot)
1.2 删除主机
]#redis-trib.rb reshard 192.168.4.51:6051
4096 移除hash槽的个数
主机id 目标主机id
主机id 源主机id
done 设置完毕
yes 提交
]# redis-trib.rb del-node 192.168.4.51:6051 8eecda17577349125df9a6fcc37107c6c5f9bdc5
2 删除slave角色的节点主机
]# redis-trib.rb del-node 192.168.4.51:6051 651f7d99965316c1b8a27a2e9b034a5b14c2be55
添加slave角色主机
1 主机运行redis服务 并设置集群配置
2 添加slave角色主机
]# redis-trib.rb add-node --slave 要添加到集群中主机ip:端口 集群中已有的任意一个主机的ip:端口
[ERR] Node 192.168.4.54:6054 is not empty. Either the node already knows other nodes (check with
CLUSTER NODES) or contains some key in database 0.
rm -rf /var/lib/redis/6379/
mkdir /var/lib/redis/6379/
chmod -R a+w /var/lib/redis/6379/
rm -rf /var/run/redis_6379.pid
/etc/init.d/redis_6379 start
211 /etc/init.d/redis_6379 status
212 redis-cli -h 192.168.4.54 -p 6054
[root@db54 redis]# redis-cli -c -h 192.168.4.54 -p 6054
192.168.4.54:6054> CLUSTER info
cluster_state:fail
cluster_slots_assigned:0
192.168.4.54:6054> CLUSTER RESET
OK
192.168.4.54:6054>quit;
]# redis-trib.rb add-node --slave 192.168.4.54:6054 192.168.4.51:6051
192.168.4.54:6054> CLUSTER nodes
5278df7384edc9774b1a36b0b9d60a813a7424a9 192.168.4.52:6052@16052 master - 0
1526121459000 2 connected 5461-9677 9679-10922
6cdb4c64c48c0ee2ca35bf139660f31ca92821dc 192.168.4.53:6053@16053 master - 0 1526121460637
3 connected 10923-16383
f7d5c6065d155b5d3d02a4efba10ca2d3722f634 192.168.4.54:6054@16054 myself,slave
0ec903c572270a90f3b140fba31aac15aaf5336b 0 1526121460000 0 connected
0ec903c572270a90f3b140fba31aac15aaf5336b 192.168.4.51:6051@16051 master - 0 1526121459635
8 connected 0-5460 9678
a3af3096ee214c92a178eadf6e9299584899e62f 192.168.4.56:6056@16056 slave
5278df7384edc9774b1a36b0b9d60a813a7424a9 0 1526121460000 2 connected
192.168.4.54:6054>
Relational Database Management System
数据按照预先设置的组织结构,存储在物理存储介质上。
数据之间可以做关联操作
主流的RDBMS软件
Oracle
DB2
WS-sqlserver
MySQ
NoSQL(NoSQL = Not Only SQL )
意思是"不仅仅是SQL“
泛指非关系型数据库
不需要预先定义数据存储结构
表的每条记录都可以有不同的类型和结构
主流软件
Redis
MongoDB
Memcached
CouchDB
Neo4j
FlockDB
Redis
Remode DIctionary Server(远程字典服务器)
使用C语言编写的,遵守BSD的开源软件
是一款高性能的(Key/Values)分布式内存数据库
并支持数据持久化的NoSQL数据库服务软件
中文网站www.redis.cn
Redis特点:
支持数据持久化,可以把内存里数据保存到硬盘中
不仅仅支持key/values类型的数据,同时还支持list hash set zset 类型
支持master-salve 模式数据备份
+++++++++++++搭建redis服务器
# tar -xzf redis-4.0.8.tar.gz
# cd redis-4.0.8
#make
#make install
tar -zxf redis-4.0.8.tar.gz
cd redis-4.0.8/
rpm -q gcc gcc-c++ || yum -y install gcc gcc-c++
make && make install
初始化配置
[root@localhost redis-4.0.8]# ./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 //命令行连接redis服务命令
Is this ok? Then press ENTER to go on or Ctrl-C to abort. //回车完成配置
Copied /tmp/6379.conf => /etc/init.d/redis_6379 //服务启动脚本
启动redis服务(软件安装完成后服务自动运行)
[root@localhost redis-4.0.8]# /etc/init.d/redis_6379 status
Redis is running (5341)
[root@localhost redis-4.0.8]# netstat -utnlp | grep :6379
tcp 0 0 127.0.0.1:6379 0.0.0.0:* LISTEN 5341/redis-server 1
[root@localhost redis-4.0.8]#
[root@localhost redis-4.0.8]# ps -C redis-server
PID TTY TIME CMD
5341 ? 00:00:00 redis-server
[root@localhost redis-4.0.
Set keyname keyvalue //存储
get keyname //获取
Select 数据库编号0-15 //切换库
Keys * //打印所以变量
Keys a? //打印指定变量
Exists keyname //测试是否存在
ttl keyname //查看生存时间
type keyname //查看类型
连接本机redis服务
[root@localhost redis-4.0.8]# redis-cli
127.0.0.1:6379> ping
PONG
127.0.0.1:6379> set k1 v1
OK
127.0.0.1:6379> get k1
"v1"
127.0.0.1:6379> SHUTDOWN
not connected> exit
[root@localhost redis-4.0.8]#
--------配置文件解析(1)
计量单位
port 6379 //端口
bind 127.0.0.1 //IP地址
tcp-backlog 511 //tcp连接总数
timeout 0 //连接超时时间
tcp-keepalive 300 //长连接时间
daemonize yes //守护进程方式运行
databases 16 //数据库个数
logfile /var/log/redis_6379.log //pid文件
maxclients 10000 //并发连接数量
dir /var/lib/redis/6379 //数据库目录
93 port 6379
70 bind 127.0.0.1
102 tcp-backlog 511
114 timeout 0
131 tcp-keepalive 300
137 daemonize yes
日志级别
163 # debug (a lot of information, useful for development/testing)
164 # verbose (many rarely useful info, but not a mess like the debug level)
165 # notice (moderately verbose, what you want in production probably)
166 # warning (only very important / critical messages are logged)
167 loglevel notice
187 databases 16
172 logfile /var/log/redis_6379.log
533 # maxclients 10000
264 dir /var/lib/redis/6379
# requirepass foobared 设置登陆密码 设置密码后的登陆方式AUTH <PASSWORD>
设置密码
grep -n requirepass /etc/redis/6379.conf
501:requirepass 123456
[root@localhost redis-4.0.8]# redis-cli
127.0.0.1:6379> ping
(error) NOAUTH Authentication required.
127.0.0.1:6379> auth 123456 //输入密码
OK
127.0.0.1:6379> ping
PONG
127.0.0.1:6379>
MEMORY MANAGEMENT 内存管理
560 # maxmemory <bytes>
内存清除策略
565 # volatile-lru -> Evict using approximated LRU among the keys with an expire set. 最近最少使用 (针对设置了过期时间的key)
566 # allkeys-lru -> Evict any key using approximated LRU. 删除最少使用的key
569 # volatile-random -> Remove a random key among the ones with an expire set. 在设置了过期的key里随机移除
570 # allkeys-random -> Remove a random key, any key. 随机移除key
571 # volatile-ttl -> Remove the key with the nearest expire time (minor TTL) 移除最近过期的key
572 # noeviction -> Don't evict anything, just return an error on write operations. 不删除 写满时报错
591 # maxmemory-policy noeviction 定义使用的策略
602 # maxmemory-samples 5 选取模板数据的个数 (针对lru 和 ttl 策略)
修改密码后如何调用/etc/init.d/redis_6379 脚本停止服务
给主机53的redis服务设置连接密码 密码是123456
vim /etc/redis/6379.conf
501 requirepass 123456
:wq
/etc/init.d/redis_6379 stop
/etc/init.d/redis_6379 start
连接带认证redis服务
[root@db53 ~]# redis-cli
127.0.0.1:6379> ping
(error) NOAUTH Authentication required.
127.0.0.1:6379> auth 123456
OK
127.0.0.1:6379> ping
PONG
127.0.0.1:6379> quit
[root@db53 ~]# redis-cli -a 123456
127.0.0.1:6379> ping
PONG
127.0.0.1:6379>
]# /etc/init.d/redis_6379 stop
[root@db53 ~]# redis-cli -p 6379 -a 123456 shutdown
]# /etc/init.d/redis_6379 start
------设置连接密码后如何使用脚本停止redis服务
vim /etc/init.d/redis_6379
43 $CLIEXEC -p $REDISPORT -a 123456 shutdown
:wq
]# /etc/init.d/redis_6379 stop
++++++++++++++++++++++++++++++
]#yum -y install pcre-devel zlib-devel
]#tar -zxf nginx-1.12.2.tar.gz
]#cd nginx-1.12.2/
]# ./configure --prefix=/usr/local/nginx
make&&make install
[root@db54 nginx-1.12.2]# ls /usr/local/nginx/
conf html logs sbin
./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre=<path> option.
./configure: error: the HTTP gzip module requires the zlib library.
You can either disable the module by using --without-http_gzip_module
option, or install the zlib library into the system, or build the zlib library
statically from the source with nginx by using --with-zlib=<path> option.
[root@db54 nginx-1.12.2]# /usr/local/nginx/sbin/nginx
[root@db54 nginx-1.12.2]# netstat -utnlp | grep :80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 23909/nginx: master
[root@db54 nginx-1.12.2]#
[root@db54 nginx-1.12.2]# ps -C nginx
PID TTY TIME CMD
23909 ? 00:00:00 nginx
23910 ? 00:00:00 nginx
[root@db54 nginx-1.12.2]#
108 yum -y install php-common
109 rpm -q php-common
110 rpm -ivh php-fpm-5.4.16-42.el7.x86_64.rpm
111 systemctl status php-fpm
112 systemctl enable php-fpm
113 systemctl start php-fpm
114 netstat -utnlp | grep :9000
[root@db54 lnmp]# systemctl start php-fpm
[root@db54 lnmp]# netstat -utnlp | grep :9000
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 24312/php-fpm:
mast
[root@db54 lnmp]#
]# 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 $document_root/$fastcgi_script_name;
include fastcgi_params;
}
:wq
[root@db54 lnmp]# /usr/local/nginx/sbin/nginx -s stop
[root@db54 lnmp]#
[root@db54 lnmp]#
[root@db54 lnmp]# /usr/local/nginx/sbin/nginx
[root@db54 lnmp]# vim /usr/local/nginx/html/test.php
<?php
phpinfo();
?>
[root@db54 lnmp]#
13 yum -y install autoconf automake
14 rpm -ivh php-devel-5.4.16-42.el7.x86_64.rpm
1 tar -zxf php-redis-2.2.4.tar.gz
2 cd phpredis-2.2.4/
]# cd phpredis-2.2.4
[root@db54 phpredis-2.2.4]# phpize
Configuring for:
PHP Api Version: 20100412
Zend Module Api No: 20100525
Zend Extension Api No: 220100525
[root@db54 phpredis-2.2.4]#
]# ll /usr/bin/php-config
]# ./configure --with-php-config=/usr/bin/php-config
]# make
]# make install
[root@db54 phpredis-2.2.4]# make install
Installing shared extensions: /usr/lib64/php/modules/
[root@db54 phpredis-2.2.4]#
[root@db54 phpredis-2.2.4]#
[root@db54 phpredis-2.2.4]# ls /usr/lib64/php/modules/
curl.so fileinfo.so json.so phar.so redis.so zip.so
[root@db54 phpredis-2.2.4]#
[root@db54 phpredis-2.2.4]# vim /etc/php.ini
728 extension_dir = "/usr/lib64/php/modules/"
729 ; On windows:
730 extension = "redis.so"
:wq
[root@db54 phpredis-2.2.4]# php -m | grep -i redis
redis
]# systemctl stop php-fpm
]# systemctl start php-fpm
+++++++++++运行Redis服务
45 tar -zxf redis-4.0.8.tar.gz
46 cd redis-4.0.8/
48 make
49 make install
51 ./utils/install_server.sh
[root@db54 redis-4.0.8]# /etc/init.d/redis_6379 status
Redis is running (32621)
[root@db54 redis-4.0.8]#
[root@db54 redis-4.0.8]# redis-cli
127.0.0.1:6379> keys *
(empty list or set)
127.0.0.1:6379> quit
[root@db54 redis-4.0.8]#
vim /usr/local/nginx/html/lkredis.php
<?php
$redis = new redis();
$redis->connect('127.0.0.1', 6379);
$redis->set('redistest','666666');
echo $redis->get('redistest');
?>
:wq
254]# http://192.168.4.54/lkredis.php
[root@db54 redis-4.0.8]# redis-cli
127.0.0.1:6379> keys *
1) "redistest"
127.0.0.1:6379> get redistest
"666666"
127.0.0.1:6379>
redis-cli -h 192.168.4.71 -p 6371 shutdown
/etc/init.d/redis_6379 start
vim /etc/redis/6379.conf
$CLIEXEC -h 192.168.4.72 -p 6372 shutdown
netstat -utnlp | grep redis
准备6台redis服务器,具体要求如下:
ip地址 端口 etho 日志文件名
192.168.4.51 6051 都可以接收连接请求 redis51.log
192.168.4.52 6052 都可以接收连接请求 redis52.log
192.168.4.53 6053 都可以接收连接请求 redis53.log
192.168.4.54 6054 都可以接收连接请求 redis54.log
192.168.4.55 6055 都可以接收连接请求 redis55.log
192.168.4.56 6056 都可以接收连接请求 redis56.log
环境准备
1 在6台redis服务器上运行服务,按照如下要求修改配置文件后,重启redis服务。
]#vim /etc/redis/6379.conf
bind 192.168.4.56 #只指定物理接口ip地址
port 6056 #不允许相同
daemonize yes
cluster-enabled yes
pidfile /var/run/redis_56.pid #不允许相同
cluster-config-file nodes-6056.conf #不允许相同
cluster-node-timeout 5000 #集群节点之间通信超时时间单位秒
:wq
停止服务
]# redis-cli -h 192.168.4.56 -p 6056 [ -a 密码 ] shutdown
启动服务
]# /etc/init.d/redis_6379 start
连接服务
[root@host56 ~]# redis-cli -h 192.168.4.56 -p 6056
装包: 在执行创建集群命令的主机安装ruby软件包(192.168.4.51)
安装解释ruby代码的软件包
[root@host51 ~]# yum -y install ruby rubygems
[root@host51 ~]# rpm -q rubygems ruby
rubygems-2.0.14.1-30.el7.noarch
ruby-2.0.0.648-30.el7.x86_64
[root@host51 ~]#
[root@host51 redis-cluster]# rpm -ivh --nodeps ruby-devel-2.0.0.648-30.el7.x86_64.rpm
安装ruby连接redis 接口程序 gem
[root@host51 redis-cluster]# which gem
/usr/bin/gem
[root@host51 redis-cluster]# gem install redis-3.2.1.gem
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@host51 redis-cluster]#
生成创建集群的脚本redis-trib.rb
[root@host51 src]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@host51 src]# cd
[root@host51 ~]# cp redis-4.0.8/src/redis-trib.rb /usr/local/bin/
创建集群:
redis-trib.rb create --replicas 1 192.168.4.1:6301 192.168.4.2:6302 192.168.4.4:6304 192.168.4.5:6305 192.168.4.3:6303 192.168.4.6:6306
[ERR] Node 192.168.4.54:6054 is not empty. Either the node already knows other nodes (check with
CLUSTER NODES) or contains some key in database 0.
[root@db54 ~]# redis-cli -h 192.168.4.54 -p 6054
192.168.4.54:6054> flushallcd
OK
192.168.4.54:6054> keys *
(empty list or set)
192.168.4.54:6054> flushall
OK
192.168.4.54:6054> save
OK
192.168.4.54:6054> quit
[root@db54 ~]#
槽位个数是16384
范围0-16383
[root@host51 ~]# redis-trib.rb create --replicas 1 192.168.4.51:6051 192.168.4.52:6052
192.168.4.53:6053 192.168.4.54:6054 192.168.4.55:6055 192.168.4.56:6056 #创建集群
--replicas 1 表示给master 配置1个slave
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
192.168.4.51:6051
192.168.4.52:6052
192.168.4.53:6053
Adding replica 192.168.4.55:6055 to 192.168.4.51:6051
Adding replica 192.168.4.56:6056 to 192.168.4.52:6052
Adding replica 192.168.4.54:6054 to 192.168.4.53:6053
M: 0ec903c572270a90f3b140fba31aac15aaf5336b 192.168.4.51:6051
slots:0-5460 (5461 slots) master
M: 5278df7384edc9774b1a36b0b9d60a813a7424a9 192.168.4.52:6052
slots:5461-10922 (5462 slots) master
M: 6cdb4c64c48c0ee2ca35bf139660f31ca92821dc 192.168.4.53:6053
slots:10923-16383 (5461 slots) master
S: 388c33e7128fc961b381ad3b3c27c3c217912666 192.168.4.54:6054
replicates 6cdb4c64c48c0ee2ca35bf139660f31ca92821dc
S: 651f7d99965316c1b8a27a2e9b034a5b14c2be55 192.168.4.55:6055
replicates 0ec903c572270a90f3b140fba31aac15aaf5336b
S: a3af3096ee214c92a178eadf6e9299584899e62f 192.168.4.56:6056
replicates 5278df7384edc9774b1a36b0b9d60a813a7424a9
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.51:6051)
M: 0ec903c572270a90f3b140fba31aac15aaf5336b 192.168.4.51:6051
slots:0-5460 (5461 slots) master
1 additional replica(s)
M: 388c33e7128fc961b381ad3b3c27c3c217912666 192.168.4.54:6054
slots:9678 (1 slots) master
0 additional replica(s)
S: 651f7d99965316c1b8a27a2e9b034a5b14c2be55 192.168.4.55:6055
slots: (0 slots) slave
replicates 0ec903c572270a90f3b140fba31aac15aaf5336b
S: a3af3096ee214c92a178eadf6e9299584899e62f 192.168.4.56:6056
slots: (0 slots) slave
replicates 5278df7384edc9774b1a36b0b9d60a813a7424a9
M: 6cdb4c64c48c0ee2ca35bf139660f31ca92821dc 192.168.4.53:6053
slots:10923-16383 (5461 slots) master
0 additional replica(s)
M: 5278df7384edc9774b1a36b0b9d60a813a7424a9 192.168.4.52:6052
slots:5461-9677,9679-10922 (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.
[root@host51 ~]#
[root@db52 redis-4.0.8]# cat /var/lib/redis/6052/nodes-6052.conf #集群配置文件
6cdb4c64c48c0ee2ca35bf139660f31ca92821dc 192.168.4.53:6053@16053 master - 0 1526096611374
3 connected 10923-16383
0ec903c572270a90f3b140fba31aac15aaf5336b 192.168.4.51:6051@16051 master - 0 1526096612000
1 connected 0-5460
388c33e7128fc961b381ad3b3c27c3c217912666 192.168.4.54:6054@16054 master - 0 1526096611043
4 connected 9678
a3af3096ee214c92a178eadf6e9299584899e62f 192.168.4.56:6056@16056 slave
5278df7384edc9774b1a36b0b9d60a813a7424a9 0 1526096611745 6 connected
651f7d99965316c1b8a27a2e9b034a5b14c2be55 192.168.4.55:6055@16055 slave
0ec903c572270a90f3b140fba31aac15aaf5336b 0 1526096613000 5 connected
5278df7384edc9774b1a36b0b9d60a813a7424a9 192.168.4.52:6052@16052 myself,master - 0
1526096612000 2 connected 5461-9677 9679-10922
vars currentEpoch 6 lastVoteEpoch 0
[root@db52 redis-4.0.8]#
set the above configuration? (type 'yes' to accept): yes
/usr/local/share/gems/gems/redis-3.2.1/lib/redis/client.rb:113:in `call': ERR Slot 0 is already busy
(Redis::CommandError)
from /usr/local/share/gems/gems/redis-3.2.1/lib/redis.rb:2556:in `block in method_missing'
from /usr/local/share/gems/gems/redis-3.2.1/lib/redis.rb:37:in `block in synchronize'
from /usr/share/ruby/monitor.rb:211:in `mon_synchronize'
from /usr/local/share/gems/gems/redis-3.2.1/lib/redis.rb:37:in `synchronize'
from /usr/local/share/gems/gems/redis-3.2.1/lib/redis.rb:2555:in `method_missing'
from /usr/local/bin/redis-trib.rb:212:in `flush_node_config'
from /usr/local/bin/redis-trib.rb:906:in `block in flush_nodes_config'
from /usr/local/bin/redis-trib.rb:905:in `each'
from /usr/local/bin/redis-trib.rb:905:in `flush_nodes_config'
from /usr/local/bin/redis-trib.rb:1426:in `create_cluster_cmd'
from /usr/local/bin/redis-trib.rb:1830:in `<main>'
[root@mysql51 ~]#
[ERR] Node 192.168.4.53:6053 is not configured as a cluster node.
每台 redis服务 在本机登录 查看集群信息
[root@host51 ~]# redis-cli -c -h 192.168.4.51 -p 6051
-c 连接集群中的主机
[root@host51 ~]# redis-cli -c -h 192.168.4.51 -p 6051
192.168.4.51:6051> CLUSTER info
192.168.4.51:6051> CLUSTER nodes
192.168.4.51:6051> quit
192.168.4.51:6051> CLUSTER nodes
388c33e7128fc961b381ad3b3c27c3c217912666 192.168.4.54:6054@16054 master - 0 1526108837942
4 connected 9678
651f7d99965316c1b8a27a2e9b034a5b14c2be55 192.168.4.55:6055@16055 slave
0ec903c572270a90f3b140fba31aac15aaf5336b 0 1526108838444 5 connected
a3af3096ee214c92a178eadf6e9299584899e62f 192.168.4.56:6056@16056 slave
5278df7384edc9774b1a36b0b9d60a813a7424a9 0 1526108838946 6 connected
6cdb4c64c48c0ee2ca35bf139660f31ca92821dc 192.168.4.53:6053@16053 master - 0 1526108837942
3 connected 10923-16383
0ec903c572270a90f3b140fba31aac15aaf5336b 192.168.4.51:6051@16051 myself,master - 0
1526108836000 1 connected 0-5460
5278df7384edc9774b1a36b0b9d60a813a7424a9 192.168.4.52:6052@16052 master - 0
1526108839447 2 connected 5461-9677 9679-10922
192.168.4.51:6051>
192.168.4.51:6051>
192.168.4.51:6051>
192.168.4.51:6051> 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:4
cluster_current_epoch:6
cluster_my_epoch:1
cluster_stats_messages_ping_sent:26580
cluster_stats_messages_pong_sent:20656
cluster_stats_messages_update_sent:2
cluster_stats_messages_sent:47238
cluster_stats_messages_ping_received:20651
cluster_stats_messages_pong_received:26580
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:47236
192.168.4.51:6051>
测试集群:连接master库 存储数据,对应的从库 会自动同步数据
[root@host51 ~]# redis-cli -c -h master库ip地址 -p 对应的端口号
[root@host51 ~]# redis-cli -c -h 192.168.4.51 -p 6051
192.168.4.51:6051> CLUSTER nodes
192.168.4.51:6051>
[root@host51 ~]# redis-cli -c -h 192.168.4.51 -p 6051
192.168.4.51:6051> set age 99
OK
192.168.4.51:6051> get age
"99"
192.168.4.51:6051>
[root@db54 ~]# redis-cli -c -h 192.168.4.54 -p 6054
192.168.4.54:6054> get age
-> Redirected to slot [741] located at 192.168.4.51:6051
"99"
192.168.4.51:6051>
二 、管理集群
rm -rf /var/lib/redis/6379/*
/etc/init.d/redis_6379 stop
/etc/init.d/redis_6379 start
netstat -anutlp |grep redis
cat /var/lib/redis/6379/nodes
redis-trib.rb create --replicas 1 192.168.4.71:6371
192.168.4.72:6372
192.168.4.73:6373
192.168.4.74:6374
192.168.4.75:6375
192.168.4.76:6376
2.1 集群选举测试 : 停止某个主库的redi服务 对应的从会自动升级主库
2.2 向集群中添加新节点主机 192.168.4.77
1 在主机57运行redis服务(要做对应的集群配置)
装包 初始配置
修改配置文件
vim /etc/redis/6379.conf
停止服务
启动服务
查看端口号
11 yum -y install gcc
12 init 6
13 ls
14 tar -zxf redis-4.0.8.tar.gz
15 cd redis-4.0.8/
16 make
17 make install
18 ./utils/install_server.sh
19 /etc/init.d/redis_6379 status
20 /etc/init.d/redis_6379 stop
21 vim /etc/redis/6379.conf
22 /etc/init.d/redis_6379 start
23 /etc/init.d/redis_6379 status
[root@redis77 redis-4.0.8]# netstat -utnalp | grep redis-server
tcp 0 0 192.168.4.77:16077 0.0.0.0:* LISTEN 4705/redis-server 1
tcp 0 0 192.168.4.77:6077 0.0.0.0:* LISTEN 4705/redis-server 1
[root@redis77 redis-4.0.8]#
redis-trib.rb 选项
add-node 添加主机 (主机的角色是master)
check 检查集群
reshard 重新分配hash slot
2 把主机77添加到集群里
redis-trib.rb add-node 192.168.4.77:6077 192.168.4.71:6071
[root@redis71 ~]# redis-trib.rb add-node 192.168.4.77:6077 192.168.4.71:6071
>>> Adding node 192.168.4.77:6077 to cluster 192.168.4.71:6071
>>> Performing Cluster Check (using node 192.168.4.71:6071)
S: 3bfd47406253a25be9b13504ff0ecdd8524e9a9a 192.168.4.71:6071
slots: (0 slots) slave
replicates abb585e1803d756eb0b2d9a1b23790b6712ee7b2
M: cc0f13a4183eb06a771c5190319f7ac40626d04b 192.168.4.73:6073
slots:10923-16383 (5461 slots) master
1 additional replica(s)
S: 5172f3a9cbd4fe8a488a8d45bdf60ae46ac9f900 192.168.4.76:6076
slots: (0 slots) slave
replicates 9717d02408e45ad996dcdbde6401bf1319727e22
S: 30aac94cf32bd5d2446b584be5e7c6b0c07f7da0 192.168.4.74:6074
slots: (0 slots) slave
replicates cc0f13a4183eb06a771c5190319f7ac40626d04b
M: 9717d02408e45ad996dcdbde6401bf1319727e22 192.168.4.72:6072
slots:5461-10922 (5462 slots) master
1 additional replica(s)
M: abb585e1803d756eb0b2d9a1b23790b6712ee7b2 192.168.4.75:6075
slots:0-5460 (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.
>>> Send CLUSTER MEET to node 192.168.4.77:6077 to make it join the cluster.
[OK] New node added correctly.
[root@redis71 ~]#
redis-trib.rb check 192.168.4.77:6077
[root@redis71 ~]# redis-trib.rb check 192.168.4.77:6077
>>> Performing Cluster Check (using node 192.168.4.77:6077)
M: 6cd754d126998090dcbe409315d7577bcd3b5605 192.168.4.77:6077
slots: (0 slots) master
0 additional replica(s)
S: 5172f3a9cbd4fe8a488a8d45bdf60ae46ac9f900 192.168.4.76:6076
slots: (0 slots) slave
replicates 9717d02408e45ad996dcdbde6401bf1319727e22
M: abb585e1803d756eb0b2d9a1b23790b6712ee7b2 192.168.4.75:6075
slots:0-5460 (5461 slots) master
1 additional replica(s)
S: 3bfd47406253a25be9b13504ff0ecdd8524e9a9a 192.168.4.71:6071
slots: (0 slots) slave
replicates abb585e1803d756eb0b2d9a1b23790b6712ee7b2
M: cc0f13a4183eb06a771c5190319f7ac40626d04b 192.168.4.73:6073
slots:10923-16383 (5461 slots) master
1 additional replica(s)
M: 9717d02408e45ad996dcdbde6401bf1319727e22 192.168.4.72:6072
slots:5461-10922 (5462 slots) master
1 additional replica(s)
S: 30aac94cf32bd5d2446b584be5e7c6b0c07f7da0 192.168.4.74:6074
slots: (0 slots) slave
replicates cc0f13a4183eb06a771c5190319f7ac40626d04b
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
[root@redis71 ~]#
手动重新分配hash slot
]# redis-trib.rb reshard 192.168.4.77:6077
4096 拿出多少个 hash slot 给主机 192.168.4.77
主机 192.168.4.77 id 值
all 从当前所有的master里 获取hash slot 个数
检查集群主机状态信息
[root@redis71 ~]# redis-trib.rb check 192.168.4.77:6077
>>> Performing Cluster Check (using node 192.168.4.77:6077)
M: 6cd754d126998090dcbe409315d7577bcd3b5605 192.168.4.77:6077
slots:0-1364,5461-6826,10923-12287 (4096 slots) master
0 additional replica(s)
删除节点主机
1 删除master角色的节点主机
1.1 移除占用的hash槽(slot)
1.2 删除主机
]#redis-trib.rb reshard 192.168.4.51:6051
4096 移除hash槽的个数
主机id 目标主机id
主机id 源主机id
done 设置完毕
yes 提交
]# redis-trib.rb del-node 192.168.4.51:6051 8eecda17577349125df9a6fcc37107c6c5f9bdc5
2 删除slave角色的节点主机
]# redis-trib.rb del-node 192.168.4.51:6051 651f7d99965316c1b8a27a2e9b034a5b14c2be55
添加slave角色主机
1 主机运行redis服务 并设置集群配置
2 添加slave角色主机
]# redis-trib.rb add-node --slave 要添加到集群中主机ip:端口 集群中已有的任意一个主机的ip:端口
[ERR] Node 192.168.4.54:6054 is not empty. Either the node already knows other nodes (check with
CLUSTER NODES) or contains some key in database 0.
rm -rf /var/lib/redis/6379/
mkdir /var/lib/redis/6379/
chmod -R a+w /var/lib/redis/6379/
rm -rf /var/run/redis_6379.pid
/etc/init.d/redis_6379 start
211 /etc/init.d/redis_6379 status
212 redis-cli -h 192.168.4.54 -p 6054
[root@db54 redis]# redis-cli -c -h 192.168.4.54 -p 6054
192.168.4.54:6054> CLUSTER info
cluster_state:fail
cluster_slots_assigned:0
192.168.4.54:6054> CLUSTER RESET
OK
192.168.4.54:6054>quit;
]# redis-trib.rb add-node --slave 192.168.4.54:6054 192.168.4.51:6051
192.168.4.54:6054> CLUSTER nodes
5278df7384edc9774b1a36b0b9d60a813a7424a9 192.168.4.52:6052@16052 master - 0
1526121459000 2 connected 5461-9677 9679-10922
6cdb4c64c48c0ee2ca35bf139660f31ca92821dc 192.168.4.53:6053@16053 master - 0 1526121460637
3 connected 10923-16383
f7d5c6065d155b5d3d02a4efba10ca2d3722f634 192.168.4.54:6054@16054 myself,slave
0ec903c572270a90f3b140fba31aac15aaf5336b 0 1526121460000 0 connected
0ec903c572270a90f3b140fba31aac15aaf5336b 192.168.4.51:6051@16051 master - 0 1526121459635
8 connected 0-5460 9678
a3af3096ee214c92a178eadf6e9299584899e62f 192.168.4.56:6056@16056 slave
5278df7384edc9774b1a36b0b9d60a813a7424a9 0 1526121460000 2 connected
192.168.4.54:6054>