Top
NSD DBA DAY09
- 案例1:搭建redis服務(wù)器
- 案例2:常用命令限
- 案例3:部署LNP+Redis
- 案例4:創(chuàng)建redis集群
1 案例1:搭建redis服務(wù)器
1.1 具體要求如下
- 在主機(jī)redis64運(yùn)行redis服務(wù)
- 修改服務(wù)運(yùn)行參數(shù)
- ip 地址192.168.88.64
- 服務(wù)監(jiān)聽(tīng)的端口6364
- redis服務(wù)的連接密碼為 tarenaplj
1.2 方案
準(zhǔn)備1臺(tái)新虛擬機(jī),要求如表-1所示。
?
1.3 步驟
實(shí)現(xiàn)此案例需要按照如下步驟進(jìn)行。
步驟一:在主機(jī)redis64運(yùn)行redis服務(wù)
命令操作如下所示:
- [root@redis64 ~]# yum -y install redis 安裝軟件
- [root@redis64 ~]# systemctl start redis 啟動(dòng)服務(wù)
- [root@redis64 ~]# netstat -utnlp | grep redis-server 查看端口
- tcp 0 0 127.0.0.1:6379 0.0.0.0:* LISTEN 1970/redis-server 1
- [root@redis64 ~]# redis-cli 連接服務(wù)
- 127.0.0.1:6379> ping 檢查能否正在訪問(wèn)
- PONG
- 127.0.0.1:6379> set school tarena 存儲(chǔ)變量
- OK
- 127.0.0.1:6379> get school 查看變量
- "tarena"
- 127.0.0.1:6379> exit 斷開(kāi)連接
- [root@redis64 ~]#
步驟二:修改服務(wù)運(yùn)行參數(shù)
命令操作如下所示:
- [root@redis64 ~]# systemctl stop redis
- [root@redis64 ~]# vim /etc/redis.conf
- 69 bind 192.168.88.64
- 92 port 6364
- 647 requirepass tarenaplj
- :wq
- [root@redis64 ~]# setenforce 0
- [root@redis64 ~]# systemctl start redis
- [root@redis64 ~]# netstat -utnlp | grep redis-server
- tcp 0 0 192.168.88.64:6364 0.0.0.0:* LISTEN 2009/redis-server 1
- [root@redis64 ~]#
- [root@redis64 ~]# redis-cli -h 192.168.88.64 -p 6364 連接服務(wù)
- 192.168.88.64:6364> ping 不輸入密碼無(wú)法正常訪問(wèn)
- (error) NOAUTH Authentication required.
- 192.168.88.64:6364> auth tarenaplj 輸入密碼
- OK
- 192.168.88.64:6364> keys * 查看存儲(chǔ)的變量
- 1) "school"
- 192.168.88.64:6364> exit 斷開(kāi)連接
- [root@redis64 ~]#
2 案例2:常用命令限
2.1 問(wèn)題
1)練習(xí)如下命令的使用
- mset mget keys type
- exists ttl expire move select
- del flushdb flushall
2.2 方案
在搭建的redis64練習(xí)常用命令的使用。
2.3 步驟
實(shí)現(xiàn)此案例需要按照如下步驟進(jìn)行。
命令操作如下所示:
- 192.168.88.64:6364> mset name plj age 80 class nsd2108 一起存儲(chǔ)多個(gè)變量
- OK
- 192.168.88.64:6364> keys *
- 1) "age"
- 2) "classs"
- 3) "name"
- 4) "school"
- 192.168.88.64:6364> mget name age 一起查看多個(gè)變量
- 1) "plj"
- 2) "80"
- keys 使用統(tǒng)配符查看變量
- * 匹配所有變量名
- ? 一個(gè)字符
- 192.168.88.64:6364> keys *
- 1) "age"
- 2) "classs"
- 3) "name"
- 4) "school"
- 192.168.88.64:6364> keys ??? 變量名是3個(gè)字符
- 1) "age"
- 192.168.88.64:6364> keys a* 變量名是a 開(kāi)頭的
- 1) "age"
- 192.168.88.64:6364>
- 192.168.88.64:6364> keys gender 查看指定變量是否存在
- (empty list or set) 說(shuō)明變量沒(méi)有定義
- 192.168.88.64:6364> keys name
- 1) "name"
- 192.168.88.64:6364>
- select 切換庫(kù) 默認(rèn)庫(kù)編號(hào) 0-15
- 192.168.88.64:6364> select 1 切換到 1號(hào)庫(kù)里
- OK
- 192.168.88.64:6364[1]> keys * 查看數(shù)據(jù)沒(méi)有數(shù)據(jù)
- (empty list or set)
- 192.168.88.64:6364[1]>
- 192.168.88.64:6364[1]> select 0
- OK
- 192.168.88.64:6364> select 16
- (error) ERR DB index is out of range
- move 命令 移動(dòng)變量到其他庫(kù)里
- 192.168.88.64:6364> keys *
- 1) "age"
- 2) "classs"
- 3) "name"
- 4) "school"
- 192.168.88.64:6364> move age 1 把a(bǔ)ge 變量移動(dòng)到1 號(hào)庫(kù)里
- (integer) 1
- 192.168.88.64:6364> keys *
- 1) "classs"
- 2) "name"
- 3) "school"
- 192.168.88.64:6364> select 1
- OK
- 192.168.88.64:6364[1]> keys *
- 1) "age"
- exists 檢查變量是否存儲(chǔ) 返回值1 變量存儲(chǔ) 返回值是0 變量不存在
- 192.168.88.64:6364[1]> select 0
- OK
- 192.168.88.64:6364> EXISTS name 檢查name變量是否存在
- (integer) 1
- 192.168.88.64:6364> get name
- "plj"
- 192.168.88.64:6364> set name bob
- OK
- 192.168.88.64:6364> get name
- "bob"
- 192.168.88.64:6364>
- EXPIRE 命令設(shè)置變量的過(guò)期時(shí)間 不設(shè)置變量永不過(guò)期
- ttl 檢查變量可以在內(nèi)存里存多久
- 192.168.88.64:6364> set sex girl
- OK
- 192.168.88.64:6364> ttl sex
- (integer) -1 表示永不過(guò)期
- 192.168.88.64:6364> EXPIRE sex 15 設(shè)置過(guò)期時(shí)間15 秒
- (integer) 1
- 192.168.88.64:6364> keys sex
- 1) "sex"
- 192.168.88.64:6364> ttl sex
- (integer) 8 還剩下8秒時(shí)間過(guò)期
- 192.168.88.64:6364> ttl sex
- (integer) -2 表示已經(jīng)過(guò)期被刪除
- 192.168.88.64:6364> keys sex
- (empty list or set)
- 192.168.88.64:6364>
- type 命令檢查變量存儲(chǔ)數(shù)據(jù)的類型
- 使用set mset命令存儲(chǔ)的數(shù)據(jù)都字符類型。
- 數(shù)據(jù)的類型不同 管理的命令也不同
- 明天講數(shù)據(jù)類型。
- 192.168.88.64:6364> set x 99
- OK
- 192.168.88.64:6364> mset y 108
- OK
- 192.168.88.64:6364> type x
- string
- 192.168.88.64:6364> type y
- string
- 192.168.88.64:6364> lpush tea nb wk zzg plj lx 定義類別類型的變量tea
- 192.168.88.64:6364> type tea 查看變量類型 list 列表類型的數(shù)據(jù)
- list
- del 刪除內(nèi)存里的變量
- 192.168.88.64:6364> keys *
- 1) "y"
- 2) "tea"
- 3) "name"
- 4) "school"
- 5) "x"
- 6) "classs"
- 192.168.88.64:6364> del tea y school
- (integer) 3
- 192.168.88.64:6364> keys *
- 1) "name"
- 2) "x"
- 3) "classs"
- 192.168.88.64:6364>
- flushdb 刪除當(dāng)前所在庫(kù)的所有數(shù)據(jù)
- 192.168.88.64:6364> keys *
- 1) "name"
- 2) "x"
- 3) "classs"
- 192.168.88.64:6364> flushdb
- OK
- 192.168.88.64:6364> keys *
- (empty list or set)
- 192.168.88.64:6364> SELECT 1
- OK
- 192.168.88.64:6364[1]> keys *
- 1) "age"
- 192.168.88.64:6364[1]> flushall 刪除內(nèi)存里的所有內(nèi)存里所有數(shù)據(jù) (慎用)
3 案例3:部署LNP+Redis
3.1 問(wèn)題
- 在主機(jī)192.168.88.64部署LNP 環(huán)境
- 配置PHP支持redis
- 編寫(xiě)網(wǎng)站腳本,把數(shù)據(jù)存儲(chǔ)到本機(jī)的內(nèi)存里
3.2 方案
在redis64主機(jī)同時(shí)運(yùn)行nginx 服務(wù),并配置nginx服務(wù)可以把數(shù)據(jù)存儲(chǔ)在本機(jī)的redis內(nèi)存服務(wù)里。
3.3 步驟
實(shí)現(xiàn)此案例需要按照如下步驟進(jìn)行。
步驟一:在主機(jī)192.168.88.64部署LNP 環(huán)境
1)安裝源碼nginx及php
- ]#yum -y install gcc pcre-devel zlib-devel make 安裝依賴
- ]#]# tar -xf nginx-1.22.1.tar.gz 解壓源碼
- ]#cd nginx-1.22.1 進(jìn)源碼目錄
- ]#./configure 配置
- ]#make 編譯
- ]#make install 安裝
- ]# ls /usr/local/nginx/ 查看安裝目錄
- conf html logs sbin
- ]# yum -y install php php-fpm php-devel 安裝php 軟件
2)配置動(dòng)靜分離
- ]# vim +65 /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;
- }
- :wq
3)啟動(dòng)nginx服務(wù)
- [root@redis64 ~]# /usr/local/nginx/sbin/nginx -t 測(cè)試配置
- nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
- nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
- [root@redis64 ~]#
- [root@redis64 ~]# /usr/local/nginx/sbin/nginx 啟動(dòng)服務(wù)
- [root@redis64 ~]# netstat -utnlp | grep 80
- tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 10469/nginx: master
- [root@redis64 ~]#
- 啟動(dòng)php-fpm服務(wù)
- ]# vim /etc/php-fpm.d/www.conf
- 38 ;listen = /run/php-fpm/www.sock
- 39 listen = 127.0.0.1:9000 非sock方式運(yùn)行 (不是必須的)
- :wq
- [root@redis64 ~]# systemctl start php-fpm 啟動(dòng)服務(wù)
- [root@redis64 ~]# netstat -utnlp | grep 9000 查看端口
- tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 10477/php-fpm: mast
- [root@redis64 ~]#
- 測(cè)試配置
- [root@redis64 ~]# vim /usr/local/nginx/html/test.php 編寫(xiě)php腳本
- <?php
- echo "hello world!!! \n";
- ?>
- [root@redis64 ~]# curl http://localhost/test.php 訪問(wèn)php腳本
- hello world!!!
- [root@redis64 ~]#
步驟二:配置PHP支持redis
1)安裝軟件提供模塊
--with-php-config=/usr/bin/php-config 獲取php版本信息
- [root@redis64 pub]# tar -xf redis-cluster-4.3.0.tgz 解壓源碼
- [root@redis64 pub]# cd redis-4.3.0/ 進(jìn)源碼目錄
- [root@redis64 redis-4.3.0]# phpize 獲取php版本信息
- Configuring for:
- PHP Api Version: 20170718
- Zend Module Api No: 20170718
- Zend Extension Api No: 320170718
- [root@redis64 redis-4.3.0]# ./configure --with-php-config=/usr/bin/php-config 配置
- [root@redis64 redis-4.3.0]# make && make install 編譯并安裝
- Build complete.
- Don't forget to run 'make test'.
- Installing shared extensions: /usr/lib64/php/modules/ 提示模塊安裝的位置
- [root@redis64 redis-4.3.0]# ls /usr/lib64/php/modules/redis.so 查看模塊
- /usr/lib64/php/modules/redis.so
- [root@redis64 redis-4.3.0]#
2)調(diào)用模塊
- [root@redis64 redis-4.3.0]# vim /etc/php.ini 編輯php進(jìn)程主配置文件
- 737 extension_dir = "/usr/lib64/php/modules/" 指定模塊所在目錄
- 739 extension = "redis.so" 指定模塊名
- :wq
3)加載模塊
- [root@redis64 redis-4.3.0]# systemctl restart php-fpm
4)查看模塊
- [root@redis64 ~]# php -m | grep redis
- redis
- [root@redis64 ~]#
步驟三:編寫(xiě)網(wǎng)站腳本,把數(shù)據(jù)存儲(chǔ)到本機(jī)的內(nèi)存里
1)允許通過(guò)lo口連接本機(jī)redis服務(wù)
- [root@redis64 ~]# systemctl stop redis
- [root@redis64 ~]# vim /etc/redis.conf
- 69 bind 127.0.0.1 192.168.88.64
- :wq
- [root@redis64 ~]# netstat -utnlp | grep redis-server
- tcp 0 0 192.168.88.64:6364 0.0.0.0:* LISTEN 12794/redis-server
- tcp 0 0 127.0.0.1:6364 0.0.0.0:* LISTEN 12794/redis-server
- [root@redis64 ~]#
2)編寫(xiě)php腳本
- [root@redis64 ~]# vim /usr/local/nginx/html/redis.php
- <?php
- $redis = new redis();
- $redis->connect("127.0.0.1", "6364");
- $redis->auth("tarenaplj");
- $redis->set("class","nsd");
- echo "save ok\n";
- ?>
3)訪問(wèn)腳本
- [root@redis64 ~]# curl http://localhost/redis.php
- save ok
- [root@redis64 ~]#
4)連接redis服務(wù)查看變量
- [root@redis64 ~]# redis-cli -h 127.0.0.1 -p 6364
- 127.0.0.1:6364> auth tarenaplj
- OK
- 127.0.0.1:6364> keys *
- 1) "school"
- 2) "class"
- 127.0.0.1:6364> get class
- "nsd"
- 127.0.0.1:6364>
4 案例4:創(chuàng)建redis集群
4.1 問(wèn)題
- 準(zhǔn)備集群環(huán)境
- 創(chuàng)建集群
- 查看集群信息
- 訪問(wèn)集群
4.2 方案
創(chuàng)建6臺(tái)新虛擬機(jī),具體要求如表-2所示。
?
4.3 步驟
實(shí)現(xiàn)此案例需要按照如下步驟進(jìn)行。
步驟一:準(zhǔn)備集群環(huán)境
說(shuō)明:準(zhǔn)備做集群的主機(jī),不允許存儲(chǔ)數(shù)據(jù)、不允許設(shè)置連接密碼
配置服務(wù)器192.168.88.51
- ]# yum -y install redis 安裝軟件
- ]# vim /etc/redis.conf 修改主配置文件夾
- 92 port 6379 //端口號(hào)
- 69 bind 192.168.88.51 //IP地址
- 838 cluster-enabled yes ???? ????????//啟用集群功能
- 846 cluster-config-file nodes-6379.conf ????????//存儲(chǔ)集群信息文件
- 852 cluster-node-timeout 5000 //集群中主機(jī)通信超時(shí)時(shí)間
- :wq
- ]# systemctl start redis
- ]# netstat -utnlp | grep redis-serve
- tcp 0 0 192.168.88.51:6379 0.0.0.0:* LISTEN 21201/redis-serve
- tcp 0 0 192.168.88.51:16379 0.0.0.0:* LISTEN 21201/redis-serve
配置服務(wù)器192.168.88.52
- ]# yum -y install redis
- ]# vim /etc/redis.conf
- 92 port 6379
- 69 bind 192.168.88.52
- 838 cluster-enabled yes ???? ????????//啟用集群功能
- 846 cluster-config-file nodes-6379.conf ????????//存儲(chǔ)集群信息文件
- 852 cluster-node-timeout 5000 //集群中主機(jī)通信超時(shí)時(shí)間
- :wq
- ]# systemctl start redis
- ]# netstat -utnlp | grep redis-serve
- tcp 0 0 192.168.88.52:6379 0.0.0.0:* LISTEN 21201/redis-serve
- tcp 0 0 192.168.88.52:16379 0.0.0.0:* LISTEN 21201/redis-serve
配置服務(wù)器192.168.88.53
- ]# yum -y install redis
- ]# vim /etc/redis.conf
- 92 port 6379
- 69 bind 192.168.88.53
- 838 cluster-enabled yes ???? ????????//啟用集群功能
- 846 cluster-config-file nodes-6379.conf ????????//存儲(chǔ)集群信息文件
- 852 cluster-node-timeout 5000 //集群中主機(jī)通信超時(shí)時(shí)間
- :wq
- ]# systemctl start redis
- ]# netstat -utnlp | grep redis-serve
- tcp 0 0 192.168.88.53:6379 0.0.0.0:* LISTEN 21201/redis-serve
- tcp 0 0 192.168.88.53:16379 0.0.0.0:* LISTEN 21201/redis-serve
配置服務(wù)器192.168.88.54
- ]# yum -y install redis
- ]# vim /etc/redis.conf
- 92 port 6379
- 69 bind 192.168.88.54
- 838 cluster-enabled yes ???? ????????//啟用集群功能
- 846 cluster-config-file nodes-6379.conf ????????//存儲(chǔ)集群信息文件
- 852 cluster-node-timeout 5000 //集群中主機(jī)通信超時(shí)時(shí)間
- :wq
- ]# systemctl start redis
- ]# netstat -utnlp | grep redis-serve
- tcp 0 0 192.168.88.54:6379 0.0.0.0:* LISTEN 21201/redis-serve
- tcp 0 0 192.168.88.54:16379 0.0.0.0:* LISTEN 21201/redis-serve
配置服務(wù)器192.168.88.55
- ]# yum -y install redis
- ]# vim /etc/redis.conf
- 92 port 6379
- 69 bind 192.168.88.55
- 838 cluster-enabled yes ???? ????????//啟用集群功能
- 846 cluster-config-file nodes-6379.conf ????????//存儲(chǔ)集群信息文件
- 852 cluster-node-timeout 5000 //集群中主機(jī)通信超時(shí)時(shí)間
- :wq
- ]# systemctl start redis
- ]# netstat -utnlp | grep redis-serve
- tcp 0 0 192.168.88.55:6379 0.0.0.0:* LISTEN 21201/redis-serve
- tcp 0 0 192.168.88.55:16379 0.0.0.0:* LISTEN 21201/redis-serve
配置服務(wù)器192.168.88.56
- ]# yum -y install redis
- ]# vim /etc/redis.conf
- 92 port 6379
- 69 bind 192.168.88.56
- 838 cluster-enabled yes ???? ????????//啟用集群功能
- 846 cluster-config-file nodes-6379.conf ????????//存儲(chǔ)集群信息文件
- 852 cluster-node-timeout 50ha00 //集群中主機(jī)通信超時(shí)時(shí)間
- :wq
- ]# systemctl start redis
- ]# netstat -utnlp | grep redis-serve
- tcp 0 0 192.168.88.56:6379 0.0.0.0:* LISTEN 21201/redis-serve
- tcp 0 0 192.168.88.56:16379 0.0.0.0:* LISTEN 21201/redis-serve
步驟二:創(chuàng)建集群
說(shuō)明:
1、在任意一臺(tái)redis服務(wù)器上都可以執(zhí)行創(chuàng)建集群的命令。
2、--cluster-replicas 1 給每個(gè)master服務(wù)器分配一臺(tái)slave服務(wù)器,每個(gè)主至少要分配1臺(tái)slave服務(wù)器,不然無(wú)法實(shí)現(xiàn)redis服務(wù)的高可用。
3、創(chuàng)建集群時(shí),會(huì)自動(dòng)創(chuàng)建主從角色,默認(rèn)把主機(jī)列表中的前3臺(tái)服務(wù)器創(chuàng)建為
Master角色的redis服務(wù)器,剩下的均配置為slave角色服務(wù)器。
4、創(chuàng)建集群時(shí),會(huì)自動(dòng)給master角色的主機(jī)分配hash槽 ,通過(guò)hash槽實(shí)現(xiàn)數(shù)據(jù)的分布式存儲(chǔ)。
- ]# redis-cli --cluster create 192.168.88.51:6379 192.168.88.52:6379 192.168.88.53:6379 192.168.88.54:6379 192.168.88.55:6379 192.168.88.56:6379 --cluster-replicas 1
- >>> Creating cluster
- >>> Performing hash slots allocation on 6 nodes...
- Using 3 masters:
- 192.168.88.51:6379
- 192.168.88.52:6379
- 192.168.88.53:6379
- Adding replica 192.168.88.55:6379 to 192.168.88.51:6379
- Adding replica 192.168.88.56:6379 to 192.168.88.52:6379
- Adding replica 192.168.88.54:6379 to 192.168.88.53:6379
- M: 0eb3b7aa0493a19189cba35b0c658202cc20884b 192.168.88.51:6379
- slots:0-5460 (5461 slots) master
- M: a9cb8ccd31bf3eb70433c00906c9f1a99b5e8441 192.168.88.52:6379
- slots:5461-10922 (5462 slots) master
- M: f2c1bdb78d8d224c3666fa6440bdf80ba563c581 192.168.88.53:6379
- slots:10923-16383 (5461 slots) master
- S: bdba77868965371680fa825badff59bf8ef95a81 192.168.88.54:6379
- replicates f2c1bdb78d8d224c3666fa6440bdf80ba563c581
- S: 11510f42bfd9cf667bab5f31c01476d94984200a 192.168.88.55:6379
- replicates 0eb3b7aa0493a19189cba35b0c658202cc20884b
- S: fe572ce003ee634c52adc4b42d92d15f847937d7 192.168.88.56:6379
- replicates a9cb8ccd31bf3eb70433c00906c9f1a99b5e8441
- Can I set the above configuration? (type 'yes' to accept): yes 同意
- ....
- ....
- [OK] All nodes agree about slots configuration.
- >>> Check for open slots...
- >>> Check slots coverage...
- [OK] All 16384 slots covered. //創(chuàng)建成功的提示
步驟三:查看集群信息
第一列:主服務(wù)器ip地址
第二列:主服務(wù)器ID
第三列:存儲(chǔ)變量個(gè)數(shù)
第四列:hash槽個(gè)數(shù) (hash槽的作用在集群存儲(chǔ)工程過(guò)程里講)
第五列:從服務(wù)器數(shù)量
- ]# redis-cli --cluster info 192.168.88.51:6379
- 192.168.88.53:6379 (f2c1bdb7...) -> 0 keys | 5461 slots | 1 slaves.
- 192.168.88.51:6379 (0eb3b7aa...) -> 0 keys | 5461 slots | 1 slaves.
- 192.168.88.52:6379 (a9cb8ccd...) -> 0 keys | 5462 slots | 1 slaves.
- [OK] 0 keys in 3 masters.
- 0.00 keys per slot on average.
步驟四:訪問(wèn)集群
- [root@host51 ~]# redis-cli -c -h 192.168.88.51 -p 6379
- 192.168.88.51:6379> set school tarena
- -> Redirected to slot [8455] located at 192.168.88.52:6379
- OK
- 192.168.88.52:6379> set teacher plj
- -> Redirected to slot [12541] located at 192.168.88.53:6379
- OK
- 192.168.88.53:6379> set class NSD
- -> Redirected to slot [741] located at 192.168.88.51:6379
- OK
- 192.168.88.51:6379>
會(huì)發(fā)現(xiàn)3個(gè)變量被分別存儲(chǔ)到集群中的3臺(tái)master服務(wù)器上,實(shí)現(xiàn)了數(shù)據(jù)的分布式存儲(chǔ)。當(dāng)連接集群中的任意一臺(tái)redis服務(wù)器存儲(chǔ)數(shù)據(jù)時(shí),會(huì)調(diào)用集群CRC16算法 得出此次
存儲(chǔ)變量使用的hash 槽,然后連接hash 槽 所在的master服務(wù)器存儲(chǔ)變量。
在創(chuàng)建集群時(shí)會(huì)把默認(rèn)的16384個(gè)槽平均的分配給集群中的3個(gè)master服務(wù)器??梢酝ㄟ^(guò)查看集群信息查看每個(gè)master服務(wù)器占用hash槽的個(gè)數(shù)。
- ]# redis-cli --cluster info 192.168.88.51:6379
- 192.168.88.53:6379 (f2c1bdb7...) -> 0 keys | 5461 slots | 1 slaves.
- 192.168.88.51:6379 (0eb3b7aa...) -> 0 keys | 5461 slots | 1 slaves.
- 192.168.88.52:6379 (a9cb8ccd...) -> 0 keys | 5462 slots | 1 slaves.
- [OK] 0 keys in 3 masters.
- 0.00 keys per slot on average.
集群中slave角色的主機(jī)會(huì)自動(dòng)同步master角色主機(jī)的數(shù)據(jù),實(shí)現(xiàn)數(shù)據(jù)的自動(dòng)備份,
分別連接集群中的3臺(tái)slave服務(wù)器查看變量
- [root@host54 ~]# redis-cli -c -h 192.168.88.54 -p 6379
- 192.168.88.54:6379> keys *
- 1) "class"
- [root@host55 ~]# redis-cli -c -h 192.168.88.55 -p 6379
- 192.168.88.55:6379> keys *
- 1) "school"
- [root@host56 ~]# redis-cli -c -h 192.168.88.56 -p 6379
- 192.168.88.56:6379> keys *
- 1) "teacher"
當(dāng)master角色的服務(wù)器宕機(jī)時(shí),對(duì)應(yīng)的slave服務(wù)器會(huì)升級(jí)為master 并接手對(duì)應(yīng)的hash槽,實(shí)現(xiàn)redis服務(wù)的高可用,例如停止host53主機(jī)的redis服務(wù),對(duì)應(yīng)的從會(huì)升級(jí)為master 。(當(dāng)宕機(jī)的master恢復(fù)后 自動(dòng)做當(dāng)前主的從服務(wù)器)
- 停止redis服務(wù)
- [root@host53 ~]# systemctl stop reddis
- 查看集群信息
- [root@host51 ~]#]# redis-cli --cluster info 192.168.88.51:6379
- 192.168.88.54:6379 (f2c1bdb7...) -> 1 keys | 5461 slots | 0 slaves.
- 192.168.88.51:6379 (0eb3b7aa...) -> 1 keys | 5461 slots | 1 slaves.
- 192.168.88.52:6379 (a9cb8ccd...) -> 1 keys | 5462 slots | 1 slaves.
- [OK] 0 keys in 3 masters.
- keys per slot on average.
- 啟動(dòng)redis服務(wù)
- [root@host53 ~]# systemctl start reddis
- 查看集群信息
- [root@host51 ~]#]# redis-cli --cluster info 192.168.88.51:6379
- 192.168.88.54:6379 (f2c1bdb7...) -> 1 keys | 5461 slots | 1 slaves.
- 192.168.88.51:6379 (0eb3b7aa...) -> 1 keys | 5461 slots | 1 slaves.
- 192.168.88.52:6379 (a9cb8ccd...) -> 1 keys | 5462 slots | 1 slaves.
- [OK] 0 keys in 3 masters.
- keys per slot on average.
- 存儲(chǔ)數(shù)據(jù)腳本
- [root@redis64 ~]# vim /usr/local/nginx/html/set.php
- <?php
- $redis_list = ['192.168.88.51:6379','192.168.88.52:6379','192.168.88.53:6379','192.168.88.54:6379','192.168.88.55:6379','192.168.88.56:6379'];
- $client = new RedisCluster(NUll,$redis_list);
- $client->set("i","tarenaA ");
- $client->set("j","tarenaB ");
- $client->set("k","tarenaC ");
- echo "save ok\n";
- ?>
- :wq
- 查看數(shù)據(jù)腳本
- [root@ redis64 ~]# vim /usr/local/nginx/html/get_data.php
- <?php
- $redis_list = ['192.168.88.51:6379','192.168.88.52:6379','192.168.88.53:6379','192.168.88.54:6379','192.168.88.55:6379','192.168.88.56:6379']; //定義redis服務(wù)器列表
- $client = new RedisCluster(NUll,$redis_list); //定義連接redis服務(wù)命令
- echo $client->get("i"); //獲取變量i 的數(shù)據(jù)
- echo $client->get("j"); //獲取變量j 的數(shù)據(jù)
- echo $client->get("k"); //獲取變量k 的數(shù)據(jù)
- ?>
- :wq
3)訪問(wèn)腳本文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-658112.html
- ]# curl http://192.168.88.64/set_data.php
- save ok
- ]# curl http://192.168.88.64/get_data.php
- tarenaA tarenaB tarenaC
4)命令行連接redis集群主機(jī)查看數(shù)據(jù)文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-658112.html
- [root@host51 ~]# redis-cli -c -h 192.168.88.51 -p 6379
- 192.168.88.51:6379> keys *
- 1) "j"
- [root@host52 ~]# redis-cli -c -h 192.168.88.52 -p 6379
- 192.168.88.52:6379> keys *
- 1) "k"
- [root@host53 ~]# redis-cli -c -h 192.168.88.53 -p 6379
- 192.168.88.53:6379> keys *
- 1) "i"
到了這里,關(guān)于概述、搭建Redis服務(wù)器、部署LNP+Redis、創(chuàng)建Redis集群、連接集群、集群工作原理的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!