MongoDB-4.2.2安装并配置账户密码进行访问-创新互联
0.目的
下载MongoDB并安装,设置密码访问。
MongoDB官网会下载以下三个安装包
mongodb-org-server-4.2.2-1.el6.x86_64.rpm #MongoDB的服务,配置文件,启动脚本
mongodb-org-shell-4.2.2-1.el6.x86_64.rpm #包含mongo操作相关的shell命令
mongodb-org-tools-4.2.2-1.el6.x86_64.rpm #对MongoDB进行备份,分析的工具集
1.安装server
# rpm -ivh mongodb-org-server-4.2.2-1.el6.x86_64.rpm
# /etc/init.d/mongod start
starting mongod: [OK]
2.安装命令行用来登录mongodb
# rpm -ivh mongodb-org-shell-4.2.2-1.el6.x86_64.rpm
# mongo
> show dbs;
admin 0.000GB
config 0.000GB
local 0.000GB
3.设置MongoDB的管理员密码
> use admin
switched to db admin
> db.createUser({ user: "admin", pwd: "test123", roles: [{ role: "userAdminAnyDatabase", db: "admin" }] })
Successfully added user: {
"user" : "admin_root",
"roles" : [
{
"role" : "userAdminAnyDatabase",
"db" : "admin"
}
]
}
##验证登录,如果返回1,则证明密码生效
> db.auth("admin", "test123")
1
4.创建一个db库test,并分配账号和密码****
#登录管理员
> use admin
> db.auth("admin","test123")
#创建test库
> use test
> db.createUser({ user: "test", pwd: "test123", roles: [{ role: "dbOwner", db: "test" }] })
Successfully added user: {
"user" : "test",
"roles" : [
{
"role" : "dbOwner",
"db" : "test"
}
]
}
#程序调用以下链接即可:mongodb://test:test123@localhost/test
运维中无法启动的一些故障
1.服务restart后,会出现无法开启的问题,查找日志如下
# /etc/init.d/mongod start
starting mongod: [FAILED]
# tail -f /var/log/mongdb/mongod.log
2019-12-25T10:43:44.336+0800 I CONTROL [initandlisten] distarch: x86_64
2019-12-25T10:43:44.336+0800 I CONTROL [initandlisten] target_arch: x86_64
2019-12-25T10:43:44.336+0800 I CONTROL [initandlisten] options: { config: "/etc/mongod.conf", net: { bindIp: "127.0.0.1", port: 27017 }, processManagement: { fork: true, pidFilePath: "/var/run/mongodb/mongod.pid", timeZoneInfo: "/usr/share/zoneinfo" }, storage: { dbPath: "/var/lib/mongo", journal: { enabled: true } }, systemLog: { destination: "file", logAppend: true, path: "/var/log/mongodb/mongod.log" } }
2019-12-25T10:43:44.336+0800 E NETWORK [initandlisten] Failed to unlink socket file /tmp/mongodb-27017.sock Operation not permitted
2019-12-25T10:43:44.336+0800 F - [initandlisten] Fatal Assertion 40486 at src/mongo/transport/transport_layer_asio.cpp 693
2019-12-25T10:43:44.336+0800 F - [initandlisten]
***aborting after fassert() failure
解决方法:
# rm -fr /tmp/mongodb-27017.sock
# /etc/init.d/mongod start
starting mongod: [OK]
另外有需要云服务器可以了解下创新互联建站www.cdcxhl.com,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
当前名称:MongoDB-4.2.2安装并配置账户密码进行访问-创新互联
网站网址:http://ybzwz.com/article/ieeic.html