"MongoDB" 2, install MongoDB 2.6.1 on Unbuntu 14.04 (learn chronological)

Source: Internet
Author: User
Tags auth deprecated install mongodb mongodb mongodb server mongodb version ssh mongo shell
Plan:
Install a virtual machine, Ubuntu bar, 14.04 trusty Tahr.
Install 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 machine:
Number of processors 1: Kernel number: 1 (more check MongoDB help is small); MM 1G (Ram bottleneck should appear); HD 20G.
Install Unbuntu, username: gd, password: CGDC.
To small super super, server in 1.4.3x, play it together.

Installation Instructions:http://docs.mongodb.org/manual/tutorial/install-mongodb-on-ubuntu/

Because the MongoDB version of the Ubuntu official apt source is older, it is installed using 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, 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 can, 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.
Altogether 38, downloads 41.5m,90 many k, the speed is OK, may go to read a while first the book.
Download to half, broken.
F.
Discard Apt.
sudo apt-get install Openssh-server
This is fast.
With "NETSTAT-TLP" or "ps-e | grep SSH confirms that the sshd is started.
Http://www.cnblogs.com/chen1987lei/archive/2010/12/02/1894768.html
After decompression, you get a bunch of binary executables, except Bsondump, which are all MONGO. Copy to/usr/bin below.
chmod a+x
Here, put it right.

/*
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 upgrades:
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, you need to create a directory manually.
created the/DATA/MONGODB1 directory.
When you start, add--dbpath.

Start without an error, but forget the--fork way to start.
Fortunately, you can ssh connection.

error: Sda1:write SAME faild. Manually zeroing. Did not modify the configuration, and then did not see this problem. Why, then?
The data directory does not have write permission. sudo boot servers are not working.


Kill [PID]
Use admin
Db.shutdownserver ()

Start config:
Startup options A lot, every time on the command line to knock trouble dead, write in config file.
Options include:
--port
--fork (performed in daemon mode)
--logpath
--config
Configuration file:
Port = 110271
Fork = True
LogPath = Mongodb.log

Anyway, get into the shell.
Knock help, let's get some fun.

> 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 the current database
Show the users show the users in the current database
Show Profiles 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 is 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 the method, the knock bracket is called, without knocking the parentheses, outputting the method prototype.


Client Shell:
Run./mongo can start the shell to manage MongoDB.
The shell uses Javascipt syntax to manage the data and to manage the database.
Official edition: http://try.mongodb.org/. Can play.
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 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])-The options are an object with these possible fields:name, unique, dropdups
Db.users.reIndex ()
Db.users.find ([Query],[fields])-The query is a optional query filter. Fields is optional the 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 a 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 "the" is the Collectio N Name
Db.users.save (obj)
Db.users.stats ()
Db.users.storageSize ()-Includes free spaces allocated to this collection
Db.users.totalIndexSize ()-size in bytes of all 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 a object with Fields:upse RT, Multi
Db.users.validate (<full>)-Slow
Db.users.getShardVersion ()-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. 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. 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 for you are "+
"Talking to a old (pre v2.6) MongoDB server");
}
}
> Db.adduser ("cgdc_readonly", "CGDC", true);
Warning:the ' AddUser ' shell helper is deprecated. Use ' CreateUser ' instead
Successfully added User: {"user": "Cgdc_readonly", "Roles": ["read"]}
>

--fork run, be sure to specify LogPath--

When starting the server, add the--auth option.


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 in 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 Action:
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 ();
>

Delete 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

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 in 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 log.

Faint death, the log says, test database is not you want to even, want to even can practice. Of course, there is no user for test.

Even CGDC library > use CGDC
switched to db CGDC
> Db.auth ("CGDC", "CGDC");
1


has a problem. Read more log ...




Other security considerations:
--bindip localhost
--noscripting (disable server-side JavaScript execution)
--nohttpinterface Transfer encryption (SSH tunneling)


come here first.



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.