1

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.

2
  • 1
    but mymarilyn/clickhouse-driver uses tcp protocol on port 9000 not http(8123) Commented Jun 30, 2020 at 18:50
  • Thank you, I've used 9000 port and clickhouse_driver Client so everything started to work Commented Jul 1, 2020 at 7:18

1 Answer 1

1

There are two issues:

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())
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.