Eclipse RSE connect to a localhost on a remote machine - python

I'm trying to work with Hbase, thrift, and python on a remote machine(Ubuntu) from Eclise RSE on windows. Everything is working fine but when I'm trying to connect to the localhost I get an error:
thrift.transport.TTransport.TTransportException: Could not connect to localhost:9090
If I run this code via ssh terminal on a remote machine, it works perfectly.
Here is my code:
#!/usr/bin/env python
import sys, glob
sys.path.append('gen-py')
sys.path.insert(0, glob.glob('lib/py/build/lib.*')[0])
from thrift import Thrift
from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol
from hbase import Hbase
# Connect to HBase Thrift server
transport = TTransport.TBufferedTransport(TSocket.TSocket('localhost', 9090))
protocol = TBinaryProtocol.TBinaryProtocolAccelerated(transport)
# Create and open the client connection
client = Hbase.Client(protocol)
transport.open()
tables = client.getTableNames()
print(tables)
# Do Something
transport.close()

Do you know what localhost means? It means the machine you're running the command on. E.g. if I type http://localhost:8080/ in a browser on my PC then it will call the server running on port 8080 on MY machine.
I'm sure your connection worked fine if you tried connecting to localhost while on the same box. If connecting from a different machine then you'll need to know the IP address or the hostname of the box you're connecting to.

Related

Access on local PostgreSQL Database from the inside of a docker container with a flask-webapp using a psycopg2 connection

I am relatively new to this topic. I try to build a webapp using flask. The webapp uses data from a postgresql database which is running local (Mac OS Monterey 12.2.1).
My application uses a python code which accesses data from the database by connecting to the database with psycopg2:
con = psycopg2.connect(
host = "192.168.178.43"
database = self.database,
port = "5432",
user = "user",
password = "password")
I already added the relevant entries to the "pg_hba.conf" file and to the "postgresql.conf" file to the needed configurations for an access in my home network. But i still got an error when starting the container. The app runs perfect outside the container. I think I miss some important steps to complete a successful connection.
This error is the following
sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) could not connect to server: Connection refused
Is the server running on host "127.0.0.1" and accepting
TCP/IP connections on port 5432?

how can i connect (ssh remote) mongodb with python instead of terminal macos?

