安裝mongodb:
brew tap mongodb/brew
brew tap | grep mongodb
brew install mongodb-community@4.4
啟動(dòng)
brew services start mongodb-community@4.4
關(guān)閉
brew services stop mongodb-community@4.4
作為后臺(tái)進(jìn)程手動(dòng)運(yùn)行
mongod --config /usr/local/etc/mongod.conf --fork
后臺(tái)進(jìn)程手動(dòng)關(guān)閉
https://docs.mongodb.com/manual/reference/command/shutdown/#dbcmd.shutdown
要驗(yàn)證MongoDB是否正在運(yùn)行,請(qǐng)執(zhí)行以下操作之一:
如果您將MongoDB作為macOS服務(wù)啟動(dòng):
brew services list
如果您手動(dòng)啟動(dòng)MongoDB作為后臺(tái)進(jìn)程:
ps aux | grep -v grep | grep mongod
可視化應(yīng)用:https://www.mongodb.com/try/download/compass
mkdir /data/db
chmod -R 777 /data/db
touch /data/db/log/mongodb.log
touch /etc/mongodb.conf
// mongodb.conf
port=27057
logpath=/data/db/log/mongodb.log
dbpath=/data/db
fork=true
logappend=true
上面創(chuàng)建了數(shù)據(jù)庫(kù)的文件路徑征堪,log路徑用狱,以及配置文件:mongodb.conf
默認(rèn)配置文件路徑:
On Linux, a default /etc/mongod.conf configuration file is included when using a package manager to install MongoDB.
On Windows, a default <install directory>/bin/mongod.cfg configuration file is included during the installation.
On macOS, a default /usr/local/etc/mongod.conf configuration file is included when installing from MongoDB’s official Homebrew tap.
配置文件啟動(dòng)
mongod --config /etc/mongod.conf
配置文件:
systemLog:
destination: file
path: /usr/local/var/log/mongodb/mongo.log
logAppend: true
storage:
dbPath: /usr/local/var/mongodb
net:
bindIp: 127.0.0.1
port: 27037
1、查看數(shù)據(jù)庫(kù)
db.stats()
2、查看數(shù)據(jù)庫(kù)表
db.system.users.stats()
3昨忆、db的幫助文檔
db.help();
db.AddUser(username,password[, readOnly=false]) 添加用戶
db.auth(usrename,password) 設(shè)置數(shù)據(jù)庫(kù)連接驗(yàn)證
db.cloneDataBase(fromhost) 從目標(biāo)服務(wù)器克隆一個(gè)數(shù)據(jù)庫(kù)
db.commandHelp(name) returns the help for the command
db.copyDatabase(fromdb,todb,fromhost) 復(fù)制數(shù)據(jù)庫(kù)fromdb---源數(shù)據(jù)庫(kù)名稱惶洲,todb---目標(biāo)數(shù)據(jù)庫(kù)名稱,fromhost---源數(shù)據(jù)庫(kù)服務(wù)器地址
db.createCollection(name,{size:3333,capped:333,max:88888}) 創(chuàng)建一個(gè)數(shù)據(jù)集裹粤,相當(dāng)于一個(gè)表
db.currentOp() 取消當(dāng)前庫(kù)的當(dāng)前操作
db.dropDataBase() 刪除當(dāng)前數(shù)據(jù)庫(kù)
db.eval(func,args) run code server-side
db.getCollection(cname) 取得一個(gè)數(shù)據(jù)集合终蒂,同用法:db['cname']
db.getCollenctionNames() 取得所有數(shù)據(jù)集合的名稱列表
db.getLastError() 返回最后一個(gè)錯(cuò)誤的提示消息
db.getLastErrorObj() 返回最后一個(gè)錯(cuò)誤的對(duì)象
db.getMongo() 取得當(dāng)前服務(wù)器的連接對(duì)象get the server
db.getMondo().setSlaveOk() allow this connection to read from then nonmaster membr of a replica pair
db.getName() 返回當(dāng)操作數(shù)據(jù)庫(kù)的名稱
db.getPrevError() 返回上一個(gè)錯(cuò)誤對(duì)象
db.getProfilingLevel() 獲取profile level
db.getReplicationInfo() 獲得重復(fù)的數(shù)據(jù)
db.getSisterDB(name) get the db at the same server as this onew
db.killOp() 停止(殺死)在當(dāng)前庫(kù)的當(dāng)前操作
db.printCollectionStats() 返回當(dāng)前庫(kù)的數(shù)據(jù)集狀態(tài)
db.printReplicationInfo() 打印主數(shù)據(jù)庫(kù)的復(fù)制狀態(tài)信息
db.printSlaveReplicationInfo() 打印從數(shù)據(jù)庫(kù)的復(fù)制狀態(tài)信息
db.printShardingStatus() 返回當(dāng)前數(shù)據(jù)庫(kù)是否為共享數(shù)據(jù)庫(kù)
db.removeUser(username) 刪除用戶
db.repairDatabase() 修復(fù)當(dāng)前數(shù)據(jù)庫(kù)
db.resetError()
db.runCommand(cmdObj) run a database command. if cmdObj is a string, turns it into {cmdObj:1}
db.setProfilingLevel(level) 設(shè)置profile level 0=off,1=slow,2=all
db.shutdownServer() 關(guān)閉當(dāng)前服務(wù)程序
db.stats() 返回當(dāng)前數(shù)據(jù)庫(kù)的狀態(tài)信息
db.version() 返回當(dāng)前程序的版本信息
數(shù)據(jù)庫(kù)備份
mongodump -h dbhost -d dbname -o dbdirectory
h:
MongDB所在服務(wù)器地址,例如:127.0.0.1,當(dāng)然也可以指定端口號(hào):127.0.0.1:27017d:
需要備份的數(shù)據(jù)庫(kù)實(shí)例拇泣,例如:testo:
備份的數(shù)據(jù)存放位置噪叙,例如:c:\data\dump,當(dāng)然該目錄需要提前建立霉翔,在備份完成后睁蕾,系統(tǒng)自動(dòng)在dump目錄下建立一個(gè)test目錄,這個(gè)目錄里面存放該數(shù)據(jù)庫(kù)實(shí)例的備份數(shù)據(jù)债朵。
創(chuàng)建數(shù)據(jù)庫(kù)用戶
db.createUser({user: "user01",pwd: "123456",roles: [ { role : "readWrite", db : "test" } })
kill mongodb
ps -ef | grep mongod
獲取進(jìn)程pid
kill -2 PID