ubuntu18.04安裝mongodb4.4


1.下載對應的mongo版本

1.查看linux版本:uname -a

  2.下載mongodb包

下載地址: https://www.mongodb.com/try/download

本地測試,我們下載community版本即可

  下載:wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-ubuntu1804-4.4.1.tgz

二、配置

1.創建數據庫目錄和log文件和所在目錄

mkdir -p /data/db
mkdir -p /data/log/mongo/

touch /data/log/mongo/mongodb.log

2.編輯配置文件

vim /etc/mongod.conf

# 詳細記錄輸出
verbose = true

# 指定服務端口號,默認端口27017
port = 27017

# 指定MongoDB日志文件,注意是指定文件不是目錄
logpath = /data/log/mongo/mongodb.log
# 使用追加的方式寫日志
logappend = true

# 指定數據庫路徑
dbpath = /data/db/mongo/
#設置每個數據庫將被保存在一個單獨的目錄
directoryperdb = true

# 啟用驗證
auth = false

#以守護進程的方式運行MongoDB,創建服務器進程
fork = true
#安靜輸出
quiet = true

 

三、啟動、驗證

cd /usr/local/mongodb/bin

1.啟動:./mongod -f /etc/mongod.conf 返回以下內容表示啟動成功

  2.驗證:pgrep mongo -l  有返回結果則證明啟動成功

 

四、常用命令

本文只為其他項目而簡單搭建環境,具體命令請查看官網等!

 1.進入shell模式:

./mongo

2.查看數據庫列表:

> show dbs
admin   0.000GB
config  0.000GB
local   0.000GB

3.進入並創建數據數據庫

> use test
switched to db test

4.查看當前在哪個數據庫

> db
test

5.創建集合(數據表)

> db.createCollection("test")
{ "ok" : 1 }

6.向集合插入數據

> db.test.insert({name:"哈哈",age:18})
WriteResult({ "nInserted" : 1 })

7.查看數據庫中有哪些集合(數據表)

> show collections
test
test2
test3

8.刪除集合

> db.test3.drop()
true

 

五、開啟認證

1.創建管理員賬號

> use admin
switched to db admin
> show collections
system.version
> db.system.version.find()
{ "_id" : "featureCompatibilityVersion", "version" : "4.4" }
> db.createUser({user:"admin",pwd:"123456",roles:["root"]})
Successfully added user: { "user" : "admin", "roles" : [ "root" ] }

 2.開啟認證

vim /etc/mongod.conf  

auth = true #將auth設置為true

3.重新登錄

root@ubuntu1:/usr/local/mongodb/bin# ./mongo
MongoDB shell version v4.4.1
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("738b99c4-a57e-467b-8d60-15f5e0007ce8") }
MongoDB server version: 4.4.1
> use admin
switched to db admin
> db.auth("admin","123456")
1
> show dbs
admin   0.000GB
config  0.000GB
local   0.000GB
test    0.000GB

 4.為數據庫添加用戶

> use test
switched to db test
> db.createUser({user:"test",pwd:"123456",roles:[{role:"dbOwner",db:"test"}]})
Successfully added user: {
    "user" : "test",
    "roles" : [
        {
            "role" : "dbOwner",  #權限
            "db" : "test"     #數據庫
        }
    ]
}

5.用數據庫用戶test登錄

root@ubuntu1:/usr/local/mongodb/bin# ./mongo
MongoDB shell version v4.4.1
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("ca9ce18c-be3b-4f7a-af77-50db106c9676") }
MongoDB server version: 4.4.1
> use test
switched to db test
> db.auth("test","123456")
1
> show dbs
test  0.000GB
> db
test
> show collections
test
test2

6.刪除用戶

