1. Create a connected user
> MONGO Ip:port
> Use test
< Span style= "font-family: Microsoft Ya-hei, song body, Arial; font-size:15px; line-height:24px; White-space:pre "> > db.adduser (" root "," 123456 ")
>db.auth ("root", "123456") (Login verification)
2. Java connection to MongoDB driver ,:https://github.com/mongodb/mongo-java-driver/downloads.
3. Test code
Import Java.net.unknownhostexception;import Com.mongodb.db;import Com.mongodb.mongo;import Com.mongodb.mongoexception;public class Util {/** * @param args * @throws mongoexception * @throws Unknownhostexcepti On */public static void Main (string[] args) {Mongo m;try {m = new Mongo ("192.168.86.1", 27027);//<span style= "Font -family:arial; " >192.168.86.1 for ip,27027mongodb port </span>db DB = M.getdb ("test"); if (db.authenticate ("root", "123456". ToCharArray ())) { System.out.println ("success");; } Else System.out.println ("false");} catch (Unknownhostexception e) {//Todo auto-generated catch Blocke.printstacktrace ();} catch (Mongoexception e) {//TODO Auto-generated catch Blocke.printstacktrace ();}}}
Java Connection MongoDB