MongoDB 4.2.2 for CentOS 7

1. 安装MongoDB

wget https://repo.mongodb.org/yum/redhat/7/mongodb-org/4.2/x86_64/RPMS/mongodb-org-server-4.2.2-1.el7.x86_64.rpm
wget https://repo.mongodb.org/yum/redhat/7/mongodb-org/4.2/x86_64/RPMS/mongodb-org-mongos-4.2.2-1.el7.x86_64.rpm
wget https://repo.mongodb.org/yum/redhat/7/mongodb-org/4.2/x86_64/RPMS/mongodb-org-tools-4.2.2-1.el7.x86_64.rpm
wget https://repo.mongodb.org/yum/redhat/7/mongodb-org/4.2/x86_64/RPMS/mongodb-org-shell-4.2.2-1.el7.x86_64.rpm

# rpm -ivh mongodb-org-server-4.2.2-1.el7.x86_64.rpm
警告:mongodb-org-server-4.2.2-1.el7.x86_64.rpm: 头V3 RSA/SHA1 Signature, 密钥 ID 058f8b6b: NOKEY
准备中...                          ################################# [100%]
正在升级/安装...
   1:mongodb-org-server-4.2.2-1.el7   ################################# [100%]
Created symlink from /etc/systemd/system/multi-user.target.wants/mongod.service to /usr/lib/systemd/system/mongod.service.

# rpm -ivh mongodb-org-mongos-4.2.2-1.el7.x86_64.rpm
警告:mongodb-org-mongos-4.2.2-1.el7.x86_64.rpm: 头V3 RSA/SHA1 Signature, 密钥 ID 058f8b6b: NOKEY
准备中...                          ################################# [100%]
正在升级/安装...
   1:mongodb-org-mongos-4.2.2-1.el7   ################################# [100%]

# rpm -ivh mongodb-org-tools-4.2.2-1.el7.x86_64.rpm
警告:mongodb-org-tools-4.2.2-1.el7.x86_64.rpm: 头V3 RSA/SHA1 Signature, 密钥 ID 058f8b6b: NOKEY
准备中...                          ################################# [100%]
正在升级/安装...
   1:mongodb-org-tools-4.2.2-1.el7    ################################# [100%]

# rpm -ivh mongodb-org-shell-4.2.2-1.el7.x86_64.rpm
警告:mongodb-org-shell-4.2.2-1.el7.x86_64.rpm: 头V3 RSA/SHA1 Signature, 密钥 ID 058f8b6b: NOKEY
准备中...                          ################################# [100%]
正在升级/安装...
   1:mongodb-org-shell-4.2.2-1.el7    ################################# [100%]

2.验证安装结果

# rpm -qa|grep mongo
mongodb-org-server-4.2.2-1.el7.x86_64
mongodb-org-shell-4.2.2-1.el7.x86_64
mongodb-org-mongos-4.2.2-1.el7.x86_64
mongodb-org-tools-4.2.2-1.el7.x86_64

# rpm -ql mongodb-org-server
/etc/mongod.conf
/lib/systemd/system/mongod.service
/usr/bin/mongod
/usr/share/doc/mongodb-org-server-4.2.2
/usr/share/doc/mongodb-org-server-4.2.2/LICENSE-Community.txt
/usr/share/doc/mongodb-org-server-4.2.2/MPL-2
/usr/share/doc/mongodb-org-server-4.2.2/README
/usr/share/doc/mongodb-org-server-4.2.2/THIRD-PARTY-NOTICES
/usr/share/man/man1/mongod.1
/var/lib/mongo
/var/log/mongodb
/var/log/mongodb/mongod.log
/var/run/mongodb

3.启动MongoDB

启动MongoDB服务
systemctl start mongod.service

MongoDB默认端口是27017,查看是否开启
netstat -natp | grep 27017

检查数据库是否安装成功
ps -aux | grep mongod

4. 验证服务开启

mongo

5.开启远程连接与防火墙配置

5.1. 修改配置文件mongodb.conf

复制代码修改绑定ip默认127.0.0.1只允许本地连接, 所以修改为bindIp:0.0.0.0, 退出保存

vim /etc/mongod.conf

 # network interfaces
net:
  port: 27017
  bindIp: 0.0.0.0  # Enter 0.0.0.0,:: to bind to all IPv4 and IPv6 addresses or, alternatively, use the net.bindIpAll setting.

systemctl restart mongod.service

5.2. 配置防火墙

firewall-cmd --zone=public --add-port=27017/tcp --permanent # mongodb默认端口号
firewall-cmd --reload
firewall-cmd --zone=public --query-port=27017/tcp # 查看端口号是否开放成功,输出yes开放成功,no则失败

6.身份认证

6.1、创建用户,设置账号,密码,权限

// admin数据库
> use admin
switched to db admin
> db.createUser({ user:"root", pwd:"Root@2020", roles:["root"] })
Successfully added user: { "user" : "root", "roles" : [ "root" ] }

// 其他数据库
> use test
switched to db test
> db.createUser({ user:"admin", pwd:"Root@2020", roles:["readWrite", "dbAdmin"] })
Successfully added user: { "user" : "admin", "roles" : [ "readWrite", "dbAdmin" ] }

6.2、修改mongodb.conf文件,启用身份验证

vim /etc/mongod.conf

security:
  authorization: "enabled"   # disable or enabled

6.3、重启MongoDB

systemctl restart mongod.service

6.4、用户认证

> use admin
switched to db admin
> db.auth("root", "Root@2020")
1 // 授权成功

7.Python 连接MongoDB

7.1、连接MongoDB

pip3 install pymongo

# 无密码连接
import pymongo
mongo_client = pymongo.MongoClient('127.0.0.1', 27017)

# 有密码连接1
import pymongo
mongo_client = pymongo.MongoClient('127.0.0.1', 27017)
mongo_auth = mongo_client.admin # 或 mongo_client['admin'] admin为authenticationDatabase
mongo_auth.authenticate('root', 'Root@2020')

# 有密码连接2
import pymongo
import urllib.parse
mongo_username = urllib.parse.quote_plus('root')
mongo_password = urllib.parse.quote_plus('Root@2020')
mongo_client = pymongo.MongoClient('mongodb://%s:%s@127.0.0.1:27017' % (mongo_username, mongo_password))
print(mongo_client.server_info()) #判断是否连接成功

7.2、获取Database 和 Collection

若没有Database 和Collection,则会自动创建

# 第一种方式:
mongo_db = mongo_client['你的database']
mongo_collection = mongo_db['你的collection']

# 第二种方式:
mongo_db = mongo_client.你的database
mongo_collection = mongo_db.你的collection

7.3、CURD操作, 插入单条数据 insert_one()

import datetime

mongo_db = mongo_client.admin
mongo_collection = mongo_db.admin
info = {
    'name' : 'NoteBook',
    'text' : 'Inserting a Document',
    'tags' : ['a', 'b', 'c'],
    'date' : datetime.datetime.now()
}
mongo_collection.insert_one(info)

7.4、查询一条

find_condition = {
    'name' : 'NoteBook',
}
select_item = mongo_collection.find_one(find_condition, projection= {'_id':False, 'name':True, 'num':True})
print(select_item)

7.5、Python操作MongoDB参考

Python中使用MongoDB详解

点赞

发表评论

电子邮件地址不会被公开。必填项已用 * 标注