1. 安装 MongoDB
MongoDB 可以从 https://docs.mongodb.com/master/administration/install-on-linux/ 获得安装信息。
● 通过系统自带仓库安装:
CentOS7 通过 yum 包管理器安装:
[devalone@devalone chapter03]$ sudo yum install mongodb mongodb-server
Fedora 24 通过 dnf 包管理器安装:
[devalone@devalone chapter03]$ sudo dnf install mongodb mongodb-server
● 安装特定版本:
新建特定仓库文件:
[devalone@devalone chapter03]$ sudo vi /etc/yum.repos.d/mongodb-org-3.6.repo
添加如下内容:
[mongodb-org-3.6]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.6/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-3.6.asc
CentOS 上执行:
[devalone@online ~]$ sudo yum install mongodb-org
Fedora 执行:
[devalone@online ~]$ sudo dnf install mongodb-org
● 如果未找到 mongodump/mongorestore 命令,则需要安装 mongo-tools 包:
CentOS 执行:
[devalone@online ~]$ sudo yum install mongo-tools
Fedora 执行:
[devalone@online ~]$ sudo dnf install mongo-tools
● 配置 SELinux:
[devalone@online ~]$ sudo semanage port -a -t mongod_port_t -p tcp 27017
[devalone@online ~]$ sudo semanage port -a -t mongod_port_t -p tcp 27018
[devalone@online ~]$ sudo semanage port -a -t mongod_port_t -p tcp 27019
● 默认安装数据存储位置:
[devalone@online ~]$ ll -Z /var/lib/mongo/
-rw-------. mongod mongod system_u:object_r:mongod_var_lib_t:s0 collection-0--3503115396375879196.wt
-rw-------. mongod mongod system_u:object_r:mongod_var_lib_t:s0 collection-2--3503115396375879196.wt
-rw-------. mongod mongod system_u:object_r:mongod_var_lib_t:s0 collection-4--3503115396375879196.wt
drwx------. mongod mongod system_u:object_r:mongod_var_lib_t:s0 diagnostic.data
-rw-------. mongod mongod system_u:object_r:mongod_var_lib_t:s0 index-1--3503115396375879196.wt
-rw-------. mongod mongod system_u:object_r:mongod_var_lib_t:s0 index-3--3503115396375879196.wt
-rw-------. mongod mongod system_u:object_r:mongod_var_lib_t:s0 index-5--3503115396375879196.wt
-rw-------. mongod mongod system_u:object_r:mongod_var_lib_t:s0 index-6--3503115396375879196.wt
drwx------. mongod mongod system_u:object_r:mongod_var_lib_t:s0 journal
-rw-------. mongod mongod system_u:object_r:mongod_var_lib_t:s0 _mdb_catalog.wt
-rw-------. mongod mongod system_u:object_r:mongod_var_lib_t:s0 mongod.lock
-rw-------. mongod mongod system_u:object_r:mongod_var_lib_t:s0 sizeStorer.wt
-rw-------. mongod mongod system_u:object_r:mongod_var_lib_t:s0 storage.bson
-rw-------. mongod mongod system_u:object_r:mongod_var_lib_t:s0 WiredTiger
-rw-------. mongod mongod system_u:object_r:mongod_var_lib_t:s0 WiredTigerLAS.wt
-rw-------. mongod mongod system_u:object_r:mongod_var_lib_t:s0 WiredTiger.lock
-rw-------. mongod mongod system_u:object_r:mongod_var_lib_t:s0 WiredTiger.turtle
-rw-------. mongod mongod system_u:object_r:mongod_var_lib_t:s0 WiredTiger.wt
2. 修改数据存储目录
准备新的存储目录。示例为 /disk2T-2/mongo
[root@online devalone]# mkdir /disk2T-2/mongo
[root@online devalone]# chmod 755 /disk2T-2/mongo
[root@online devalone]# chown mongodb:root /disk2T-2/mongo
复制 /var/lib/mongo/ 下所有文件至新的目标目录,注意保留文件及目录属性:
[root@online devalone]# cp -R -p /var/lib/mongo/* /disk2T-2/mongo/
修改配置文件 /etc/mongod.conf 的 storage.dbPath 选项:
# storage Options - How and Where to store data storage:
# Directory for datafiles (defaults to /data/db/)
# dbPath: /var/lib/mongodb
dbPath: /disk2T-2/mongo
重新配置新目录的读写权限和 SeLinux 配置:
[root@online ~]# chcon -R --reference=/var/lib/mongo /disk2T-2/mongo
[root@online ~]# sudo systemctl restart mongod.service
默认日志文件:
/var/log/mongodb/mongod.log
修改日志文件位置,配置 /etc/mongod.conf 文件的 systemLog.path 选项。
启动 mongod 服务:
[devalone@online ~]$ sudo systemctl start mongod.service
随系统自启动:
[devalone@online ~]$ sudo systemctl enable mongod.service
2.3 测试
-------------------------------------------------------------------------------------------------------------------------
检查版本:
[devalone@online ~]$ mongo --version
MongoDB shell version v3.6.7
git version: 2628472127e9f1826e02c665c1d93880a204075e
OpenSSL version: OpenSSL 1.0.1e-fips 11 Feb 2013
allocator: tcmalloc
modules: none
build environment:
distmod: rhel70
distarch: x86_64
target_arch: x86_64
启动客户端 shell:
[devalone@online ~]$ mongo
MongoDB shell version v3.6.7
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.6.7
要支持远程访问需要配置防火墙开启默认端口: 27017、27018、27019
2.4 安装 python 驱动 pymongo
-------------------------------------------------------------------------------------------------------------------------
PyMongo 支持 MongoDB 2.6, 3.0, 3.2, 3.4, 3.6 and 4.0.
使用如下命令安装 Python 封装库 pymongo:
● 使用 easy_install 安装:
$ python -m easy_install pymongo
$ python -m easy_install -U pymongo #更新操作
● 使用 pip 安装:
$ python -m pip install pymongo
$ python -m pip install --upgrade pymongo #更新操作
● 下载项目源代码执行安装: https://github.com/mongodb/mongo-python-driver
$ python setup.py install
在Python 中,使用MongoDB 的默认端口尝试连接 MongoDB:
>>> from pymongo import MongoClient
>>> client = MongoClient('localhost', 27017)
>>>
OK.