MongoDB installation Configuration

Source: Internet
Author: User
Tags failover mongodb download mongodb server

MongoDBIt is a high-performance, open-source, non-pattern document-type database developed using C ++. It is a popular nosql database, in many scenarios, it can be used to replace traditional relational databases or key/value storage methods.

MongoDB uses bson as the data storage and transmission format. bson is a JSON-like binary serialization document that supports nested objects and arrays. MongoDB is similar to MySQL. document corresponds to MySQL row and collection corresponds to MySQL table.

The MongoDB server can run on Linux, windows, or OS X. It supports 32-bit and 64-bit applications. The default port is 27017. It is recommended to run on a 64-bit platform because MongoDB supports a maximum file size of 2 GB when running in 32-Bit mode.


1. features and functions

1) Features

  • High performance, easy to deploy, easy to use, and easy to store data.
  • Data of the object type can be easily stored in a set storage mode.
  • Supports dynamic queries and full indexes, including internal objects.
  • Supports replication and fault recovery.
  • Use efficient binary data storage, including large objects (such as videos ).
  • Automatic fragment processing to support scalability at the cloud computing level
  • It supports drivers in Python, PHP, Ruby, Java, C, C #, JavaScript, Perl, and C ++ languages. The community also provides Erlang and.. net.
  • The file storage format is bson (a json extension), which can be accessed through the network.

2) Functions

  • Set-oriented storage: Suitable for storing objects and JSON data.
  • Dynamic query: Mongo supports multiple Query expressions. Query commands use JSON tags to easily query embedded objects and arrays in a document.
  • Complete indexing support: Includes embedded object and array of documents. The Query Optimizer of Mongo analyzes the query expression and generates an efficient query plan.
  • Query monitoring: Mongo provides a monitoring tool to analyze the performance of database operations.
  • Replication and automatic failover: The Mongo Database supports data replication between servers and master-slave mode and mutual replication between servers. The primary objective of replication is to provide redundancy and automatic failover.
  • Efficient traditional storage: supports binary data and large objects (such as photos or images)
  • Automatic sharding to support cloud-level Scalability: the automatic sharding function supports horizontal database clusters and allows you to dynamically add additional machines.

2. Applicable scenarios

  1. Website data: Mongo is ideal for real-time insertion, update, and query, as well as the replication and high scalability required for real-time website data storage.
  2. Cache: because of its high performance, Mongo is also suitable for serving as a cache layer for information infrastructure. After the system is restarted, the persistent cache layer established by Mongo can avoid data source overload at the lower layer.
  3. Large-sized and low-value data: traditional relational databases may be expensive to store some data. Previously, programmers often choose traditional files for storage.
  4. High scalability: Mongo is ideal for databases consisting of dozens or hundreds of servers. The Mongo roadmap contains built-in support for the mapreduce engine.
  5. For object and JSON Data Storage: Mongo's bson data format is very suitable for storing and querying document-based data.

3. Download and install

MongoDB download, latest mongodb-linux-x86_64-2.2.3.tgz

MongoDB installation:

Tar zxvf mongodb-linux-x86_64-2.2.3.tgz

CD mongodb-linux-x86_64-2.2.3

Sudo mkdir/opt/mongodb-2.2.3

Sudo./bin/mongod -- dbpath =/opt/mongodb-2.2.3/

After the installation is complete, enter http: // localhost: 27017/in the URL bar of the browser. If the following content appears, the installation is successful!

4. Disable startup

1) Start by default

Sudo./bin/mongod (the default file directory is/data/DB/. If no file is saved, create sudo mkdir-P/data/DB/. The default port is 27017)

2) Specify the directory to start

Sudo./bin/mongod -- dbpath =/opt/mongodb-2.2.3/(save the file directory as/opt/mongodb-2.2.3/, new if no)

3) Specify the port to start

Sudo./bin/mongod -- dbpath =/opt/mongodb-2.2.3/-P 27000 (Port changed to specified 27000), the startup output is as follows:

MongoDB starting: pid = 4222 Port = 27000 dbpath =/opt/mongodb-2.2.3/32-bit host = Ubuntu

4) Background startup command

Sudo./bin/mongod-shardsvr-replset shard1-Port = 27017-dbpath =/opt/mongodb-2.2.3/-logpath =/opt/mongodb-2.2.3/log-logappend-rest-Fork

5) Close the command in the background

$. Bin/mongod // log on to the client> Use admin // switch to administrator> dB. shutdownserver () // close MongoDB

Or

> DB. admincommand ({shutdown: 1, force: true })
> // Or
> DB. shutdownserver ({force: true })

