mongodb installed in azure ubuntu cannot connect - mongodb

I am trying to connect to a mongodb database I installed on an Ubuntu VM on Microsoft Azure. I did the following:
Created virtual machine.
sudo apt-get mongodb (I connected to the VM with ssh).
Created an Endpoint on the Azure Management Portal with both public and private ports set to 27017.
When connected via ssh, running the mongo command allows me to view and access the data stored in the mongodb, but when done remotely, the connection fails with:
Sat Oct 11 13:34:08.378 JavaScript execution failed: Error: couldn't connect to server xxxxxx.cloudapp.net:27017 at src/mongo/shell/mongo.js:L114
I think I am missing something pretty basic here. Hopefully someone out there can help me?

It looks like mongo has some problems when connecting with a regular string. Might be related to this JIRA.
Try to connect with the following syntax: mongo -u <user> -p <password> hostIP:port/db

make sure you unbind the IP for connecting remotely to it by editing:
sudo vi /etc/mongod.conf
Set bind_ip=0.0.0.0
sudo service mongod restart
to test, use: `mongo SERVER-IP:27017/DBNAME -u DBUser -p DBPass

Related

Programmatically connect to remote MongoDB with SSH

I need to use terminal to connect to MongoDB. I have almost precisely same issue as this StackExchange question.
In my case I can correctly use Robo3T to connect. As well as use command
mongo --host 111.111.111.111 --port 111 --authenticationDatabase DB --username USER --password PASS locally. With same command executed remotely I receive following error:
No connection could be made because the target machine actively refused it.
I wanted to precisely recreate my Robo3T connection setup to see if SSH tunnel solves my issue

Postgres ODBC connection issue from Ubuntu VM

I am attempting to use a Zabbix server running on an Ubuntu virtual machine to monitor the Postgres database in our application running on the same host machine (not a VM). To be clear, I am trying to connect from a Linux Ubuntu virtual machine on my computer to Postgres also running not in a VM on the same computer. Zabbix makes use of ODBC, so a preliminary step in the process is to get the ODBC connection to Postgres working correctly. However, I am having a problem.
Steps I have taken:
installed unixODBC via sudo apt-get install unixodbc unixodbc-dev
installed unixODBC driver for Postgres via sudo apt-get install odbc-postgresql
configured odbc.ini to the following:
[test]
Description = test database
Driver = /usr/lib/x86_64-linux-gnu/odbc/psqlodbca.so
Setup = /usr/lib/x86_64-linux-gnu/odbc/libodbcpsqlS.so
Server = 192.168.240.1
User = postgres
Password =
Port = 5432
Database = mydb
Yet when I test the connection via:
isql test -v
I get the following error:
[08001][unixODBC]could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
More notes:
I can successfully connect to Postgres from the admin running on the local (non VM) machine
port 5432 has been completely opened from Windows Firewall on the local machine
telnet to 192.168.240.1 (the network IP of the local machine) on port 5432 succeeds
This all implies that the problem has to do with the ODBC configuration in the Ubuntu VM. I spent several hours searching and trying various things but to no avail. If I can get isql to work correctly, I should be in business, as Zabbix basically sits right on top of ODBC for its database monitoring functions.
Thanks in advance for your help.
I think your configuration options are a little off. Try this:
[test]
Driver = /usr/lib/x86_64-linux-gnu/odbc/psqlodbca.so
Setup = /usr/lib/x86_64-linux-gnu/odbc/libodbcpsqlS.so
Database = mydb
Servername = 192.168.240.1
UserName = postgres
Password =
Port = 5432
Protocol = 7.4
Using Servername instead of Server might be sufficient.
I'd recommend the following steps to getting ODBC and PostgreSQL to play together (ignoring the apt install steps, since you already did these):
sudo odbcinst -i -d -f /usr/share/psqlodbc/odbcinst.ini.template
sudo odbcinst -i -s -l -n test -f /usr/share/doc/odbc-postgresql/examples/odbc.ini.template
sudo nano /etc/odbc.ini
Here's what these do:
Sets up your odbcinst.ini file with the files in the right places.
Sets up your odbc.ini file (for the system).
Edits the system odbc.ini file you created in step 2, where you can replace options to match your needs.
If you do not want the odbc.ini file to be system-wide, you can also limit it to just the user if you call step #2 without the -l parameter. In that case, it'll create or modify a ~/.odbc.ini file, which you can edit for your needs.
The unixODBC folks seem to recommend using odbcinst for setting this stuff up, as it knows where to put the files. Unfortunately, to use it to great effect, you'd need to know where to find the drivers' template files for your driver. The paths I've provided here match the ones for the Ubuntu package.

Mongo "auth failed" Only for Remote Connections. Local Works fine

I have a Bitnami MEAN instance running on EC2. After much finagling, I've been able to successfully connect to the DB using the local shell. I created authenticated users with all of the permissions necessary to access the data, and when I run the below code -- I am able to access the DB with no problem.
sudo mongo admin -u <USERNAME-p <PASSWORD>
That said, when I try to repeat this using a remote connection I am repeatedly given an "auth failed" error from MongoDB.
mongo <HOST>:<PORT>/<DATABASE> -u <USERNAME> -p <PASSWORD>
...
This is strange because I am using the exact same credentials as I do in running the local shell. The only difference is I'm including the host and port information. I've since also confirmed that my remote connection DOES work if I disable the auth parameter in mongodb.config.
mongo <HOST>:<PORT>/<DATABASE>
Obviously, in production I want to be able to authenticate. Do any of you have suggestions as to why there is a discrepancy between remote and local authentication?
I was facing the same issue.
The problem for me:
My local mongo shell was v2.6.10. It uses an authentication method called MONGODB-CR that has been deprecated.
My server version is v3.0.4. It uses an authentication method called SCRAM-SHA-1.
Try to check your local shell and remote server versions with:
mongo --version
mongod --version
If they are different, upgrade your local shell to v3. (I had to uninstall and install it again.)
I had previously be installing MongoDB version 3.2.12 and was able to connect to a remote instance using:
mongo -u ‘<USERNAME>’ -p ‘<PASSWORD>’ --host <REPLICA_SET>/<HOST>:<PORT> admin
I am creating a new cluster with version 3.4.2 and was not able to connect with the same command. After trying many different options I was finally able to figure out that I needed to add --authenticationDatabase before the admin database.
mongo -u ‘<USERNAME>’ -p ‘<PASSWORD>’ --host <REPLICA_SET>/<HOST>:<PORT> --authenticationDatabase admin
If you're using more recent versions of MongoDB (server version 4.2.6 / shell version v3.6.9 in my case) you don't have to force them to match like in #Alexandre's example. For instance, if you're getting this error:
[thread1] Error: Authentication failed. :
DB.prototype._authOrThrow#src/mongo/shell/db.js:1608:20
You can connect with this syntax:
mongo --host mongodb://username:password#IP:PORT/ --authenticationDatabase admin
Install the same version both on the server and on the client solved the problem for me.
As #Alexandre explained above, it is probably a problem of password encryption.
MongoDB version 3.2.7
I tried successfully with the two methods:
mongo --host "your_host" --port "your_port" --username "your_user" --password "your_pass" --authenticationDatabase "your_admin_db"
mongo "your_host:your_port/your_db" --username "your_user" --password "your_pass" --authenticationDatabase "your_admin_db"
Besides, make sure that your server is available for remote accesses. See details about net.bindIp at https://docs.mongodb.com/v3.2/reference/configuration-options/
This is mainly due to security reasons.
When you have access to the local environment, it is easy to supposed that you are an administrator of the system or a developer because you have access to the machine itself.
If you don't have access to the local machine, you can't guarantee this, and since a database security is really important (in most cases), it makes sense not to enable remote access. You can, of course, disable this, but it is not recommended.
Hope I helped.
Just in case someone bumps into the same problem, the authenticationDatabase is only required if you created the user in ANOTHER database. If you create the user in the database you connect to, no problems.
So be careful : use then create user .
If you happen to create your user in the admin database then yes you need the authenticationDatabase flag.

Access MongoDB from other server

I have a MongoDB database on my Linux server. I want to access it from another server. I tried to make a connection from my local computer with the Robomongo. The connection is succesfull, but the authentication fails.
How can I get the authentication credentials? Or should I change something in MongoDB before I can acces the database from another server / pc?
Someone else have set up this database, and there is no possibility to ask him this questions.
I have found the solution by my self:
The File etc/mongod.conf has a line 'bind_ip'. In this line, you originally have to add the IP address which you want to access your database. But, it don't work! You should better comment this line.
But, you don't have any authentication now, so you have to add authentication. Here you have an tutorial about this: http://ghosttx.com/2012/03/how-to-connect-to-a-remote-mongodb-server-with-mongohub-for-mac/
When you have done that, you have to enable authentication. You can do this by editing etc/mongod.conf again, and uncomment the line 'Auth = true'.
Now you can connect with you Mongo Database ;)
Ive sorted it by adding ssh option to RoboMongo following this link:
http://www.mongovue.com/2011/08/04/mongovue-connection-to-remote-server-over-ssh/
Im on OSX and connecting to Ubuntu 14 / Mongo 2.6.7 on VPS and when Ive added my ssh details to the Robomongo all seem to work ok (Ive also changed the mongo config to remove the ip_bing and enabled port 27017)
If you do not like to bother with authentication and stuff just make an SSH Tunnel:
ssh -fN -l username -i .ssh/id_rsa -L 9999:localhost:27017 remote.com
Just connect to mongodb on localhost:9999 and it will establish a connection to your mongodb on port 27017 on your server at remote.com.
Run your mongodb with following command to access mongodb from other servers
mongod --port 10945 --bind_ip 0.0.0.0
I was not able to use Robomongo with MongoDB 3.0 too (connecting from a Windows machine to a Linux one, using SSH). The only tool that works for me is MongoChef (http://3t.io/mongochef/).

Unable to connect to mongolab host

I am trying to connect to mongolab from terminal via below command
mongo ds061158.mongolab.com:61158/order_it -u <dbuser> -p <dbpassword>
I am getting the below error.
MongoDB shell version: 2.6.3
connecting to: ds061158.mongolab.com:61158/order_it
2014-07-09T13:52:44.890+0530 Error: couldn't connect to server ds061158.mongolab.com:61158 (23.22.170.205), connection attempt failed at src/mongo/shell/mongo.js:148
exception: connect failed
What has to be done in this case?
Thanks in Advance.
It looks like your network is blocking access to that port. I'd recommend contacting your network administrator or trying from a different network.
To test your network connectivity alone (no credentials necessary) you can run this command. This example was run from my unprivileged laptop just now and demonstrates a successful test.
% mongo ds061158.mongolab.com:61158
MongoDB shell version: 2.6.1
connecting to: ds061158.mongolab.com:61158/test
rs-ds061158:PRIMARY> db.runCommand({ping:1});
{ "ok" : 1 }
rs-ds061158:PRIMARY> exit
bye
Our full connectivity troubleshooting guide is here: http://docs.mongolab.com/connecting/#help
Also, feel free to contact us as support#mongolab.com if you'd like us to dig into the specifics of your server or code. We're always happy to help!
Regards,
Jared
I know this question is old, but in case someone still faces a similar issue, this is what helped me:
sudo rm /var/lib/mongodb/mongod.lock
sudo service mongodb restart
You should try to explicitly specify the port you want to use with the --port option:
mongo ds061158.mongolab.com/order_it --port 61158 -u <dbuser> -p <dbpassword>
From the mongo man pages:
--port <port>
Specifies the port where the mongod or mongos instance is listening. Unless specified mongo connects to mongod instances on port 27017, which is the default mongod port.