Connecting to MongoDb DB that is installed in different server - python

We have to servers. I have installed MongoDB on one of the servers (UBUNTU - Digital Ocean VPS).
When I run a script to retrieve data from the same server using a localhost, I can do that perfectly.
import pymongo
//SERVER = 'mongodb://localhost:27017/myproject'
SERVER = 'mongodb://root:password#x.x.x.x:27017/myproject' where x.x.x.x is the address of my server
connection=pymongo.MongoClient(SERVER)
db = connection.myproject
print list(db.coll.find())
The problem is thqt I can't connect to this DB. Note that I can ssh and run the script using localhost inside the server; but not the case out of the server.
Do I need to go through some configuration:

You must allow remote access
vi /etc/mongod.conf
Listen only local interface.
bind_ip = 127.0.0.1
you must add the IP of your other servers. For Example:
Listen local interface and 192.168.0.100.
bind_ip = 127.0.0.1, 192.168.0.100
Comment out to listen on all interfaces
Nota: Comma Separated
I hope to help

For development purposes you can open an ssh tunnel like
ssh <UBUNTU - Digital Ocean VPS> -L27018:localhost:27017
and then connect to the remote db as
SERVER = 'mongodb://root:password#localhost:27018/myproject'
while ssh connection remains open. You can use any free port instead of 27018.
Otherwise you need to reconfigure mongodb to listen to all interfaces. Comment out bindIp line in mongodb config and restart the server. This will make the DB publicly accessible, so make sure you use strong passwords and don't allow anonymous access.
Finally, if you are using VPN, you need to uncomment bindIp line in the mongodb config, and add VPN interface there, e.g.:
bindIp = 127.0.0.1,10.0.1.12
where 10.0.1.12 should be replaced with vpn interface of your ubuntu box. You can find exact value with ifconfig. Important: there are no spaces around coma.

Related

Redash - Change Postgres connection port

Is it possible to change the port Redash connects to Postgres?
I had initially set-up Redash successfully and connected to Postgres, but after a few days, it was impossible to start Postgres on port 5432. I even tried reinstalling but it forcefully sets the port to 5433. I have tried to change the port to 5433 on redash/query_runner/pg.py but there is no change.
How can I change the port flask listens to, to 5433 or any other for that matter?
The file 'query_runner/pg.py' is designed to connnect remote database like mysql.py, oracle.py and so on. It's not the backend database saving users' infomation. So you should try to change the value like
SQLALCHEMY_DATABASE_URI = os.environ.get(
"REDASH_DATABASE_URL", os.environ.get("DATABASE_URL", "postgresql:///postgres"))
It is in 'redash/settings/__init__.py', but I am sorry that I don't know the detail about how to change it.

Why my tcp client skip over the proxy and connect to server directly

I write a reverse proxy accroding to this https://gist.github.com/voorloopnul/415cb75a3e4f766dc590#file-proxy-py.
I need this to overwrite the authentication infomation from client side. Like following.
Client(passA) ---> Proxy(overwrite passA into passB) ---> Server(passB)
Where passB is the correct password and passA is random number.
The algorithms is SCRAMSHA256, a little bit complex but I manage to do this.
Eventhing works well when the proxy and the server is not on the same machine.
I have tried to deploy the proxy on both windows and linux. The proxy uses 'ip address' to point to Server
While, when the proxy using 'localhost' to point the Server, it is broken, that the authentication cannot be passed with one certain client(for which I made the proxy). But with the other clients, it also works well.
Shouldn't this be encapsulation and transparent to user?
Why the localhost so special and how can I fix this?
Update the latest research
The authenication failed because the client connect to the server directly, so the password is not modified by my proxy.
Condition 1: Proxy on another machine. The proxy works.
Client(192.168.1.1) ==> Proxy(192.168.1.3:8000) ==> Server(192.168.1.2:6000)
-
Condition 2: Proxy on the same machine as the Server.
The proxy listen 0.0.0.0:8000 and forward packets to localhost:6000.
Client(192.168.1.1) ==> Proxy(192.168.1.2:8000) ==> Server(192.168.1.2:6000)
After the first connection, the rest connection becomes
Client(192.168.1.1) =====> Server(192.168.1.2:6000) without proxy.
That makes the proxy not work anymore.
Why the client will skip it in condition 2?

Connecting to IB TWS using IBpy

I downloaded the standalone TWX for Mac OS X. Then I also installed IBpy through pip. I have the TWX open and I ran the following lines:
from ib.opt import ibConnection
con = ibConnection()
print(con.connect())
However, it prints False. What am I doing wrong? In TWX I have the localhost IP 127.0.0.1 as a trusted address.
You also have to check "enable activex and socket clients" in the API settings.
There may be a bug in newer versions 950-952 where you don't specify 127.0.0.1, but instead check the box where it says "allow connections from localhost only". I haven't tested that, but have read about it.
If you use IB gateway, "enable socket clients" isn't required as the only way to use it is with an API. Note that gateway is port 4001 by default. Use con = ibConnection(port=4001,clientId=123)
You're connecting with clientId 0, you could use con = ibConnection(123) for example to use a different clientId.
Don't forget to call con.disconnect() to close the connection or the server won't allow you to re-connect using that id.
add: here's the bug I read about at IB-API yahoo user group.
But as soon as I uncheck ‘Allow connections from localhost only’, it
won’t accept connections from any address, not even local ones (ie
even with 127.0.0.1 as a Trusted IP address).