> DB. admincommand (shutdown: 1, force: True, timeoutsec: 5) // timeout
> // Or
> DB. shutdownserver ({force: True, timeoutsec: 5 })

6) Start the client

Default startup:./bin/Mongo

Specify the port to start:./bin/Mongo -- Port 27000

7) backup and recovery

$ Sudo. /bin/mongodump-D foo-O/opt/mongodb-2.2.3/BK // back up to BK directory connected to: 127.0.0.1tue Feb 5 20:58:13 database: foo to/opt/mongodb-2.2.3/BK/footue Feb 5 20:58:13 Foo. foo_test to/opt/mongodb-2.2.3/BK/Foo/foo_test.bsontue Feb 5 20:58:13 1 objectstue Feb 5 20:58:13 metadata for foo. foo_test to/opt/mongodb-2.2.3/BK/Foo/foo_test.metadata.json $. /bin/mongorestore-D foo-C foo_test/opt/mongodb-2.2.3/BK/Foo/foo_test.bson // restore the foo_test set connected: 127.0.0.1tue Feb 5 20:58:20/opt/mongodb-2.2.3/BK/Foo/foo_test.bsontue Feb 5 20:58:20 going into namespace [Foo. foo_test] Tue Feb 5 20:58:20 warning: restoring to foo. foo_test without dropping. restored data will be inserted without raising errors; check your server logtue Feb 5 20:58:20 warning: Collection Foo. foo_test exists with different options than are in the metadata. JSON file and not using -- drop. options in the metadata file will be ignored.1 objects foundtue Feb 5 20:58:20 creating index: {key: {_ ID: 1}, NS: "foo. foo_test ", name:" _ id _"}

5. Command example

1) Start the server

Sudo./bin/mongod -- dbpath =/opt/mongodb-2.2.3/

2) Start the client

./Bin/Mongo

3) simple client commands

Help

Show Database: Show dBs;

Select Database: Use test;

Insert data: DB. Foo. Save ({HOMER: 2 });

Query: DB. Foo. Find ();

Result: {"_ id": objectid ("511078ad857b699e9eaaf516"), "Homer": 2}

Update: DB. foo. update ({"Homer": 2 },{ "Homer": 3}); // ({},{}) The first {} is the query condition, the second {} is the re-assignment.

Result: {"_ id": objectid ("511078ad857b699e9eaaf516"), "Homer": 3}

Delete: DB. Foo. Remove ({"Homer": 3 });

Composite insert:

> T = {"Homer": 2, "name": {"name1": "yang", "name2": "Gang "}}
{"Homer": 2, "name": {"name1": "yang", "name2": "Gang "}}
> DB. Foo. insert (t );
> DB. Foo. Find ();
{"_ Id": objectid ("5110d3e0af69ad089b234001"), "Homer": 2, "name": {"name1": "yang", "name2 ": "Gang "}}

Compound query:

> DB. Foo. Find (); // first view all records in the Set
{"_ Id": objectid ("5110ca1aaf69ad089b233fff"), "Homer": 222}
{"_ Id": objectid ("5110caffaf69ad089b234000"), "Homer": 222}
{"_ Id": objectid ("5110d3e0af69ad089b234001"), "Homer": 2, "name": {"name1": "yang", "name2 ": "Gang "}}
>
> DB. Foo. Find ({"Homer": 2}); // query records containing specific content
{"_ Id": objectid ("5110d3e0af69ad089b234001"), "Homer": 2, "name": {"name1": "yang", "name2 ": "Gang "}}

Complete MongoDB creation example:

> Use Test2 // Test2 the database does not exist or can be executed, but it is not created immediately. switched to DB Test2> show dbs/Test2 is created only when the insert operation is executed, it indicates that the local (empty) test0.0625gb> dB is not created immediately. // The database in use is displayed. Test2 does not exist but is in use. Test2> dB. createcollection ("t2_test") // create a collection {"OK": 1}> T = {"name": "Yanggang", "sex": "male ", "Lover": {"lover1": "Little Dog", "lover2": "fish" }}// initialize collection data {"name": "Yanggang ", "Sex": "male", "lover": {"lover1": "Little Dog", "lover2": "fish" }}> dB. t2_test.insert (t) // insert data. At this moment, the Test2 database> dB will be created. t2_test.find () // query {"_ id": objectid ("5110d685af69ad089b234002"), "name": "Yanggang", "sex": "male", "lover ": {"lover1": "Little Dog", "lover2": "fish" }}> show DBS // view the database. test2local (empty) test0.0625gbtest1_0625gb is added.

