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())
Related
I need to connect to Postgres DB via SSH tunnel. I use following code:
try:
with SSHTunnelForwarder(
(ssh_host, ssh_port),
ssh_username=ssh_user,
ssh_password=ssh_password,
remote_bind_address=(sql_hostname, 5432)) as server:
server.start()
print("server connected")
params = {
'database': sql_database,
'user': sql_username,
'password': sql_password,
'host': sql_hostname,
'port': server.local_bind_port
}
conn = psycopg2.connect(**params)
curs = conn.cursor()
print("database connected")
except:
print("Connection Failed")
As the result I get this warnings in the command line:
2023-02-09 23:09:16,285| ERROR | Password is required for key /Users/aleksandrlozko/.ssh/id_rsa
2023-02-09 23:09:16,286| ERROR | Password is required for key /Users/aleksandrlozko/.ssh/id_ed25519
Traceback (most recent call last):
File "/Users/aleksandrlozko/stages-customer-service-dashboard-atollhoding/modules/db/connect.py", line 73, in <module>
db = psql.PostgreSQLDB(
File "/Users/aleksandrlozko/stages-customer-service-dashboard-atollhoding/modules/db/psql.py", line 25, in __init__
self.pool = psycopg2.pool.SimpleConnectionPool(
File "/opt/anaconda3/envs/activity-dashboard-atollholding/lib/python3.10/site-packages/psycopg2/pool.py", line 59, in __init__
self._connect()
File "/opt/anaconda3/envs/activity-dashboard-atollholding/lib/python3.10/site-packages/psycopg2/pool.py", line 63, in _connect
conn = psycopg2.connect(*self._args, **self._kwargs)
File "/opt/anaconda3/envs/activity-dashboard-atollholding/lib/python3.10/site-packages/psycopg2/__init__.py", line 122, in connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
psycopg2.OperationalError: connection to server at "172.26.201.14", port 5432 failed: Operation timed out
Is the server running on that host and accepting TCP/IP connections?
Through the pdAdmin it is possible to connect, but not using Python. What could be the problem? And at what stage am I making a mistake?
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).
I've tried a bunch of thinks including trying to specify the UNIX socket to no avail, I'm not running any queries and I haven't even initialized a cursor but I keep getting this error, what gives?
Python Block:
connection = mysql.connect(user = "root", password = None, port = 8080, host = 'localhost', db ='Articles')
Error:
Traceback (most recent call last):
File "/Users/Adrian/Desktop/Python/webcrawl.py", line 10, in <module>
connection = mysql.connect(user = "root", password = None, port = 8080, host = 'localhost', db ='Articles')
File "/Users/Adrian/anaconda3/lib/python3.6/site-packages/pymysql/__init__.py", line 90, in Connect
return Connection(*args, **kwargs)
File "/Users/Adrian/anaconda3/lib/python3.6/site-packages/pymysql/connections.py", line 699, in __init__
self.connect()
File "/Users/Adrian/anaconda3/lib/python3.6/site-packages/pymysql/connections.py", line 935, in connect
self._get_server_information()
File "/Users/Adrian/anaconda3/lib/python3.6/site-packages/pymysql/connections.py", line 1249, in _get_server_information
packet = self._read_packet()
File "/Users/Adrian/anaconda3/lib/python3.6/site-packages/pymysql/connections.py", line 991, in _read_packet
packet_header = self._read_bytes(4)
File "/Users/Adrian/anaconda3/lib/python3.6/site-packages/pymysql/connections.py", line 1037, in _read_bytes
CR.CR_SERVER_LOST, "Lost connection to MySQL server during query")
pymysql.err.OperationalError: (2013, 'Lost connection to MySQL server during query')
Edit: I'm running XAMPP 7.2 on MacOSX with port forwarding enabled over SSH (localhost:8080 -> 80) and the opt/lampp volumes are mounted
It's not advisable to use root without password.
In some databases you are forced to set it if you want to connect as root.
You can set any password: dev.mysql.com/doc/refman/5.7/en/resetting-permissions.html
Also, 8080 might be the port of your web server, not the mySQL one, try with 3306 port which is the default one for mySQL.
You can open my.cnf file located in the /Applications/XAMPP/xamppfiles/etc/ directory to chech in which port is your database listening.
Also check that the mySQL daemon is running. In mac os X go to /Applications/XAMPP/XAMPP Control in Finder and check that Apache and MySQL are running.
If your MySQL server isn't starting, you may need to set the permissions for it using Terminal with this command:
chmod -R 777 /Applications/XAMPP/xamppfiles/var
To check that your Articles database exists:
mysql -u root -p
USE Articles;
If it's not created;
mysql -u root -p
CREATE DATABASE Articles;
And connect this way:
#!/usr/bin/python
import MySQLdb
connection = MySQLdb.connect(host="localhost",
user="root",
passwd="your pwd",
db="Articles")
We are trying to access the database of Odoo running on 104.154.90.232 through Python. I have used psycopg2 to connect.
My code is like this:
import psycopg2
import sys
import pprint
conn_string = "host='104.154.90.232' dbname='dbname' user='user' password='password'"
print "connecting to the database\n ->%s"%(conn_string)
conn = psycopg2.connect(conn_string)
cursor = conn.cursor()
cursor.execute("SELECT * FROM hr_employee")
records = cursor.fetchall()
pprint.pprint(records)
The error is:
Traceback (most recent call last):
File "test.py", line 6, in <module>
conn = psycopg2.connect(conn_string)
File "C:\Python27\lib\site-packages\psycopg2\__init__.py", line 130, in connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
psycopg2.OperationalError: could not connect to server: Connection timed out (0x0000274C/10060)
Is the server running on host "104.154.90.232" and accepting TCP/IP connections on port 5432?
Can someone help me establish the connection?
As far as i know, the default pg_hba.conf file only allows connections resp. authentification from the local system. You have to define new rules for external access. It's described in the file, how to do that.
Then ofcourse there has to be a correctly configured database user for odoo.
I am newbie to python ,started with implementing small programs ,now trying with connecting database .I installed python 3.4 and mysql.connector 2.0.4 .
Below given code is what i used to connnect the database
#!"C:\python34\python.exe"
import sys
import mysql.connector
print("Content-Type: text/html;charset=utf-8")
print()
conn = mysql.connector.connect(host='localhost:8051',
database='test',
user='root',
password='tiger')
if conn.is_connected():
print('Connected to MySQL database')
But i am getting error as given below . not getting why this error are occurring ,because of setup environment is wrong or of some other reason
Please suggest
Traceback (most recent call last):
File "C:\Python34\lib\site-packages\mysql\connector\network.py", line 448, in open_connection
socket.SOL_TCP)
File "C:\Python34\lib\socket.py", line 533, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 11004] getaddrinfo failed
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<pyshell#8>", line 2, in <module>
password='tiger',database='test')
File "C:\Python34\lib\site-packages\mysql\connector\__init__.py", line 179, in connect
return MySQLConnection(*args, **kwargs)
File "C:\Python34\lib\site-packages\mysql\connector\connection.py", line 95, in __init__
self.connect(**kwargs)
File "C:\Python34\lib\site-packages\mysql\connector\abstracts.py", line 719, in connect
self._open_connection()
File "C:\Python34\lib\site-packages\mysql\connector\connection.py", line 209, in _open_connection
self._socket.open_connection()
File "C:\Python34\lib\site-packages\mysql\connector\network.py", line 464, in open_connection
errno=2003, values=(self.get_address(), _strioerror(err)))
mysql.connector.errors.InterfaceError: 2003: Can't connect to MySQL server on 'localhost:8051:3306' (11004 getaddrinfo failed)
The error message is quite clear:
Can't connect to MySQL server on 'localhost:8051:3306'
See the double port definition?
host='localhost:8051'
should most likey be
host='localhost', port='8051'
You are using the connection paramater wrong. I think you need to do this:
conn = mysql.connector.connect(host='localhost',
port=8051,
database='test',
user='root',
password='tiger')
Check out this doc for more detail.
The default Port for MySQL is 3306 , when you don't pass "port" argument to mysql.connector.connect it assumes you are using default port which is 3306 , if you use another port you should pass :
port=xxxx
to the constructor.
For me, I added all connector on my IDE.
mysql-connector
mysql-connector-async-dd
mysql-connector-python
mysql-connector-python-dd
mysql-connector-repackaged
then you can see your all database names here:
import mysql.connector
mydb = mysql.connector.connect(host='localhost',user='root', passwd = 'Root#1234')
mycursor = mydb.cursor()
mycursor.execute("show databases")
for i in mycursor:
print(i)