Python Planetscale DB MySQL连接

biswetbf  于 2023-04-22  发布在  Python
关注(0)|答案(2)|浏览(197)

我一直在尝试制作一个连接到Planetscale MySQL DB的Python程序,我使用了两个库,分别是mysql-connector-python和mysqlclient。
我想我每次都输入了正确的细节,但它没有工作。
我尝试了Planetscale推荐的方法,如下所示(顺便说一句,它对我不起作用):

import MySQLdb # pip install mysqlclient

# This is planetscale's copy/paste method to connect to the DB

conn = MySQLdb.connect(
  host=[HOST],
  user=[USER],
  passwd=[PASSWORD],
  db=[DATABASE],
  ssl_mode = "VERIFY_IDENTITY",
  ssl      = {
    "ca": "/etc/ssl/cert.pem"
  }
)

这是我目前使用的代码

import mysql.connector # pip install mysql-connector-python

config = {
    "user": hidden for security purposes,
    "password": hidden for security purposes,
    "host": hidden for security purposes,
    "database": hidden for security purposes,
    "ssl_verify_identity": True,
    "ssl_ca": "/etc/ssl/cert.pem",
}
conn = mysql.connector.connect(**config)

cursor = conn.cursor()

cursor.execute("""CREATE TABLE IF NOT EXISTS qr_table (
    type VARCHAR(255),
    date VARCHAR(255),
    path VARCHAR(255),
    unique_id VARCHAR(255)
)""")

conn.commit()
cursor.close()
conn.close()

这是我得到的错误

Traceback (most recent call last):
  File "C:\Users\Axelr\Python coding\lib\site-packages\mysql\connector\connection_cext.py", line 268, in _open_connection
    self._cmysql.connect(**cnx_kwargs)
_mysql_connector.MySQLInterfaceError: SSL connection error: SSL_CTX_set_default_verify_paths failed

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\Axelr\PycharmProjects\PC01\main\Special Main\ARC QR Generator\SQL Script.py", line 26, in <module>
    conn = mysql.connector.connect(**config)
  File "C:\Users\Axelr\Python coding\lib\site-packages\mysql\connector\pooling.py", line 286, in connect
    return CMySQLConnection(*args, **kwargs)
  File "C:\Users\Axelr\Python coding\lib\site-packages\mysql\connector\connection_cext.py", line 101, in __init__
    self.connect(**kwargs)
  File "C:\Users\Axelr\Python coding\lib\site-packages\mysql\connector\abstracts.py", line 1108, in connect
    self._open_connection()
  File "C:\Users\Axelr\Python coding\lib\site-packages\mysql\connector\connection_cext.py", line 273, in _open_connection
    raise get_mysql_exception(
mysql.connector.errors.InterfaceError: 2026 (HY000): SSL connection error: SSL_CTX_set_default_verify_paths failed

Process finished with exit code 1

有人能帮我建立一个有效的连接吗?
另外,使用另一个库进行连接也不会有问题。
[Python 3.10]

eni9jsuy

eni9jsuy1#

我想你不应该使用列表作为输入参数。简单地使用字符串作为所有参数。此外,你应该检查文档以获得正确的参数名称(请参阅https://mysqlclient.readthedocs.io/user_guide.html#mysqldb)
一段工作代码应该是:

import MySQLdb

conn = MySQLdb.connect(
  host=HOST,
  user=USER,
  password=PASSWORD,
  database=DATABASE,
  ssl_mode = "VERIFY_IDENTITY",
  ssl      = {
    "ca": "/etc/ssl/cert.pem"
  }
)
c3frrgcw

c3frrgcw2#

我已经解决了这个问题。
问题是SSL,我使用字典来存储细节,然后使用**config在连接中使用字典。
下面是我的正确和工作代码:

import mysql.connector as sql

conn = sql.connect(host="*Hidden For Security Purposes*",
                   database="*Hidden For Security Purposes*",
                   user="*Hidden For Security Purposes*",
                   password="*Hidden For Security Purposes*",
                    )

感谢所有帮助我解决这个问题的人,我希望这个StackOverflow问题能帮助很多有同样问题的人。

相关问题