ZooKeeper提供了一個命令行的客戶端zkCli.sh妄痪,主要使用命令來操作ZooKeeper茧痒。
1.登錄
(1)直接使用zkCli.sh默認登錄本機上的ZooKeeper服務(wù)器的2181端口
[root@bigdata131 bin]# zkCli.sh
...log...
[zk: localhost:2181(CONNECTED) 0]
(2)使用 -server 參數(shù)指定登錄遠程ZooKeeper服務(wù)器的指定端口
[root@bigdata132 ~]# zkCli.sh -server bigdata131:2181
...log...
[zk: bigdata131:2181(CONNECTED) 0]
(3)使用 -timeout參數(shù)指定會話的超時時間(單位毫秒)
如果服務(wù)器在timeout規(guī)定的時間范圍內(nèi),沒有收到本客戶端的心跳包敏储,就認為本客戶端死掉了。
[root@bigdata132 ~]# zkCli.sh -timeout 5000 -server bigdata131:2181
...log...
Session establishment complete on server bigdata131/192.168.126.131:2181,
sessionid = 0x1675c6d55d60005, negotiated timeout = 5000
...log...
[zk: bigdata131:2181(CONNECTED) 0]
(4)使用-r參數(shù),即使ZooKeeper集群一半以上的節(jié)點掛了糙捺,剩下的節(jié)點也給客戶端提供只讀服務(wù)
[root@bigdata132 ~]# zkCli.sh -timeout 5000 -r -server bigdata131:2181
...log...
Session establishment complete on server bigdata131/192.168.126.131:2181,
sessionid = 0x1675c6d55d60006, negotiated timeout = 5000
...log...
[zk: bigdata131:2181(CONNECTED) 0]
2.查看幫助(h/help)
[zk: localhost:2181(CONNECTED) 0] h
ZooKeeper -server host:port cmd args
stat path [watch]
set path data [version]
ls path [watch]
delquota [-n|-b] path
ls2 path [watch]
setAcl path acl
setquota -n|-b val path
history
redo cmdno
printwatches on|off
delete path [version]
sync path
listquota path
rmr path
get path [watch]
create [-s] [-e] path data acl
addauth scheme auth
quit
getAcl path
close
connect host:port
3.ls命令
[zk: localhost:2181(CONNECTED) 2] ls /
[zookeeper]
[zk: localhost:2181(CONNECTED) 3] ls /zookeeper
[quota]
4.stat命令
[zk: localhost:2181(CONNECTED) 5] stat /zookeeper
# 當前節(jié)點的創(chuàng)建時的ID
cZxid = 0x0
# 當前節(jié)點的創(chuàng)建時的時間(毫秒)
ctime = Thu Jan 01 08:00:00 CST 1970
# 該節(jié)點最近一次修改的ID
mZxid = 0x0
# 當前節(jié)點的最近一次修改的時間(毫秒)
mtime = Thu Jan 01 08:00:00 CST 1970
# 當前節(jié)點的子節(jié)點最近一次變更(增加或者刪除)時的ID
pZxid = 0x0
# 當前節(jié)點的子節(jié)點版本號
cversion = -1
# 當前節(jié)點的數(shù)據(jù)版本號
dataVersion = 0
# 當前節(jié)點的acl權(quán)限版本號
aclVersion = 0
# 如果當前節(jié)點是臨時節(jié)點,則表示節(jié)點所有者的會話ID笙隙;如果不是臨時節(jié)點洪灯,則為零。
ephemeralOwner = 0x0
# 當前節(jié)點的數(shù)據(jù)長度
dataLength = 0
# 當前節(jié)點的子節(jié)點個數(shù)
numChildren = 1
5.ls2命令(等于ls+stat)
格式:ls2 path
[zk: localhost:2181(CONNECTED) 7] ls2 /zookeeper
[quota]
cZxid = 0x0
ctime = Thu Jan 01 08:00:00 CST 1970
mZxid = 0x0
mtime = Thu Jan 01 08:00:00 CST 1970
pZxid = 0x0
cversion = -1
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 0
numChildren = 1
6.create命令
格式:create [-s] [-e] path data acl
參數(shù):
-s 表示是順序節(jié)點
-e 標識是臨時節(jié)點
path 節(jié)點路徑
data 節(jié)點數(shù)據(jù)
acl 節(jié)點權(quán)限
(1)創(chuàng)建永久節(jié)點
[zk: localhost:2181(CONNECTED) 8] ls /
[zookeeper]
[zk: localhost:2181(CONNECTED) 9] create /tmp1 111
Created /tmp1
[zk: localhost:2181(CONNECTED) 10] ls /
[zookeeper, tmp1]
(2)創(chuàng)建臨時節(jié)點
[zk: localhost:2181(CONNECTED) 11] create -e /tmp2 222
Created /tmp2
[zk: localhost:2181(CONNECTED) 12] ls /
[zookeeper, tmp1, tmp2]
[zk: localhost:2181(CONNECTED) 13] quit
[root@bigdata131 ~]# zkCli.sh
[zk: localhost:2181(CONNECTED) 0] ls /
[zookeeper, tmp1]
注:臨時節(jié)點在當前客戶端結(jié)束會話后竟痰,自動消失签钩。
(3)創(chuàng)建永久順序節(jié)點
[zk: localhost:2181(CONNECTED) 1] create -s /node 333
Created /node0000000004
[zk: localhost:2181(CONNECTED) 2] ls /
[node0000000004, zookeeper, tmp1]
注:順序節(jié)點node的后面有一串數(shù)字。
(4)創(chuàng)建臨時順序節(jié)點
[zk: localhost:2181(CONNECTED) 3] create -s -e /node2 444
Created /node20000000005
[zk: localhost:2181(CONNECTED) 4] ls /
[node0000000004, zookeeper, tmp1, node20000000005]
[zk: localhost:2181(CONNECTED) 5] quit
[root@bigdata131 ~]# zkCli.sh
[zk: localhost:2181(CONNECTED) 0] ls /
[node0000000004, zookeeper, tmp1]
注:臨時順序節(jié)點在當前客戶端結(jié)束會話后坏快,也會自動消失铅檩。
7.get命令
格式:get path
[zk: localhost:2181(CONNECTED) 3] get /tmp1
111
cZxid = 0x40000000d
ctime = Thu Nov 29 07:44:54 CST 2018
mZxid = 0x40000000d
mtime = Thu Nov 29 07:44:54 CST 2018
pZxid = 0x40000000d
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 3
numChildren = 0
8.set命令
格式:set path data
[zk: localhost:2181(CONNECTED) 4] set /tmp1 222
cZxid = 0x40000000d
ctime = Thu Nov 29 07:44:54 CST 2018
mZxid = 0x40000001b
mtime = Thu Nov 29 08:02:15 CST 2018
pZxid = 0x40000000d
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 3
numChildren = 0
[zk: localhost:2181(CONNECTED) 5] get /tmp1
222
cZxid = 0x40000000d
ctime = Thu Nov 29 07:44:54 CST 2018
mZxid = 0x40000001b
mtime = Thu Nov 29 08:02:15 CST 2018
pZxid = 0x40000000d
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 3
numChildren = 0
9.getAcl命令
格式:getAcl /path
[zk: localhost:2181(CONNECTED) 6] getAcl /tmp1
'world,'anyone
: cdrwa
10.setAcl命令
ZK的節(jié)點有5種操作權(quán)限:CREATE、READ莽鸿、WRITE昧旨、DELETE、ADMIN 也就是 增祥得、刪兔沃、改、查级及、管理權(quán)限乒疏,這5種權(quán)限簡寫為crwda(即:每個單詞的首字符縮寫)
注:這5種權(quán)限中,delete是指對子節(jié)點的刪除權(quán)限饮焦,其它4種權(quán)限指對自身節(jié)點的操作權(quán)限怕吴。
身份的認證有4種方式:
- world:默認方式,相當于全世界都能訪問
- auth:代表已經(jīng)認證通過的用戶(cli中可以通過addauth digest user:pwd 來添加當前上下文中的授權(quán)用戶)
- digest:即用戶名:密碼這種方式認證追驴,這也是業(yè)務(wù)系統(tǒng)中最常用的
- ip:使用Ip地址認證
設(shè)置訪問控制有兩種方式:
方式一:明文密碼(推薦)
(1)增加一個認證用戶
addauth digest 用戶名:密碼明文
addauth digest user1:password1
(2)設(shè)置權(quán)限
setAcl /path auth:用戶名:密碼明文:權(quán)限
setAcl /test auth:user1:password1:cdrwa
[zk: localhost:2181(CONNECTED) 7] addauth digest zookeeper:zookeeper
[zk: localhost:2181(CONNECTED) 8] setAcl /tmp1 auth:zookeeper:zookeeper:cdrw
cZxid = 0x40000000d
ctime = Thu Nov 29 07:44:54 CST 2018
mZxid = 0x40000001b
mtime = Thu Nov 29 08:02:15 CST 2018
pZxid = 0x40000000d
cversion = 0
dataVersion = 1
aclVersion = 1
ephemeralOwner = 0x0
dataLength = 3
numChildren = 0
[zk: localhost:2181(CONNECTED) 9] getAcl /tmp1
'digest,'zookeeper:4lvlzsipXVaEhXMd+2qMrLc0at8=
: cdrw
方式二:密文密碼
setAcl /path digest:用戶名:密碼密文:權(quán)限
注:這里的加密規(guī)則是SHA1加密械哟,然后base64編碼。
11.delete命令
格式:delete path [version]
只能刪除空節(jié)點殿雪,即下面沒有子節(jié)點。
[zk: localhost:2181(CONNECTED) 16] ls /
[node0000000004, zookeeper, tmp1, tmp2]
[zk: localhost:2181(CONNECTED) 17] ls /tmp1
[]
[zk: localhost:2181(CONNECTED) 18] ls /tmp2
[tmp2222]
[zk: localhost:2181(CONNECTED) 19] delete /tmp2
Node not empty: /tmp2
[zk: localhost:2181(CONNECTED) 20] delete /tmp1
[zk: localhost:2181(CONNECTED) 21] ls /
[node0000000004, zookeeper, tmp2]
12.rmr命令
格式:rmr path
遞歸刪除一個節(jié)點锋爪,包括子節(jié)點丙曙。
[zk: localhost:2181(CONNECTED) 22] rmr /tmp2
[zk: localhost:2181(CONNECTED) 23] ls /
[node0000000004, zookeeper]
13.listquota命令
格式:listquota path
查看路徑節(jié)點的配額信息
[zk: localhost:2181(CONNECTED) 26] ls /
[node0000000004, node11, zookeeper]
[zk: localhost:2181(CONNECTED) 27] listquota /node11
absolute path is /zookeeper/quota/node11/zookeeper_limits
quota for /node11 does not exist.
14.setquota命令
格式:setquota -n|-b val path
設(shè)置節(jié)點配額(比如限制節(jié)點數(shù)據(jù)長度,限制節(jié)點中子節(jié)點個數(shù))
參數(shù):
-n 是限制子節(jié)點個數(shù)
-b是限制節(jié)點數(shù)據(jù)長度
超出配額后其骄,ZooKeeper不會報錯亏镰,而是在日志信息中記錄。
[zk: localhost:2181(CONNECTED) 28] setquota -n 1 /node11
Comment: the parts are option -n val 1 path /node11
[zk: localhost:2181(CONNECTED) 29] listquota /node11
absolute path is /zookeeper/quota/node11/zookeeper_limits
Output quota for /node11 count=1,bytes=-1
Output stat for /node11 count=1,bytes=2
[zk: localhost:2181(CONNECTED) 30] create /node11/son222
[zk: localhost:2181(CONNECTED) 31] create /node11/son333
[zk: localhost:2181(CONNECTED) 32] create /node11/son444
15.delquota命令
格式:delquota [-n|-b] path
刪除節(jié)點路徑的配額信息
[zk: localhost:2181(CONNECTED) 5] listquota /node11
absolute path is /zookeeper/quota/node11/zookeeper_limits
Output quota for /node11 count=1,bytes=-1
Output stat for /node11 count=1,bytes=2[zk: localhost:2181(CONNECTED) 6] delquota -n /node11
[zk: localhost:2181(CONNECTED) 7] listquota /node11
absolute path is /zookeeper/quota/node11/zookeeper_limits
Output quota for /node11 count=-1,bytes=-1
Output stat for /node11 count=1,bytes=2
16.connect命令
格式:connect host:port
在當前連接中連接其他的ZooKeeper服務(wù)器
[zk: localhost:2181(CONNECTED) 8] connect bigdata133:2181
...log...
[zk: bigdata133:2181(CONNECTED) 9]
17.close命令
格式:close
關(guān)閉會話連接
[zk: localhost:2181(CONNECTED) 0] close
2018-11-29 08:42:06,377 [myid:] - INFO [main:ZooKeeper@684] - Session: 0x1675c6d55d6000c closed
[zk: localhost:2181(CLOSED) 1] 2018-11-29 08:42:06,378 [myid:] - INFO [main-EventThread:ClientCnxn$EventThread@519] - EventThread shut down for session: 0x1675c6d55d6000c
[zk: localhost:2181(CLOSED) 1]
18.printwatches命令
格式:printwatches [on | off]
查看或者設(shè)置節(jié)點watch的狀態(tài)拯爽,on輸出WATCHER事件索抓,off不輸出WATCHER事件。
[zk: localhost:2181(CONNECTED) 13] printwatches on
[zk: localhost:2181(CONNECTED) 14] printwatches
printwatches is on
[zk: localhost:2181(CONNECTED) 15] ls /node11 1
[child2, child1]
[zk: localhost:2181(CONNECTED) 16] create /node11/child3 child3WATCHER::
WatchedEvent state:SyncConnected type:NodeChildrenChanged path:/node11
Created /node11/child3[zk: localhost:2181(CONNECTED) 17] printwatches off
[zk: localhost:2181(CONNECTED) 18] printwatches
printwatches is off
[zk: localhost:2181(CONNECTED) 19] ls /node11 1
[child2, child3, child1]
[zk: localhost:2181(CONNECTED) 20] create /node11/child4 child4
Created /node11/child4
19.sync命令
格式:sync path
會強制客戶端所連接的服務(wù)器狀態(tài)與leader的狀態(tài)同步,這樣再讀取path的值就是最新的值了逼肯。
[zk: bigdata133:2181(CONNECTED) 23] sync /
[zk: bigdata133:2181(CONNECTED) 24] Sync returned 0[zk: bigdata133:2181(CONNECTED) 24]
20.history命令
格式:history
查看命令歷史
[zk: localhost:2181(CONNECTED) 4] history
0 - help
1 - history
2 - printwatches
3 - ls /
4 - history
21.redo命令
格式:redo cmdno
重新執(zhí)行命令歷史中指定的命令耸黑。
[zk: localhost:2181(CONNECTED) 4] history
0 - help
1 - history
2 - printwatches
3 - ls /
4 - history
[zk: localhost:2181(CONNECTED) 5] redo 2
printwatches is on
22.退出
[zk: bigdata131:2181(CONNECTED) 0] quit