溫馨提示×

溫馨提示×

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

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

centOS 6 安裝mongoDB

發(fā)布時間:2020-07-13 01:49:47 來源:網(wǎng)絡(luò) 閱讀:2081 作者:a8752311 欄目:數(shù)據(jù)庫

mongoDB安裝與配置


第一步,去mongoDB官網(wǎng)下載mongoDB數(shù)據(jù)庫

下載版本要根據(jù)你的操作系統(tǒng)來下,

mongoDB如:mongodb-linux-x86_64-3.2.10.tgz


第二步,安裝虛擬機(jī)

虛擬機(jī)安裝略過


第三步,安裝linux系統(tǒng)

linux安裝略過


第四步,安裝SSH連接linx工具

略過


第五步,linx系統(tǒng)安裝好了之后,根據(jù)你的用戶密碼登錄linx

使用SSH工具連接linx


以上自己百度一下就能找到



第六步,干貨來了。

linux下安裝mongoDB

解壓mongoDB

修改一下文件夾名字:mongodb

文件結(jié)構(gòu)如下:

mongodb 

->bin

->GNU-AGPL-3.0

->MPL-2

->README

->THIRD-PARTY-NOTICES

在此文件夾創(chuàng)建兩個文件夾,用于存放數(shù)據(jù)文件以及日志文件(當(dāng)然你也可以自己選擇自己喜歡的位置)

文件夾名為:db

       log

  

輸入命令:cd /usr/local/

將之前的mongodb復(fù)制一下粘帖進(jìn)這個目錄下



輸入命令: cd /usr/local/mongodb/bin

/** 啟動服務(wù)  **/

./mongod --dbpath=/usr/local/mongodb/db/ --logpath=/usr/local/mongodb/log/mongo.log --logappend  --auth  --port=27017 --fork


/** 成功信息 **/

about to fork child process, waiting until server is ready for connections.

forked process: 12999

child process started successfully, parent exiting


終于啟動成功了

./mongod


use admin


/** 創(chuàng)建超級管理員,mongoDB 3.0以后的版本適用create創(chuàng)建,不是add  **/

db.createUser({user: "super",pwd: "super",roles: [ { role: "root", db: "admin" } ]})


db.auth("super","super")


/** 創(chuàng)建數(shù)據(jù)庫  **/

use mydb

添加數(shù)據(jù)

db.usr.insert({'name':'tompig'});




/** 上面只是創(chuàng)建了admin數(shù)據(jù)庫的賬戶,新創(chuàng)建的數(shù)據(jù)庫還不能訪問 **/

使用super賬戶登錄進(jìn)去創(chuàng)建

./mongo

use admin

db.auth("super","super")

use mydb

db.createUser({user: "testx",pwd: "testx",roles: [ { role: "dbAdmin", db: "mydb" } ]})

創(chuàng)建成功后,使用testx用戶連接數(shù)據(jù)進(jìn)行訪問。


/** 關(guān)閉服務(wù)  **/

1、普通命令:

$ ./mongo

> use admin

> db.shutdownServer()



注意:必須關(guān)閉防火墻,否則訪問不到

/** 關(guān)閉防火墻  **/

service iptables stop


/** 永久關(guān)閉防火墻 **/

chkconfig --level 35 iptables off



遇到下面

報錯:

Permission denied.


輸入命令:chmod -R 777 /usr/local/mongodb(這里的路徑是mongodb的路徑)   原因=先看是否是root權(quán)限,而且是ssh復(fù)制粘帖進(jìn)去的,并沒有賦予權(quán)限


報錯:

./mongod: /usr/lib64/libcrypto.so.10: no version information available (required by ./mongod)

./mongod: /usr/lib64/libssl.so.10: no version information available (required by ./mongod)

./mongod: relocation error: ./mongod: symbol TLSv1_2_client_method, version libssl.so.10 not defined in file libssl.so.10 with link time reference


輸入命令:yum install -y openssl-devel openssl解決,安裝openssl依賴運(yùn)行是需要的



報錯:

about to fork child process, waiting until server is ready for connections.

forked process: 12981

ERROR: child process failed, exited with error number 1

輸入命令:./mongod --repair --dbpath /usr/local/mongodb/db/ --repairpath /tmp/mongodb




上面的設(shè)置多麻煩啊,下面配置快捷啟動方式


新建記事本,把名字改為mongo.conf,把下面的復(fù)制粘貼進(jìn)去

dbpath=/usr/local/mongodb/db

logpath=/usr/local/mongodb/log/mongo.log

port=27017

fork=true

auth=true

bind_ip=0.0.0.0


相信大家看了上面的配置也能看懂,

dbpath=mongodb的數(shù)據(jù)庫存放路徑

logpath=mongodb的日志文件存放路徑

port=端口號

fork=true這個是以守護(hù)進(jìn)程的方式運(yùn)行

auth=true是否需要認(rèn)證登錄

bind_ip=綁定端口號,0.0.0.0是任何ip都能訪問進(jìn)來,否則需要指定ip如:192.168.1.102




配置好了之后呢,把此文件放入/usr/mongodb/bin目錄下

輸入命令:cd /usr/local/mongodb/bin進(jìn)入目錄

輸入命令:./mongod -f mongo.conf啟動


over,下面是db的幫助












db的幫助

db.createUser(username,password) 添加用戶 

db.auth(usrename,password)   設(shè)置數(shù)據(jù)庫連接驗(yàn)證 

db.cloneDataBase(fromhost)   從目標(biāo)服務(wù)器克隆一個數(shù)據(jù)庫 

db.commandHelp(name)      returns the help for the command 

