Python용 Azure MySQL/PostgreSQL 라이브러리Azure MySQL/PostgreSQL libraries for Python

MySQLMySQL

MySQL 관리자 및 pyodbc를 사용하여 Python에서 Azure MySQL Database에 저장된 리소스 및 데이터로 작업합니다.Work with resources and data stored in Azure MySQL Database from python with the MySQL manager and pyodbc.

Client ODBC 드라이버 및 pyodbcClient ODBC driver and pyodbc

Azure Database for MySQL에 액세스하는 데 권장되는 클라이언트 라이브러리는 Microsoft ODBC 드라이버입니다.The recommended client library for accessing Azure Database for MySQL is the Microsoft ODBC driver. ODBC 드라이버를 사용하여 데이터베이스에 연결하고 SQL 문을 직접 실행합니다.Use the ODBC driver to connect to the database and execute SQL statements directly.

Example

Azure Database for MySQL에 연결하고 sales 테이블의 모든 레코드를 선택합니다.Connect to a Azure Database for MySQL and select all records in the sales table. Azure Portal에서 데이터베이스에 대한 ODBC 연결 문자열을 가져올 수 있습니다.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)

관리 APIManagement API

관리 API를 사용하여 구독의 MySQL 리소스를 만들고 관리합니다.Create and manage MySQL resources in your subscription with the management API.

요구 사항Requirements

Python용 MySQL 관리 라이브러리를 설치해야 합니다.You must install the MySQL management libraries for Python.

pip install azure-mgmt-rdbms

관리 클라이언트에서 인증할 자격 증명을 얻는 방법에 대한 자세한 내용은 Python SDK 인증 페이지를 참조하세요.Please see the Python SDK authentication page for details on obtaining credentials to authenticate with the management client.

Example

MySQL 5.7 Database 리소스를 만들고 방화벽 규칙을 사용하여 IP 주소 범위에 대한 액세스를 제한합니다.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

ODBC 드라이버 및 pyodbc를 사용하여 데이터베이스에 연결하고 SQL 문을 직접 실행합니다.Use the ODBC driver and pyodbc to connect to the database and execute SQL statements directly.

Azure Database for PostgreSQL에 대해 자세히 알아보세요.Learn more about Azure Database for PostgreSQL.

Client ODBC 드라이버 및 pyodbcClient ODBC driver and pyodbc

Azure Database for PostgreSQL에 액세스하는 데 권장되는 클라이언트 라이브러리는 Microsoft ODBC 드라이버 및 pyodbc입니다.The recommended client library for accessing Azure Database for PostgreSQL is the Microsoft ODBC driver and pyodbc.

Example

Azure Database for PostgreSQL에 연결하고 SALES 테이블의 모든 레코드를 선택합니다.Connect to a Azure Database for PostgreSQL and select all records in the SALES table. Azure Portal에서 데이터베이스에 대한 ODBC 연결 문자열을 가져올 수 있습니다.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)

관리 APIManagement API

요구 사항Requirements

Python용 PostgreSQL 관리 라이브러리를 설치해야 합니다.You must install the PostgreSQL management libraries for Python.

pip install azure-mgmt-rdbms

관리 클라이언트에서 인증할 자격 증명을 얻는 방법에 대한 자세한 내용은 Python SDK 인증 페이지를 참조하세요.Please see the Python SDK authentication page for details on obtaining credentials to authenticate with the management client.

Example

이 예제에서는 기존 Postgres 서버에 새 Postgres 데이터베이스를 만듭니다.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()