溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

MongoDB-4.2.2安裝并配置賬戶密碼進行訪問

發(fā)布時間:2020-02-14 11:09:13 來源:網(wǎng)絡 閱讀:1345 作者:Mengix 欄目:MongoDB數(shù)據(jù)庫

0.目的

下載MongoDB并安裝,設置密碼訪問。
MongoDB官網(wǎng)會下載以下三個安裝包
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.創(chuàng)建一個db庫test,并分配賬號和密碼****

#登錄管理員
> use admin
> db.auth("admin","test123")
#創(chuàng)建test庫
> use test
> db.createUser({ user: "test", pwd: "test123", roles: [{ role: "dbOwner", db: "test" }] })
Successfully added user: {
    "user" : "test",
    "roles" : [
        {
            "role" : "dbOwner",
            "db" : "test"
        }
    ]
}

#程序調(diào)用以下鏈接即可:mongodb://test:test123@localhost/test

運維中無法啟動的一些故障

1.服務restart后,會出現(xiàn)無法開啟的問題,查找日志如下

# /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]
向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。

AI