暂无图片
暂无图片
4
暂无图片
暂无图片
暂无图片

快速上手!MongoDB 3.4安装配置

原创 阿林哥 2021-07-13
2661

关闭SElinux

vi /etc/selinux/config
...
SELINUX=disabled
...

关闭防火墙

systemctl disable firewalld
systemctl stop firewalld

上传rpm包

mongodb-org-mongos-3.4.24-1.el8.x86_64.rpm
mongodb-org-server-3.4.24-1.el8.x86_64.rpm
mongodb-org-shell-3.4.24-1.el8.x86_64.rpm
mongodb-org-tools-3.4.24-1.el8.x86_64.rpm

安装

[root@fwl-node01 mongodb]# rpm -ivh *.rpm
warning: mongodb-org-mongos-3.4.24-1.el8.x86_64.rpm: Header V3 RSA/SHA1 Signature, key ID a15703c6: NOKEY
Verifying...                          ################################# [100%]
Preparing...                          ################################# [100%]
Updating / installing...
   1:mongodb-org-tools-3.4.24-1.el8   ################################# [ 25%]
   2:mongodb-org-shell-3.4.24-1.el8   ################################# [ 50%]
   3:mongodb-org-server-3.4.24-1.el8  ################################# [ 75%]
Created symlink /etc/systemd/system/multi-user.target.wants/mongod.service → /usr/lib/systemd/system/mongod.service.
   4:mongodb-org-mongos-3.4.24-1.el8  ################################# [100%]
/sbin/ldconfig: /etc/ld.so.conf.d/kernel-5.4.17-2102.201.3.el8uek.x86_64.conf:6: hwcap directive ignored
[root@fwl-node01 mongodb]# 

Note:安装完成会自动创建mongod用户和mongod服务

创建目录

mkdir -p /mongodb/data
mkdir -p /mongodb/log
chown mongod.mongod -R /mongodb

修改参数文件

[root@fwl-node01 mongodb]# vi /etc/mongod.conf

# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: /mongodb/log/mongod.log

# Where and how to store data.
storage:
  dbPath: /mongodb/data
  journal:
    enabled: true
#  engine:
#  mmapv1:
#  wiredTiger:

Note:自定义数据目录和日志目录

启动服务

[root@fwl-node01 mongodb]# systemctl start mongod


查看日志
[root@fwl-node01 mongodb]# tail -f /mongodb/log/mongod.log

2021-07-13T18:47:19.669+0800 I CONTROL  [initandlisten] 
2021-07-13T18:47:19.669+0800 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2021-07-13T18:47:19.669+0800 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2021-07-13T18:47:19.669+0800 I CONTROL  [initandlisten] 
2021-07-13T18:47:19.678+0800 I FTDC     [initandlisten] Initializing full-time diagnostic data capture with directory '/mongodb/data/diagnostic.data'
2021-07-13T18:47:19.691+0800 I INDEX    [initandlisten] build index on: admin.system.version properties: { v: 2, key: { version: 1 }, name: "incompatible_with_version_32", ns: "admin.system.version" }
2021-07-13T18:47:19.691+0800 I INDEX    [initandlisten] 	 building index using bulk method; build may temporarily use up to 500 megabytes of RAM
2021-07-13T18:47:19.692+0800 I INDEX    [initandlisten] build index done.  scanned 0 total records. 0 secs
2021-07-13T18:47:19.692+0800 I COMMAND  [initandlisten] setting featureCompatibilityVersion to 3.4
2021-07-13T18:47:19.693+0800 I NETWORK  [thread1] waiting for connections on port 27017

初始化配置

连接

[root@fwl-node01 ~]# mongo
MongoDB shell version v3.4.24
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.24
Server has startup warnings: 
2021-07-13T18:48:24.238+0800 I CONTROL  [initandlisten] 
2021-07-13T18:48:24.238+0800 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2021-07-13T18:48:24.238+0800 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2021-07-13T18:48:24.238+0800 I CONTROL  [initandlisten] 
>

创建管理用户

> show dbs
admin  0.000GB
local  0.000GB
> use admin
switched to db admin
> 
> db.createUser( {user: "admin",pwd: "admin",roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]})
Successfully added user: {
	"user" : "admin",
	"roles" : [
		{
			"role" : "userAdminAnyDatabase",
			"db" : "admin"
		}
	]
}
>

开启数据库认证

vi /etc/mongod.conf

#security:
security:
  authorization: enabled

修改端口

# network interfaces
net:
  port: 27018
  bindIp: 0.0.0.0

重启服务

systemctl restart mongod

登录

[root@fwl-node01 ~]# mongo --port 27018 -u "admin"  --authenticationDatabase "admin" -p "admin"
MongoDB shell version v3.4.24
connecting to: mongodb://127.0.0.1:27018/
MongoDB server version: 3.4.24
> 

创建数据库

> use fwldb
switched to db fwldb
> show dbs
admin  0.000GB
local  0.000GB
> 
创建用户
> db.createUser( {user: "fwl",pwd: "fwl",roles: [ { role: "readWrite", db: "fwldb" } ]})
Successfully added user: {
	"user" : "fwl",
	"roles" : [
		{
			"role" : "readWrite",
			"db" : "fwldb"
		}
	]
}
> db.auth("fwl","fwl")
1
> show dbs
admin  0.000GB
local  0.000GB
> db.fwldb.insert({"id":"111","name":"aaaaa"})
WriteResult({ "nInserted" : 1 })
> show dbs
admin  0.000GB
fwldb  0.000GB
local  0.000GB
>
查看数据
> db.fwldb.find()
{ "_id" : ObjectId("60ed74c78dacef0d325ef374"), "id" : "111", "name" : "aaaaa" }
>
Note:插入不存在集合的数据,会自动创建
> show collections
fwldb
>

查看数据库状态

> db.getName()
fwldb
> db.stats()
{
	"db" : "fwldb",
	"collections" : 1,
	"views" : 0,
	"objects" : 1,
	"avgObjSize" : 50,
	"dataSize" : 50,
	"storageSize" : 16384,
	"numExtents" : 0,
	"indexes" : 1,
	"indexSize" : 16384,
	"ok" : 1
}
>

创建集合

创建
> db.createCollection("test")
{ "ok" : 1 }
>
查看集合
> show collections
fwldb
test
> show tables
fwldb
test
> 
>
插入数据
> db.test.insert( {number:'1234',name:'json',sex:'man'} )
WriteResult({ "nInserted" : 1 })
> 
查看
> db.test.find()
{ "_id" : ObjectId("60ed77ff8dacef0d325ef375"), "number" : "1234", "name" : "json", "sex" : "man" }
> 

创建索引

> db.test.createIndex({"number":1})
{
	"createdCollectionAutomatically" : false,
	"numIndexesBefore" : 1,
	"numIndexesAfter" : 2,
	"ok" : 1
}

「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

文章被以下合辑收录

评论