db.copyDatabase(fromdb,todb,fromhost) 復(fù)制數(shù)據(jù)庫fromdb---源數(shù)據(jù)庫名稱,todb---目標(biāo)數(shù)據(jù)庫名稱,fromhost---源數(shù)據(jù)庫服務(wù)器地址 

db.createCollection(name,{size:3333,capped:333,max:88888}) 創(chuàng)建一個數(shù)據(jù)集,相當(dāng)于一個表 

db.currentOp()         取消當(dāng)前庫的當(dāng)前操作 

db.dropDataBase()       刪除當(dāng)前數(shù)據(jù)庫 

db.eval(func,args)       run code server-side 

db.getCollection(cname)    取得一個數(shù)據(jù)集合,同用法:db['cname'] or 

db.getCollenctionNames()    取得所有數(shù)據(jù)集合的名稱列表 

db.getLastError()       返回最后一個錯誤的提示消息 

db.getLastErrorObj()      返回最后一個錯誤的對象 

db.getMongo()         取得當(dāng)前服務(wù)器的連接對象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ù)庫的名稱 

db.getPrevError()       返回上一個錯誤對象 

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)前庫的當(dāng)前操作 

db.printCollectionStats()   返回當(dāng)前庫的數(shù)據(jù)集狀態(tài) 

db.printReplicationInfo()    打印主數(shù)據(jù)庫的復(fù)制狀態(tài)信息 

db.printSlaveReplicationInfo()    打印從數(shù)據(jù)庫的復(fù)制狀態(tài)信息 

db.printShardingStatus()    返回當(dāng)前數(shù)據(jù)庫是否為共享數(shù)據(jù)庫 

db.removeUser(username)    刪除用戶 

db.repairDatabase()      修復(fù)當(dāng)前數(shù)據(jù)庫 

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.version()          返回當(dāng)前程序的版本信息 


Read:允許用戶讀取指定數(shù)據(jù)庫
readWrite:允許用戶讀寫指定數(shù)據(jù)庫
dbAdmin:允許用戶在指定數(shù)據(jù)庫中執(zhí)行管理函數(shù),如索引創(chuàng)建、刪除,查看統(tǒng)計或訪問system.profileuserAdmin:允許用戶向system.users集合寫入,可以找指定數(shù)據(jù)庫里創(chuàng)建、刪除和管理用戶
clusterAdmin:只在admin數(shù)據(jù)庫中可用,賦予用戶所有分片和復(fù)制集相關(guān)函數(shù)的管理權(quán)限。
readAnyDatabase:只在admin數(shù)據(jù)庫中可用,賦予用戶所有數(shù)據(jù)庫的讀權(quán)限
readWriteAnyDatabase:只在admin數(shù)據(jù)庫中可用,賦予用戶所有數(shù)據(jù)庫的讀寫權(quán)限
userAdminAnyDatabase:只在admin數(shù)據(jù)庫中可用,賦予用戶所有數(shù)據(jù)庫的userAdmin權(quán)限dbAdminAnyDatabase:只在admin數(shù)據(jù)庫中可用,賦予用戶所有數(shù)據(jù)庫的dbAdmin權(quán)限。
root:只在admin數(shù)據(jù)庫中可用。超級賬號,超級權(quán)限


表的幫助,格式,db.表名.help()   

db.test.find({id:10})     返回test數(shù)據(jù)集ID=10的數(shù)據(jù)集 

db.test.find({id:10}).count() 返回test數(shù)據(jù)集ID=10的數(shù)據(jù)總數(shù) 

db.test.find({id:10}).limit(2) 返回test數(shù)據(jù)集ID=10的數(shù)據(jù)集從第二條開始的數(shù)據(jù)集 

db.test.find({id:10}).skip(8) 返回test數(shù)據(jù)集ID=10的數(shù)據(jù)集從0到第八條的數(shù)據(jù)集 

db.test.find({id:10}).limit(2).skip(8) 返回test數(shù)據(jù)集ID=1=的數(shù)據(jù)集從第二條到第八條的數(shù)據(jù) 

db.test.find({id:10}).sort()  返回test數(shù)據(jù)集ID=10的排序數(shù)據(jù)集 

db.test.findOne([query])    返回符合條件的一條數(shù)據(jù) 

db.test.getDB()        返回此數(shù)據(jù)集所屬的數(shù)據(jù)庫名稱 

db.test.getIndexes()      返回些數(shù)據(jù)集的索引信息 

db.test.group({key:...,initial:...,reduce:...[,cond:...]})  返回分組信息 

db.test.mapReduce(mayFunction,reduceFunction,<optional params>) 這個有點(diǎn)像存儲過程 

db.test.remove(query)           在數(shù)據(jù)集中刪除一條數(shù)據(jù) 

db.test.renameCollection(newName)     重命名些數(shù)據(jù)集名稱 

db.test.save(obj)             往數(shù)據(jù)集中插入一條數(shù)據(jù) 

db.test.stats()              返回此數(shù)據(jù)集的狀態(tài) 

db.test.storageSize()           返回此數(shù)據(jù)集的存儲大小 

db.test.totalIndexSize()          返回此數(shù)據(jù)集的索引文件大小 

db.test.totalSize()            返回些數(shù)據(jù)集的總大小 

db.test.update(query,object[,upsert_bool]) 在此數(shù)據(jù)集中更新一條數(shù)據(jù) 

db.test.validate()             驗(yàn)證此數(shù)據(jù)集 

db.test.getShardVersion()         返回數(shù)據(jù)集共享版本號 



向AI問一下細(xì)節(jié)

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

AI