Java Driver Remote Connection MongoDB

Source: Internet
Author: User
Tags mongoclient mongodb

MongoDB is not remotely connected by default, and after Linux installation you will find that its directory is extremely simple, not even a configuration file. The version of my MongoDB is 3.6, currently the newest. https://www.mongodb.com/mongodb-3.6

Baidu a bit to see is a configuration file: mongodb.conf. and checked the next directory. And then you can only create it yourself.

A description of the configuration file was found in the official documentation: https://docs.mongodb.com/manual/reference/configuration-options/

The document is very long:
Configuration File
File Format
Use the Configuration File
Core Options
systemlog Options
processmanagement Options
net Options
Security Options
Setparameter Option
storage Options
operationprofiling Options
Replication Options
sharding Options
Auditlog Options
SNMP Options
Text Search Options
mongos-only Options
Windows Service Options

I put the remote connection and log files related to the red, if only to modify the remote connection, these several key to see.

File format

Configuration file using Yaml format, should have seen, have not seen self-Baidu.

A simple configuration file:

Systemlog:   destination:file   #mongod或mongos应发送所有诊断日志信息的日志文件的路径, not standard output or syslog for host. MongoDB creates a log file on the specified path.   path: "/home/xxx/mongod.log"   #追加在文件后边   logappend:falsestorage:   #数据库文件存放位置   dbPath: "/home/ xxx/db "Processmanagement:   fork:truenet:   #绑定所有的ip地址: 0.0.0.0   bindip:0.0.0.0   Port: 27017security:   authorization:disabled

Put path and dbPath replace your mongod.log own path, where the console logs are stored.

Start

Starting with a configuration file

Mongod--config./mongod.conf?

Or

Mongod-f/etc/mongod.conf?

Java programs

A super-simple java Program

Import Com.mongodb.mongoclient;import Com.mongodb.client.mongodatabase;import com.mongodb.client.MongoCollection; Import Org.bson.document;public class Quicktour {mongoclient mongoclient;public void Connect () {mongoclient = new Mongoclient ("10.80.18.1"); Mongodatabase database = Mongoclient.getdatabase ("Firstdb"); Mongocollection<document> collection = Database.getcollection ("FirstCollection1");D ocument MyDoc = Collection.find (). First (); System.out.println (Mydoc.tojson ());} public static void Main (string[] args) {Quicktour quicktour = new Quicktour (); Quicktour.connect ();}}

Output:

January 22, 2018 8:15:35 pm Com.mongodb.diagnostics.logging.JULLogger Log
Info: Cluster created with settings {hosts=[10.80.18.1:27017], mode=single, Requiredclustertype=unknown, Serverselectiontimeout= ' 30000 ms ', maxwaitqueuesize=500}
January 22, 2018 8:15:36 pm Com.mongodb.diagnostics.logging.JULLogger Log
Info: No server chosen by readpreferenceserverselector{readpreference=primary} from cluster description Clusterdescription{type=unknown, Connectionmode=single, serverdescriptions=[serverdescription{address= 10.80.18.1:27017, Type=unknown, state=connecting}]}. Waiting for 30000 ms before timing out
January 22, 2018 8:15:36 pm Com.mongodb.diagnostics.logging.JULLogger Log
Info: Opened connection [Connectionid{localvalue:1, servervalue:1}] to 10.80.18.1:27017
January 22, 2018 8:15:36 pm Com.mongodb.diagnostics.logging.JULLogger Log
Info: Monitor thread successfully connected to server with description serverdescription{address=10.80.18.1:27017, type= STANDALONE, state=connected, Ok=true, Version=serverversion{versionlist=[3, 6, 2]}, Minwireversion=0, maxwireversion= 6, maxdocumentsize=16777216, roundtriptimenanos=758049}
January 22, 2018 8:15:36 pm Com.mongodb.diagnostics.logging.JULLogger Log
Info: Opened connection [Connectionid{localvalue:2, servervalue:2}] to 10.80.18.1:27017
{"_id": {"$oid": "5a631d80070db90c43a3477d"}, "X": 1.0}

Success!

Forwarding Annotated Source: http://www.cnblogs.com/jycboy/p/8331019.html

Java Driver Remote Connection 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.