Not able to connect to remote database through ssh tunneling in python - python

I'm trying to connect to a remote database through ssh tunneling. But I am getting an error when I try to connect. Below is my code and the error
def query(q):
with SSHTunnelForwarder(
("bastion.example.co", 22),
ssh_username="ubuntu",
ssh_pkey="~/.ssh/id_rsa.pub",
remote_bind_address=("127.0.0.1", 3306)
) as server:
conn = mysql.connect(host="xyz.rds.amazonaws.com",
port=server.local_bind_port,
user="prod_db",
passwd="123abc",
db="prod_db")
return pd.read_sql_query(q, conn)
print(query('select * from abc_table'))
Error:
Traceback (most recent call last):
File "conn_mysql.py", line 20, in <module>
print(query('select * from abc_table'))
File "conn_mysql.py", line 11, in query
remote_bind_address=("127.0.0.1", 3306)
File "/Users/mohnishmallya/PycharmProjects/3Bvs2A/venv/lib/python3.7/site-packages/sshtunnel.py", line 904, in __init__
logger=self.logger
File "/Users/mohnishmallya/PycharmProjects/3Bvs2A/venv/lib/python3.7/site-packages/sshtunnel.py", line 1095, in _consolidate_auth
raise ValueError('No password or public key available!')
ValueError: No password or public key available!
How do I solve this?

Use ssh_pkey parameter of SSHTunnelForwarder constructor to provide the path to your private key file (not the public key .pub).

Related

Trouble connecting to Cloud SQL in python

