简体   繁体   English

使用 Python 从 Azure Databricks 连接到外部 MySQL 数据库

[英]Connecting to external MySQL database from Azure Databricks with Python

I am trying to connect to an external private MySQL Database from Azure Databricks.我正在尝试从 Azure Databricks 连接到外部私有 MySQL 数据库。

I have tried with sqlalchemy and pymysql: pymysql:我试过 sqlalchemy 和 pymysql:

conn = pymysql.connect(
    host='.....',
    user='.....', 
    password = "....",
    db='....')
cur = conn.cursor()

sqlalchemy: sqlalchemy:

engine = create_engine("mysql+pymysql://{0}:{1}@hostname:3306/database".format('user','password'))
connection = engine.connect()

The error code was the same: OperationalError: (pymysql.err.OperationalError) (1045, "Access denied for user 'user'@'IP_address' (using password: YES)" [The external database was configured for remote access and the IPs we were trying to access it with were whitelisted]错误代码相同: OperationalError: (pymysql.err.OperationalError) (1045, "Access denied for user 'user'@'IP_address' (using password: YES)" [外部数据库配置为远程访问和 IP我们试图访问它被列入白名单]

With mysql.connector the connection was successful.使用 mysql.connector 连接成功。

connection = connect(
  host=dbutils.secrets.get(scope="resource_group", key="key"),
  port=dbutils.secrets.get(scope="resource_group", key="key"),
  user=dbutils.secrets.get(scope="resource_group", key="key"),
  password=dbutils.secrets.get(scope="resource_group", key="key"),
  database="db")
cursor = connection.cursor()

Question is: Does anyone know why sqlalchemy and pymysql did not work?问题是:有谁知道为什么 sqlalchemy 和 pymysql 不起作用? Is there only certain python sql connector libraries that work in Databricks and why?是否只有某些 python sql 连接器库可以在 Databricks 中使用,为什么?

There are various ways to connect to a MySQL database in Spark.有多种方法可以连接到 Spark 中的 MySQL 数据库。 The below image summarizes some of common approaches to connect to MySQL using Python as programming language.下图总结了使用 Python 作为编程语言连接到 MySQL 的一些常用方法。

Note: For python environment - it's recommended to use mysql-connector-python library.注意:对于 python 环境 - 建议使用mysql-connector-python库。

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM