MongoDB-4.2.2 install and configure account password for access

Keywords: Database MongoDB RPM shell

0. purpose

Download MongoDB and install it. Set password access.
The MongoDB official website will download the following three installation packages
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 񖓿 contains shell commands related to mongo operation
MongoDB-org-tools-4.2.2-1.el6.x86 × 64.rpm 񖓿 backup MongoDB, analysis tool set

1. Install 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 to log in to 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 of 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"
        }
    ]
}
##Verify the login, and if 1 is returned, the password will take effect
> db.auth("admin", "test123") 
1

4. Create a db database test and assign an account and password****

#Login administrator
> use admin
> db.auth("admin","test123")
#Create test library
> use test
> db.createUser({ user: "test", pwd: "test123", roles: [{ role: "dbOwner", db: "test" }] })
Successfully added user: {
    "user" : "test",
    "roles" : [
        {
            "role" : "dbOwner",
            "db" : "test"
        }
    ]
}

#The program calls the following link: mongodb://test:test123@localhost/test

Some failures that cannot be started in operation and maintenance

1. After the service restart s, there will be a problem that cannot be opened. The search 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

resolvent:

# rm -fr /tmp/mongodb-27017.sock
# /etc/init.d/mongod start
starting mongod:           [OK]

Posted by GameMusic on Wed, 25 Dec 2019 06:42:05 -0800