I am trying to connect to a database on Cloud SQL, but I keep getting the same error. Not sure what it is, and tried several approaches.
Input:
import pymysql
connection = pymysql.connect(host='127.0.0.1',
user='',
password='XXXX',
db='cmcsql')
output:
C:\Users\Ejer\anaconda3\envs\pythonProject\python.exe C:/Users/Ejer/PycharmProjects/pythonProject/CloudSQL_test.py
Traceback (most recent call last):
File "C:\Users\Ejer\anaconda3\envs\pythonProject\lib\site-packages\pymysql\connections.py", line 569, in connect
sock = socket.create_connection(
File "C:\Users\Ejer\anaconda3\envs\pythonProject\lib\socket.py", line 808, in create_connection
raise err
File "C:\Users\Ejer\anaconda3\envs\pythonProject\lib\socket.py", line 796, in create_connection
sock.connect(sa)
ConnectionRefusedError: [WinError 10061] Der kunne ikke oprettes forbindelse, fordi destinationscomputeren aktivt nægtede det
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:/Users/Ejer/PycharmProjects/pythonProject/CloudSQL_test.py", line 2, in <module>
connection = pymysql.connect(host='127.0.0.1',
File "C:\Users\Ejer\anaconda3\envs\pythonProject\lib\site-packages\pymysql\__init__.py", line 94, in Connect
return Connection(*args, **kwargs)
File "C:\Users\Ejer\anaconda3\envs\pythonProject\lib\site-packages\pymysql\connections.py", line 327, in __init__
self.connect()
File "C:\Users\Ejer\anaconda3\envs\pythonProject\lib\site-packages\pymysql\connections.py", line 619, in connect
raise exc
pymysql.err.OperationalError: (2003, "Can't connect to MySQL server on '127.0.0.1' ([WinError 10061] Der kunne ikke oprettes forbindelse, fordi destinationscomputeren aktivt nægtede det)")
Process finished with exit code 1
You should not be using 127.0.0.1 to connect to cloud sql instance.
127.0.0.1 is (in most cases) for localhost connection (when you run the db server locally on the same machine as where the client is). Instead you should be using the ip address given to your cloud sql instance. You can find it on the overview tab of the sql instance under Connect to this instance.
You should also create your user on the users tab and then use it in your code.
Don't forget about authentication, check out connections tab and read more about it here
Making sure that you have proper ip, user, existing db and connecting from authorized network should do the trick.
PyMysql connects to port 3306 by default, if your process is running on a different port it won't be able to connect.
Apart from the host, user, password parameters you also have to provide the port on which the sql process is running.

Python clickhouse_driver through ssh connection error

struggling for a week already with my issue, hope you'll be able to explain to me what is wrong here.
I'm trying to connect Clickhouse DB I have on the remote server, which is on the absolutely default settings. So I'm connecting to a remote server and creating a tunnel on my machine.
import sshtunnel as sshtunnel
from clickhouse_driver import connect
server = sshtunnel.SSHTunnelForwarder(
('host', 22),
ssh_username='username',
ssh_pkey="username.openssh",
ssh_private_key_password="password",
remote_bind_address=('localhost', 8123),
local_bind_address=('localhost', 555)
)
server.start()
conn = connect(host='localhost', port=555, database='ertb')
cursor = conn.cursor()
cursor.execute('SHOW TABLES')
cursor.fetchall()
server.stop()
and I receive this error
Traceback (most recent call last):
File "C:/Users/user/PycharmProjects/alerts/ssh_coonection.py", line 42, in <module>
cursor.execute('SHOW TABLES')
File "C:\Users\user\PycharmProjects\alerts\venv\lib\site-packages\clickhouse_driver\dbapi\cursor.py", line 102, in execute
**execute_kwargs
File "C:\Users\user\PycharmProjects\alerts\venv\lib\site-packages\clickhouse_driver\client.py", line 205, in execute
self.connection.force_connect()
File "C:\Users\user\PycharmProjects\alerts\venv\lib\site-packages\clickhouse_driver\connection.py", line 180, in force_connect
self.connect()
File "C:\Users\user\PycharmProjects\alerts\venv\lib\site-packages\clickhouse_driver\connection.py", line 256, in connect
return self._init_connection(host, port)
File "C:\Users\user\PycharmProjects\alerts\venv\lib\site-packages\clickhouse_driver\connection.py", line 237, in _init_connection
self.send_hello()
File "C:\Users\user\PycharmProjects\alerts\venv\lib\site-packages\clickhouse_driver\connection.py", line 325, in send_hello
write_binary_str(self.user, self.fout)
File "C:\Users\user\PycharmProjects\alerts\venv\lib\site-packages\clickhouse_driver\writer.py", line 19, in write_binary_str
text = text.encode('utf-8')
AttributeError: 'NoneType' object has no attribute 'encode'
I really tried to understand from where this NoneType object appears, but a bit stuck in code.
There are two issues:
is not passed user and password that leads to the error 'NoneType' object has no attribute 'encode'
used the wrong port to access to CH (clickhouse-driver is designed to communicate with ClickHouse server over native protocol (TCP) that by default use port 9000 for non-secure communication (see Issue connecting to Dockerized Clickhouse Server with Python driver for details))
import sshtunnel as sshtunnel
from clickhouse_driver import connect
with sshtunnel.SSHTunnelForwarder(
('localhost', 22),
ssh_username="root",
ssh_password="root",
remote_bind_address=('localhost', 9000)) as server:
local_port = server.local_bind_port
print(local_port)
conn = connect(f'clickhouse://default:#localhost:{local_port}/ertb')
#conn = connect(host='localhost', port=local_port, database='ertb', user='default', password='')
cursor = conn.cursor()
cursor.execute('SHOW TABLES')
print(cursor.fetchall())

ssh connection in python

I'm trying to read a text file from a server using ssh from python 3.5. I'm using paramiko to connect to the server but unfortunately, I'm having trouble actually connecting to the server.
this is the code I'm using to connect to the server
import paramiko
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect('IP ADDRESS OF THE SERVER', key_filename="/home/user/.ssh/id_ecdsa" ,look_for_keys=True)
but every time I try to connect to the server I'm getting an authentication failed error message, can anyone see what I'm doing wrong here?
Any advice would be appreciated
This is the error I'm getting everytime i connect.
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.5/site-packages/paramiko/client.py", line 380, in connect
look_for_keys, gss_auth, gss_kex, gss_deleg_creds, gss_host)
File "/usr/local/lib/python3.5/site-packages/paramiko/client.py", line 603, in _auth
raise saved_exception
File "/usr/local/lib/python3.5/site-packages/paramiko/client.py", line 580, in _auth
allowed_types = set(self._transport.auth_publickey(username, key))
File "/usr/local/lib/python3.5/site-packages/paramiko/transport.py", line 1331, in auth_publickey
return self.auth_handler.wait_for_response(my_event)
File "/usr/local/lib/python3.5/site-packages/paramiko/auth_handler.py", line 208, in wait_for_response
raise e
paramiko.ssh_exception.AuthenticationException: Authentication failed.
You should use your private key to connect to a remote server. Your public key must be already installed in the server side, i.e. it must be listed in ~/.ssh/authorized_keys.
Try first from the command line, and only then use Python/paramiko. Check the permissions of the files/directories if all that fails.
Turns out I simply just forgot to add in the username in the connection string. works perfectly now.

Python connect to 000webhost web server MySQL database

I am trying to connect Python to a web server's database, 000webhost I am using. Here is my code:
import MySQLdb
>>> conn = MySQLdb.connect(host="mysql9.000webhost.com",user="user_name",passwd="password",db="db_name")
I think I install MySQLdb successfully. And all the username, password, host, database name are exactly the same as they are in my php file, which works perfectly. However, I got this error:
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
conn = MySQLdb.connect(host="mysql9.000webhost.com",user="a6969519_123",passwd="lianshiyu08",db="a6969519_shiyu")
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/MySQLdb/__init__.py", line 81, in Connect
return Connection(*args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/MySQLdb/connections.py", line 193, in __init__
super(Connection, self).__init__(*args, **kwargs2)
OperationalError: (2003, "Can't connect to MySQL server on 'mysql9.000webhost.com' (60)")
What did I miss?
The database is only reachable from the internal web of your hoster.

Python mysql connector access denied error

I've read everything possible and I just can't connect to my mysql server. Here is my code. very simple. (I used xxx to hide all the private info. everything else is the way i got it.
import mysql.connector
conn = mysql.connector.connect(user='xxxx',password='xxxxx',host='xxxx',db='xxx',port=3306)
The login info is definitely correct. I tried it in a mysql client and it connected just fine. The MySQL server is hosted on dreamhost. I set the allowable IPs for the db user to wildcard %.%.%.% so anyone could connect.I don't know what else could possibly be wrong. When I try to connect in python i get:
C:\Python33\python.exe D:/Dropbox/python/v2/test.py
Traceback (most recent call last):
File "D:/Dropbox/python/v2/test.py", line 3, in <module>
conn = mysql.connector.connect(user='xxx',password='xxx',host='xxx',db='xxx',port=3306)
File "C:\Python33\lib\site-packages\mysql\connector\__init__.py", line 101, in connect
return MySQLConnection(*args, **kwargs)
File "C:\Python33\lib\site-packages\mysql\connector\connection.py", line 117, in __init__
self.connect(**kwargs)
File "C:\Python33\lib\site-packages\mysql\connector\connection.py", line 383, in connect
self._open_connection()
File "C:\Python33\lib\site-packages\mysql\connector\connection.py", line 350, in _open_connection
self._ssl)
File "C:\Python33\lib\site-packages\mysql\connector\connection.py", line 176, in _do_auth
raise errors.get_exception(packet)
mysql.connector.errors.ProgrammingError: 1044 (42000): Access denied for user 'xxx'#'%.%.%.%' to database 'xxx'
Any help would be greatly appreciated.
You will have to GRANT permissions to the user.
Something like:
GRANT ALL PRIVILEGES ON *.* TO 'user2'#'localhost' IDENTIFIED BY PASSWORD 'xxxxx'

Categories

Resources