Total Pageviews

Saturday, 24 October 2020

在Raspbian上, 编译mongodb 3.2

 

我的机器

uname -a
Linux pi118 4.14.34-v7+ #1110 SMP Mon Apr 16 15:18:51 BST 2018 armv7l GNU/Linux

32位编译

下载源码

proxychains wget https://fastdl.mongodb.org/src/mongodb-src-r3.2.20.tar.gz
tar zxvf mongodb-src-r3.2.20.tar.gz
cd mongodb-src-r3.2.20

安装依赖

cat docs/building.md

To install dependencies on Debian or Ubuntu systems:

    # aptitude install scons build-essential
    # aptitude install libboost-filesystem-dev libboost-program-options-dev libboost-system-dev libboost-thread-dev

To run tests as well, you will need PyMongo:

    # aptitude install python-pymongo

临时增加swap空间(如果需要)

sudo dd if=/dev/zero of=/mytempswapfile bs=1024 count=524288
sudo chmod 0600 /mytempswapfile
sudo mkswap /mytempswapfile
sudo swapon /mytempswapfile

生成额外的源码

cd src/third_party/mozjs-38/
./get_sources.sh
./gen-config.sh arm linux
cd ../../../

如果没有这一步会导致一个编译错误

开始编译

  1. 32位架构不支持WiredTiger存储引擎,所以不能直接scons all,需要排除掉WiredTiger存储引擎,只使用MMAPv1存储引擎.
  2. 你可以选择mongod/mongos和mongo的其中一项或几项编译,这种指定方式编译不会编译任何测试代码,否则将会花会长时间编译所有测试代码
  3. 不指定方式编译需要25G的硬盘空间,指定方式编译需要4G的硬盘空间

编译全部

scons core --wiredtiger=off --mmapv1=on

指定方式编译

假如不需要mongos

scons mongo mongod --wiredtiger=off --mmapv1=on

指定项还有 mongoperf mongosniff mongobridge

编译mongosniff需要libpcap-dev包,需要重新scons -c,运行依赖需要libpcap0.8

可以指jobs个数,使用python -c 'import multiprocessing as mp; print(mp.cpu_count())'grep -sc ^processor /proc/cpuinfo命令查看个数

如果不显式指定mmapv1为on,则不会把mmapv1编译进mongod 如果碰见[-Werror=placement-new=]错误,可以加上--disable-warnings-as-errors

减少二进制文件体积

编译成功后生成的文件在./build/opt/mongo,这些可执行文件包含调试信息,所以会很大,需要使用strip命令来移除这些调试信息.

cp build/opt/mongo/mongo* /usr/local/bin/
strip -s /usr/local/bin/mongo*

64位编译

在 ./src/third_party/SConscript 增加两行CFLAGS参数

elif env.TargetOSIs('linux'):
    env.Append(CFLAGS=["-march=armv8-a+crc"])
    env.Append(CFLAGS=["-mtune=generic"])
    env.Append(CPPPATH=["build_linux"])
    env.Append(CPPDEFINES=["_GNU_SOURCE"])
scons mongod --wiredtiger=on --mmapv1=off --disable-warnings-as-errors

编译mongo-tools

proxychains git clone -b r3.2.20 https://github.com/mongodb/mongo-tools
cd mongo-tools
. ./set_gopath.sh
./build.sh
strip -s bin/*
cp bin/* /usr/local/bin/

mongodb-core-raspbian-stretch-3.2.20.tar.gz

----------------------------

Raspberry(2B/3B/3B+)安装mongodb 3.0/3.2(Raspbian stretch)步骤

安装dependencies

ssl版需要安装libssl1.0.2

apt install libssl1.0.2

先备份好数据

mongodump -d leanote -o /home/git/mongodb/fistbackup/
mongodump -d bookbrower -o /home/git/mongodb/fistbackup/
``` 
<!--more-->
## 检查mongodb用户

grep mongodb /etc/passwd

 
如果没有,新建mongdb用户
 

sudo adduser –ingroup nogroup –shell /bin/false –disabled-password –gecos “”
–no-create-home mongodb


## 把bin文件复制到/usr/bin下,并赋予合适的用户和权限属性,再用strip缩减文件大小

cd core/ chown root:root mongo* chmod 755 mongo* strip mongo* cp -p mongo* /usr/local/bin


## 创建log目录

mkdir /root/log/mongodb chown mongodb:nogroup /root/log/mongodb


## 创建data目录

mkdir /root/data/mongodb chown mongodb:mongodb /root/data/mongodb chmod 775 /root/data/mongodb


## 创建配置文件

cat > /etc/mongodb.conf <

/etc/mongodb.conf

minimal config file (old style)

Run mongod –help to see a list of options

bind_ip = 0.0.0.0 quiet = true dbpath = /root/data/mongodb logpath = /root/log/mongodb/mongod.log logappend = true storageEngine = mmapv1 EOF

//新版配置 yaml格式 cat > /etc/mongodb.conf <

/etc/mongodb.conf

yaml style

Run mongod –help to see a list of options

net: bindIp: 0.0.0.0 port: 27017 storage: dbPath: “/root/data/mongodb” engine: mmapv1

systemLog: destination: file path: “/root/log/mongodb/mongod.log” logAppend: true quiet: true EOF


## 创建服务

cat > /lib/systemd/system/mongodb.service <

[Unit] Description=High-performance, schema-free document-oriented database After=network.target

[Service] User=mongodb ExecStart=/usr/local/bin/mongod –quiet –config /etc/mongodb.conf

[Install] WantedBy=multi-user.target

EOF


## 激活服务并测试

systemctl enable mongodb.service systemctl daemon-reload service mongodb start service mongodb status

 
## 安装tools包

``` 
cd tools/
strip *
chown root:root *
chmod 755 *
mv * /usr/local/bin

恢复数据

mongorestore -d leanote  /home/git/mongodb/fistbackup/leanote
mongorestore -d bookbrower  /home/git/mongodb/fistbackup/bookviewer

No comments:

Post a Comment