移植指导
介绍
简要介绍
MongoDB是一个基于分布式文件存储的数据库,由C++语言编写,旨在为Web应用提供可扩展的高性能数据存储解决方案。
一句话描述:NoSQL数据库
语言:C++
开源协议:MIT
建议的版本
建议最低版本为“MongoDB-3.6.13”。
配置编译环境
执行以下命令,安装依赖库。
sudo yum -y install unzip
sudo yum -y install libcurl-devel
sudo yum -y install openssl
sudo yum -y install openssl-devel
sudo yum -y install libxml2-devel
sudo yum -y install libxml2
sudo yum -y install glibc-static
sudo yum -y install libstdc++-static
sudo yum -y install lzip
sudo yum -y install libffi-devel
sudo yum -y install wget
获取源码
- 本文档所测试版本为:MongoDB-3.6.13
- 软件获取路径为:https://github.com/mongodb/mongo/releases
- MongoDB官网:https://www.mongodb.com/
编译和安装
本文以MongoDB-3.6.13为例,下载MongoDB-3.6.13源码,并编译安装。
- CentOS系统需执行以下命令,安装gcc7编译环境(OpenEuler可直接进入下一步):
sudo yum -y install centos-release-scl
mv /etc/yum.repos.d/CentOS-SCLo-scl.repo /etc/yum.repos.d/CentOS-SCLo-scl.repo.ignore
sudo yum makecache
sudo yum -y install devtoolset-7-gcc*
- 执行以下命令,获取较新的Python2.7解释环境,推荐版本为Python2.7.17。
- 通过源码编译并安装python解释器。
wget https://mirrors.huaweicloud.com/python/2.7.17/Python-2.7.17.tgz
tar zxvf Python-2.7.17.tgz
cd Python-2.7.17
scl enable devtoolset-7 "./configure --prefix=/usr/local --enable-optimizations"
scl enable devtoolset-7 "make -j4"
scl enable devtoolset-7 "make altinstall"
- 安装配套的setuptools工具。
wget https://github.com/pypa/setuptools/archive/v41.0.1.zip
unzip v41.0.1.zip
cd setuptools-41.0.1
/usr/local/bin/python2.7 bootstrap.py
/usr/local/bin/python2.7 setup.py install
- 安装配套的pip工具。
wget https://github.com/pypa/pip/archive/19.2.2.tar.gz
tar zxvf 19.2.2.tar.gz
cd pip-19.2.2
/usr/local/bin/python2.7 setup.py install
- 通过源码编译并安装python解释器。
- 执行以下命令,获取MongoDB源码。
cd /usr/local/src
wget https://github.com/mongodb/mongo/archive/r3.6.13.tar.gz
- 执行以下命令,解压包。
tar -zxvf r3.6.13.tar.gz
- 执行以下命令,进入“mongo-r3.6.13”目录。
cd mongo-r3.6.13
- 执行以下命令,构建编译环境。
scl enable devtoolset-7 "/usr/local/bin/pip2 install -r buildscripts/requirements.txt"
- 执行以下命令,切换到gcc7及python2.7.17的编译环境,并编译MongoDB。
scl enable devtoolset-7 "/usr/local/bin/python2.7 buildscripts/scons.py --prefix=/opt/mongo install MONGO_VERSION=3.6.13 CCFLAGS="-march=armv8-a+crc" --disable-warnings-as-errors --variables-files=etc/scons/propagate_shell_environment.vars -j 4"
引号中增加的-j参数用于实现多核编译加速,但gcc在多核编译时会消耗大量内存,请根据自己的可用内存大小设定合理的并发度。
- 执行以下命令,创建MongoDB的数据库目录。
mkdir -p /data/db
可以在此步完成之后,为该目录挂载其他文件系统
运行和验证
- 安装完成后,启动MongoDB。
- 用客户端测试MongoDB数据库服务。
- 执行以下命令,连接MongoDB服务端。
./mongo
系统回显如下,则连接成功,进入MongoDB后台管理Shell环境。
MongoDB shell version v3.6.13 connecting to: mongodb://127.0.0.1:27017/?gssapiServiceName=mongodb Implicit session: session { "id" : UUID("d7f8e07a-e4b6-41d0-a65a-cd1c92095648") } MongoDB server version: 3.6.13 Welcome to the MongoDB shell. For interactive help, type "help". For more comprehensive documentation, see http://docs.mongodb.org/ Questions? Try the support group http://groups.google.com/group/mongodb-user Server has startup warnings: 2019-07-05T08:57:25.541+0800 I STORAGE [initandlisten] 2019-07-05T08:57:25.541+0800 I STORAGE [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine 2019-07-05T08:57:25.541+0800 I STORAGE [initandlisten] ** See http://dochub.mongodb.org/core/prodnotes-filesystem 2019-07-05T08:57:25.689+0800 I CONTROL [initandlisten] 2019-07-05T08:57:25.689+0800 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database. 2019-07-05T08:57:25.689+0800 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted. 2019-07-05T08:57:25.689+0800 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended. 2019-07-05T08:57:25.689+0800 I CONTROL [initandlisten] 2019-07-05T08:57:25.689+0800 I CONTROL [initandlisten] ** WARNING: This server is bound to localhost. 2019-07-05T08:57:25.689+0800 I CONTROL [initandlisten] ** Remote systems will be unable to connect to this server. 2019-07-05T08:57:25.689+0800 I CONTROL [initandlisten] ** Start the server with --bind_ip <address> to specify which IP 2019-07-05T08:57:25.689+0800 I CONTROL [initandlisten] ** addresses it should serve responses from, or with --bind_ip_all to 2019-07-05T08:57:25.689+0800 I CONTROL [initandlisten] ** bind to all interfaces. If this behavior is desired, start the 2019-07-05T08:57:25.689+0800 I CONTROL [initandlisten] ** server with --bind_ip 127.0.0.1 to disable this warning. 2019-07-05T08:57:25.689+0800 I CONTROL [initandlisten] 2019-07-05T08:57:25.706+0800 I CONTROL [initandlisten]
- 执行以下命令,插入数据到MongoDB中。
系统回显如下,表示成功插入数据:
WriteResult({ "nInserted" : 1 })
- 执行以下命苦,查询插入的数据。
系统回显如下,表示成功查询到数据:
{ "_id" : ObjectId("5d1b10c2935172824ee11e2e"), "x" : 10 }
- 执行以下命令,连接MongoDB服务端。
