利用java代码连接加密的linux上的mongoDB数据库

先将mongoDB现有用户全部删除:

[root@centos7 bin]# use admin;
[root@centos7 bin]# show tables;
[root@centos7 bin]# db.system.users.remove({});

先为mongoDB数据库创建一个超级用户:

[root@centos7 bin]# use admin;

先为mongoDB数据库创建一个超级用户:

[root@centos7 bin]# db.createUser({"user":"administrator","pwd":"welcome","roles":["root"]});

创建成功返回值:

Successfully added user: { "user" : "administrator", "roles" : [ "root" ] }

然后结束mongoDB服务,以加密的方式重新启动:

[root@centos7 bin]# ./mongod -auth

此时你发现你不用密码依然可以执行 ./mongo 命令连接.,但是当你操作数据库的时候会遇到阻碍:

2017-07-28T09:49:43.033+0800 E QUERY    [thread1] Error: listCollections failed: {
    "ok" : 0,
    "errmsg" : "not authorized on admin to execute command { listCollections: 1.0, filter: {} }",
    "code" : 13,
    "codeName" : "Unauthorized"
} :

两种方法可以在命令行里操作加密的数据库:
第一种,连接的时候附带上用户名和密码:

[root@centos7 bin]# mongo -u administrator -p welcome --authenticationDatabase "admin"

第二种,连接进去之后再验证用户名密码:

[root@centos7 bin]# use admin;
[root@centos7 bin]# db.auth("administrator","welcome");

下面是在java代码中连接:

//连接到MongoDB服务 如果是远程连接可以替换“localhost”为服务器所在IP地址
        //ServerAddress()两个参数分别为 服务器地址 和 端口
        ServerAddress serverAddress = new ServerAddress("10.168.10.41",27017);
        List<ServerAddress> addrs = new ArrayList<ServerAddress>();
        addrs.add(serverAddress);

        //MongoCredential.createScramSha1Credential()三个参数分别为 用户名 数据库名称 密码
        MongoCredential credential = MongoCredential.createScramSha1Credential("administrator", "admin", "welcome".toCharArray());
        List<MongoCredential> credentials = new ArrayList<MongoCredential>();
        credentials.add(credential);

        //通过连接认证获取MongoDB连接
        MongoClient mongoClient = new MongoClient(addrs,credentials);

        //连接到数据库
        MongoDatabase mongoDatabase = mongoClient.getDatabase("admin");

        Document buildInfo = mongoDatabase.runCommand(new Document("serverStatus", 1));
        System.out.println(buildInfo.get("host"));
发布了70 篇原创文章 · 获赞 26 · 访问量 26万+
展开阅读全文

springboot连接linuxmongodb超时

04-13

**com.mongodb.MongoSocketOpenException: Exception opening socket** _ at com.mongodb.connection.SocketStream.open(SocketStream.java:62) ~[mongodb-driver-core-3.6.3.jar:na] at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:126) ~[mongodb-driver-core-3.6.3.jar:na] at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:114) ~[mongodb-driver-core-3.6.3.jar:na] at java.lang.Thread.run(Thread.java:748) [na:1.8.0_161]_ **Caused by: java.net.SocketTimeoutException: connect timed out** _at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method) ~[na:1.8.0_161] at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85) ~[na:1.8.0_161] at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) ~[na:1.8.0_161] at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) ~[na:1.8.0_161] at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) ~[na:1.8.0_161] at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172) ~[na:1.8.0_161] at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[na:1.8.0_161] at java.net.Socket.connect(Socket.java:589) ~[na:1.8.0_161] at com.mongodb.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:59) ~[mongodb-driver-core-3.6.3.jar:na] at com.mongodb.connection.SocketStream.open(SocketStream.java:57) ~[mongodb-driver-core-3.6.3.jar:na] ... 3 common frames omitted _ **网页提示:** Whitelabel Error Page This application has no explicit mapping for /error, so you are seeing this as a fallback. Fri Apr 13 18:04:16 CST 2018 There was an unexpected error (type=Internal Server Error, status=500). Timed out after 30000 ms while waiting to connect. Client view of cluster state is {type=UNKNOWN, servers=[{address=39.106.34.232:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by {java.net.SocketTimeoutException: connect timed out}}]; nested exception is com.mongodb.MongoTimeoutException: Timed out after 30000 ms while waiting to connect. Client view of cluster state is {type=UNKNOWN, servers=[{address=39.106.34.232:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by {java.net.SocketTimeoutException: connect timed out}}] **linux 内容:** [root@root bin]# ps aux |grep mongodb root 3234 0.3 4.2 330032 43740 ? Sl 17:37 0:04 ./mongod -f mongodb.cnf root 3293 0.0 0.0 105368 880 pts/1 S+ 18:01 0:00 grep mongodb [root@root bin]# netstat -nalp | grep mongod tcp 0 0 0.0.0.0:27017 0.0.0.0:* LISTEN 3234/./mongod unix 2 [ ACC ] STREAM LISTENING 31464 3234/./mongod /tmp/mongodb-27017.sock [root@root bin]# 我连接的是阿里云的服务器.直接搭建的centos6.8 下的mongodb 问答

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览