python clickhouse\u驱动程序通过ssh连接错误

fruv7luv  于 2021-07-15  发布在  ClickHouse
关注(0)|答案(1)|浏览(585)

已经为我的问题挣扎了一个星期,希望你能向我解释这里出了什么问题。
我正在尝试连接远程服务器上的clickhouse数据库,这是绝对默认的设置。所以我连接到一个远程服务器并在我的机器上创建一个隧道。

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()

我收到这个错误

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'

我真的试着去理解这个非类型对象出现在哪里,但是代码中有点卡住了。

cedebl8k

cedebl8k1#

有两个问题:
未传递导致错误的用户和密码“nonetype”对象没有属性“encode”
使用错误的端口访问ch(clickhouse驱动程序设计用于通过本机协议(tcp)与clickhouse服务器通信,默认情况下,本机协议使用端口9000进行非安全通信(有关详细信息,请参阅使用python驱动程序连接到停靠的clickhouse服务器的问题))

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())

相关问题