如何从源码构建mongo-tools deb包?求技术解决方案
构建MongoDB Tools 3.6版本Deb包的详细步骤
我之前也碰到过一模一样的问题——MongoDB Tools源码仓库本身确实没自带Deb打包的配置文件,得我们自己准备打包所需的debian目录和配置,再结合Go语言的Deb打包工具来完成。下面是亲测可行的具体操作步骤:
1. 安装前置依赖
先确保你的Debian Jessie系统上装好了所有必要的构建工具和依赖:
sudo apt-get update sudo apt-get install -y build-essential debhelper dh-golang devscripts golang-go git
⚠️ 注意:MongoDB Tools 3.6版本要求Go 1.9.x版本,而Jessie默认的Go版本偏低,你需要手动安装Go 1.9:从Go官方下载对应二进制包,解压到/usr/local/go,然后配置环境变量export PATH=/usr/local/go/bin:$PATH。
2. 检出对应版本的MongoDB Tools源码
因为你要构建3.6版本的包,得先检出对应分支的源码:
git clone https://github.com/mongodb/mongo-tools.git cd mongo-tools git checkout r3.6.23 # 选3.6系列的最新稳定tag,你可以根据需求调整版本号
3. 准备Deb打包配置文件
MongoDB Tools源码没有自带debian目录,我们得手动创建并配置相关文件:
3.1 创建debian目录结构
mkdir -p debian/source
3.2 编写debian/control文件
这个文件定义包的基本信息、依赖等,内容如下:
Source: mongo-tools Section: database Priority: optional Maintainer: Your Name <your.email@example.com> Build-Depends: debhelper (>=9), dh-golang, golang-go, git Standards-Version: 3.9.6 Homepage: https://github.com/mongodb/mongo-tools Vcs-Git: https://github.com/mongodb/mongo-tools.git Vcs-Browser: https://github.com/mongodb/mongo-tools Package: mongo-tools Architecture: amd64 Depends: ${shlibs:Depends}, ${misc:Depends} Description: MongoDB tools suite This package contains the MongoDB tools: mongoimport, mongoexport, mongodump, mongorestore, mongostat, mongotop, mongofiles, mongooplog, mongoreplay.
3.3 编写debian/rules文件
这个文件定义构建规则,因为是Go项目,我们用dh-golang来处理:
#!/usr/bin/make -f %: dh $@ --buildsystem=golang --with=golang override_dh_auto_build: # 构建所有mongo工具 for tool in bsondump mongodump mongoexport mongofiles mongoimport mongoreplay mongorestore mongostat mongotop; do \ go build -o build/$$tool github.com/mongodb/mongo-tools/$$tool ; \ done override_dh_auto_install: # 将构建好的工具安装到debian/mongo-tools/usr/bin/ mkdir -p debian/mongo-tools/usr/bin cp build/* debian/mongo-tools/usr/bin/
记得给rules文件添加可执行权限:
chmod +x debian/rules
3.4 编写debian/copyright文件
注明版权信息,示例如下:
Format: https://www.debian.org/doc/packaging-manuals/copyright-format/1.0/ Upstream-Name: mongo-tools Upstream-Contact: MongoDB, Inc. <info@mongodb.com> Source: https://github.com/mongodb/mongo-tools Files: * Copyright: 2010-2020 MongoDB, Inc. License: Apache-2.0 License: Apache-2.0 Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at . http://www.apache.org/licenses/LICENSE-2.0 . Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
3.5 编写debian/changelog文件
可以用dch命令快速生成:
dch --create -v 3.6.23-1 -D jessie "Initial release for Debian Jessie"
生成后可以根据需要编辑内容。
3.6 添加debian/source/format文件
echo "3.0 (quilt)" > debian/source/format
4. 构建Deb包
所有配置准备好后,执行以下命令开始构建:
dpkg-buildpackage -us -uc -b
构建完成后,上级目录会生成类似mongo-tools_3.6.23-1_amd64.deb的包,结构和你提到的官方仓库里的包基本一致。
额外注意事项
- 版本兼容:3.6版本的工具必须用Go 1.9,用过高版本的Go可能会构建失败。
- 依赖修复:如果构建时遇到Go模块依赖问题,3.6版本的mongo-tools用的是godep,先安装
godep(go get github.com/tools/godep),再执行godep restore拉取依赖。 - 扩展优化:官方Deb包还包含man页、系统配置等内容,如果需要这些,可以参考官方包的结构,把对应内容添加到debian配置中。
内容的提问来源于stack exchange,提问作者Pensu
相关产品推荐
相关产品推荐

