ZooKeeper常用命令

ZooKeeper常用命令

leo 608 2021-04-10

ZooKeeper 常用命令

注:我是通过 docker-compose 安装的ZooKeeper集群。
安装方式:docker-compose安装ZooKeeper集群

服务端

交互式进入容器,查看 bin 目录下文件:

root@zoo1:/apache-zookeeper-3.6.2-bin/bin# ls -al
total 72
drwxr-xr-x 2 zookeeper zookeeper  4096 Sep  4 12:43 .
drwxr-xr-x 6 zookeeper zookeeper  4096 Sep 22 18:09 ..
-rwxr-xr-x 1 zookeeper zookeeper   232 Sep  4 12:43 README.txt
-rwxr-xr-x 1 zookeeper zookeeper  2066 Sep  4 12:43 zkCleanup.sh
-rwxr-xr-x 1 zookeeper zookeeper  1158 Sep  4 12:43 zkCli.cmd
-rwxr-xr-x 1 zookeeper zookeeper  1620 Sep  4 12:43 zkCli.sh
-rwxr-xr-x 1 zookeeper zookeeper  1843 Sep  4 12:43 zkEnv.cmd
-rwxr-xr-x 1 zookeeper zookeeper  3690 Sep  4 12:43 zkEnv.sh
-rwxr-xr-x 1 zookeeper zookeeper  4559 Sep  4 12:43 zkServer-initialize.sh
-rwxr-xr-x 1 zookeeper zookeeper  1286 Sep  4 12:43 zkServer.cmd
-rwxr-xr-x 1 zookeeper zookeeper 11116 Sep  4 12:43 zkServer.sh
-rwxr-xr-x 1 zookeeper zookeeper   988 Sep  4 12:43 zkSnapShotToolkit.cmd
-rwxr-xr-x 1 zookeeper zookeeper  1377 Sep  4 12:43 zkSnapShotToolkit.sh
-rwxr-xr-x 1 zookeeper zookeeper   996 Sep  4 12:43 zkTxnLogToolkit.cmd
-rwxr-xr-x 1 zookeeper zookeeper  1385 Sep  4 12:43 zkTxnLogToolkit.sh

zkCli即为ZooKeeper客户端工具,zkServer即为服务端。(其中.sh为linux环境下、.cmd为window环境下)

  • 启动服务

    ./zkServer.sh start
    
  • 停止服务

    ./zkServer.sh stop
    
  • 重启服务

    ./zkServer.sh restart
    
  • 执行状态

    ./zkServer.sh status
    

客户端

  • 连接服务器

    ./zkCli.sh -server <ip>:<port>
    
  • 创建结点

    create /test "Hello ZooKeeper"
    
  • 获取结点

    get /test
    

    可以加上 -s 参数获取详细信息:

    [zk: localhost:2181(CONNECTED) 3] get /test -s
    Hello ZooKeeper
    cZxid = 0x100000006
    ctime = Fri Oct 09 06:44:34 UTC 2020
    mZxid = 0x100000006
    mtime = Fri Oct 09 06:44:34 UTC 2020
    pZxid = 0x100000006
    cversion = 0
    dataVersion = 0
    aclVersion = 0
    ephemeralOwner = 0x0
    dataLength = 4
    numChildren = 0
    
  • 删除结点

    delete /test
    

更多命令