python - Python clickhouse_driver 通过 ssh 连接报错
问题描述
已经为我的问题苦苦挣扎了一周,希望您能够向我解释这里出了什么问题。
我正在尝试连接我在远程服务器上拥有的 Clickhouse DB,这是绝对默认设置。所以我连接到远程服务器并在我的机器上创建一个隧道。
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'
我真的很想了解这个 NoneType 对象出现在哪里,但有点卡在代码中。
解决方案
有两个问题:
未传递导致错误“NoneType”对象没有属性“编码”的用户和密码
使用错误的端口访问 CH(clickhouse-driver旨在通过本机协议 (TCP)与 ClickHouse 服务器通信,默认情况下使用端口 9000 进行非安全通信(有关详细信息,请参阅使用 Python 驱动程序连接到 Dockerized 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())
推荐阅读
- reactjs - 反应“这还没有被初始化 - super() 还没有被调用” 仅在构建
- sql - 带有 Where 子句的 SQL 求和
- ironpython - 是否可以在较新版本的 Spotfire 上使用 Ironpython 将 HTMLTextArea 导出为 PDF
- javascript - 如何显示 DataTable JQuery 的长度菜单?
- visual-c++ - 错误 LNK1104:无法打开文件“libcpmtd.lib”
- google-cloud-platform - 我们可以使用 Google Cloud Drive Storage 代替 Google 云存储吗?
- c++ - 无法理解带有特殊字符的 C++ 字符串的输出
- visual-studio - 如何修复 PCL All-In-One 安装错误,PATH 太长安装程序无法修改 PATH
- python - 如何在 Python 中使用全局超时分叉和加入多个子进程?
- python - Python max() 函数忽略列表中第一项的最大值