【MongoDB】2、安装MongoDB 2.6.1 on Unbuntu 14.04(学习流水账)
生活随笔
收集整理的這篇文章主要介紹了
【MongoDB】2、安装MongoDB 2.6.1 on Unbuntu 14.04(学习流水账)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
計劃:
裝一個虛機,ubuntu吧,14.04 Trusty Tahr。
安裝MongoDB
網(wǎng)絡資源:
http://mirrors.aliyun.com/ubuntu-releases/14.04/ubuntu-14.04-server-amd64.iso.torrent(公網(wǎng))
http://mirrors.tuna.tsinghua.edu.cn/ubuntu-releases/14.04/ubuntu-14.04-server-amd64.iso.torrent(教育網(wǎng))
http://fastdl.mongodb.org/linux/mongodb-linux-x86_64-2.6.1.tgz
虛擬機:
處理器數(shù)量1:內(nèi)核數(shù):1(多核對MongoDB幫助不大);MM 1G(RAM瓶頸應該會出現(xiàn));HD 20G。?
安裝Unbuntu,用戶名:gd,密碼:cgdc。
TO小超超,服務器在1.4.3x,一起玩兒吧。
由于Ubuntu官方APT源里的MongoDB版本比較舊,這里用MongoDB提供的APT源進行安裝。
要使用第三方源上的軟件,除了在sources.list上添加對應的源外,還要有增加相應的公鑰Key才能正確安裝軟件。
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10
echo 'deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen' | sudo tee /etc/apt/sources.list.d/mongodb.list
sudo apt-get update
sudo apt-get install mongodb-org
報錯:unable to locate package
sudo apt-get install mongodb就可以,可能版本會老一點,無所謂吧。
要更新的包包括:libboost、libc、libgcc、mongodb、mongodb-clients、mongodb-dev、mongodb-server。
一共38個,下載41.5M,90多K,速度還好,可以先去看會兒書了。
下載到一半,斷了。
F!
棄用apt。
sudo apt-get install openssh-server
這個很快。
用“netstat -tlp”或“ps -e | grep ssh”確認sshd啟動。
http://www.cnblogs.com/chen1987lei/archive/2010/12/02/1894768.html
解壓后得到一堆二進制可執(zhí)行文件,除了bsondump其他都是mongo打頭的。拷貝到 /usr/bin 下面。
chmod a+x?
到此,裝好。
/*
如果安裝特定版本:
apt-get install mongodb-org=2.6.1 mongodb-org-server=2.6.1 mongodb-org-shell=2.6.1 mongodb-org-mongos=2.6.1 mongodb-org-tools=2.6.1
禁用自動升級:
echo "mongodb-org hold" | sudo dpkg --set-selections
echo "mongodb-org-server hold" | sudo dpkg --set-selections
echo "mongodb-org-shell hold" | sudo dpkg --set-selections
echo "mongodb-org-mongos hold" | sudo dpkg --set-selections
echo "mongodb-org-tools hold" | sudo dpkg --set-selections
運行:
sudo /etc/init.d/mongod start
sudo /etc/init.d/mongod stop
sudo /etc/init.d/mongod restart
*/
啟動,報錯,說dbpath不存在,需要手動創(chuàng)建目錄。
創(chuàng)建了 /data/mongodb1 目錄。
啟動的時候加--dbpath。
啟動沒有報錯,但忘了 --fork方式啟動了。
還好,可以ssh連。
報錯:sda1:WRITE SAME faild.Manually zeroing.沒有修改配置,后來沒見過這個問題了。為什么呢?
數(shù)據(jù)目錄沒開寫權限?sudo啟動服務器都不行?
use admin
db.shutdownServer()
啟動config:
啟動選項很多,每次在命令行敲麻煩死了,寫在config文件里吧。
選項包括:
--port
--fork(以守護進程方式執(zhí)行)
--logpath
--config
配置文件寫法:
port = 110271
fork = true
logpath = mongodb.log
無論如何,進入Shell了。
敲help,先爽一下吧。> help? ? ? ? db.help() ? ? ? ? ? ? ? ? ? ?help on db methods
? ? ? ? db.mycoll.help() ? ? ? ? ? ? help on collection methods
? ? ? ? sh.help() ? ? ? ? ? ? ? ? ? ?sharding helpers
? ? ? ? rs.help() ? ? ? ? ? ? ? ? ? ?replica set helpers
? ? ? ? help admin ? ? ? ? ? ? ? ? ? administrative help
? ? ? ? help connect ? ? ? ? ? ? ? ? connecting to a db help
? ? ? ? help keys ? ? ? ? ? ? ? ? ? ?key shortcuts
? ? ? ? help misc ? ? ? ? ? ? ? ? ? ?misc things to know
? ? ? ? help mr ? ? ? ? ? ? ? ? ? ? ?mapreduce
? ? ? ? show dbs ? ? ? ? ? ? ? ? ? ? show database names
? ? ? ? show collections ? ? ? ? ? ? show collections in current database
? ? ? ? show users ? ? ? ? ? ? ? ? ? show users in current database
? ? ? ? show profile ? ? ? ? ? ? ? ? show most recent system.profile entries with time >= 1ms
? ? ? ? show logs ? ? ? ? ? ? ? ? ? ?show the accessible logger names
? ? ? ? show log [name] ? ? ? ? ? ? ?prints out the last segment of log in memory, 'global' is default
? ? ? ? use <db_name> ? ? ? ? ? ? ? ?set current database
? ? ? ? db.foo.find() ? ? ? ? ? ? ? ?list objects in collection foo
? ? ? ? db.foo.find( { a : 1 } ) ? ? list objects in foo where a == 1
? ? ? ? it ? ? ? ? ? ? ? ? ? ? ? ? ? result of the last line evaluated; use to further iterate
? ? ? ? DBQuery.shellBatchSize = x ? set default number of items to display on shell
? ? ? ? exit ? ? ? ? ? ? ? ? ? ? ? ? quit the mongo shell
如果是方法,敲上括號是調用,不敲括號,輸出方法原型。
客戶端Shell:
運行./mongo既可以啟動Shell對mongodb進行管理。
Shell使用Javascipt語法,可以對數(shù)據(jù)進行管理,可以對數(shù)據(jù)庫進行管理。
官網(wǎng)版本:http://try.mongodb.org/。 可以上手玩玩。
很簡單,沒什么可說的。
創(chuàng)建個數(shù)據(jù)庫吧:
>?
> use cgdc
switched to db cgdc
> show collections
> show dbs
admin ?(empty)
cgdc ? (empty)
local ?0.078GB
> db.users.help()
DBCollection help
? ? ? ? db.users.find().help() - show DBCursor help
? ? ? ? db.users.count()
? ? ? ? db.users.copyTo(newColl) - duplicates collection by copying all documents to newColl; no indexes are copied.
? ? ? ? db.users.convertToCapped(maxBytes) - calls {convertToCapped:'users', size:maxBytes}} command
? ? ? ? db.users.dataSize()
? ? ? ? db.users.distinct( key ) - e.g. db.users.distinct( 'x' )
? ? ? ? db.users.drop() drop the collection
? ? ? ? db.users.dropIndex(index) - e.g. db.users.dropIndex( "indexName" ) or db.users.dropIndex( { "indexKey" : 1 } )
? ? ? ? db.users.dropIndexes()
? ? ? ? db.users.ensureIndex(keypattern[,options]) - options is an object with these possible fields: name, unique, dropDups
? ? ? ? db.users.reIndex()
? ? ? ? db.users.find([query],[fields]) - query is an optional query filter. fields is optional set of fields to return.
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? e.g. db.users.find( {x:77} , {name:1, x:1} )
? ? ? ? db.users.find(...).count()
? ? ? ? db.users.find(...).limit(n)
? ? ? ? db.users.find(...).skip(n)
? ? ? ? db.users.find(...).sort(...)
? ? ? ? db.users.findOne([query])
? ? ? ? db.users.findAndModify( { update : ... , remove : bool [, query: {}, sort: {}, 'new': false] } )
? ? ? ? db.users.getDB() get DB object associated with collection
? ? ? ? db.users.getPlanCache() get query plan cache associated with collection
? ? ? ? db.users.getIndexes()
? ? ? ? db.users.group( { key : ..., initial: ..., reduce : ...[, cond: ...] } )
? ? ? ? db.users.insert(obj)
? ? ? ? db.users.mapReduce( mapFunction , reduceFunction , <optional params> )
? ? ? ? db.users.aggregate( [pipeline], <optional params> ) - performs an aggregation on a collection; returns a cursor
? ? ? ? db.users.remove(query)
? ? ? ? db.users.renameCollection( newName , <dropTarget> ) renames the collection.
? ? ? ? db.users.runCommand( name , <options> ) runs a db command with the given name where the first param is the collection name
? ? ? ? db.users.save(obj)
? ? ? ? db.users.stats()
? ? ? ? db.users.storageSize() - includes free space allocated to this collection
? ? ? ? db.users.totalIndexSize() - size in bytes of all the indexes
? ? ? ? db.users.totalSize() - storage allocated for all data and indexes
? ? ? ? db.users.update(query, object[, upsert_bool, multi_bool]) - instead of two flags, you can pass an object with fields: upsert, multi
? ? ? ? db.users.validate( <full> ) - SLOW
? ? ? ? db.users.getShardVersion() - only for use with sharding
? ? ? ? db.users.getShardDistribution() - prints statistics about data distribution in the cluster
? ? ? ? db.users.getSplitKeysForChunks( <maxChunkSize> ) - calculates split points over all chunks and returns splitter function
? ? ? ? db.users.getWriteConcern() - returns the write concern used for any operations on this collection, inherited from server/db if set
? ? ? ? db.users.setWriteConcern( <write concern doc> ) - sets the write concern for writes to the collection
? ? ? ? db.users.unsetWriteConcern( <write concern doc> ) - unsets the write concern for writes to the collection
> db.users.save({name:"liuhailong",hometown:"Hebei"});
WriteResult({ "nInserted" : 1 })
> show dbs;
admin ?(empty)
cgdc ? 0.078GB
local ?0.078GB
> db.cgdc.users.find()
> ;
> cgdc.users.find()
2014-05-17T03:14:37.528-0700 ReferenceError: cgdc is not defined
> db.users.find()
{ "_id" : ObjectId("5377366b70e0a3f0e2e28f91"), "name" : "liuhailong", "hometown" : "Hebei" }
>?
HOHO?
安全和權限:
use [databasename]
db.addUser(username,pwd,isReadOnly);
> use admin
switched to db admin
> db.addUser("gd","cgdc");
WARNING: The 'addUser' shell helper is DEPRECATED. Please use 'createUser' instead
Successfully added user: { "user" : "gd", "roles" : [ "root" ] }
> use cgdc
switched to db cgdc
> db.createUser("cgdc","cgdc");
2014-05-17T03:17:58.058-0700 Error: couldn't add user: no such cmd: 0 at src/mongo/shell/db.js:1004
> db.addUser("cgdc","cgdc");
WARNING: The 'addUser' shell helper is DEPRECATED. Please use 'createUser' instead
Successfully added user: { "user" : "cgdc", "roles" : [ "dbOwner" ] }
> db.createUser().help()
2014-05-17T03:19:16.032-0700 TypeError: Cannot read property 'user' of undefined at src/mongo/shell/db.js:979
> db.createUser
function (userObj, writeConcern) {
? ? var commandExisted = this._createUser(userObj, writeConcern);
? ? if (!commandExisted) {
? ? ? ? throw Error("'createUser' command not found. ?This is most likely because you are " +
? ? ? ? ? ? ? ? ? ? "talking to an old (pre v2.6) MongoDB server");
? ? }
}
> db.addUser("cgdc_readonly","cgdc",true);
WARNING: The 'addUser' shell helper is DEPRECATED. Please use 'createUser' instead
Successfully added user: { "user" : "cgdc_readonly", "roles" : [ "read" ] }
>?
--fork 運行,一定要指定logpath --
啟動服務器時,加 --auth 選項。
gd@ubuntu:~$ sudo /usr/bin/mongo?
MongoDB shell version: 2.6.1
connecting to: test
Error while trying to show server startup warnings: not authorized on admin to execute command { getLog: "startupWarnings" }
> show dbs
2014-05-17T03:24:14.308-0700 listDatabases failed:{
? ? ? ? "ok" : 0,
? ? ? ? "errmsg" : "not authorized on admin to execute command { listDatabases: 1.0 }",
? ? ? ? "code" : 13
} at src/mongo/shell/mongo.js:47
>?
登錄操作:
db.auth(username,pwd)
登錄不成功,去掉--auth選項重新連。
> db.system.users.find()
{ "_id" : "admin.gd", "user" : "gd", "db" : "admin", "credentials" : { "MONGODB-CR" : "d41a08d27097c5b67823dfdd85e9494d" }, "roles" : [ { "role" : "root", "db" : "admin" } ] }
{ "_id" : "cgdc.cgdc", "user" : "cgdc", "db" : "cgdc", "credentials" : { "MONGODB-CR" : "714ab4fd93e3012475de54246a444516" }, "roles" : [ { "role" : "dbOwner", "db" : "cgdc" } ] }
{ "_id" : "cgdc.cgdc_readonly", "user" : "cgdc_readonly", "db" : "cgdc", "credentials" : { "MONGODB-CR" : "c1a9ebbe08d3d0a27a33bd0435ee86f1" }, "roles" : [ { "role" : "read", "db" : "cgdc" } ] }
>?
刪掉重來:
> db.system.users.remove();
2014-05-17T03:33:45.736-0700 remove needs a query at src/mongo/shell/collection.js:299
> db.system.users.remove({});
WriteResult({ "nRemoved" : 3 })
> db.system.users.find();
>?
刪除用戶:
db.system.users.remove({"user":username});
這次語法是這樣:
use admin
db.createUser( {
user: "admin",
pwd: "cgdc",
roles: [ {?
role: "userAdminAnyDatabase",?
db: "admin"?
} ]
}
);
db.createUser( {
user: "root",
pwd: "cgdc",
roles: [ { role: "root", db: "admin" } ]
}
);
use cgdc
db.createUser({
user: "cgdc",
pwd: "cgdc",
roles:[{
role: "dbOwner",
db: "cgdc"
}]
}
);
真難用!!
試試看
gd@ubuntu:~$ sudo /usr/bin/mongo -u admin -p
MongoDB shell version: 2.6.1
Enter password:?
connecting to: test
2014-05-17T03:43:55.825-0700 Error: 18 { ok: 0.0, errmsg: "auth failed", code: 18 } at src/mongo/shell/db.js:1210
exception: login failed
gd@ubuntu:~$ sudo /usr/bin/mongo -u cgdc -p
MongoDB shell version: 2.6.1
Enter password:?
connecting to: test
2014-05-17T03:44:10.909-0700 Error: 18 { ok: 0.0, errmsg: "auth failed", code: 18 } at src/mongo/shell/db.js:1210
exception: login failed
gd@ubuntu:~$ sudo /usr/bin/mongo
MongoDB shell version: 2.6.1
connecting to: test
Error while trying to show server startup warnings: not authorized on admin to execute command { getLog: "startupWarnings" }
> db.auth("cgdc","cgdc");
Error: 18 { ok: 0.0, errmsg: "auth failed", code: 18 }
0
>?
還是不行。
看看日志。
switched to db cgdc
> db.auth("cgdc","cgdc");
1
>?
遇到問題多看日志!!!
其他安全考慮:
--bindip localhost
--noscripting (禁止服務器端JavaScript執(zhí)行)
--nohttpinterface
傳輸加密(SSH隧道)
先到這里吧。
裝一個虛機,ubuntu吧,14.04 Trusty Tahr。
安裝MongoDB
網(wǎng)絡資源:
http://mirrors.aliyun.com/ubuntu-releases/14.04/ubuntu-14.04-server-amd64.iso.torrent(公網(wǎng))
http://mirrors.tuna.tsinghua.edu.cn/ubuntu-releases/14.04/ubuntu-14.04-server-amd64.iso.torrent(教育網(wǎng))
http://fastdl.mongodb.org/linux/mongodb-linux-x86_64-2.6.1.tgz
虛擬機:
處理器數(shù)量1:內(nèi)核數(shù):1(多核對MongoDB幫助不大);MM 1G(RAM瓶頸應該會出現(xiàn));HD 20G。?
安裝Unbuntu,用戶名:gd,密碼:cgdc。
TO小超超,服務器在1.4.3x,一起玩兒吧。
安裝指導:
http://docs.mongodb.org/manual/tutorial/install-mongodb-on-ubuntu/由于Ubuntu官方APT源里的MongoDB版本比較舊,這里用MongoDB提供的APT源進行安裝。
要使用第三方源上的軟件,除了在sources.list上添加對應的源外,還要有增加相應的公鑰Key才能正確安裝軟件。
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10
echo 'deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen' | sudo tee /etc/apt/sources.list.d/mongodb.list
sudo apt-get update
sudo apt-get install mongodb-org
報錯:unable to locate package
sudo apt-get install mongodb就可以,可能版本會老一點,無所謂吧。
要更新的包包括:libboost、libc、libgcc、mongodb、mongodb-clients、mongodb-dev、mongodb-server。
一共38個,下載41.5M,90多K,速度還好,可以先去看會兒書了。
下載到一半,斷了。
F!
棄用apt。
sudo apt-get install openssh-server
這個很快。
用“netstat -tlp”或“ps -e | grep ssh”確認sshd啟動。
http://www.cnblogs.com/chen1987lei/archive/2010/12/02/1894768.html
解壓后得到一堆二進制可執(zhí)行文件,除了bsondump其他都是mongo打頭的。拷貝到 /usr/bin 下面。
chmod a+x?
到此,裝好。
/*
如果安裝特定版本:
apt-get install mongodb-org=2.6.1 mongodb-org-server=2.6.1 mongodb-org-shell=2.6.1 mongodb-org-mongos=2.6.1 mongodb-org-tools=2.6.1
禁用自動升級:
echo "mongodb-org hold" | sudo dpkg --set-selections
echo "mongodb-org-server hold" | sudo dpkg --set-selections
echo "mongodb-org-shell hold" | sudo dpkg --set-selections
echo "mongodb-org-mongos hold" | sudo dpkg --set-selections
echo "mongodb-org-tools hold" | sudo dpkg --set-selections
運行:
sudo /etc/init.d/mongod start
sudo /etc/init.d/mongod stop
sudo /etc/init.d/mongod restart
*/
啟動,報錯,說dbpath不存在,需要手動創(chuàng)建目錄。
創(chuàng)建了 /data/mongodb1 目錄。
啟動的時候加--dbpath。
啟動沒有報錯,但忘了 --fork方式啟動了。
還好,可以ssh連。
報錯:sda1:WRITE SAME faild.Manually zeroing.沒有修改配置,后來沒見過這個問題了。為什么呢?
數(shù)據(jù)目錄沒開寫權限?sudo啟動服務器都不行?
use admin
db.shutdownServer()
啟動config:
啟動選項很多,每次在命令行敲麻煩死了,寫在config文件里吧。
選項包括:
--port
--fork(以守護進程方式執(zhí)行)
--logpath
--config
配置文件寫法:
port = 110271
fork = true
logpath = mongodb.log
無論如何,進入Shell了。
敲help,先爽一下吧。> help? ? ? ? db.help() ? ? ? ? ? ? ? ? ? ?help on db methods
? ? ? ? db.mycoll.help() ? ? ? ? ? ? help on collection methods
? ? ? ? sh.help() ? ? ? ? ? ? ? ? ? ?sharding helpers
? ? ? ? rs.help() ? ? ? ? ? ? ? ? ? ?replica set helpers
? ? ? ? help admin ? ? ? ? ? ? ? ? ? administrative help
? ? ? ? help connect ? ? ? ? ? ? ? ? connecting to a db help
? ? ? ? help keys ? ? ? ? ? ? ? ? ? ?key shortcuts
? ? ? ? help misc ? ? ? ? ? ? ? ? ? ?misc things to know
? ? ? ? help mr ? ? ? ? ? ? ? ? ? ? ?mapreduce
? ? ? ? show dbs ? ? ? ? ? ? ? ? ? ? show database names
? ? ? ? show collections ? ? ? ? ? ? show collections in current database
? ? ? ? show users ? ? ? ? ? ? ? ? ? show users in current database
? ? ? ? show profile ? ? ? ? ? ? ? ? show most recent system.profile entries with time >= 1ms
? ? ? ? show logs ? ? ? ? ? ? ? ? ? ?show the accessible logger names
? ? ? ? show log [name] ? ? ? ? ? ? ?prints out the last segment of log in memory, 'global' is default
? ? ? ? use <db_name> ? ? ? ? ? ? ? ?set current database
? ? ? ? db.foo.find() ? ? ? ? ? ? ? ?list objects in collection foo
? ? ? ? db.foo.find( { a : 1 } ) ? ? list objects in foo where a == 1
? ? ? ? it ? ? ? ? ? ? ? ? ? ? ? ? ? result of the last line evaluated; use to further iterate
? ? ? ? DBQuery.shellBatchSize = x ? set default number of items to display on shell
? ? ? ? exit ? ? ? ? ? ? ? ? ? ? ? ? quit the mongo shell
如果是方法,敲上括號是調用,不敲括號,輸出方法原型。
客戶端Shell:
運行./mongo既可以啟動Shell對mongodb進行管理。
Shell使用Javascipt語法,可以對數(shù)據(jù)進行管理,可以對數(shù)據(jù)庫進行管理。
官網(wǎng)版本:http://try.mongodb.org/。 可以上手玩玩。
很簡單,沒什么可說的。
創(chuàng)建個數(shù)據(jù)庫吧:
>?
> use cgdc
switched to db cgdc
> show collections
> show dbs
admin ?(empty)
cgdc ? (empty)
local ?0.078GB
> db.users.help()
DBCollection help
? ? ? ? db.users.find().help() - show DBCursor help
? ? ? ? db.users.count()
? ? ? ? db.users.copyTo(newColl) - duplicates collection by copying all documents to newColl; no indexes are copied.
? ? ? ? db.users.convertToCapped(maxBytes) - calls {convertToCapped:'users', size:maxBytes}} command
? ? ? ? db.users.dataSize()
? ? ? ? db.users.distinct( key ) - e.g. db.users.distinct( 'x' )
? ? ? ? db.users.drop() drop the collection
? ? ? ? db.users.dropIndex(index) - e.g. db.users.dropIndex( "indexName" ) or db.users.dropIndex( { "indexKey" : 1 } )
? ? ? ? db.users.dropIndexes()
? ? ? ? db.users.ensureIndex(keypattern[,options]) - options is an object with these possible fields: name, unique, dropDups
? ? ? ? db.users.reIndex()
? ? ? ? db.users.find([query],[fields]) - query is an optional query filter. fields is optional set of fields to return.
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? e.g. db.users.find( {x:77} , {name:1, x:1} )
? ? ? ? db.users.find(...).count()
? ? ? ? db.users.find(...).limit(n)
? ? ? ? db.users.find(...).skip(n)
? ? ? ? db.users.find(...).sort(...)
? ? ? ? db.users.findOne([query])
? ? ? ? db.users.findAndModify( { update : ... , remove : bool [, query: {}, sort: {}, 'new': false] } )
? ? ? ? db.users.getDB() get DB object associated with collection
? ? ? ? db.users.getPlanCache() get query plan cache associated with collection
? ? ? ? db.users.getIndexes()
? ? ? ? db.users.group( { key : ..., initial: ..., reduce : ...[, cond: ...] } )
? ? ? ? db.users.insert(obj)
? ? ? ? db.users.mapReduce( mapFunction , reduceFunction , <optional params> )
? ? ? ? db.users.aggregate( [pipeline], <optional params> ) - performs an aggregation on a collection; returns a cursor
? ? ? ? db.users.remove(query)
? ? ? ? db.users.renameCollection( newName , <dropTarget> ) renames the collection.
? ? ? ? db.users.runCommand( name , <options> ) runs a db command with the given name where the first param is the collection name
? ? ? ? db.users.save(obj)
? ? ? ? db.users.stats()
? ? ? ? db.users.storageSize() - includes free space allocated to this collection
? ? ? ? db.users.totalIndexSize() - size in bytes of all the indexes
? ? ? ? db.users.totalSize() - storage allocated for all data and indexes
? ? ? ? db.users.update(query, object[, upsert_bool, multi_bool]) - instead of two flags, you can pass an object with fields: upsert, multi
? ? ? ? db.users.validate( <full> ) - SLOW
? ? ? ? db.users.getShardVersion() - only for use with sharding
? ? ? ? db.users.getShardDistribution() - prints statistics about data distribution in the cluster
? ? ? ? db.users.getSplitKeysForChunks( <maxChunkSize> ) - calculates split points over all chunks and returns splitter function
? ? ? ? db.users.getWriteConcern() - returns the write concern used for any operations on this collection, inherited from server/db if set
? ? ? ? db.users.setWriteConcern( <write concern doc> ) - sets the write concern for writes to the collection
? ? ? ? db.users.unsetWriteConcern( <write concern doc> ) - unsets the write concern for writes to the collection
> db.users.save({name:"liuhailong",hometown:"Hebei"});
WriteResult({ "nInserted" : 1 })
> show dbs;
admin ?(empty)
cgdc ? 0.078GB
local ?0.078GB
> db.cgdc.users.find()
> ;
> cgdc.users.find()
2014-05-17T03:14:37.528-0700 ReferenceError: cgdc is not defined
> db.users.find()
{ "_id" : ObjectId("5377366b70e0a3f0e2e28f91"), "name" : "liuhailong", "hometown" : "Hebei" }
>?
HOHO?
安全和權限:
use [databasename]
db.addUser(username,pwd,isReadOnly);
> use admin
switched to db admin
> db.addUser("gd","cgdc");
WARNING: The 'addUser' shell helper is DEPRECATED. Please use 'createUser' instead
Successfully added user: { "user" : "gd", "roles" : [ "root" ] }
> use cgdc
switched to db cgdc
> db.createUser("cgdc","cgdc");
2014-05-17T03:17:58.058-0700 Error: couldn't add user: no such cmd: 0 at src/mongo/shell/db.js:1004
> db.addUser("cgdc","cgdc");
WARNING: The 'addUser' shell helper is DEPRECATED. Please use 'createUser' instead
Successfully added user: { "user" : "cgdc", "roles" : [ "dbOwner" ] }
> db.createUser().help()
2014-05-17T03:19:16.032-0700 TypeError: Cannot read property 'user' of undefined at src/mongo/shell/db.js:979
> db.createUser
function (userObj, writeConcern) {
? ? var commandExisted = this._createUser(userObj, writeConcern);
? ? if (!commandExisted) {
? ? ? ? throw Error("'createUser' command not found. ?This is most likely because you are " +
? ? ? ? ? ? ? ? ? ? "talking to an old (pre v2.6) MongoDB server");
? ? }
}
> db.addUser("cgdc_readonly","cgdc",true);
WARNING: The 'addUser' shell helper is DEPRECATED. Please use 'createUser' instead
Successfully added user: { "user" : "cgdc_readonly", "roles" : [ "read" ] }
>?
--fork 運行,一定要指定logpath --
啟動服務器時,加 --auth 選項。
gd@ubuntu:~$ sudo /usr/bin/mongo?
MongoDB shell version: 2.6.1
connecting to: test
Error while trying to show server startup warnings: not authorized on admin to execute command { getLog: "startupWarnings" }
> show dbs
2014-05-17T03:24:14.308-0700 listDatabases failed:{
? ? ? ? "ok" : 0,
? ? ? ? "errmsg" : "not authorized on admin to execute command { listDatabases: 1.0 }",
? ? ? ? "code" : 13
} at src/mongo/shell/mongo.js:47
>?
登錄操作:
db.auth(username,pwd)
登錄不成功,去掉--auth選項重新連。
> db.system.users.find()
{ "_id" : "admin.gd", "user" : "gd", "db" : "admin", "credentials" : { "MONGODB-CR" : "d41a08d27097c5b67823dfdd85e9494d" }, "roles" : [ { "role" : "root", "db" : "admin" } ] }
{ "_id" : "cgdc.cgdc", "user" : "cgdc", "db" : "cgdc", "credentials" : { "MONGODB-CR" : "714ab4fd93e3012475de54246a444516" }, "roles" : [ { "role" : "dbOwner", "db" : "cgdc" } ] }
{ "_id" : "cgdc.cgdc_readonly", "user" : "cgdc_readonly", "db" : "cgdc", "credentials" : { "MONGODB-CR" : "c1a9ebbe08d3d0a27a33bd0435ee86f1" }, "roles" : [ { "role" : "read", "db" : "cgdc" } ] }
>?
刪掉重來:
> db.system.users.remove();
2014-05-17T03:33:45.736-0700 remove needs a query at src/mongo/shell/collection.js:299
> db.system.users.remove({});
WriteResult({ "nRemoved" : 3 })
> db.system.users.find();
>?
刪除用戶:
db.system.users.remove({"user":username});
這次語法是這樣:
use admin
db.createUser( {
user: "admin",
pwd: "cgdc",
roles: [ {?
role: "userAdminAnyDatabase",?
db: "admin"?
} ]
}
);
db.createUser( {
user: "root",
pwd: "cgdc",
roles: [ { role: "root", db: "admin" } ]
}
);
use cgdc
db.createUser({
user: "cgdc",
pwd: "cgdc",
roles:[{
role: "dbOwner",
db: "cgdc"
}]
}
);
真難用!!
試試看
gd@ubuntu:~$ sudo /usr/bin/mongo -u admin -p
MongoDB shell version: 2.6.1
Enter password:?
connecting to: test
2014-05-17T03:43:55.825-0700 Error: 18 { ok: 0.0, errmsg: "auth failed", code: 18 } at src/mongo/shell/db.js:1210
exception: login failed
gd@ubuntu:~$ sudo /usr/bin/mongo -u cgdc -p
MongoDB shell version: 2.6.1
Enter password:?
connecting to: test
2014-05-17T03:44:10.909-0700 Error: 18 { ok: 0.0, errmsg: "auth failed", code: 18 } at src/mongo/shell/db.js:1210
exception: login failed
gd@ubuntu:~$ sudo /usr/bin/mongo
MongoDB shell version: 2.6.1
connecting to: test
Error while trying to show server startup warnings: not authorized on admin to execute command { getLog: "startupWarnings" }
> db.auth("cgdc","cgdc");
Error: 18 { ok: 0.0, errmsg: "auth failed", code: 18 }
0
>?
還是不行。
看看日志。
暈死,日志里說,test數(shù)據(jù)庫不是你想連,想連就能練。?當然啊,沒有給test加用戶。
連cgdc庫
> use cgdcswitched to db cgdc
> db.auth("cgdc","cgdc");
1
>?
遇到問題多看日志!!!
其他安全考慮:
--bindip localhost
--noscripting (禁止服務器端JavaScript執(zhí)行)
--nohttpinterface
傳輸加密(SSH隧道)
先到這里吧。
轉載于:https://www.cnblogs.com/liuhailong2008/p/4055268.html
超強干貨來襲 云風專訪:近40年碼齡,通宵達旦的技術人生總結
以上是生活随笔為你收集整理的【MongoDB】2、安装MongoDB 2.6.1 on Unbuntu 14.04(学习流水账)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: IIS认证
- 下一篇: 关于iframe的contentDocu