MongoDB deletion example:

> Show collections // display the collection system. indexest2_test> dB. t2_test.drop () // Delete the collection t2_testtrue> show collections // verify that the system is successfully deleted. indexes> dB. test2.remove ()> show DBS // display the database local (empty) test0.0625gbtest1_0625gb> dB // display the current database Test2> dB. dropdatabase () // delete database {"dropped": "Test2", "OK": 1}> show DBS // verify that the local (empty) test0.0625gb is successfully deleted

MongoDB Index

> DB. foo. find () {"_ id": objectid ("5110ca1aaf69ad089b233fff"), "Homer": 222} {"_ id": objectid ("5110caffaf69ad089b234000"), "Homer ": 222} {"_ id": objectid ("5110d3e0af69ad089b234001"), "Homer": 2, "name": {"name1": "yang", "name2 ": "Gang" }}> dB. foo. getindexes () // view the index (_ id is the default value and cannot be deleted or modified) [{"v": 1, "key": {"_ id": 1 }, "ns": "test. foo "," name ":" _ id _ "}]> dB. foo. ensureindex ({"Homer": 1}) // create an index> dB. foo. getindexes () // verify that the new index [{"v": 1, "key": {"_ id": 1}, "ns": "test. foo "," name ":" _ id _ "},{" v ": 1," key ": {" Homer ": 1}," ns ":" test. foo "," name ":" homer_1 "}]> dB. foo. dropindex ({"Homer": 1}) // Delete the index {"nindexeswas": 2, "OK": 1}> dB. foo. getindexes () // verify that the index [{"v": 1, "key": {"_ id": 1}, "ns": "test. foo "," name ":" _ id _ "}]

6. Application Example

> DB. foo_test.insert ({"Homer": 1})> for (VAR I = 1; I <= 5; I ++) dB. foo_test.save ({"Homer": 2, "score": I}) // array generation> dB. foo_test.find () {"_ id": objectid ("20171016cc0e0000db7d954ae7"), "Homer": 1 }{ "_ id": objectid ("timer"), "Homer ": 2, "score": 6 }{ "_ id": objectid ("2017103200e2d3a50c56b49aa"), "Homer": 2, "score": 1 }{ "_ id ": objectid ("2017103200e2d3a50c56b49ab"), "Homer": 2, "score": 2 }{ "_ id": objectid ("2017103200e2d3a50c56b49ac"), "Homer": 2, "score": 3 }{ "_ id": objectid ("2017103200e2d3a50c56b49ad"), "Homer": 2, "score": 4 }{ "_ id ": objectid ("2017103200e2d3a50c56b49ae"), "Homer": 2, "score": 5 }>> var cursor = dB. foo_test.find (); While (cursor. hasnext () printjson (cursor. next (); // traverse the Array {"_ id": objectid ("20171016cc0e0000db7d954ae7"), "Homer": 1 }{ "_ id ": objectid ("20171030f0e2d3a50c56b49a9"), "Homer": 2, "score": 6 }{ "_ id": objectid ("2017103200e2d3a50c56b49aa"), "Homer": 2, "score": 1 }{ "_ id": objectid ("2017103200e2d3a50c56b49ab"), "Homer": 2, "score": 2 }{ "_ id ": objectid ("2017103200e2d3a50c56b49ac"), "Homer": 2, "score": 3 }{ "_ id": objectid ("2017103200e2d3a50c56b49ad"), "Homer": 2, "score": 4 }{ "_ id": objectid ("2017103200e2d3a50c56b49ae"), "Homer": 2, "score": 5 }>> var cursor = dB. foo_test.find (); printjson (cursor [4]) // query (iteration) {"_ id": objectid ("2017103200e2d3a50c56b49ac"), "Homer": 2, "score ": 3}> var arr = dB. foo_test.find (). toarray (); arr [5] // query (array) {"_ id": objectid ("2017103200e2d3a50c56b49ad"), "Homer": 2, "score ": 4}> dB. foo_test.find (). limit (3) // query (first three) {"_ id": objectid ("20171016cc0e0000db7d954ae7"), "Homer": 1 }{ "_ id ": objectid ("20171030f0e2d3a50c56b49a9"), "Homer": 2, "score": 6 }{ "_ id": objectid ("2017103200e2d3a50c56b49aa"), "Homer": 2, "score": 1}

Reference recommendations:

MongoDB (official)

MongoDB Study Notes

Mongod parameter description

MongoDB Application

Usage of indexes in MongoDB

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.