本文共 36519 字,大约阅读时间需要 121 分钟。
[ root@redis1~ ] # yum - y install gcc gcc- c++ make[ root@redis1~ ] # tar - zxf redis- 4.0.8.tar.gz[ root@redis1~ ] # cd redis- 4.0.8/[ root@redis1redis- 4.0.8] # ls 00- RELEASENOTES CONTRIBUTING deps Makefile README.md runtest runtest- sentinel src utils BUGS COPYING INSTALL MANIFESTO redis.conf runtest- cluster sentinel.conf tests [ root@redis1redis- 4.0.8] # make [ root@redis1redis- 4.0.8] # make install [ root@redis1redis- 4.0.8] # cd utils/ [ root@redis1utils] # ./install_server.sh Welcome to the redis serv ice installer This script will help y ou 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 serv ice... Successfully added to chkconfig! Successfully added to runlevels 345! Starting Redis server... Installation successful! #安装成功
[ root@redis1utils] # /etc/init.d/redis_6379 statusRedis is running ( 15203)
[ root@redis1utils] # netstat - antupl | grep :6379tcp 0 0 127.0.0.1:6379 0.0.0.0:* LISTEN 15203/redis- server[ root@redis1utils] # ps - C redis- serverPID TTY TIME CMD 15203 ? 00:00:00 redis- server
[ root@redis1utils] # /etc/init.d/redis_6379 stopStopping ...Waiting for Redis to shutdown ...Redis stopped[ root@redis1utils] # /etc/init.d/redis_6379 status# 再次查看,显示 没有那个文件或目录cat: /var/run/redis_6379.pid: No such file or directoryRedis is running ( )
[ root@redis1utils] # /etc/init.d/redis_6379 startStarting Redis server...[ root@redis1utils] # redis- cli127.0.0.1:6379> pingPONG #PONG说明服务正常
常用指令操作: set keyname keyvalue 存储 get keyname 获取
127.0.0.1:6379> set test 123OK127.0.0.1:6379> get test"123"
127.0.0.1:6379> set k1 v1OK127.0.0.1:6379> get k1"v1"127.0.0.1:6379> del k1( integer) 1
127.0.0.1:6379> keys *1) "test"
127.0.0.1:6379> exists k1( integer) 0
127.0.0.1:6379> set k2 v1OK127.0.0.1:6379> type k2string
127.0.0.1:6379> move k2 1 #移动k2到1库( integer) 1
127.0.0.1:6379> select 1 #切换到1库OK127.0.0.1:6379[ 1] > keys * #查看有k21) "k2"
127.0.0.1:6379[ 1] > expire k2 10( integer) 1
127.0.0.1:6379[ 1] > ttl k2(integer) -2
127.0.0.1:6379[ 1] > flushallOK
127.0.0.1:6379[ 1] > saveOK
shutdown 关闭redis服务
修改redis运行参数 [ root@redis1utils] # cp /etc/redis/6379.conf /root/6379.conf # 可以先备份一份,防止修改错误没法还原 [ root@redis1utils] # /etc/init.d/redis_6379 stop [ root@redis1utils] # vim /etc/redis/6379.conf ....... bind 192.168.4.51 #设置服务使用的ip port 6351 #更改端口号 requirepass 123456 #设置密码 [ root@redis1utils] # /etc/init.d/redis_6379 start Starting Redis server... [ root@redis1utils] # ss - antul | grep 6351 #查看有端口6351 tcp LISTEN 0 128 192.168.4.51:6351 *:*
[ root@redis1utils] # redis-cli -h 192.168.4.51 -p 6351192.168.4.51:6351> ping( error) NOAUTH Authentication required.192.168.4.51:6351> auth 123456 #输入密码才能操作(因为之前设置过密码)OK192.168.4.51:6351> pingPONG
[ root@redis1utils] # redis- cli - h 192.168.4.51- p 6351 - a 123456192.168.4.51:6351> pingPONG
由于修改Redis服务运行参数,所以在停止服务的时候也不能用默认的方法停止
[ root@redis1utils] # /etc/init.d/redis_6379 stop #停止失败 Stopping ... Could not connect to Redis at 127.0.0.1:6379: Connection refused Waiting for Redis to shutdown ... Waiting for Redis to shutdown ... Waiting for Redis to shutdown ... Waiting for Redis to shutdown ... ....... [ root@redis1utils] # redis- cli - h 192.168.4.51- p 6351 - a 123456 shutdown #停止成功[ root@redis1utils] # ss - antul | grep 6351 #查看没有端口
部署LNMP+Redis(1)安装redis,(不会搭建的请参考案例1)(2)安装php支持的功能模块(52上面操作)
[ root@nginx utils] # which php/usr/bin/which: no php in ( /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin)[ root@nginx utils] # php - mbash: php: command not found...[ root@nginx utils] # yum - y install php-cli [ root@nginx utils] # which php /usr/bin/php [ root@nginx utils] # php - m [ PHP Modules] bz2 calendar Core cty pe curl date ereg exif fileinfo filter ftp gettext gmp hash iconv json libxml mhash openssl pcntl pcre Phar readline Reflection session shmop SimpleXML sockets SPL standard tokenizer xml zip zlib [ Zend Modules]
[ root@nginx utils] # php - m | grep - i redis #没有redis模块[ root@nginx redis] # cd lnmp/[ root@nginx lnmp] # ls nginx- 1.12.2.tar.gz php- devel- 5.4.16- 42.el7.x86_64.rpm php- fpm- 5.4.16- 42.el7.x86_64.rpm php- redis- 2.2.4.tar.gz [ root@nginx lnmp] # tar - zxf php- redis- 2.2.4.tar.gz [ root@nginx lnmp] # cd phpredis- 2.2.4/ [ root@nginx phpredis- 2.2.4] # which phpize /usr/bin/phpize [ root@nginx 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@nginx phpredis- 2.2.4] # yum - y install autoconf automake pcre- devel [ root@nginx phpredis- 2.2.4] # cd .. [ root@nginx lnmp] # rpm -ivh php- devel- 5.4.16- 42.el7.x86_64.rpm [ root@nginx lnmp] # cd phpredis- 2.2.4/ [ root@nginx phpredis- 2.2.4] # phpize #生成一个php的文件 Configuring for: PHP Api Version: 20100412 Zend Module Api No: 20100525 Zend Extension Api No: 220100525 [ root@nginx phpredis- 2.2.4] # find / - name "php- config" /usr/bin/php- config [ root@nginx phpredis- 2.2.4] # ./configure - - with- php- config=/usr/bin/php- config #指定模块编译的路径 [ root@nginx phpredis- 2.2.4] # make && make install ......... Installing shared extensions: /usr/lib64/php/modules/ #模块文件存放的路径 [ root@nginx phpredis- 2.2.4] # ls /usr/lib64/php/modules/ curl.so fileinfo.so json.so phar.so redis.so zip.so [ root@nginx phpredis- 2.2.4] # vim /etc/php.ini 728 extension_dir = "/usr/lib64/php/modules/" 729 ; On windows: 730 extension = "redis.so" [ root@nginx phpredis- 2.2.4] # php - m | grep - i redis redis #出现redis
[ root@nginx ~] # cd redis/lnmp/[ root@nginx lnmp] # lsnginx- 1.12.2.tar.gz[ root@nginx lnmp] # tar - xf nginx- 1.12.2.tar.gz[ root@nginx lnmp] # cd nginx- 1.12.2/[ root@nginx nginx- 1.12.2] # yum - y install gcc pcre- devel openssl- devel[ root@nginx nginx- 1.12.2] # useradd - s /sbin/nologin nginx[ root@nginx nginx- 1.12.2] # ./configure - - user=nginx - - group=nginx - - with- http_ssl_module[ root@nginx nginx- 1.12.2] # make && make install[ root@nginx nginx- 1.12.2] # ln - s /usr/local/nginx/sbin/nginx /sbin/[ root@nginx nginx- 1.12.2] # cd /usr/local/nginx/html/[ root@nginx html] # echo "aa" > text.html[ root@nginx html] # yum - y install mariadb mariadb- server mariadb- devel php php- mysql[ root@nginx html] # cd /root/redis/lnmp/[ root@nginx lnmp] # rpm - ivh php- fpm- 5.4.16- 42.el7.x86_64.rpm #安装php[ root@nginx lnmp] # cd /usr/local/nginx/html/[ root@nginx html] # vim test.php [ root@nginx html] # php test.php #在命令行测试 oK [ root@nginx html] # sy stemctl restart mariadb [ root@nginx html] # systemctl restart php-fpm [ root@nginx html] # v im /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@nginx html] # nginx #启动nginx
[ root@room9pc01~] # firefox 192.168.4.56/text.html #成功[ root@room9pc01~] # firefox 192.168.4.56/test.php #成功
[ root@nginx html] # vim lkredis.php connect( '192.168.4.51',6351) ;$redis - >auth( "123456") ;$redis- >set( "redistest","666666") ;echo $redis- >get( "redistest") ;?>[ root@nginx html] # php lkredis.php #命令行测试666666[ root@nginx html] # firefox lkredis.php #命令行测试666666
[ root@redis1lnmp] # redis- cli - h 192.168.4.51- p 6351- a 123456192.168.4.51:6351> pingPONG192.168.4.51:6351> keys *1) "redistest"192.168.4.51:6351> get redistest"666666"192.168.4.51:6351>
[ root@redisA ~] # y um - y install gcc gcc- c++ make [ root@redisA ~] # cd redis redis/ redis-cluster/ [ root@redisA ~] # cd redis/ [ root@redisA redis] # ls lnmp redis- 4.0.8.tar.gz [ root@redisA redis] # tar - xf redis- 4.0.8.tar.gz [ root@redisA redis] # cd redis- 4.0.8/ [ root@redisA redis- 4.0.8] # make && make install [ root@redisA redis- 4.0.8] # ./utils/install_server.sh Welcome to the redis serv ice installer This script will help y ou 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 serv ice... Successfully added to chkconfig! Successfully added to runlevels 345! Starting Redis server... Installation successful! //安装成功 [ root@redisA redis- 4.0.8] # ss - antlp | grep 6379 //查看时有端口 LISTEN 0 128 127.0.0.1:6379 *:* users:( ( "redis- server",pid=10788,fd=6) )
[ root@redisA redis- 4.0.8] # /etc/init.d/redis_6379 stop//停止已经开启的redis服务Stopping ...Waiting for Redis to shutdown ...Redis stopped[ root@redisA redis- 4.0.8] # v im /etc/redis/6379.conf.........bind 192.168.4.51 //修改ipport 6351 //不允许相同,只指定物理接口的ipdaemonize y es //以守护进程方式运行pidfile /var/run/redis_6351.pidcluster- enabled y es //是否启用集群,前提是以守护进程方式运行cluster- config- file nodes- 6351.conf//存储集群信息的配置文件,自动生成,不允许相同cluster- node- timeout 5000 //集群节点通信超时时间..........[ root@redisA redis- 4.0.8] # /etc/init.d/redis_6379 start //启动服务Starting Redis server...[ root@redisA redis- 4.0.8] # ss - antlp | grep 6351 //查看有端口LISTEN 0 128 192.168.4.51:6351 *:* users:( ( "redis- server",pid=11092,fd=6) )LISTEN 0 128 192.168.4.51:16351 *:* users:( ( "redis- server",pid=11092,fd=8) ) //16051:集群中的主机通信时用[ root@redisA redis- 4.0.8] # ps - C redisPID TTY TIME CMD
[ root@redisA redis- 4.0.8] # getenforcePermissive[ root@redisA redis- 4.0.8] # sy stemctl disable firewalld//关闭防火墙不自启
[ root@redisA redis- 4.0.8] # redis- cli - h 192.168.4.51- p 6351192.168.4.51:6351> pingPONG192.168.4.51:6351> cluster infocluster_state:failcluster_slots_assigned:0cluster_slots_ok:0cluster_slots_pfail:0cluster_slots_fail:0cluster_known_nodes:1cluster_size:0..........192.168.4.51:6351> cluster nodesf81f997d5ed988ec1587558e78d5f7dbc96abcbf :6351@16351my self,master - 0 0 0 connected
[ root@redisA redis- 4.0.8] # cd /root/redis- cluster/[ root@redisA redis- cluster] # lsredis- 3.2.1.gem ruby - devel- 2.0.0.648- 30.el7.x86_64.rpm[ root@redisA redis- cluster] # y um - y install ruby ruby gems[ root@redisA redis- cluster] # rpm - ivh – nodeps ruby - devel- 2.0.0.648- 30.el7.x86_64.rpmwarning: ruby - devel- 2.0.0.648- 30.el7.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID f4a80eb5: NOKEYPreparing... ################################# [ 100%]Updating / installing...1:ruby - devel- 2.0.0.648- 30.el7 ################################# [ 100%][ root@redisA redis- cluster] # which gem/usr/bin/gem[ root@redisA redis- cluster] # gem install redisSuccessfully installed redis- 3.2.1Parsing documentation for redis- 3.2.1Installing ri documentation for redis- 3.2.11gem installed
[ root@redisA redis- cluster] # cd /root/redis/redis- 4.0.8/src/[ root@redisA src] # cp redis- trib.rb /usr/local/bin/[ root@redisA src] # ll /usr/local/bin/redis- trib.rb- rwxr- xr- x. 1root root 65991Sep 27 16:12 /usr/local/bin/redis- trib.rb
[ root@redisA src] # redis- trib.rb create - - replicas 1\ 192.168.4.51:6351 192.168.4.52:6352 \ 192.168.4.53:6353 192.168.4.54:6354 \ 192.168.4.55:6355 192.168.4.56:6356 //- - replicas 1给每一个主配置一个从库 [ root@redisA log] # redis- trib.rb create - - replicas 1192.168.4.51:6351 192.168.4.52:6352 192.168.4.53:6353 192.168.4.54:6354 192.168.4.55:6355 >>> Creating cluster >>> Performing hash slots allocation on 6 nodes... Using 3 masters: 192.168.4.51:6351 192.168.4.52:6352 192.168.4.53:6353 Adding replica 192.168.4.55:6355 to 192.168.4.51:6351 Adding replica 192.168.4.56:6356 to 192.168.4.52:6352 Adding replica 192.168.4.54:6354 to 192.168.4.53:6353 .......... .......... .......... [ OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [ OK] All 16384 slots covered.
cluster info 查看集群信息 cluster nodes 查看集群节点信息
[ root@redisA log] # redis- cli - h 192.168.4.52 - p 6352192.168.4.52:6352> CLUSTER INFOcluster_state:ok //状态cluster_slots_assigned:16384cluster_slots_ok:16384cluster_slots_pfail:0cluster_slots_fail:0cluster_known_nodes:6cluster_size:3cluster_current_epoch:6cluster_my _epoch:2cluster_stats_messages_ping_sent:367cluster_stats_messages_pong_sent:327cluster_stats_messages_meet_sent:5cluster_stats_messages_sent:699cluster_stats_messages_ping_received:327cluster_stats_messages_pong_received:372cluster_stats_messages_received:699192.168.4.52:6352> CLUSTER NODES //查看集群节点信息63afbb5e7d63b1f142358634578a3488e3c9e634 192.168.4.54:6354@16354 slave bc5c4e082a5a3391b634cf433a6486c867cf c44b 0 15380392788714 connected bc5c4e082a5a3391b634cf433a6486c867cf c44b 192.168.4.53:6353@16353 master - 0 15380392785713 connected 10923- 16383 28e06c5f24a2b6c6412f81369e09bc9653cc51ff 192.168.4.56:6356@16356 slave 8568fbd73cb296cad6915d524e34761b2114af47 0 1538039278069 6 conne7e8d9121f44d8331ff55b45c218b87df9bda1b70 192.168.4.55:6355@16355 slave a3083123bc5c87a76aab2655171634d4ee84f418 0 1538039278000 5 conn8568fbd73cb296cad6915d524e34761b2114af47 192.168.4.52:6352@16352 my self,master - 0 1538039277000 2 connected 5461- 10922a3083123bc5c87a76aab2655171634d4ee84f418 192.168.4.51:6351@16351master - 0 1538039277869 1connected 0- 5460192.168.4.52:6352>
命令:redis-cli -c -h ip地址 -p 端口
[ root@redisA log] # redis-cli -c -h 192.168.4.51 -p 6351 192.168.4.51:6351> set name jim - > Redirected to slot [ 5798] located at 192.168.4.52:6352 OK 192.168.4.52:6352> get name "jim" 192.168.4.52:6352> set class linux OK 192.168.4.52:6352> get class "linux" 192.168.4.52:6352> set pay 26800 - > Redirected to slot [ 4013] located at 192.168.4.51:6351 OK 192.168.4.51:6351> get pay "26800"
有半数或者半数以上的主库机器挂掉,集群就不能用了 把一个从库升级成主,没有从库,集群不能用(前提是:有半数或者半数以上的主库机器挂掉) 一个主库挂掉,它的从库自动顶替为主库,正常使用(前提是:有半数或者半数以上的主库机器能用),挂掉的主库修复好后,会成为从库,不会 抢占为主
停止某个主库的redis服务,对应的从库会自动升级为主库 先查看节点信息的主从情况
[ root@redisA log] # redis- cli - c - h 192.168.4.51- p 6351192.168.4.51:6351> CLUSTER nodes8568fbd73cb296cad6915d524e34761b2114af47 192.168.4.52:6352@16352 master - 0 1538040400840 2 connected 5461- 1092228e06c5f24a2b6c6412f81369e09bc9653cc51ff 192.168.4.56:6356@16356 slave 8568fbd73cb296cad6915d524e34761b2114af47 0 1538040400000 6 connec..............192.168.4.51:6351>
看节点前后的编号id是否有相同的 如:8568fbd73cb296cad6915d524e34761b2114af47 发现52的从库为56 停止主库52
[ root@redisA log] # redis- cli - h 192.168.4.52 - p 6352 shutdown[ root@redisA log] # redis- cli - c - h 192.168.4.51- p 6351192.168.4.51:6351> CLUSTER NODES...............................8568fbd73cb296cad6915d524e34761b2114af47 192.168.4.52:6352@16352 master,fail - 1538041052349 1538041051000 2 disconnected //52的主库坏掉28e06c5f24a2b6c6412f81369e09bc9653cc51ff 192.168.4.56:6356@16356 master - 0 1538041066000 7 connected 5461- 10922 //56成为主库...............................
[ root@redisB redis- 4.0.8] # /etc/init.d/redis_6352 startStarting Redis server...[ root@redisA log] # redis- cli - c - h 192.168.4.51- p 6351192.168.4.51:6351> CLUSTER NODES 8568fbd73cb296cad6915d524e34761b2114af47 192.168.4.52:6352@16352 slave 28e06c5f24a2b6c6412f81369e09bc9653cc51ff 0 1538041254000 7 connected
部署一台新redis服务器,ip为192.168.4.58,装包,初始化,启用集群配置,重启服务(这里之前已经操作过) 添加集群4.58(添加master节点) 格式:redis-trib.rb 选项 参数 选项: add-nade 添加主机(不指定角色为主) 由于之前是在51上面创建ruby脚本,所以只有51上面有redis-trib.rb命令,在51上面执行
[ root@redisA ~] # redis-trib.rb add- node 192.168.4.58:6358 192.168.4.51:6351>>> Adding node 192.168.4.58:6358 to cluster 192.168.4.51:6351>>> Performing Cluster Check ( using node 192.168.4.51:6351)S: a3083123bc5c87a76aab2655171634d4ee84f418 192.168.4.51:6351slots: ( 0 slots) slavereplicates 7e8d9121f44d8331ff55b45c218b87df9bda1b70M: 7e8d9121f44d8331ff55b45c218b87df9bda1b70 192.168.4.55:6355slots:0- 5460 ( 5461slots) master1additional replica( s)S: 8568fbd73cb296cad6915d524e34761b2114af47 192.168.4.52:6352slots: ( 0 slots) slavereplicates 28e06c5f24a2b6c6412f81369e09bc9653cc51ffM: bc5c4e082a5a3391b634cf433a6486c867cf c44b 192.168.4.53:6353slots:10923- 16383 ( 5461slots) master1additional replica( s)S: 63afbb5e7d63b1f142358634578a3488e3c9e634 192.168.4.54:6354slots: ( 0 slots) slavereplicates bc5c4e082a5a3391b634cf433a6486c867cf c44bM: 28e06c5f24a2b6c6412f81369e09bc9653cc51ff 192.168.4.56:6356slots:5461- 10922 ( 5462 slots) master1additional 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.58:6358 to make it join the cluster. [ OK] New node added correctly .
选项:check 检查权限[ root@redisA ~] # redis- trib.rb check 192.168.4.58:6358 //查看状态>>> Performing Cluster Check ( using node 192.168.4.58:6358)M: c5e0da48f335c46a2ec199faa99b830f537dd8a0 192.168.4.58:6358slots: ( 0 slots) master //发现没有hash槽0 additional replica( s)M: 7e8d9121f44d8331ff55b45c218b87df9bda1b70 192.168.4.55:6355slots:0- 5460 ( 5461slots) master1additional replica( s)................S: a3083123bc5c87a76aab2655171634d4ee84f418 192.168.4.51:6351slots: ( 0 slots) slavereplicates 7e8d9121f44d8331ff55b45c218b87df9bda1b70[ OK] All nodes agree about slots configuration.>>> Check for open slots...>>> Check slots coverage...[ OK] All 16384 slots covered.
选项:reshard 重新分配hash槽[ root@redisA ~] # redis- trib.rb reshard 192.168.4.58:6358How many slots do y ou want to move ( from 1to 16384) ?4096//拿出多少个hash 槽给主机192.168.4.58 What is the receiv ing node ID? c5e0da48f335c46a2ec199faa99b830f537dd8a0 //主机192.168.4.58的id值 Source node #1:all //从当前所有的主里面获取hash槽 Doyou want to proceed with the proposed reshard plan ( y es/no) ?yes .................... Mov ing slot 12283 from 192.168.4.53:6353 to 192.168.4.58:6358: Mov ing slot 12284 from 192.168.4.53:6353 to 192.168.4.58:6358: Mov ing slot 12285 from 192.168.4.53:6353 to 192.168.4.58:6358: Mov ing slot 12286 from 192.168.4.53:6353 to 192.168.4.58:6358: Mov ing slot 12287 from 192.168.4.53:6353 to 192.168.4.58:6358:
[ root@redisA ~] # redis- trib.rb check 192.168.4.58:6358>>> Performing Cluster Check ( using node 192.168.4.58:6358)M: c5e0da48f335c46a2ec199faa99b830f537dd8a0 192.168.4.58:6358slots:0- 1364,5461- 6826,10923- 12287 ( 4096 slots) master0 additional replica( s)
先删除主机占用的hash槽
[ root@redisA ~] # redis- trib.rb reshard 192.168.4.58:6358How many slots do y ou want to move ( from 1to 16384) ?4096//移除hash 槽的个数What is the receiv ing node ID? bc5c4e082a5a3391b634cf433a6486c867cf c44b//要移动给谁的id即目标主机(这里可以随机写一个master的ID)Source node #1: c5e0da48f335c46a2ec199faa99b830f537dd8a0//从谁那移动即源主机(这里写4.58的ID)Source node #2:done //设置完毕...............Mov ing slot 12282 from c5e0da48f335c46a2ec199faa99b830f537dd8a0Mov ing slot 12283 from c5e0da48f335c46a2ec199faa99b830f537dd8a0Mov ing slot 12284 from c5e0da48f335c46a2ec199faa99b830f537dd8a0Mov ing slot 12285 from c5e0da48f335c46a2ec199faa99b830f537dd8a0Mov ing slot 12286 from c5e0da48f335c46a2ec199faa99b830f537dd8a0Mov ing slot 12287 from c5e0da48f335c46a2ec199faa99b830f537dd8a0 Do y ou want to proceed with the proposed reshard plan ( y es/no) ?y es //提交 .................................... Mov ing slot 12282 from 192.168.4.58:6358 to 192.168.4.53:6353: Mov ing slot 12283 from 192.168.4.58:6358 to 192.168.4.53:6353: Mov ing slot 12284 from 192.168.4.58:6358 to 192.168.4.53:6353: Mov ing slot 12285 from 192.168.4.58:6358 to 192.168.4.53:6353: Mov ing slot 12286 from 192.168.4.58:6358 to 192.168.4.53:6353: Mov ing slot 12287 from 192.168.4.58:6358 to 192.168.4.53:6353:
[ root@redisA ~] # redis- trib.rb del- node 192.168.4.58:6358 \c5e0da48f335c46a2ec199faa99b830f537dd8a0 //删除谁+删除的id>>> Remov ing node c5e0da48f335c46a2ec199faa99b830f537dd8a0 from cluster 192.168.4.58:6358>>> Sending CLUSTER FORGET messages to the cluster...>>> SHUTDOWN the node.
[ root@redisA ~] # redis- trib.rb add- node - - slave \192.168.4.57:6357 192.168.4.51:6351 >>> Adding node 192.168.4.57:6357 to cluster 192.168.4.51:6351 >>> Performing Cluster Check ( using node 192.168.4.51:6351) ............... ............... [ OK] All 16384 slots covered. Automatically selected master 192.168.4.51:6351 >>> Send CLUSTER MEET to node 192.168.4.57:6357 to make it join the cluster. Waiting for the cluster to join. >>> Configure node as replica of 192.168.4.51:6351. [ OK] New node added correctly .
命令格式: redis-trib.rb del-node 192.168.4.57:6357 主机id值
[ root@redisA ~] # redis- trib.rb del- node 192.168.4.57:6357 \f6649ea99b2f01faca26217691222c17a3854381>>> Remov ing node f6649ea99b2f01faca26217691222c17a3854381from cluster 192.168.4.57:6351 >>> Sending CLUSTER FORGET messages to the cluster... >>> SHUTDOWN the node.
配置主从,4.51为主,4.52为从 若主机做过redis集群,需要在配置文件里面把开启集群,存储集群信息的配置文件都关闭,新主机则不用,这里用之前的redis集群做主从,需要还 原redis服务器
[ root@redisA ~] # redis- cli - c - h 192.168.4.51- p 6351shutdown //先关闭redis集群 [ root@redisA ~] # v im /etc/redis/6379.conf bind 192.168.4.51 port 6379 # cluster- enabled yes # cluster- config- file nodes- 6351.conf [ root@redisA ~] # /etc/init.d/redis_6379 start Starting Redis server... [ root@redisA ~] # ss - antlp | grep 6379 LISTEN 0 511 192.168.4.51:6379 *:* users:( ( "redis- server",pid=22274,fd=6) ) [ root@redisA ~] # redis- cli - h 192.168.4.51 192.168.4.51:6379> info replication //查看主从配置信息 # Replication role:master //默认是master 服务器 connected_slaves:0master_replid:eaa14478158a71c41f947eaea036658c2087e8f2master_replid2:0000000000000000000000000000000000000000master_repl_offset:0second_repl_offset:- 1repl_backlog_active:0repl_backlog_size:1048576repl_backlog_first_by te_offset:0repl_backlog_histlen:0192.168.4.51:6379>
192.168.4.52:6379> SLAVEOF 192.168.4.51 6379 //把52配置为51的从库OK
192.168.4.52:6379> INFO replication# Replicationrole:slavemaster_host:192.168.4.51 //主库为4.51master_port:6379master_link_status:upmaster_last_io_seconds_ago:3master_sy nc_in_progress:0
[ root@redisA ~] # redis- cli - h 192.168.4.51192.168.4.51:6379> info replication# Replicationrole:masterconnected_slaves:1slave0:ip=192.168.4.52,port=6379,state=online,offset=14,lag=1 //从库为4.52master_replid:db7932eb0ea4302bddbebd395efa174fb079319fmaster_replid2:0000000000000000000000000000000000000000master_repl_offset:14second_repl_offset:- 1repl_backlog_active:1repl_backlog_size:1048576repl_backlog_first_by te_offset:1repl_backlog_histlen:14192.168.4.51:6379>
[ root@redisA ~] # redis- cli - h 192.168.4.51shutdown //关闭主库192.168.4.52:6379> SLAVEOF no one //手动设为主库OK192.168.4.52:6379> INFO replication# Replicationrole:masterconnected_slaves:0master_replid:00e35c62d2b673ec48d3c8c7d9c7ea3366eac33amaster_replid2:db7932eb0ea4302bddbebd395efa174fb079319fmaster_repl_offset:420second_repl_offset:421repl_backlog_active:1repl_backlog_size:1048576repl_backlog_first_by te_offset:1repl_backlog_histlen:420192.168.4.52:6379>
主库宕机后,从库自动升级为主库 在slave主机编辑sentinel.conf文件 在slave主机运行哨兵程序
[ root@redisB ~] # redis- cli - h 192.168.4.52192.168.4.52:6379> SLAVEOF 192.168.4.51 6379OK192.168.4.52:6379> INFO replication# Replicationrole:slavemaster_host:192.168.4.51master_port:6379...................[ root@redisA ~] # /etc/init.d/redis_6379 startStarting Redis server...[ root@redisA ~] # redis- cli - h 192.168.4.51192.168.4.51:6379> info replication# Replicationrole:masterconnected_slaves:1slave0:ip=192.168.4.52,port=6379,state=online,offset=451,lag=1master_replid:4dfa0877c740507ac7844f8dd996445d368d6d0fmaster_replid2:0000000000000000000000000000000000000000..........................[ root@redisB ~] # v im /etc/sentinel.confsentinel monitor redisA 192.168.4.51 6379 1关键字 关键字 主机名自定义 ip 端口 票数sentinel auth- pass redis51密码 //连接主库密码,若主库有密码加上这一行[ root@redisB ~] # redis- sentinel /etc/sentinel.conf //执行,之后把主库宕机..........................25371:X 28 Sep 11:16:54.993 # +sdown master redis51192.168.4.51637925371:X 28 Sep 11:16:54.993 # +odown master redis51192.168.4.516379 #quorum 1/125371:X 28 Sep 11:16:54.993 # +new- epoch 325371:X 28 Sep 11:16:54.993 # +try - failover master redis51192.168.4.51637925371:X 28 Sep 11:16:54.994 # +vote- for- leader be035801d4d48eb63d8420a72796f52f c5cec047 3.....................25371:X 28 Sep 11:16:55.287 * +slave slave 192.168.4.51:6379 192.168.4.516379 @ redis51192.168.4.52 637925371:X 28 Sep 11:17:25.316 # +sdown slave 192.168.4.51:6379 192.168.4.516379 @ redis51192.168.4.52 6379
关闭4.51和4.52,启动之后用info replication查看,各自为主 主库设置密码,在51上面操作
[ root@redisA ~] # redis- cli - h 192.168.4.51shutdown[ root@redisA ~] # vim /etc/redis/6379.confrequirepass 123456[ root@redisA ~] # /etc/init.d/redis_6379 startStarting Redis server...[ root@redisA ~] # redis- cli - h 192.168.4.51- a 123456192.168.4.51:6379> pingPONG192.168.4.51:6379>
[ root@redisB ~] # redis- cli - h 192.168.4.52 shutdownslaveof 192.168.4.516379masterauth 123456[ root@redisB ~] # /etc/init.d/redis_6352 startStarting Redis server...
[ root@redisB ~] # redis- cli - h 192.168.4.52 - a 123456192.168.4.52:6379> info replication# Replicationrole:slavemaster_host:192.168.4.51master_port:6379master_link_status:up
[ root@redisA ~] # redis- cli - h 192.168.4.51- a 123456192.168.4.51:6379> info replication# Replicationrole:masterconnected_slaves:1slave0:ip=192.168.4.52,port=6379,state=online,offset=98,lag=0
RDB介绍: Redis数据库文件,全称Reids DataBase 数据持久化方式之一 在指定时间间隔内,将内存中的数据集快照写入硬盘 术语叫Snapshot快照 恢复时,将快照文件直接读到内存里 相关配置参数 文件名 dbfilename “dump.rdb” 文件名 save “” 禁用RDB 数据从内存保存到硬盘的频率 save 900 1 900秒内且有1次修改 save 300 10 300秒内且有10次修改 save 60 10000 60秒内且有10000修改
[ root@redisA ~] # redis- cli - h 192.168.4.51- a 123456 shutdown[ root@redisA ~] # v im /etc/redis/6379.confdbfilename dump.rdb#save "" //启用RDB,去掉#号为禁用RDBsave 120 10 //120秒内且有1次修改(满足三个条件中的任意一个都会保存) save 300 10 save 60 10000[ root@redisA ~] # /etc/init.d/redis_6379 start Starting Redis server...[ root@redisA ~] # redis- cli - h 192.168.4.51- a 123456 192.168.4.51:6379>[ root@redisA ~] # redis- cli - h 192.168.4.51- a 123456 192.168.4.51:6379> 192.168.4.51:6379> set v1k1 OK 192.168.4.51:6379> set v2 k1 OK 192.168.4.51:6379> set v3 k1 OK 192.168.4.51:6379> set v4 k1 OK 192.168.4.51:6379> set v45 k1 OK 192.168.4.51:6379> set v46 k1 OK 192.168.4.51:6379> set v7 k1 OK 192.168.4.51:6379> set v8 k1 OK 192.168.4.51:6379> set v9 k1 OK 192.168.4.51:6379> set v10 k1 OK 192.168.4.51:6379> keys *1) "v2"2) "v9"3) "v10"4) "v45"5) "v4"6) "v1"7) "v46"8) "v8"9) "v7"10) "v3"192.168.4.51:6379>exit
[ root@redisA 6379] # redis- cli - h 192.168.4.51- a 123456 shutdown //停止服务[ root@redisA ~] # cd /var/lib/redis/6379/[ root@redisA 6379] # lsdump.rdb nodes- 6351.conf[ root@redisA 6379] # cp dump.rdb dump.rdb.bak //备份dump.rdb,之后删除
[ root@redisA 6379] # rm - rf dump.rdb[ root@redisA 6379] # /etc/init.d/redis_6379 start Starting Redis server...[ root@redisA 6379] # lsdump.rdb dump.rdb.bak nodes- 6351.conf[ root@redisA 6379] # redis- cli - h 192.168.4.51- a 123456192.168.4.51:6379> key s * //已经没有数据( empty list or set)192.168.4.51:6379>
[ root@redisA 6379] # redis- cli - h 192.168.4.51- a 123456 shutdown[ root@redisA 6379] # mv dump.rdb.bak dump.rdbmv : overwrite ‘ dump.rdb’ ? y[ root@redisA 6379] # /etc/init.d/redis_6379 startStarting Redis server...[ root@redisA 6379] # redis- cli - h 192.168.4.51- a 123456192.168.4.51:6379> keys * 1) "v7" 2) "v46" 3) "v45" 4) "v8" 5) "v4" 6) "v2" 7) "v1" 8) "v3" 9) "v9" 10) "v10" 192.168.4.51:6379>
高性能的持久化实现:创建一个子进程来执行持久化,先将数据写入临时文件,持久化过程结束后,再用这个临时文件替换上次持久化好的文件; 过程中主进程不做任何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 从不记录
[ root@redisA 6379] # redis- cli - h 192.168.4.51- a 123456 shutdown[ root@redisA 6379] # rm - rf dump.rdb[ root@redisA 6379] # v im /etc/redis/6379.confappendonly y es //启用aof,默认noappendfilename "appendonly .aof " //文件名appendfsy nc every sec //每秒记录一次[ root@redisA 6379] # vim /etc/redis/6379.conf[ root@redisA 6379] # /etc/init.d/redis_6379 startStarting Redis server...[ root@redisA 6379] # ls //会出现appendonly .aof文件appendonly .aof dump.rdb nodes- 6351.conf[ root@redisA 6379] # cat appendonly .aof //无内容[ root@redisA 6379] # redis- cli - h 192.168.4.51- a 123456192.168.4.51:6379> set v1 a1OK192.168.4.51:6379> set v2 a2OK192.168.4.51:6379> set v3 a3OK192.168.4.51:6379> set v4 a4OK192.168.4.51:6379> set v5 a5OK192.168.4.51:6379> set v6 a6OK192.168.4.51:6379> set v7 a7OK192.168.4.51:6379> set v8 a7OK192.168.4.51:6379> set v9 a9OK192.168.4.51:6379> set v10 a10OK192.168.4.51:6379> keys *1) "v2"2) "v5"3) "v10"4) "v9"5) "v6"6) "v8"7) "v3"8) "v7"9) "v1"10) "v4"192.168.4.51:6379> exit[ root@redisA 6379] # cat appendonly .aof //有数据
[ root@redisA 6379] # cp appendonly .aof appendonly .aof.bak[ root@redisA 6379] # redis- cli - h 192.168.4.51- a 123456 shutdown
[ root@redisA 6379] # rm - rf appendonly .aof[ root@redisA 6379] # /etc/init.d/redis_6379 start Starting Redis server...[ root@redisA 6379] # redis- cli - h 192.168.4.51- a 123456192.168.4.51:6379> keys * ( empty list or set)192.168.4.51:6379> exit
[ root@redisA 6379] # mv appendonly .aof.bak appendonly .aofmv : overwrite ‘ appendonly .aof’ ? y[ root@redisA 6379] # redis- cli - h 192.168.4.51- a 123456 shutdown[ root@redisA 6379] # /etc/init.d/redis_6379 startStarting Redis server...[ root@redisA 6379] # redis- cli - h 192.168.4.51- a 123456192.168.4.51:6379> keys *1) "v9"2) "v5"3) "v8"4) "v2"5) "v1"6) "v4"7) "v10"8) "v6"9) "v7"10) "v3"192.168.4.51:6379>
[ root@redisA 6379] # v im appendonly .aof*2 //可以把这一行删除$6SELECT$10*3set$2a1*3$3.........[ root@redisA 6379] # redis- check- aof - - fix appendonly .aof //恢复文件0x 0: Expected prefix '*', got: '$'AOF analy zed: size=311, ok_up_to=0, diff=311This will shrink the AOF from 311by tes, with 311by tes, to 0 by tesContinue? [ y /N] : ySuccessfully truncated AOF
可以灵活的设置同步持久化appendfsync always或异步持久化appendfsync verysec 宕机时,仅可能丢失1秒的数据
AOF文件的体积通常会大于RDB文件的体积 执行fsync策略时的速度可能会比RDB慢
String字符串 set key value [ex seconds] [px milliseconds] [nx|xx] 设置key及值,过期时间可以使用秒或毫秒为单位 setrange key offset value 从偏移量开始复写key的特定位的值
[ root@redisA 6379] # redis- cli - h 192.168.4.51- a 123456 192.168.4.51:6379> set first "hello world" OK 192.168.4.51:6379> setrange first 6 "Redis" //改写为hello Redis ( integer) 11 192.168.4.51:6379> get first "hello Redis"
192.168.4.51:6379> strlen first ( integer) 11
192.168.4.51:6379> append my name jacob( integer) 5
192.168.4.51:6379> setbit bit 0 1 //设置bit第0位为1( integer) 0192.168.4.51:6379> setbit bit 1 0 //设置bit第1位为0( integer) 0
192.168.4.51:6379> setbit bits 0 1 //0001 ( integer) 0192.168.4.51:6379> setbit bits 3 1 //1001( integer) 0192.168.4.51:6379> bitcount bits //结果为2( integer) 2
192.168.4.51:6379> setbit peter 100 1 //网站上线100天用户登录了一次( integer) 0192.168.4.51:6379> setbit peter 105 1 //网站上线105天用户登录了一次( integer) 0192.168.4.51:6379> bitcount peter( integer) 2
192.168.4.51:6379> set z 10OK192.168.4.51:6379> decr z( integer) 9192.168.4.51:6379> decr z( integer) 8192.168.4.51:6379> decr bb( integer) - 1192.168.4.51:6379> decr bb( integer) - 2
192.168.4.51:6379> set count 100OK192.168.4.51:6379> DECRBY cc 20 //定义每次减少20(步长)( integer) - 20192.168.4.51:6379> DECRBY cc 20( integer) - 40
192.168.4.51:6379> get a( nil)
192.168.4.51:6379> set x 123456789OK192.168.4.51:6379> getrange x - 5 - 1"56789"192.168.4.51:6379> getrange x 0 4"12345"
192.168.4.51:6379> set page 20OK192.168.4.51:6379> incr page( integer) 21
192.168.4.51:6379> set x 10OK 192.168.4.51:6379> incr x ( integer) 11 192.168.4.51:6379> incr x ( integer) 12
192.168.4.51:6379> set num 16.1OK192.168.4.51:6379> incrby float num 1.1"17.2"
192.168.4.51:6379> mset j 9 k 29OK
192.168.4.51:6379> mget j k1) "9"2) "29"
Redis的list是一个字符队列,先进后出,一个key可以有多个值 lpush key value [value…] 将一个或多个值value插入到列表key的表头,Key不存在,则创建key
192.168.4.51:6379> lpush list a b c //list值依次为c b a( integer) 3
192.168.4.51:6379> lrange list 0 2 //从0位开始,读到2位为止1) "c"2) "b"3) "a"192.168.4.51:6379> lrange list 0 - 1 //从开始读到结束为止1) "c"2) "b"3) "a"192.168.4.51:6379> lrange list 0 - 2 //从开始读到倒数第2位值1) "c"2) "b"
192.168.4.51:6379> lpop list //删除表头元素,可以多次执行"c"192.168.4.51:6379> LPOP list"b"
192.168.4.51:6379> llen list"c"
192.168.4.51:6379> lpush list a b c d( integer) 5192.168.4.51:6379> lset list 3 test //将list中第3个值修改为testOK
192.168.4.51:6379> rpush list3 a b c //list3值为a b c( integer) 3192.168.4.51:6379> rpush list3 d //末尾插入d( integer) 4
192.168.4.51:6379> RPOP list3"d"
hset key field value 将hash表中field值设置为value
192.168.4.51:6379> hset site google 'www.g.cn' ( integer) 1192.168.4.51:6379> hset site baidu 'www.baidu.com'( integer) 1
192.168.4.51:6379> hget site google"www.g.cn"
192.168.4.51:6379> hmset site google www.g.cn baidu www.baidu.comOK
192.168.4.51:6379> hmget site google baidu1) "www.g.cn"2) "www.baidu.com"
192.168.4.51:6379> hmset site google www.g.cn baidu www.baidu.comOK192.168.4.51:6379> hkey s site1) "google"2) "baidu"
192.168.4.51:6379> hgetall site1) "google"2) "www.g.cn"3) "baidu"4) "www.baidu.com"
192.168.4.51:6379> hvals site1) "www.g.cn"2) "www.baidu.com"
192.168.4.51:6379> hdel site google baidu( integer) 2
转载地址:http://elnwi.baihongyu.com/