Tag Archives: mongo

show dbs Error: [js] uncaught exception: Error: listDatabases failed

When mongo is built with replica set cluster.
If you log into one of the SECONDARY nodes and execute the command show dbs, you will get an error:

2022-08-16T15:20:00.606+0800 E  QUERY    [js] uncaught exception: Error: listDatabases failed:{
    "operationTime" : Timestamp(1660634399, 2),
    "ok" : 0,
    "errmsg" : "not master and slaveOk=false",
    "code" : 13435,
    "codeName" : "NotMasterNoSlaveOk",
    "$clusterTime" : {
        "clusterTime" : Timestamp(1660634399, 2),
        "signature" : {
            "hash" : BinData(0,"NPF356c5NKl0PqHLlmiQ9vey9e4="),
            "keyId" : NumberLong("7101680539345616897")
        }
    }
} :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
Mongo.prototype.getDBs/<@src/mongo/shell/mongo.js:147:19
Mongo.prototype.getDBs@src/mongo/shell/mongo.js:99:12
shellHelper.show@src/mongo/shell/utils.js:906:13
shellHelper@src/mongo/shell/utils.js:790:15
@(shellhelp2):1:1

 

This is because the SECONDARY node does not have the permission to execute the show dbs command.
In this case, there are two solutions
1. Login to the PRIMARY node
2. Use the cluster connection method
mongo –host  testmongo/ip:port,ip:port,ip:port -uusername -p’passwd’ –authenticationDatabase admin

[Solved] Failed to instantiate java.util.List using constructor NO_CONSTRUCTOR with arguments

Failed to instantiate java.util.List using constructor NO_CONSTRUCTOR with arguments

Report errors

MappingInstantiationException: Failed to instantiate java.util.List using constructor NO_CONSTRUCTOR with arguments 

Reason

Entity classes mapped by a set in Mongo Library

@Data
@NoArgsConstructor
@AllArgsConstructor
@Document(collection = "a")
public class A {
	
	private List<B> b; // this is list
	
	@Data
    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    public static class B {

        private String bb;
    }
}

A data format of set

{ 
    "_id" : ObjectId("62df884326d4311d9c80de8d"), 
    "b" : {
        "bb" : "test" //this is object
    }
}

Solution:

Modify entity classes or process problem data.

 

[Solved] mongo Startup Error: ERROR: child process failed, exited with error number 1

Because Mongo is abnormally closed, an error message of “error: child process failed, exited with error number 1” may appear when it is started again.

Step 1: delete the lock file

Find Mongo directory and delete mogod lock

Step 2: create a new log file

Find the log file directory of Mongo and delete or modify the original log file name
[because I don’t want to modify Mongo’s configuration file, I modified the file name and added a file with the same name as the original file. I can also take another name and modify Mongo configuration]

Step 3 try to restart

Generally, the restart is successful at this step.

If it still fails, take a look at Mongo’s log file
the errors reported in my log file are as follows:

2021-12-16T11:58:12.810+0800 I CONTROL  [main] ***** SERVER RESTARTED *****
2021-12-16T11:58:12.815+0800 I CONTROL  [main] Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'
2021-12-16T11:58:12.820+0800 I CONTROL  [main] ERROR: Cannot write pid file to /var/run/mongodb/mongod2.pid: No such file or directory

Then go to the prompt “/var/run” directory, create a new mongodb folder, and restart. Start successfully and solve the problem!

Mongo connection remote address error

The remote address is clearly configured, but it is still connected to the localhost

Error log:

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

com.mongodb.MongoSocketOpenException: Exception opening socket

terms of settlement:

1. Springboot startup class, remove these two configuration classes

@SpringBootApplication(exclude = {MongoAutoConfiguration.class, MongoDataAutoConfiguration.class})

2. Add mongoconfiguration.java configuration class, and then you can connect it. Next, enjoy mongotemplate

@Configuration
public class MongoConfiguration {
    @Value("${spring.data.mongodb.uri}")
    private String mongodbUri;

    @Value("${spring.data.mongodb.option.min-connection-per-host}")
    private Integer minConnectionPerHost;

    @Value("${spring.data.mongodb.option.max-connection-per-host}")
    private Integer maxConnectionPerHost;

    @Bean
    public MongoTemplate mongoTemplate() throws Exception {
        MongoClientOptions.Builder builder = new Builder();
        builder.minConnectionsPerHost(minConnectionPerHost);
        builder.connectionsPerHost(maxConnectionPerHost);
        final SimpleMongoDbFactory simpleMongoDbFactory = new SimpleMongoDbFactory(new MongoClientURI(mongodbUri, builder));
        MongoTemplate mongoTemplate = new MongoTemplate(simpleMongoDbFactory);
        return mongoTemplate;
    }
}