Tag Archives: MongoDB

Robo3T Remote Connect MongoDB Error: Failed to refresh ‘Collections‘. Error: ListCollections failed

First, set up the remote connection of Mongo server:

vim  /usr/local/mongodb/etc/mongodb.conf

Note out:

bindIp: 127.0.0.1  # Listen to local interface only, comment to listen on all interfaces.

The binding IP is 127.0.0.1 by default. Only local connections are allowed

bind_ip = 0.0.0.0

Restart Mongo remote connection:

service mongod restart

Robo3T reports error connecting to remote MongoDB: Failed to refresh ‘Collections’. Error: ListCollections failed:
Possible version issue.
Robo 3T 1.4 -> MongoDB 4.2
Robo 3T 1.3 -> MongoDB 4.0
Robo 3T 1.1 -> MongoDB 3.4
Robo 3T 0.9 -> MongoDB 3.2
Robo 3T 0.8.x -> MongoDB 2.4.0
Download the corresponding version.
https://github.com/Studio3T/robomongo/releases/tag/v1.3.1

Mongodb std::exception::what(): basic_filebuf::underflow error reading the file: iostream error

Error Message:
2021-08-30T21:37:47.238+0800 F – [main] terminate() called. An exception is active; attempting to gather more information
2021-08-30T21:37:47.258+0800 F – [main] std::exception::what(): basic_filebuf::underflow error reading the file: iostream error
Actual exception type: std::__ios_failure

Solution:
Check whether the keyfile file path is correctly configured

[Solved] Mongo Error: cant post the change to mongodb there is transaction error

Mongo database
Edit, add data error:
can post the change to mongodb there is transaction error

submit transaction error:

add set error in query:

prompt: not master

Reason:
the slave node used in Mongo master-slave only has read permission by default, and the slave node connected by itself
solution:
setting in Mongo allows the slave node to be writable, or directly connects to the IP address of the master node

[Solved] org.mongodb.driver.cluster – Exception in monitor thread while connecting to server node1:27017

spring   An error was reported when the boot project started to connect mongodb.

2021-06-09 17:10:34,663 INFO  org.mongodb.driver.cluster - Exception in monitor thread while connecting to server node1:27017
com.mongodb.MongoSocketException: node1
	at com.mongodb.ServerAddress.getSocketAddresses(ServerAddress.java:211)
	at com.mongodb.internal.connection.SocketStream.initializeSocket(SocketStream.java:75)
	at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:65)
	at com.mongodb.internal.connection.InternalStreamConnection.open(InternalStreamConnection.java:128)
	at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:117)
	at java.lang.Thread.run(Thread.java:745)
Caused by: java.net.UnknownHostException: node1
	at java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method)
	at java.net.InetAddress$2.lookupAllHostAddr(InetAddress.java:928)
	at java.net.InetAddress.getAddressesFromNameService(InetAddress.java:1323)
	at java.net.InetAddress.getAllByName0(InetAddress.java:1276)
	at java.net.InetAddress.getAllByName(InetAddress.java:1192)
	at java.net.InetAddress.getAllByName(InetAddress.java:1126)
	at com.mongodb.ServerAddress.getSocketAddresses(ServerAddress.java:203)
	... 5 common frames omitted

Because it’s a new project, Mongo library belongs to other projects. This is just a test application. So the first doubt is that the project version driver is not compatible with mongodb library. After replacing other versions of mongodb library, it is OK.

Detailed explanation

View the

<dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-mongodb</artifactId>
        <version>2.1.3.RELEASE</version>
    </dependency>

Because of spring   Mongodb driver is also the official driver of encapsulated mongodb. When you enter the spring data mongodb project, you can see that the official version of mongodb driver is 3.8.2

