软件介绍
MongoDB 是一个基于分布式文件存储的数据库,由 C++ 语言编写,旨在为Web应用提供可扩展的高性能数据存储解决方案。
建议最低版本为“MongoDB-3.6.13”。
环境要求
云服务器要求
本文以云服务器KC1实例测试,云服务器配置如表1-1所示。
表1-1 云服务器配置
项目 | 说明 |
规格 | kc1.large.2 | 4vCPUs | 8GB |
磁盘 | 系统盘:高IO(40GB) |
操作系统要求
操作系统要求如表1-2所示。
表1-2 操作系统要求
项目 | 说明 | 下载地址 |
CentOS |
| 在公共镜像中已提供。 |
EulerOS |
| 在公共镜像中已提供。 |
1. 配置编译环境
执行以下命令,安装依赖库。
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
2. 获取源代码
l 本文档所测试版本为:MongoDB-3.6.13
l 软件获取路径为:https://github.com/mongodb/mongo/releases
l MongoDB官网:https://www.mongodb.com/
3. 编译和安装
本文以MongoDB-3.6.13为例,下载MongoDB-3.6.13源码,并编译安装。
1) 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*
2) 执行以下命令,获取较新的Python2.7解释环境,推荐版本为Python2.7.17。
源码编译安装python解释器
cd /usr/local/src
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 -j8"
scl enable devtoolset-7 "make altinstall"
安装配套的setuptools工具
cd /usr/local/src
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工具
cd /usr/local/src
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
3) 执行以下命令,获取MongoDB源码。
cd /usr/local/src
wget https://github.com/mongodb/mongo/archive/r3.6.13.tar.gz
4) 执行以下命令,解压包。
tar -zxvf r3.6.13.tar.gz
5) 执行以下命令,进入“mongo-r3.6.13”目录。
cd mongo-r3.6.13
6) 执行以下命令,构建编译环境。
scl enable devtoolset-7 "/usr/local/bin/pip2 install -r buildscripts/requirements.txt"
7) 执行以下命令,切换到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在多核编译时会消耗大量内存,请根据自己的可用内存大小设定合理的并发度。
8) 执行以下命令,创建MongoDB的数据库目录。
mkdir -p /data/db
可以在此步完成之后,为该目录挂载其他文件系统
4. 运行和验证
1) 安装完成后,启动MongoDB。
a. 执行以下命令,进行MongoDB安装目录。
cd /opt/mongo/bin
b. 执行以下命令,运行MongoDB。
./mongod
2) 用客户端测试MongoDB数据库服务。
a. 执行以下命令,连接MongoDB服务端。
cd /opt/mongo/bin
./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]
b. 执行以下命令,插入数据到MongoDB中。
db.runoob.insert({x:10})
系统回显如下,表示成功插入数据:
WriteResult({ "nInserted" : 1 })
c. 执行以下命苦,查询插入的数据。
db.runoob.find()
系统回显如下,表示成功查询到数据:
{ "_id" : ObjectId("5d1b10c2935172824ee11e2e"), "x" : 10 }
已知问题汇总
问题描述:
构建编译环境时,提示出错,如下所示:
OSError: [Errno 2] No such file or directory: '/usr/lib/python2.7/site-packages/setuptools-19.6.2-py2.7.egg'
解决方法:
重新执行下述命令构建编译环境:
pip2 install -r buildscripts/requirements.txt