HTML tutorial
CSS3 tutorial
Bootstrap tutorial
JavaScript tutorial
JQuery tutorial
AngularJS tutorial
React tutorial
NodeJS tutorial
PHP tutorial
Python tutorial
Python3 tutorial
Django tutorial
Linux tutorial
Docker tutorial
Ruby tutorial
Java tutorial
C tutorial
C ++ tutorial
Perl tutorial
JSP tutorial
Lua tutorial
Scala tutorial
Go tutorial
ASP.NET tutorial
C # tutorial
In this tutorial we will discuss the different connectors for MongoDB.
In the previous tutorial, we have discussed how to start the MongoDB server service, you only need to perform 'mongod' can be in the bin directory MongoDB installation directory.
After the start operation, mongodb output after some necessary information will not output any information, then wait for the connection is established, when the connection is established, it will start printing log information.
You can use MongoDB MongoDB shell to connect to the server. You can also use PHP to connect to MongoDB. This tutorial we will use to connect to MongoDB shell Mongodb services later chapters we will explain how to connect to the MongoDB service via php.
You can connect MongoDB service by executing the following command.
Note: localhost is the host name, this option is required:
When you execute the above command, you can see the following output:
$ ./mongo MongoDB shell version: 3.0.6 connecting to: test > mongodb://localhostmongodb://localhost /en.
This time you check back run ./mongod command window, from where you can see a connection to MongoDB server, you can see the following information:
……省略信息…… 2015-09-25T17:22:27.336+0800 I CONTROL [initandlisten] allocator: tcmalloc 2015-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 27017 2015-09-25T17:22:36.012+0800 I NETWORK [initandlisten] connection accepted from 127.0.0.1:37310 #1 (1 connection now open) # 该行表明一个来自本机的连接 ……省略信息……
Using a user name and password to connect to the MongoDB server, you must use 'username: password @ hostname / dbname ' format, 'username' username, 'password' as a password.
User login name and password to connect to the default database:
$ ./mongo MongoDB shell version: 3.0.6 connecting to: test mongodb://admin:123456@localhost/
The above command, the user admin password 123456 to connect to the local MongoDB service. The output is as follows: <, p>
> mongodb://admin:123456@localhost/ /en.
User login name and password to connect to the specified database:
Connect to the specified database in the following format:
Connect to the local database server, port is the default.
User name fred, login password foobar localhost the admin database.
User name fred, login password foobar localhost baz's database.
Connection replica pair, server 1 example1.com server 2 example2.
Connecting replica set three servers (port 27017, 27018, and 27019):
Connecting replica set three servers, writes in the main application server and distributed query to the server.
Direct connection to the first server, whether it is part of the replica set or the main server or from the server.
When you connect to a server has priority, but also need to list all the servers, you can use the connection mode.
Safe Mode to connect to localhost:
Safe Mode is connected to the replica set, and wait at least two written successfully replicated servers, set the timeout to 2 seconds.
standard format:
Standard connection format contains several options (options), as follows:
Options | description |
---|---|
replicaSet = name | Verify that the name of the replica set. Impliesconnect = replicaSet. |
slaveOk = true | false |
|
safe = true | false |
|
w = n | Drive add {w: n} command applies to getLastError safe = true/en |
wtimeoutMS = ms | Drive Add {wtimeout: ms} command applies to getlasterror safe = true/en |
fsync = true | false |
|
journal = true | false | If set to true, synchronized to the journal (in written to the database before submitting to the entity). Applies to safe = true |
connectTimeoutMS = ms | You can open the connection time. |
socketTimeoutMS = ms | Send and receive sockets of time. |