ZooKeeper提供了一个命令行的客户端zkCli.sh,主要使用命令来操作ZooKeeper。
1.登录
(1)直接使用zkCli.sh默认登录本机上的ZooKeeper服务器的2181端口
[root@bigdata131 bin]# zkCli.sh
...log...
[zk: localhost:2181(CONNECTED) 0]
(2)使用 -server 参数指定登录远程ZooKeeper服务器的指定端口
[root@bigdata132 ~]# zkCli.sh -server bigdata131:2181
...log...
[zk: bigdata131:2181(CONNECTED) 0]
(3)使用 -timeout参数指定会话的超时时间(单位毫秒)
如果服务器在timeout规定的时间范围内,没有收到本客户端的心跳包,就认为本客户端死掉了。
[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参数,即使ZooKeeper集群一半以上的节点挂了,剩下的节点也给客户端提供只读服务
[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
# 当前节点的创建时的ID
cZxid = 0x0
# 当前节点的创建时的时间(毫秒)
ctime = Thu Jan 01 08:00:00 CST 1970
# 该节点最近一次修改的ID
mZxid = 0x0
# 当前节点的最近一次修改的时间(毫秒)
mtime = Thu Jan 01 08:00:00 CST 1970
# 当前节点的子节点最近一次变更(增加或者删除)时的ID
pZxid = 0x0
# 当前节点的子节点版本号
cversion = -1
# 当前节点的数据版本号
dataVersion = 0
# 当前节点的acl权限版本号
aclVersion = 0
# 如果当前节点是临时节点,则表示节点所有者的会话ID;如果不是临时节点,则为零。
ephemeralOwner = 0x0
# 当前节点的数据长度
dataLength = 0
# 当前节点的子节点个数
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
参数:
-s 表示是顺序节点
-e 标识是临时节点
path 节点路径
data 节点数据
acl 节点权限
(1)创建永久节点
[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)创建临时节点
[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]
注:临时节点在当前客户端结束会话后,自动消失。
(3)创建永久顺序节点
[zk: localhost:2181(CONNECTED) 1] create -s /node 333
Created /node0000000004
[zk: localhost:2181(CONNECTED) 2] ls /
[node0000000004, zookeeper, tmp1]
注:顺序节点node的后面有一串数字。
(4)创建临时顺序节点
[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]
注:临时顺序节点在当前客户端结束会话后,也会自动消失。
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的节点有5种操作权限:CREATE、READ、WRITE、DELETE、ADMIN 也就是 增、删、改、查、管理权限,这5种权限简写为crwda(即:每个单词的首字符缩写)
注:这5种权限中,delete是指对子节点的删除权限,其它4种权限指对自身节点的操作权限。
身份的认证有4种方式:
- world:默认方式,相当于全世界都能访问
- auth:代表已经认证通过的用户(cli中可以通过addauth digest user:pwd 来添加当前上下文中的授权用户)
- digest:即用户名:密码这种方式认证,这也是业务系统中最常用的
- ip:使用Ip地址认证
设置访问控制有两种方式:
方式一:明文密码(推荐)
(1)增加一个认证用户
addauth digest 用户名:密码明文
addauth digest user1:password1
(2)设置权限
setAcl /path auth:用户名:密码明文:权限
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:用户名:密码密文:权限
注:这里的加密规则是SHA1加密,然后base64编码。
11.delete命令
格式:delete path [version]
只能删除空节点,即下面没有子节点。
[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
递归删除一个节点,包括子节点。
[zk: localhost:2181(CONNECTED) 22] rmr /tmp2
[zk: localhost:2181(CONNECTED) 23] ls /
[node0000000004, zookeeper]
13.listquota命令
格式:listquota path
查看路径节点的配额信息
[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
设置节点配额(比如限制节点数据长度,限制节点中子节点个数)
参数:
-n 是限制子节点个数
-b是限制节点数据长度
超出配额后,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
删除节点路径的配额信息
[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服务器
[zk: localhost:2181(CONNECTED) 8] connect bigdata133:2181
...log...
[zk: bigdata133:2181(CONNECTED) 9]
17.close命令
格式:close
关闭会话连接
[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]
查看或者设置节点watch的状态,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
会强制客户端所连接的服务器状态与leader的状态同步,这样再读取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
重新执行命令历史中指定的命令。
[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