How to connect mongodb database in python on a server ubuntu? - python

I have the IP address of my apache server, running on ubuntu 18.04. I have installed MongoDB on the server. I have a script in python to connect the database but it doesn't work. I have shh connection without authentication for the database
I have already tried SSHTunnelForwarder but unsuccessfully.
from sshtunnel import SSHTunnelForwarder
from pymongo import MongoClient
MONGO_HOST = 'MY_IP_ADDRESS'
server = SSHTunnelForwarder(
MONGO_HOST,
remote_bind_address=('127.0.0.1', 27017)
)
server.start()
client = MongoClient('127.0.0.1', server.local_bind_port)
db=client.myDatabaseName
I have also tried
client = MongoClient('mongodb://MY_IP_ADDRESS/')

Related

How to connect remote Python FTP server to local Python FTP client

I am using python FTP server and client program. My need is to run Python FTP server on a remote machine that is connected on the same network as my local machine. FTP client will run from local machine, I need to connect FTP server with my FTP client running on local machine.
Please help!
This is my ftpserver.py:
from pyftpdlib.servers import FTPServer
from pyftpdlib.authorizers import DummyAuthorizer
from pyftpdlib.handlers import FTPHandler
authorizer = DummyAuthorizer()
authorizer.add_user("lokesh", "123", "current_dir", perm="elradfmw")
authorizer.add_anonymous("curent_dir", perm="elradfmw")
handler = FTPHandler
handler.authorizer = authorizer
server=FTPServer(("localhost",8080),handler)
server.serve_forever()
This is my ftpclient.py that needs to connect with the above server:
from ftplib import FTP
ftp = FTP('')
host='localhost'
port=8080
ftp.connect(host,port)
ftp.login()
print(ftp.getwelcome())
print('Current Directory ',ftp.pwd())
ftp.dir()
ftp.quit()
When I test my server and client on same machine it worked. But when I run the same server on another machine and tried to connect with my client it gave me error:
error: [Errno 10061] No connection could be made because the target
machine actively refused it
If you run the client on another machine, you have to connect to the host of the server, not to "localhost":
host='<server_host>'
Run ipconfig on your Windows server machine and look for "IPv4 address".
Replace port = 1027 with port = 8080 in your ftpclient file.

Connecting to Amazon rds with Python

Using MySQL Workbench, I can connect mysql running on amazon rds. I supply SSH Hostname, SSH Username, SSH Password, SSH Key File, MySQL Hostname, MySQL Server Port, Username, and Password.
However, I have a python script with which I'd like to connect to this same database. I tried using ssh tunneling:
with SSHTunnelForwarder(
(SSH Hostname, 22),
ssh_password=SSH Password,
ssh_username=SSH Username,
ssh_pkey= SSH Key File path,
remote_bind_address=(MySQL Hostname, MySQL Port) as server:
conn = MySQLdb.connect(
host = MySQL Hostname,
port = MySQL Port,
user = Username,
passwd = Password)
When I run the script, it hangs for about a minute, then says
'Can't connect to MySQL server on ~MySQL Hostname~'
I am unsure about the remote_bind_address. Since I don't supply that info when successfully connecting from MySQLWorkbench, is there a way to connect from Python without specifying remote_bind_address?
Additionally, I tried using the scripting tool in MySQL Workbench. I am able to load my python script, but then an error occurs at one of my imports, specifically 'import module requests'. The error indicates there is no module named requests. However, this module has been installed for a while, and I have imported and successfully used it outside of MySQL Workbench.

PyMongo MongoClient SSH Connection

I'm trying to establish a connection with a MongoDB database via an SSH connection programmatically from python. I can create a MongoClient object to connect locally but I can't see how to establish an SSH connection for my MongoClient to use.
How would I do this?
First make sure you have no local MongoDB running on your machine, then ssh to the server where MongoDB is running:
ssh -L 27017:MYHOST:27017 MYUSER#MYHOST
Replace MYUSER and MYHOST with your username and host. Then, in another terminal window, run the "mongo" shell from your local computer. By default it connects to localhost:27017, which you've port-forwarded to the remote host. The "mongo" shell should connect correctly.
Then, create a PyMongo connection normally in Python:
>>> from pymongo import MongoClient
>>> c = MongoClient()
>>> c.test.command('buildinfo')
...
More info about SSH tunneling here:
https://help.ubuntu.com/community/SSH/OpenSSH/PortForwarding
This is not MongoDB-specific at all, any network protocol can be tunneled with SSH port-forwarding.

how to connect mongo read replica from python

I can connect with my mongo access with read preference on replica by following command:
mongo DBHOST:27017/db
db.auth('username','password')
rs.slaveOk()
I tried with python, but didnt worked.
Python
from pymongo import Connection
from pymongo import MongoClient
client = MongoClient('mongodb://username:password#DBHOST:27017/DB',readPreference='secondaryPreferred')
Any help?

Connection Error while connecting to PostgreSQL as postgres user?

I am not able connect to PostgreSQL remotely using python and psycopg2:
Here is my code.
>>> import psycopg2
>>> conn_string = "host='localhost' dbname='mydb' user='postgres'"
>>> print "Connecting to database\n ->%s" % (conn_string)
Connecting to database
->host='localhost' dbname='mydb' user='postgres'
>>> conn = psycopg2.connect(conn_string)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/tools/lib/python2.7/site-packages/psycopg2/__init__.py", line 164, in connect
conn = _connect(dsn, connection_factory=connection_factory, async=async)
psycopg2.OperationalError: could not connect to server: Connection refused
Is the server running on host "localhost" (::1) and accepting
TCP/IP connections on port 5432?
could not connect to server: Connection refused
Is the server running on host "localhost" (127.0.0.1) and accepting
TCP/IP connections on port 5432?
The password is not set for postgres user.
Generally, I can connect to database by running following method on host.
1. SSH to box
2. su - postgres
3. psql
4. \c mydb
The server runs PostgreSQL 9.1.
You're trying to connect to PostgreSQL on localhost using a script running on your computer, but there's no PostgreSQL server running there.
For this to work, you'd have to ssh to the remote server, then run your Python script there, where the PostgreSQL server is "local" relative to the Python script.
(That's why running psql works - because you're running it on the remote server, where PostgreSQL is "local" relative to psql).
Alternately, you could:
Use an SSH tunnel to forward the PostgreSQL port from the local computer to the remote one; or
Connect directly over TCP/IP to the remote PostgreSQL server using its host name or IP address, after enabling remote connections on the server.
Note that just putting the server's IP address or host name into the connection string instead of localhost will not work unless you also configure the server to accept remote connections. You must set listen_addresses to listen for non-local connections, add any required firewall rules, set pg_hba.conf to permit connections from remote machines, and preferably set up SSL. All this is covered in the Client Authentication chapter of the PostgreSQL user manual.
You'll probably find an SSH tunnel simpler and easier to understand.

Categories