> use admin
switched to db admin
> db.system.users.find()
{ "_id" : "admin.admin", "userId" : UUID("a3a3c4b2-6984-44a1-80d4-112bad3fd86f"), "user" : "admin", "db" : "admin", "credentials" : { "SCRAM-SHA-1" : { "iterationCount" : 10000, "salt" : "Edm0oEf+DCxBvjIAbg9V6Q==", "storedKey" : "GY8/71b+NRD2IlNAtPJTjxGZ34o=", "serverKey" : "AoSfWXn3MkOJcax7vU02a4LmTmA=" }, "SCRAM-SHA-256" : { "iterationCount" : 15000, "salt" : "nc3gzY+tLBNy4HGE0SXBQz0+7QMjYpbkZ9I+1w==", "storedKey" : "dlEriAdL5XE5CyhOwXGhvm9pmKRtfIGyVXkz8x0YX3w=", "serverKey" : "Oxw7bKeEcZ1FftFLY3xON6tTZS7KS/VsR5bUPQwWlXM=" } }, "roles" : [ { "role" : "root", "db" : "admin" } ] }
{ "_id" : "test.test", "userId" : UUID("5ddc0a8e-d799-4900-ba58-4adc4f04d73f"), "user" : "test", "db" : "test", "credentials" : { "SCRAM-SHA-1" : { "iterationCount" : 10000, "salt" : "7JzzsLSOQbd3+/pfavT3aQ==", "storedKey" : "M/9gUQTJGEm+Jp/z2e8hPyHeBeo=", "serverKey" : "sCPbv9Aj7Pdub9mPmQHG7Fq6hbY=" }, "SCRAM-SHA-256" : { "iterationCount" : 15000, "salt" : "bLSGy+lL7nepxvbhOCxGejmGd04p2ZM2QU1YYw==", "storedKey" : "NAoLCNhAZ2MtVJk/LKTKD3JiX501SiG9AL5MDn9FzcA=", "serverKey" : "kQ3SK6QEquFuUrlPKE5s0xveQ4s5SIaii8w5hx4jDt4=" } }, "roles" : [ { "role" : "dbOwner", "db" : "test" } ] }
> db.dropUser("test")   #admin數據庫內不能刪除test數據庫下面的test用戶
false
> use test
switched to db test
> db.dropUser("test")  #必須切換到被刪除用戶所在數據庫才能刪除
true
> use admin
switched to db admin
> db.system.users.find()
{ "_id" : "admin.admin", "userId" : UUID("a3a3c4b2-6984-44a1-80d4-112bad3fd86f"), "user" : "admin", "db" : "admin", "credentials" : { "SCRAM-SHA-1" : { "iterationCount" : 10000, "salt" : "Edm0oEf+DCxBvjIAbg9V6Q==", "storedKey" : "GY8/71b+NRD2IlNAtPJTjxGZ34o=", "serverKey" : "AoSfWXn3MkOJcax7vU02a4LmTmA=" }, "SCRAM-SHA-256" : { "iterationCount" : 15000, "salt" : "nc3gzY+tLBNy4HGE0SXBQz0+7QMjYpbkZ9I+1w==", "storedKey" : "dlEriAdL5XE5CyhOwXGhvm9pmKRtfIGyVXkz8x0YX3w=", "serverKey" : "Oxw7bKeEcZ1FftFLY3xON6tTZS7KS/VsR5bUPQwWlXM=" } }, "roles" : [ { "role" : "root", "db" : "admin" } ] }
> 

 

 數據庫用戶角色

  • 數據庫用戶角色(Database User Roles)

    read:授予User只讀數據的權限
    readWrite:授予User讀寫數據的權限

  • 數據庫管理角色(Database Administration Roles):

    dbAdmin:在當前dB中執行管理操作
    dbOwner:在當前DB中執行任意操作
    userAdmin:在當前DB中管理User

  • 備份和還原角色(Backup and Restoration Roles):

    backup
    restore

  • 跨庫角色(All-Database Roles):

    readAnyDatabase:授予在所有數據庫上讀取數據的權限
    readWriteAnyDatabase:授予在所有數據庫上讀寫數據的權限
    userAdminAnyDatabase:授予在所有數據庫上管理User的權限
    dbAdminAnyDatabase:授予管理所有數據庫的權限

  • 集群管理角色(Cluster Administration Roles):

    clusterAdmin:授予管理集群的最高權限
    clusterManager:授予管理和監控集群的權限,A user with this role can access the config and local databases, which are used in sharding and replication, respectively.
    clusterMonitor:授予監控集群的權限,對監控工具具有readonly的權限
    hostManager:管理Server

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM