无法使用 python 从我的计算机访问 Azure SQL 数据库



我在azure中创建了自己的数据库,现在没有任何数据。我遵循此文档创建DB。我想创建表并使用python从我的机器访问它。这是我迄今为止尝试的

import mysql.connector
from mysql.connector import errorcode
# Obtain connection string information from the portal
config = {
'host': '<servername>.database.windows.net',
'user': '<username>@servername.database.windows.net',
'password': '<passwword>',
'database': '<DB name>'
}
# Construct connection string
try:
conn = mysql.connector.connect(**config)
print("Connection established")
except mysql.connector.Error as err:
if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
print("Something is wrong with the user name or password")
elif err.errno == errorcode.ER_BAD_DB_ERROR:
print("Database does not exist")
else:
print(err)

但当我运行它时,它会抛出以下错误

9002 (28000): The server name you tried cannot be found: <servername>.database.windows.net. Please check the Username and retry connection. The Username should be in <username@hostname> format.

我把我的证件填上了。

提前感谢

我可以从Microsoft SQL Server Management Studio 访问

为什么我不能通过Python访问?

我想你误解了SQLMySQL。你可以在这里查一下它们之间的区别。

您正在尝试使用MySQL连接器访问SQL数据库。这是不可能的。这里有两个解决方案

  1. 将数据库更改为MySQL请在此处查看
  2. 在此处使用SQL代码进行访问检查

如果您需要任何澄清,请随时询问

最新更新