Hinweis
Für den Zugriff auf diese Seite ist eine Autorisierung erforderlich. Sie können versuchen, sich anzumelden oder das Verzeichnis zu wechseln.
Für den Zugriff auf diese Seite ist eine Autorisierung erforderlich. Sie können versuchen, das Verzeichnis zu wechseln.
MySQLMySQL
Verwenden Sie über Python mit dem MySQL-Manager und pyodbc in Azure MySQL-Datenbank gespeicherte Ressourcen und Daten.Work with resources and data stored in Azure MySQL Database from python with the MySQL manager and pyodbc.
ODBC-Clienttreiber und pyodbcClient ODBC driver and pyodbc
Für den Zugriff auf Azure-Datenbank für MySQL wird als Clientbibliothek der ODBC-Treiber von Microsoft empfohlen.The recommended client library for accessing Azure Database for MySQL is the Microsoft ODBC driver. Verwenden Sie den ODBC-Treiber zum Herstellen einer Verbindung mit der Datenbank und zum direkten Ausführen von SQL-Anweisungen.Use the ODBC driver to connect to the database and execute SQL statements directly.
BeispielExample
Stellen Sie eine Verbindung mit Azure-Datenbank für MySQL her, und wählen Sie alle Datensätze in der Vertriebstabelle aus.Connect to a Azure Database for MySQL and select all records in the sales table. Sie können die ODBC-Verbindungszeichenfolge für die Datenbank aus dem Azure-Portal abrufen.You can get the ODBC connection string for the database from the Azure Portal.
SERVER = 'YOUR_SEVER_NAME' + '.mysql.database.azure.com'
DATABASE = 'YOUR_DATABASE_NAME'
USERNAME = 'YOUR_MYSQL_USERNAME'
PASSWORD = 'YOUR_MYSQL_PASSWORD'
DRIVER = '{MySQL ODBC 5.3 UNICODE Driver}'
cnxn = pyodbc.connect(
'DRIVER=' + DRIVER + ';PORT=3306;SERVER=' + SERVER + ';PORT=3306;DATABASE=' + DATABASE + ';UID=' + USERNAME + ';PWD=' + PASSWORD)
cursor = cnxn.cursor()
selectsql = "SELECT * FROM SALES" # SALES is an example table name
cursor.execute(selectsql)
Verwaltungs-APIManagement API
Über die Verwaltungs-API können Sie MySQL-Ressourcen in Ihrem Abonnement erstellen und verwalten.Create and manage MySQL resources in your subscription with the management API.
Requirements (Anforderungen)Requirements
Sie müssen die MySQL-Verwaltungsbibliotheken für Python installieren.You must install the MySQL management libraries for Python.
pip install azure-mgmt-rdbms
Ausführliche Informationen zum Abrufen von Anmeldeinformationen für die Authentifizierung beim Verwaltungsclient finden Sie unter Authenticate with the Azure Management Libraries for Python (Authentifizieren bei den Azure-Verwaltungsbibliotheken für Python).Please see the Python SDK authentication page for details on obtaining credentials to authenticate with the management client.
BeispielExample
Erstellen Sie eine MySQL 5.7-Datenbank-Ressource, und schränken Sie den Zugriff mithilfe einer Firewallregel auf einen bestimmten IP-Adressbereich ein.Create a MySQL 5.7 Database resource and restrict access to a range of IP addresses using a firewall rule.
from azure.mgmt.rdbms.mysql import MySQLManagementClient
from azure.mgmt.rdbms.mysql.models import ServerForCreate, ServerPropertiesForDefaultCreate, ServerVersion
SUBSCRIPTION_ID = "YOUR_AZURE_SUBSCRIPTION_ID"
RESOURCE_GROUP = "YOUR_AZURE_RESOURCE-GROUP_WITH_POSTGRES"
MYSQL_SERVER = "YOUR_DESIRED_MYSQL_SERVER_NAME"
MYSQL_ADMIN_USER = "YOUR_MYSQL_ADMIN_USERNAME"
MYSQL_ADMIN_PASSWORD = "YOUR_MYSQL_ADMIN_PASSOWRD"
LOCATION = "westus" # example Azure availability zone, should match resource group
client = MySQLManagementClient(credentials=creds,
subscription_id=SUBSCRIPTION_ID)
server_creation_poller = client.servers.create_or_update(
resource_group_name=RESOURCE_GROUP,
server_name=MYSQL_SERVER,
ServerForCreate(
ServerPropertiesForDefaultCreate(
administrator_login=MYSQL_ADMIN_USER,
administrator_login_password=MYSQL_ADMIN_PASSWORD,
version=ServerVersion.five_full_stop_seven
),
location=LOCATION
)
)
server = server_creation_poller.result()
# Open access to this server for IPs
rule_creation_poller = client.firewall_rules.create_or_update(
RESOURCE_GROUP
MYSQL_SERVER,
"some_custom_ip_range_whitelist_rule_name", # Custom firewall rule name
"123.123.123.123", # Start ip range
"167.220.0.235" # End ip range
)
firewall_rule = rule_creation_poller.result()
PostgreSQLPostgreSQL
Verwenden Sie den ODBC-Treiber und pyodbc zum Herstellen einer Verbindung mit der Datenbank und zum direkten Ausführen von SQL-Anweisungen.Use the ODBC driver and pyodbc to connect to the database and execute SQL statements directly.
Weitere Informationen zu Azure-Datenbank für PostgreSQLLearn more about Azure Database for PostgreSQL.
ODBC-Clienttreiber und pyodbcClient ODBC driver and pyodbc
Für den Zugriff auf Azure Database for PostgreSQL werden als Clientbibliothek der ODBC-Treiber von Microsoft und pyodbc empfohlen.The recommended client library for accessing Azure Database for PostgreSQL is the Microsoft ODBC driver and pyodbc.
BeispielExample
Stellen Sie eine Verbindung mit Azure-Datenbank für PostgreSQL her, und wählen Sie alle Datensätze in der Tabelle SALES aus.Connect to a Azure Database for PostgreSQL and select all records in the SALES table. Sie können die ODBC-Verbindungszeichenfolge für die Datenbank aus dem Azure-Portal abrufen.You can get the ODBC connection string for the database from the Azure Portal.
import pyodbc
SERVER = 'YOUR_SERVER_NAME.postgres.database.azure.com'
DATABASE = 'YOUR_DB_NAME'
USERNAME = 'YOUR_USERNAME'
PASSWORD = 'YOUR_PASSWORD'
DRIVER = '{PostgreSQL ODBC Driver}'
cnxn = pyodbc.connect(
'DRIVER=' + DRIVER + ';PORT=5432;SERVER=' + SERVER +
';PORT=5432;DATABASE=' + DATABASE + ';UID=' + USERNAME +
';PWD=' + PASSWORD)
cursor = cnxn.cursor()
selectsql = "SELECT * FROM SALES" # SALES is an example table name
cursor.execute(selectsql)
Verwaltungs-APIManagement API
Requirements (Anforderungen)Requirements
Sie müssen die PostgreSQL-Verwaltungsbibliotheken für Python installieren.You must install the PostgreSQL management libraries for Python.
pip install azure-mgmt-rdbms
Ausführliche Informationen zum Abrufen von Anmeldeinformationen für die Authentifizierung beim Verwaltungsclient finden Sie unter Authenticate with the Azure Management Libraries for Python (Authentifizieren bei den Azure-Verwaltungsbibliotheken für Python).Please see the Python SDK authentication page for details on obtaining credentials to authenticate with the management client.
BeispielExample
In diesem Beispiel wird eine neue Postgres-Datenbank auf unserem vorhandenen Postgres-Server erstellt:In this example we will create a new Postgres database on our existing Postgres server.
from azure.mgmt.rdbms.postgresql import PostgreSQLManagementClient
SUBSCRIPTION_ID = "YOUR_AZURE_SUBSCRIPTION_ID"
RESOURCE_GROUP = "YOUR_AZURE_RESOURCE_GROUP_WITH_POSTGRES"
POSTGRES_SERVER = "YOUR_POSTGRES_SERVER_NAME"
DB_NAME = "YOUR_DESIRED_DATABASE_NAME"
client = PostgreSQLManagementClient(credentials, SUBSCRIPTION_ID)
db_creation_poller = client.databases.create_or_update(
resource_group_name=RESOURCE_GROUP,
server_name=POSTGRES_SERVER, database_name=DB_NAME)
db = db_creation_poller.result()