SSH接続を通してReplit上のPythonにエックスサーバー上のWordpressのMySQLからデータを引き出したい。

実現したいこと

SSH接続を通してReplit上のPythonにエックスサーバー上のWordpressのMySQLからデータを引き出したい。

前提

SSH接続を通してReplit上のPythonにエックスサーバー上のWordpressのMySQLからデータを引き出したいです。
connect successful!!
disconnect successful!!
がプリントされるので接続まではできているようなのですが…

発生している問題・エラーメッセージ

2023-02-28 07:19:45,593| ERROR | Could not establish connection from local ('172.31.128.75', 32915) to remote ('mysql0000.xserver.jp', 3306) side of the tunnel: open new channel ssh error: Unable to open channel. Traceback (most recent call last): File "main.py", line 40, in <module> posts = db.get_posts() File "main.py", line 33, in get_posts cursor.execute("SELECT post_title, post_content FROM wp_posts") File "/home/runner/TEST-INT/venv/lib/python3.10/site-packages/pymysql/cursors.py", line 148, in execute result = self._query(query) File "/home/runner/TEST-INT/venv/lib/python3.10/site-packages/pymysql/cursors.py", line 310, in _query conn.query(q) File "/home/runner/TEST-INT/venv/lib/python3.10/site-packages/pymysql/connections.py", line 547, in query self._execute_command(COMMAND.COM_QUERY, sql) File "/home/runner/TEST-INT/venv/lib/python3.10/site-packages/pymysql/connections.py", line 793, in _execute_command raise err.InterfaceError(0, "") pymysql.err.InterfaceError: (0, '')

該当のソースコード

Python

1import os 2from sshtunnel import SSHTunnelForwarder 3import pymysql 4class MySQLDatabase:5 def __init__(self):6 self.connection = None7 def open_connection(self):8 """Connect to MySQL Database."""9 # MySQL に接続10 with SSHTunnelForwarder(11 ('sv00000.xserver.jp', 10022),12 ssh_username='username',13 ssh_password='password',14 ssh_pkey='./ssh.key',15 remote_bind_address=('mysql0000.xserver.jp', 3306)16 ) as server:17 if self.connection is None:18 self.connection = pymysql.connect(19 host='localhost',20 port = server.local_bind_port,21 user = 'id_xxxx',22 password = 'sqlpassword',23 database = 'id_wp0',24 charset='utf8'25 )26 print ("connect successful!!")27 self.connection.close()28 print ("disconnect successful!!")29 30 def get_posts(self):31 """Get all post titles and content from wp_posts table."""32 with self.connection.cursor() as cursor:33 cursor.execute("SELECT post_title, post_content FROM wp_posts")34 result = cursor.fetchall()35 return result 36 37if __name__ == "__main__":38 db = MySQLDatabase()39 db.open_connection()40 posts = db.get_posts()41 for post in posts:42 print(post)43

試したこと

接続はできるのですが、データの読み込みができません。

2023-02-28 07:19:45,593| ERROR | Could not establish connection from local ('172.31.128.75', 32915) to remote ('mysql0000.xserver.jp', 3306) side of the tunnel: open new channel ssh error: Unable to open channel.

このエラーが時間を置くとなくなったりもします。ポート接続が不安定なのでしょうか…

補足情報(FW/ツールのバージョンなど)

ここにより詳細な情報を記載してください。

コメントを投稿

0 コメント