0. purpose
Download and install MongoDB, set a password to access.
MongoDB network will download the installation package of three official
mongodb-org-server-4.2.2-1.el6.x86_64.rpm #MongoDB services, configuration files, startup scripts
mongodb-org-shell-4.2.2-1.el6. x86_64.rpm # shell comprising mongo operation command related
tools mongodb-org-tools-4.2.2-1.el6.x86_64.rpm # MongoDB for backup, Analytical
1. Install the server
# rpm -ivh mongodb-org-server-4.2.2-1.el6.x86_64.rpm
# /etc/init.d/mongod start
starting mongod: [OK]
2. Install the command line used to log 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. Set the administrator password 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. Create a library db test, and assign the account number and password ****
#登录管理员
> 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
Operation and maintenance of some of the fault can not start
1. After service restart, there will not be a problem to open and look for log is as follows
# /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
Solution:
# rm -fr /tmp/mongodb-27017.sock
# /etc/init.d/mongod start
starting mongod: [OK]