http://blog.csdn.net/stationxp/article/details/26077439
Plan:
Install a virtual machine, Ubuntu bar, 14.04 trusty Tahr.
Installing MongoDB
Network resources:
Http://mirrors.aliyun.com/ubuntu-releases/14.04/ubuntu-14.04-server-amd64.iso.torrent (Public network)
Http://mirrors.tuna.tsinghua.edu.cn/ubuntu-releases/14.04/ubuntu-14.04-server-amd64.iso.torrent (Education Network)
Http://fastdl.mongodb.org/linux/mongodb-linux-x86_64-2.6.1.tgz
Virtual machines:
Number of Processors 1: Number of Cores: 1 (multi-check MongoDB help is not small), MM 1G (Ram bottleneck should appear); HD 20G.
Install Unbuntu, user name: GD, Password: CGDC.
To small ultra-super, server in 1.4.3x, play together.
Installation Instructions:
http://docs.mongodb.org/manual/tutorial/install-mongodb-on-ubuntu/
Since the version of MongoDB in the official Ubuntu apt source is older, it is installed with the APT source provided by MongoDB.
To use the software on a third-party source, in addition to adding the corresponding source on the sources.list, you have to add the corresponding public key key to properly install the software.
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
Error: Unable to locate package
sudo apt-get Install MongoDB, maybe the version will be older, it doesn't matter.
The packages to be updated include: Libboost, libc, LIBGCC, MongoDB, Mongodb-clients, Mongodb-dev, Mongodb-server.
A total of 38, download 41.5m,90 K, the speed is good, you can go to read a book.
Download to half, broken.
F!
Deprecated apt.
sudo apt-get install Openssh-server
This is fast.
With "NETSTAT-TLP" or "ps-e | grep ssh "confirms that sshd is booting.
Http://www.cnblogs.com/chen1987lei/archive/2010/12/02/1894768.html
After extracting a bunch of binary executable files, except bsondump others are MONGO. Copy to/usr/bin below.
chmod a+x
Here, put it in a good fit.
/*
If you install a specific version:
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
To disable automatic upgrade:
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
Run:
Sudo/etc/init.d/mongod start
Sudo/etc/init.d/mongod stop
Sudo/etc/init.d/mongod restart
*/
Start, error, say DBPath does not exist, need to create directory manually.
The/DATA/MONGODB1 directory was created.
When you start, add--dbpath.
Start without error, but forgot the--fork way to start up.
Fortunately, can ssh connect.
error: Sda1:write same faild. Manually zeroing. No configuration has been modified and I have not seen this problem since. Why is it?
Data Catalog does not have write permission? sudo starting the server is not working?
Kill [PID]
Use admin
Db.shutdownserver ()
Start config:
A lot of startup options, every time at the command line to knock the trouble to die, write in the config file it.
Options include:
--port
--fork (daemon-style execution)
--logpath
--config
Configuration file notation:
Port = 110271
Fork = True
LogPath = Mongodb.log
Anyway, into the shell.
Knock help, have a good first.
[Java]View Plaincopy
- > 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
If it is a method, knocking the parentheses is called, without opening the parentheses, the output method is prototyped.
Client Shell:
Run./mongo can both start the shell to manage MongoDB.
The shell uses the Javascipt syntax to manage the data and to manage the database.
Official website version: http://try.mongodb.org/. You can get started with the game.
It's simple, there's nothing to say.
Create a database:
>
> 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 is 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 a object with these possible fields:name, unique, dropdups
Db.users.reIndex ()
Db.users.find ([query],[fields])-query is a optional query filter. Fields are 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 Collectio N 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 the flags, you can pass a object with Fields:upse RT, Multi
Db.users.validate (<full>)-SLOW
Db.users.getShardVersion ()-only if 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 func tion
Db.users.getWriteConcern ()-Returns the write concern used for all operations on this collection, inherited from server/d b 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
Security and Permissions:
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 the most likely because "+
"Talking to a 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 run, be sure to specify LogPath--
When you start the server, add the--auth option.
[Email protected]:~$ Sudo/usr/bin/mongo
MongoDB Shell version:2.6.1
Connecting To:test
Error while trying-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 in Admin to execute command {listdatabases:1.0}",
"Code": 13
} at src/mongo/shell/mongo.js:47
>
Login Operation:
Db.auth (USERNAME,PWD)
Login unsuccessful, remove--auth option to reconnect.
> 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": "c1a9ebbe08d3d0a 27a33bd0435ee86f1 "}," Roles ": [{" Role ":" read "," DB ":" CGDC "}]}
>
Delete again:
> 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 ();
>
To delete a user:
Db.system.users.remove ({"User": username});
This syntax is this:
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"
}]
}
);
It's hard to use!!
Try
[Email protected]:~$ 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
[Email protected]:~$ 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
[Email protected]:~$ Sudo/usr/bin/mongo
MongoDB Shell version:2.6.1
Connecting To:test
Error while trying-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
>
It's still not working.
Look at the logs.
Dizzy dead, the log said, test database is not you want to even, want to even can practice. Of course, there is no user for test.
Lian CGDC Library
> use CGDC
switched to db CGDC
> Db.auth ("Cgdc", "CGDC");
1
>
encounters a problem read more logs!!!
Other security considerations:
--bindip localhost
--noscripting (disable server-side JavaScript execution)
--nohttpinterface
Transport encryption (SSH tunnel)
come here first.
MongoDB 2, installing MongoDB 2.6.1 on Unbuntu 14.04 (learning journal)