Linux下安装zookeeper、配置zookeeper开机⾃启动本⽂主要介绍如何在Centos7下安装zookeeper以及如何使⽤。
安装JDK
下载安装zookeeper
启动zookeeper
设置zookeeper开机⾃动启动
⼀、安装JDK
使⽤zookeeper前,必须先安装JDK,如何安装JDK,可以查看:
⼆、下载安装zookeeper
2.1 通过官⽹下载:。本⽂中使⽤
3.7.0版本。
// 进⼊下载⽬录并下载
cd /usr/local/
mkdir zookeeper
cd zookeeper
wget mirrors.bfsu.edu/apache/zookeeper/zookeeper-3.7.0/apache-zookeeper-3.7.
注意:没有bin的是zk的源码包,可以看到也是maven项⽬,是需要编译打包才能使⽤
如果下载的不是bin包,执⾏启动命令 zkServer.sh start
zkServer启动失败,查看zk的⽇志⽂件,报如下错误:
Error:Could not find or Load main class keeper.server.quorum.QuorumPeerMain
因此,需要下载bin包⽅可启动成功。
2.2 解压
// 解压
tar -zxvf apache-zookeeper-3.7.
2.3 修改配置⽂件
// 1.进⼊配置⽂件⽬录
cd apache-zookeeper-3.7.0-bin/conf
// 2.将zoo_sample.cfg这个⽂件复制为zoo.cfg (注意:⽂件名⼀定要是zoo.cfg)
cp zoo_sample.cfg  zoo.cfg
// 3.修改配置⽂件
vim zoo.cfg
修改zoo.cfg,修改前:
修改后:
注意:数据存放⽬录data由⾃⼰决定,但是启动服务前⼀定要确保对应的⽬录存在,不然启动会失败
2.4 创建data⽂件夹
// 创建data⽬录
[root@localhost conf]# cd ../
[root@localhost apache-zookeeper-3.7.0-bin]# pwd
/usr/local/zookeeper/apache-zookeeper-3.7.0-bin
[root@localhost apache-zookeeper-3.7.0-bin]# mkdir data
[root@localhost apache-zookeeper-3.7.0-bin]# ll
总⽤量44
drwxr-xr-x. 2100010004096 3⽉172021 bin
drwxr-xr-x. 2100010004096 12⽉2818:09 conf
drwxr-xr-x. 2 root root  4096 12⽉2818:09 data
drwxr-xr-x. 5100010004096 3⽉172021 docs
drwxr-xr-x. 2 root root  4096 12⽉2818:04 lib
-rw-r--r--. 11000100011358 3⽉
-rw-r--r--. 110001000432 3⽉
-rw-r--r--. 1100010002214 3⽉172021 README.md
-rw-r--r--. 1100010003570 3⽉172021 README_packaging.md
[root@localhost apache-zookeeper-3.7.0-bin]#
三、启动zookeeper
3.1 进⼊zookeeper下的bin⽬录
cd /usr/local/zookeeper/apache-zookeeper-3.7.0-bin/bin
3.2 执⾏启动命令
.
/zkServer.sh start
启动成功后如下:
除了开启,再备注⼏个其他命令:
// 停⽌
./zkServer.sh stop
// 重启
./zkServer.sh restart
// 查看状态
./zkServer.sh status
虽然这样可以正常启动,但是每次都要进⼊到zookeeper的bin⽬录下,为了避免⿇烦,可以将zookeeper设置开机启动
四、设置zookeeper开机⾃动启动
centos7
4.1 切换到/lib/systemd/system/⽬录,创建⾃启动⽂件
cd /lib/systemd/system/
vim zookeeper.service
⽂件内容如下:
[Unit]
Description=zookeeperservice
After=network.target
[Service]
WorkingDirectory=/usr/local/zookeeper/apache-zookeeper-3.7.0-bin      #zookeeper路径
ExecStart=/usr/local/zookeeper/apache-zookeeper-3.7.0-bin/bin/zkServer.sh start      #zookeeper执⾏⽂件
User=root
Group=root
Restart=always
RestartSec=10
[Install]
WantedBy=multi-user.target
注意:zookeeper的路径需要修改成⾃⼰的!
重新加载配置信息:
systemctl daemon-reload
4.2 设置⾃启动
systemctl enable zookeeper.service
4.3 启动服务
systemctl start zookeeper.service
4.4 查看启动状态
systemctl status zookeeper.service
4.5 ⾃启动相关命令
重新加载配置信息:systemctl daemon-reload
启动zookeeper:systemctl start zookeeper.service
关掉zookeeper:systemctl stop zookeeper.service
虚拟机linux安装jdk查看进程状态及⽇志:systemctl status zookeeper.service
开机⾃启动:systemctl enable zookeeper.service
关闭⾃启动:systemctl disable zookeeper.service
centos6
第⼀种:修改vim /etc/rc.local⽂件
vim /etc/rc.local
加⼊:
export JAVA_HOME=/usr/local/jdk/jdk1.8.0_281 -- jdk安装路径
/usr/local/zookeeper/apache-zookeeper-3.7.0-bin/bin/zkServer.sh start -- zookeeper安装路径重启后⽣效。
第⼆种:建⽴zookeeper服务
1、进⼊到/etc/rc.d/init.d⽬录下,新建⼀个zookeeper脚本
vim /etc/init.d/zookeeper
加⼊:
注意替换JAVA_HOME⽬录以及zookeeper安装⽬录
#!/bin/bash
#chkconfig:23452090
#description:zookeeper
#processname:zookeeper
export JAVA_HOME=/usr/local/jdk/jdk1.8.0_281
case $1in
start) su root /usr/local/zookeeper/apache-zookeeper-3.7.0-bin/bin/zkServer.sh start;;
stop) su root /usr/local/zookeeper/apache-zookeeper-3.7.0-bin/bin/zkServer.sh stop;;
status) su root /usr/local/zookeeper/apache-zookeeper-3.7.0-bin/bin/zkServer.sh status;;        restart) su /usr/local/zookeeper/apache-zookeeper-3.7.0-bin/bin/zkServer.sh restart;;
*) echo "require start|stop|status|restart" ;;
esac
2、添加执⾏权限
chmod +x /etc/init.d/zookeeper
3、service zookeeper start/stop/status命令启动、停⽌或查看状态
-- 启动
service zookeeper start
-- 停⽌
service zookeeper stop-- 状态
service zookeeper status
4、添加到开机⾃启
chkconfig  --add zookeeper
开机⾃启配置好了,重启⼀下试试,如下所⽰:
-
- 重启
reboot
-- 检查是否开机⾃启动
service zookeeper status
⾄此,在Linux下安装zookeeper就完成了!

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。