I want to connect MongoDB server with python.
I have connected it on the terminal of MacOS and I run some MongoDB queries. (as described this link http://ghtorrent.org/raw.html)
sshtunnel: ssh -L 27017:dutihr.st.ewi.tudelft.nl:27017 ghtorrent#dutihr.st.ewi.tudelft.nl
connect to mongo: mongo -u ghtorrentro -p ghtorrentro github
what are the parameters of the above "ssh" and "mongo" commands?
How to connect remote mongodb with pymongo and
Can I connect to GHTorrent MySQL/Mongodb database through ssh?
I see these question, I tried to write my code according to these answers but it's not working.
import pymongo
import sshtunnel
from sshtunnel import SSHTunnelForwarder
import paramiko
mypkey = paramiko.RSAKey.from_private_key_file("/Users/aaa/.ssh/id_rsa","xxx") #username(aaa) and password(xxx)
server = SSHTunnelForwarder(
('dutihr.st.ewi.tudelft.nl', 22),
ssh_username="",
ssh_pkey=mypkey,
ssh_private_key_password="xxx", #my password for key
remote_bind_address=('0.0.0.0',27017))
server.start()
how can I find the right parameters of these codes? how can I connect this MongoDB server? It give this error;
2019-10-31 00:11:51,304| ERROR | Secsh channel 49 open FAILED: open failed: Administratively prohibited
2019-10-31 00:11:51,305| ERROR | Could not establish connection from ('127.0.0.1', 51634) to remote side of the tunnel

remote python client failing to reach couchbase server

I have a couchbase 6.0 server running on linode and I'm using the python SDK to insert data into my couchbase bucket. When run directly on the Linode server, my data gets inserted.
However, when I run my code from a remote machine I get network error:
CouchbaseNetworkError, CouchbaseTransientError): <RC=0x2C[The remote host refused the connection.
I have ports 8091, 8092, 8093, 8094 open on linode.
from couchbase.cluster import Cluster
from couchbase.cluster import PasswordAuthenticator
# linode ip: 1.2.3.4
cluster = Cluster('couchbase://1.2.3.4:8094')
cluster.authenticate(PasswordAuthenticator('admin', 'password'))
bucket = cluster.open_bucket('test_bucket')
bucket.upsert('1',{"foo":"bar"})
My code executes when run on the server with couchbase://localhost but it fails when run from a remote machine. is there any port or configuration I am missing?
Client-to-node: Between any clients/app-servers/SDKs and all nodes of each cluster they require access to.
Unencrypted*: 8091-8096, 11210, 11211
Encrypted: 18091-18096†††, 11207
using ports 11210 and 11211 worked for me. source

Conecting to MySQL in a remote server from python

I'm using Python 3.5, pymysql 0.7.6 on MacOS X 10.12.
I'm trying to use python to access a MySQL database in a remote server. I have no problems to access from the command line using:
ssh root#XXX.XXX.XXX.XXX
root#XXX.XXX.XXX.XXX's password: my_server_password
and then in the server:
mysql my_database -p
Enter password: my_database_password
And it works and I can do all sort of things with my database. Now, I try to do the same within python, following the documentation or the numerous examples I've found in other posts here:
import pymysql
cnx = pymysql.connect(host='XXX.XXX.XXX.XXX', port='3306', user='root', password='my_server_password', db='my_database')
And it does not work, getting as error:
pymysql.err.OperationalError: (2003, "Can't connect to MySQL server on 'XXX.XXX.XXX.XXX' ([Errno 61] Connection refused)")
The credentials are the correct credentials and I've checked that the port is the correct port, as it is suggested in other posts. I suspect it might be related with the database having also a password, not just the server, but I haven't found any way of including both passwords. Indeed, I'm not sure which password should be included in the connect command, if the server password or the database password. It does not work with neither of them.
So, do you have any suggestion about what might be the issue here or if I'm missing an important bit?
When you run the mysql command, you are doing this in an SSH shell. That is you are connecting to the server running on the remote machine via a localhost connection. That remote server doesn't seem to be set up to allow remote connections to it, only connections from the machine itself.
You'll need to have your python script connect to the MySQL server the same way you are, via SSH. You can open an SSH tunnel to port 3306 on the remote server.
The module I like to use for this purpose is: https://pypi.python.org/pypi/sshtunnel
from sshtunnel import SSHTunnelForwarder
import pymysql
server = SSHTunnelForwarder(
'XXX.XXX.XXX.XXX',
ssh_username='root',
ssh_password='my_server_password',
remote_bind_address=('127.0.0.1', 3306)
)
server.start()
cnx = pymysql.connect(
host='127.0.0.1',
port=server.local_bind_port,
user='root',
password='my_database_password',
db='my_database'
)
# Make sure to call server.stop() when you want to disconnect
# after calling cnx.close()
Based on the information given, I would suggest you try the following:
Ensure the MySQL bind address is "0.0.0.0". You can do this by editing /etc/mysql/my.cnf and checking the bind-address option is set to "0.0.0.0"
Make sure the port is open from your remote machine. Install "nmap" then use nmap -sS -O -p3306 <public-server-ip>. If see something like 3306/mysql open all good. If not, you need to ensure your firewall isn't blocking that port.
As Rocket Hazmat suggests you probably will want to use an SSH tunnel to reduce the risks of exposing MySQL port directly as data is probably sent in the clear. If you just need to test your code locally before deploying to the server you can use the following command before you run your Python code: ssh -f user#serverip -L 3306:127.0.0.1:3306 -N. In this case you don't need to complete step one and two.
working code
#!/usr/local/bin/python3
# -*- coding: utf-8 -*-
import pymysql
import sshtunnel
from sshtunnel import SSHTunnelForwarder
server = SSHTunnelForwarder(
('xx.xx.xx.xx', 22),
ssh_username='deploy',
ssh_pkey='~/.ssh/id_rsa',
remote_bind_address=('127.0.0.1', 3306)
)
server.start()
con = pymysql.connect(host='127.0.0.1', user='root', passwd='mysql', db='backoffice_demo', port=server.local_bind_port)
with con:
cur = con.cursor()
cur.execute("SELECT VERSION()")
version = cur.fetchone()
print("Database version: {}".format(version[0]))

Operational Error(2003, "Can't connect to MySQL server on '127.3.138.130' (111)")

I am trying to connect MySQL by using ip which I got from PhpMyAdmin using python. But I face Operational Error(2003, "Can't connect to MySQL server on '127.3.138.130' (111)")
I know how to use mysql to connect to localhost
I am following this tutorial
I have written following lines of code in python
I am using ubuntu terminal to run python code and I do have mysql-server installed on my laptop.
import MySQLdb
db = MySQLdb.connect('127.3.138.130','my_username','my_password','my_db_name')
So what the problem is ? how to solve this problem ,please explain me in very simple manner. Thanks!
Make sure the server listens to outside requests. To do this go to /etc/mysql/my.cnf on the server and edit:
bind-address = 127.0.0.1
To:
bind-address = 0.0.0.0
You may need to use sudo when editing the file.
You will need to restart the server after changing the configuration:
sudo service mysql restart
You can also see what port is the server listening on (the default being 3306) in /etc/mysql/my.cnf. Just look for a line that says:
port = 3306
Make sure you're connecting to through same port, this is how you can specify a port:
db = MySQLdb.connect(
host = '127.3.138.130',
user = 'my_username',
passwd = 'my_password',
db = 'my_db_name',
port = 3306 # should be same as in /etc/mysql/my.cnf on server.
)
use 'pymysql' lib...it may help u....
import pymysql
conn = pymysql.connect(host=''127.3.138.130', port=3306, user='root', passwd='password', db='dbname')

Categories

Resources