我有一台运行 LangChain 和 MySQL 的 Ubuntu 22 服务器。
但是LangChain无法从本地环境内部连接到MySQL(代码运行正常,没有异常,只是不会打印任何表格)。
from langchain_community.utilities.sql_database import SQLDatabase
import pyodbc
db = SQLDatabase.from_uri('mysql+mysqlconnector://root:abcd1234@localhost:3306/test?auth_plugin=mysql_native_password')
print(db.dialect) # print out: mysql
print(db.get_usable_table_names()) #print out empty list: []
我进一步测试了常规连接的工作原理:
import mysql.connector
DB_HOST = 'localhost'
DB_PASS = 'abcd1234'
DB_NAME = 'test'
DB_USER = 'root'
def db_select(sql):
con = mysql.connector.connect(host = DB_HOST,
user = DB_USER,
passwd = DB_PASS,
db = DB_NAME,
auth_plugin = 'mysql_native_password')
cursor = con.cursor(dictionary=True)
cursor.execute(sql)
result = cursor.fetchall()
con.close()
return result
a = db_select('select * from test_table limit 2')
for item in a:
print(item) # this works
有什么建议吗?
问题解决了,原因是我安装了mysql-connector和mysql-connector-python-rf来连接MySQL。但是 mysql-connector-python 丢失了,应该使用它。 LangChain代码不会抱怨,只会默默返回任何内容。
库差异:
mysql-connector-python、mysql-connector-python-rf 和 mysql-connector-repackaged 有什么区别?