Then go to the official website of mongodb to see that version 3.8 supports version 4.0 database at most. I started testing with the 4.2 library (Official website https://docs.mongodb.com/drivers/java/)

So that’s it.

Mongoose Error: e11000 duplicate key error collection, code: 11000

Problem description:

MongoError: E11000 duplicate key error collection: test.elementusers index: username_1 dup key: { username: "admin" }
    at Function.create (/Users/liangrumeng/Documents/HBuilderProjects/workspace/myapp/node_modules/mongodb/lib/core/error.js:44:12)
    at toError (/Users/liangrumeng/Documents/HBuilderProjects/workspace/myapp/node_modules/mongodb/lib/utils.js:150:22)
    at coll.s.topology.insert (/Users/liangrumeng/Documents/HBuilderProjects/workspace/myapp/node_modules/mongodb/lib/operations/common_functions.js:266:39)
    at handler (/Users/liangrumeng/Documents/HBuilderProjects/workspace/myapp/node_modules/mongodb/lib/core/sdam/topology.js:1000:24)
    at wireProtocol.(anonymous function) (/Users/liangrumeng/Documents/HBuilderProjects/workspace/myapp/node_modules/mongodb/lib/core/sdam/server.js:457:5)
    at /Users/liangrumeng/Documents/HBuilderProjects/workspace/myapp/node_modules/mongodb/lib/core/connection/pool.js:408:18
    at process._tickCallback (internal/process/next_tick.js:61:11)
  driver: true,
  name: 'MongoError',
  index: 0,
  code: 11000,
  keyPattern: { username: 1 },
  keyValue: { username: 'admin' },
  errmsg:
   'E11000 duplicate key error collection: test.elementusers index: username_1 dup key: { username: "admin" }',
  [Symbol(mongoErrorContextSymbol)]: {} 

MongoError: E11000 duplicate key error collection: test.elementusers index: username_1 dup key: {username: “admin”}
at Function. Create
driver: true,
name: ‘MongoError’,
index: 0,
code: 11000,
keyPattern: {username: 1},
keyValue: {username: ‘admin’},
errmsg:
‘E11000 duplicate key error collection: Test.elementusers index: username_1 dup key: {username: “admin”}’,
[Symbol(mongoErrorContextSymbol)]: {}
The reason:
Because unique:true is set in the Schema, the registered user name must be unique.

We can make a corresponding judgment in ERR.

if(err.code == '11000'){
    res.json({
        msg:'This username has already been registered, please change your username',
        success:false
    })
}

Mongodb java version 3.X, prompt “XXX (an encryption algorithm) is not available” when there is a user name and password

First of all, describe the project environment: Maven + mongodb-java-driver-3.2.2

The original project was written with Mongo 2. X driver, and then upgraded to 3. X. The first big change is user name and password verification.

This is the way to get mongoclient.


import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;

/**
 * MongoDBUtils
 * @author administration
 *
 */
public class DBUtil {

	/**
	 * Connect to mongoDB with username password (MongoCredential method)
	 * @param mongodbAddr
	 * @param databaseName
	 * @return
	 */
	public static MongoClient getMongoClientByCredent(String mongodbAddr, String databaseName){
		MongoClient mongoClient;
		Properties p = TDTFile.getProperAddr("db.properties");
		String user = p.getProperty("username");
		String pswd = p.getProperty("password");
		List<ServerAddress> serverAddrList = new ArrayList<ServerAddress>();
		ServerAddress serverAddress = new ServerAddress(mongodbAddr);
		serverAddrList.add(serverAddress);
		List<MongoCredential> credentialList = new ArrayList<MongoCredential>();
		MongoCredential credential = MongoCredential.createCredential(user, databaseName, pswd.toCharArray());
		credentialList.add(credential);
		mongoClient = new MongoClient(serverAddrList, credentialList);
		return mongoClient;
	}
	
	/**
	 * Connect to mongoDB with username password (URI method)
	 * @param mongodbAddr
	 * @param databaseName
	 * @return
	 */
	public static MongoClient getMongoClientByURI(String mongodbAddr, String databaseName){
		MongoClient mongoClient;
		Properties p = TDTFile.getProperAddr("db.properties");
		String user = p.getProperty("username");
		String pswd = p.getProperty("password");
		//System.out.println(user + "," + pswd);
		String uri = String.format("mongodb://%s:%s@%s/%s", user, pswd, mongodbAddr, databaseName);
		System.out.println(uri);
		MongoClientURI mongoClientURI = new MongoClientURI(uri);
		mongoClient = new MongoClient(mongoClientURI);
		return mongoClient;
	}
	
}

Two ways are no problem, run well in eclipse. But!! After packaging with MVN package, it can’t be used. After a night and a day’s analysis, I locked the root cause of the error in the statement that the command line executed the command. My sentence is like this:

java -Djava.ext.dirs=../lib Other parameters

The problem lies in the – D parameter. Baidu knows that – D is equivalent to setting external environment variables. Since my main method relies on many third-party jar packages, it seems that there is nothing wrong with it. But it ignores a problem, that is

After using the – D parameter to specify other directories, Java needs to load% Java_ The jar package in the home% \ JRE/lib/ext directory is no longer loaded!! I searched the usage of – D parameter on the Internet for a long time, but no one mentioned it.. But it’s hard for me. One day is wasted here..

 

Solution:

Package the required external dependent packages, including some packages such as JDK’s own encryption algorithm, into lib with Maven. Problem solving. By the way, the algorithm package required by mongodb3. X is% Java_ HOME%\jre\lib\ext\sunjce_ provider.jar

 

Mongodb — startup exception, error report ERROR:NUMBER 100

When doing sharding — replica set, I missed a port and wanted to restart one of mongod, but I always reported an error

 

 

[ root@centos mongodb]# mongod –port 28018 –shardsvr –replSet shard1 –dbpath=/usr/local/mongodb/shard1_ 3 –logpath=/usr/local/mongodb/shard1_ 3/shard1_ 3.log –logappend –fork
Fri Apr 11 13:24:48.136
Fri Apr 11 13:24:48.136 warning: 32-bit servers don’t have journaling enabled by default. Please use –journal if you want durability.
Fri Apr 11 13:24:48.136
about to fork child process, waiting until server is ready for connections.
all output going to: /usr/local/mongodb/shard1_ 3/shard1_ 3.log
forked process: 5281
ERROR: child process failed, exited with error number 100

I checked online and found that it seems that I used violence to shut down DB before, resulting in data locking.

You need to enter the database directory and delete it mongod.lock Then, restart. That’s it.

 

 

In addition, the correct way to close mongodb.

1.kill -2 PID

2.use admin;

  db.shutdownServer ()



More attention is paid to the official account “Python column”. The background reply to “Tencent architecture resource 1” gets the big data resources prepared by Tencent architects.

Error: could’t connect to server 127.0.0.1:27017 Src / Mongo / shell/ mongo.js

A. check whether the service is started first. If the service is not started, start the service first: Net stat mongodb

B. if the service has been started, it may be that Mongo was shut down abnormally last time, resulting in the file storing data being occupied and locked. Just add the mongod.lock The file is deleted. Restart the service net stat mongodb.

UnhandledPromiseRejectionWarning: MongoError: not authorized on elm to execute comman

Error in starting node project

UnhandledPromiseRejectionWarning: MongoError: not authorized on elm to execute command { insert: “hongbaos”, ordered: false, $db: “elm” }

The reason is that you do not have permission to connect to the database

The reason is that the database mongo.conf There is an auth = true solution

1. Comment out auth = true

2. Enter adimn to set the user name and password, and use the user name and password to access the database

Switch use admin before creating user

db.createUser(
	{
		user:"root",
		pwd:"123456",
		roles:[{role:"root",db:"admin"}]
	}
);