If you are trying to connect Mongo DB Server and it insanely throwing following error.
com.mongodb.MongoTimeoutException: Timed out after 1000 ms while waiting for a server that matches ReadPreferenceServerSelector{readPreference=primary}. Client view of cluster state is {type=UNKNOWN, servers=[{address=192.168.1.10:27010, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSecurityException: Exception authenticating MongoCredential{mechanism=null, userName='user123', source='admin', password=<hidden>, mechanismProperties={}}}, caused by {com.mongodb.MongoCommandException: Command failed with error 18: 'Authentication failed.' on server 192.168.1.10:27010. The full response is { "ok" : 0.0, "code" : 18, "errmsg" : "Authentication failed." }}}]
If you start looking the error content First you encounter with Timeout Exception which may mislead you. It is basically an authentication error.
In mongo db at the time of creation db authentication does not affect it create connections anyway. When you start any command it will throw exception if authentication failed [As you can see in debug mode of your IDE, the exception does not throw in connection creation line but in line of a command execution].
Solution:
if you are using following uri.
mongodb://user_name:password@<server ip>:<port>/socketTimeoutMS=1000
then append one more parameter
mongodb://user_name:password@<server ip>:<port>/socketTimeoutMS=1000&authSource=db_name
because may user_name and password is for only a single db.
Sample data for a uri:
user_name = user123
password = pass!123
server = 192.168.1.10
prort = 27017
db name = my_db
The following uri shoud be used for the above critera.
mongodb://user123:pass!123@192.168.1.10:27017/socketTimeoutMS=1000&authSource=my_db
To get connected from terminal use following command:
~$:mongo -u user123 --authenticationDatabase my_db -p pass!123 192.168.1.100:27017/my_db
Yes! you have to use db_name twice.
com.mongodb.MongoTimeoutException: Timed out after 1000 ms while waiting for a server that matches ReadPreferenceServerSelector{readPreference=primary}. Client view of cluster state is {type=UNKNOWN, servers=[{address=192.168.1.10:27010, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSecurityException: Exception authenticating MongoCredential{mechanism=null, userName='user123', source='admin', password=<hidden>, mechanismProperties={}}}, caused by {com.mongodb.MongoCommandException: Command failed with error 18: 'Authentication failed.' on server 192.168.1.10:27010. The full response is { "ok" : 0.0, "code" : 18, "errmsg" : "Authentication failed." }}}]
If you start looking the error content First you encounter with Timeout Exception which may mislead you. It is basically an authentication error.
In mongo db at the time of creation db authentication does not affect it create connections anyway. When you start any command it will throw exception if authentication failed [As you can see in debug mode of your IDE, the exception does not throw in connection creation line but in line of a command execution].
Solution:
if you are using following uri.
mongodb://user_name:password@<server ip>:<port>/socketTimeoutMS=1000
then append one more parameter
mongodb://user_name:password@<server ip>:<port>/socketTimeoutMS=1000&authSource=db_name
because may user_name and password is for only a single db.
Sample data for a uri:
user_name = user123
password = pass!123
server = 192.168.1.10
prort = 27017
db name = my_db
The following uri shoud be used for the above critera.
mongodb://user123:pass!123@192.168.1.10:27017/socketTimeoutMS=1000&authSource=my_db
To get connected from terminal use following command:
~$:mongo -u user123 --authenticationDatabase my_db -p pass!123 192.168.1.100:27017/my_db
Yes! you have to use db_name twice.
Heya¡my very first comment on your site. ,I have been reading your blog for a while and thought I would completely pop in and drop a friendly note. . It is great stuff indeed. I also wanted to ask..is there a way to subscribe to your site via email?
ReplyDeleteMongoDB Training in Chennai
Hi Yash, You are most most welcome at blog and thank you for giving the idea to add option to subscribe. I added the a box just above the Archive, You can put eamil id and submit.
DeleteThis comment has been removed by the author.
ReplyDeleteThanks for sharing the good post.,
ReplyDeleteMachine Learning training in Pallikranai Chennai
Pytorch training in Pallikaranai chennai
Data science training in Pallikaranai
Python Training in Pallikaranai chennai
Deep learning with Pytorch training in Pallikaranai chennai
Bigdata training in Pallikaranai chennai
Mongodb training in Pallikaranai chennai
Thanks for the useful blog, Very interesting.
ReplyDeleteMachine Learning training in Pallikranai Chennai
Data science training in Pallikaranai
Python Training in Pallikaranai chennai
Bigdata training in Pallikaranai chennai