MongoDB Learning Three

Source: Internet
Author: User
Tags mongodb server

MongoDB-Connection

In this tutorial we will discuss different ways of connecting MongoDB.

Start MongoDB Service

In the previous tutorial, we have discussed how to start the MongoDB service, you only need to execute ' mongod ' in the bin directory of the MongoDB installation directory.

After the boot operation, MongoDB does not output any information after outputting some necessary information, then waits for the connection to be established, and when the connection is established, it will start printing the log information.

You can use the MongoDB shell to connect to the MongoDB server. You can also use PHP to connect to MongoDB. In this tutorial we'll use the MongoDB shell to connect to the MongoDB service, and then we'll show you how to connect to the MongoDB service via PHP.

Connecting MongoDB services through the shell

You can connect MongoDB services by executing the following command.

Note:localhost is the hostname and this option is required:

Mongodb://localhost

When you execute the above command, you can see the following output:

When you return to the window where you are running the ./mongod command, you can see where the server is connected to MongoDB, and you can see the following information:

...... Omit information ... 2015-09-25t17:22:27.336+0800 I control  [Initandlisten] allocator:tcmalloc2015-09-25t17:22:27.336+0800 I control  [Initandlisten] options: {storage: {dbPath: "/data/db"}}2015-09-25t17:22:27.350+0800 I NETWORK  [Initandlisten] Waiting for connections on port 270172015-09-25t17:22:36.012+0800 I NETWORK  [Initandlisten] Connection accepted from 127.0.0.1:37310 #1 (1 connection now OPEN)  # This line indicates a connection from this machine ... Omit information ...

MongoDB Connection Command format

To connect to the MONGODB server using the username and password, you must use the 'username:[email protected]/dbname' format, ' username ' for the user name, ' password ' for the password.

Log in to the default database using a user name and password connection:

$./mongomongodb Shell version:3.0.6connecting To:testmongodb://admin:[email protected]/

In the above command, the user admin uses password 123456 to connect to the local MongoDB service. The output results are as follows:<, p>

Log in to the specified database using the user name and password connection:

The format for connecting to the specified database is as follows:

Mongodb://admin:[email Protected]/test

More Connection Instances

Connect to the local database server, the port is the default.

Mongodb://localhost

Use username fred, password foobar login to localhost's admin database.

Mongodb://fred:[email protected]

Use user name Fred, password foobar log on to localhost's Baz database.

Mongodb://fred:[email Protected]/baz

Connect replica pair, server 1 is example1.com server 2 is example2.

mongodb://example1.com:27017,example2.com:27017

Connect replica set three servers (ports 27017, 27018, and 27019):

mongodb://localhost,localhost:27018,localhost:27019

Connect replica set three servers, write operations to the primary server, and distribute the query to the slave server.

Mongodb://host1,host2,host3/?slaveok=true

Connect directly to the first server, either replica set part or master server or slave server.

Mongodb://host1,host2,host3/?connect=direct;slaveok=true

When your connection server has a priority, you also need to list all the servers you can use to connect.

Safe Mode connection to localhost:

Mongodb://localhost/?safe=true

Connect to replica set in Safe mode, and wait for at least two replication servers to write successfully, and the time-out is set to 2 seconds.

mongodb://host1,host2,host3/?safe=true;w=2;wtimeoutms=2000

Parameter option description

Standard format:

Mongodb://[username:[email protected]]host1[:p ort1][,host2[:p Ort2],... [, hostn[:p Ortn]] [/[database][?options]]

The standard connection format contains several options, as follows:

Options Description
Replicaset=name Verify the name of the replica set. Impliesconnect=replicaset.
Slaveok=true|false
  • true: In Connect=direct mode, the driver will connect to the first machine, even if the server is not the primary one. In Connect=replicaset mode, the driver sends all write requests to the master and distributes the read operations to the other slave servers.
  • False: In Connect=direct mode, the driver will automatically find the primary server. In Connect=replicaset mode, the driver only connects to the primary server, and all read and write commands are connected to the primary server.
Safe=true|false
  • True: After the update operation, the driver sends the GetLastError command to ensure that the update succeeds. (also refer to WTIMEOUTMS).
False: After each update, the driver does not send GetLastError to ensure that the update succeeds.
W=n The driver adds {w:n} to the GetLastError command. Apply to Safe=true.
Wtimeoutms=ms The driver adds {wtimeout:ms} to the GetLastError command. Apply to Safe=true.
Fsync=true|false
  • True: The driver adds {fsync:true} to the GetLastError command. Applies to safe=true.
  • False: The driver is not added to the GetLastError command.
Journal=true|false If set to True, synchronizes to Journal (written to the entity before committing to the database). Apply to Safe=true
Connecttimeoutms=ms The time at which the connection can be opened.
Sockettimeoutms=ms Time to send and receive sockets.

MongoDB Learning Three

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.