解决Mac上安装Zookeeper问题:FAILED TO WRITE PID
今天在尝试本地安装Zookeeper,在官网下载稳定版本apache-zookeeper-3.7.0到本地后,解压文件并将apache-zookeeper-3.7.0-bin文件夹移动至/usr/local目录。
照例先将安装目录conf中的zoo_sample.cfg文件名修改为zoo.cfg。然后进入bin目录,开始执行启动操作:sh zkServer.sh start 出现了问题1如下:
Using config: /usr/local/apache-zookeeper-3.7.0-bin/bin/../conf/zoo.cfg
mkdir: illegal option -- e
usage: mkdir [-pv] [-m mode] directory ...
-n Starting zookeeper ...
zkServer.sh: line 175: -e /tmp/zookeeper/zookeeper_server.pid: No such file or directory
FAILED TO WRITE PID
最开始以为是操作权限问题,于是开始修改文件权限,但是依然无效。
经过查询后得知原来问题出在zkServer.sh这个启动文件上,打开启动文件找到如下指令:
ZOO_DATADIR="$(echo -e "${ZOO_DATADIR}" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')"
修改为:
ZOO_DATADIR="$(echo "${ZOO_DATADIR}" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')"
在3.7.0版本,这段配置在zkServer.sh文件的第120行。
修改完后继续执行启动操作:sh zkServer.sh start 现在出现了问题2如下:
2022-01-06 10:24:34,946 [myid:] - ERROR [main:ZooKeeperServerMain@86] - Unable to start AdminServer, exiting abnormally
org.apache.zookeeper.server.admin.AdminServer$AdminServerException: Problem starting AdminServer on address 0.0.0.0, port 8080 and command URL /commands
at org.apache.zookeeper.server.admin.JettyAdminServer.start(JettyAdminServer.java:179)
at org.apache.zookeeper.server.ZooKeeperServerMain.runFromConfig(ZooKeeperServerMain.java:155)
at org.apache.zookeeper.server.ZooKeeperServerMain.initializeAndRun(ZooKeeperServerMain.java:113)
at org.apache.zookeeper.server.ZooKeeperServerMain.main(ZooKeeperServerMain.java:68)
at org.apache.zookeeper.server.quorum.QuorumPeerMain.initializeAndRun(QuorumPeerMain.java:141)
at org.apache.zookeeper.server.quorum.QuorumPeerMain.main(QuorumPeerMain.java:91)
Caused by: java.io.IOException: Failed to bind to /0.0.0.0:8080
at org.eclipse.jetty.server.ServerConnector.openAcceptChannel(ServerConnector.java:349)
at org.eclipse.jetty.server.ServerConnector.open(ServerConnector.java:310)
at org.eclipse.jetty.server.AbstractNetworkConnector.doStart(AbstractNetworkConnector.java:80)
at org.eclipse.jetty.server.ServerConnector.doStart(ServerConnector.java:234)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:73)
at org.eclipse.jetty.server.Server.doStart(Server.java:401)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:73)
at org.apache.zookeeper.server.admin.JettyAdminServer.start(JettyAdminServer.java:170)
... 5 more
Caused by: java.net.BindException: Address already in use
at java.base/sun.nio.ch.Net.bind0(Native Method)
at java.base/sun.nio.ch.Net.bind(Net.java:455)
at java.base/sun.nio.ch.Net.bind(Net.java:447)
at java.base/sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:227)
at java.base/sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:80)
at org.eclipse.jetty.server.ServerConnector.openAcceptChannel(ServerConnector.java:344)
... 12 more
这个比较简单,因为AdminServer启动时默认使用8080端口,但是我本地8080端口已经被占用了。
找到conf/zoo.cfg配置文件,添加一行admin.serverPort=xxxx,修改一下启动的端口即可。(是的,默认配置8080而且没有把配置项写入配置文件中
修改后继续执行启动操作:sh zkServer.sh start 然后遇到了问题3:
2022-01-06 10:38:32,300 [myid:] - INFO [main:NIOServerCnxnFactory@660] - binding to port 0.0.0.0/0.0.0.0:2181
2022-01-06 10:38:32,301 [myid:] - ERROR [main:ZooKeeperServerMain@91] - Unexpected exception, exiting abnormally
java.net.BindException: Address already in use
at java.base/sun.nio.ch.Net.bind0(Native Method)
at java.base/sun.nio.ch.Net.bind(Net.java:455)
at java.base/sun.nio.ch.Net.bind(Net.java:447)
at java.base/sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:227)
at java.base/sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:80)
at java.base/sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:73)
at org.apache.zookeeper.server.NIOServerCnxnFactory.configure(NIOServerCnxnFactory.java:662)
at org.apache.zookeeper.server.ZooKeeperServerMain.runFromConfig(ZooKeeperServerMain.java:160)
at org.apache.zookeeper.server.ZooKeeperServerMain.initializeAndRun(ZooKeeperServerMain.java:113)
at org.apache.zookeeper.server.ZooKeeperServerMain.main(ZooKeeperServerMain.java:68)
at org.apache.zookeeper.server.quorum.QuorumPeerMain.initializeAndRun(QuorumPeerMain.java:141)
at org.apache.zookeeper.server.quorum.QuorumPeerMain.main(QuorumPeerMain.java:91)
对的,又是一次端口被占用,但是这次是2181端口,细心的同学可能发现了,这是zookeeper client的默认端口。我怀疑我刚才在启动server的时候server虽然因为端口被占用的原因没有启动,但是client却正常启动了。
执行操作:sh zkCli.sh close 即可解决问题。
现在再次执行zkserver启动操作:sh zkServer.sh start
终于正常启动了,打印日志如下:
Using config: /usr/local/apache-zookeeper-3.7.0-bin/bin/../conf/zoo.cfg
-n Starting zookeeper ...
STARTED