Pymongo connection timeout from remote machine

I have a Bitnami MEAN Stack running on AWS EC2. I'm trying to connect from a remote machine using PyMongo.
from pymongo import MongoClient
conn = MongoClient('mongodb://username:password#ec2blah.us-east-1.compute.amazonaws.com:27017/dbname')
but I keep getting an error along the lines of pymongo.errors.ConnectionFailure: timed out
I have edited /opt/bitnami/mongodb/mongodb.conf to supposedly allow external connections by commenting out bind_ip = 127.0.0.1 and uncommented bind_ip = 0.0.0.0 and all permutations of commenting/uncommenting those lines.
I've looked over the web for about 90 minutes now trying different things but without luck!
On the mongoDB server, do the port connection test, and make sure the DB service running well. If not, start the service.
telnet ec2blah.us-east-1.compute.amazonaws.com 27017
On the remote machine, do the port connection test, to make sure there is no firewall issue.
telnet ec2blah.us-east-1.compute.amazonaws.com 27017
If you have issue to connect, you need check security groups on this instance.
Click the ec2 instance name --> Description --> view rules, you should see the ports are opened
If not, create a new security group , such as `mongoDB`, tcp port 27017 should be opened for inbound traffic, then assign to that instance.
You should be fine to connect it now.
At the time of start-up of MongoDB, set the bind_ip argument to ::,0.0.0.0
mongod --bind_ip ::,0.0.0.0
Read more in the docs of MongoDB: IP Binding.

Remote tcp connection in python with zeromq

I have a python client that needs to talk to a remote server I manage. They communicate using zeromq. When I tested the client/server locally everything worked. But now I have the client and server deployed on the cloud, each using a different provider. My question is, what's the simplest way (that is safe) to make the connection? I'm assuming I can't pass the password over, and even if I could I'm guessing there are safer alternatives.
I know how to set an ssh connection without a password using ssh-keygen. Would that work? Would the client need to make an ssh connection with the server before sending the tcp req? If there's a python library that helps with this it'd be a big help.
Thanks!
Update:
So more than 24 hours passed and no one replied/answered. I think I'm getting closer to solve this, but not quite there yet. I added my client's key to .ssh/authorized_key on the server, and now I can ssh from the client to the server without a password. Next, I followed this post about "Tunneling PyZMQ Connections with SSH". Here's what I have in my client code:
1 context = zmq.Context()
2 socket = context.socket(zmq.REQ)
3 socket.connect("tcp://localhost:5555")
4 ssh.tunnel_connection(socket, "tcp://locahost:5555", "myuser#remote-server-ip:5555")
5 socket.send_string(some_string)
6 reply = socket.recv()
This doesn't work. I don't really understand lines 3 & 4 and I assume I do something wrong there. Also, my server (hosted on linode) has a "Default Gateway" IP and a "Public IP" -- in the tunnel connection I only specify the public ip, which is also the ip I use to ssh to the machine.
Indeed, ZMQ way is - tunnelling connection with the SSH. Your example is exactly what needs to be done, except that one should either use connect or tunnel_connection, not both.
Also, when specifying server to connect to, make sure to define the SSH port, not the ZMQ REP socket port. That is, instead of myuser#remote-server-ip:5555 you might try myuser#remote-server-ip or myuser#remote-server-ip:22.
import zmq
import zmq.ssh
context = zmq.Context()
socket = context.socket(zmq.REQ)
zmq.ssh.tunnel_connection(socket, "tcp://locahost:5555", "myuser#remote-server-ip")
socket.send(b"Hello")
reply = socket.recv()
Finally, make sure you've installed either pexpect or paramiko - they will do the tunnelling actually. Note that if you're using Windows, paramiko is the only solution which will work - pexpect openssh tunnelling won't work on Windows.
If you use paramiko instead of pexpect, make sure to set paramiko=True in the tunnel_connection arguments.
I have found ssh in Python to be iffy at best, even with paramiko and fabric libraries, so to debug, you might try setting up a tunnel separately, just to see if that's the issue with the broken connection.
For example:
ssh myuser#remote-server-ip -L 5050:localhost:5555 -N
This says: connect to myuser#remote-server-ip, and whenever I request a connection to localhost:5050 on my machine, forward it across the ssh connection so that the server at remote-server-ip thinks it's receiving a connection from localhost:5555.
-L constructs the tunnel, and -N means don't do anything else on the connection.
With that running in another shell, e.g., a different Terminal window, on your local development machine, try to connect to a zeromq server at localhost:5050, which will actually be the zeromq running on the remote server.
You could use 5555:localhost:5555 in the ssh command above, but I find that can be confusing and often conflicts with a local copy of the same service.

Categories