Questions? Feedback? powered by Olark live chat software
Hoppa över navigering

Getting Started with Azure SQL Management in Python

Senast uppdaterad: 2016-12-08
Deltagare:
Redigera på GitHub

This sample shows how to manage SQL Server using the Azure Storage Resource Provider for Python.

On this page

Run this sample

  1. If you don't already have it, install Python.

  2. We recommend using a virtual environment to run this example, but it's not mandatory. You can initialize a virtual environment this way:

    pip install virtualenv
    virtualenv mytestenv
    cd mytestenv
    source bin/activate
    
  3. Clone the repository.

    git clone https://github.com/Azure-Samples/sql-database-python-manage.git
    
  4. Install the dependencies using pip.

    cd sql-database-python-manage
    pip install -r requirements.txt
    
  5. Create an Azure service principal, using Azure CLI, PowerShell or Azure Portal.

  6. Export these environment variables into your current shell.

    export AZURE_TENANT_ID={your tenant id}
    export AZURE_CLIENT_ID={your client id}
    export AZURE_CLIENT_SECRET={your client secret}
    export AZURE_SUBSCRIPTION_ID={your subscription id}
    
  7. Run the sample.

    python example.py
    

What is example.py doing?

The sample walks you through several SQL Server operations. It starts by setting up a ResourceManagementClient and SQLManagementClient objects using your subscription and credentials.

#
# Create the Resource Manager Client with an Application (service principal) token provider
#
subscription_id = os.environ.get(
    'AZURE_SUBSCRIPTION_ID',
    '11111111-1111-1111-1111-111111111111') # your Azure Subscription Id
credentials = ServicePrincipalCredentials(
    client_id=os.environ['AZURE_CLIENT_ID'],
    secret=os.environ['AZURE_CLIENT_SECRET'],
    tenant=os.environ['AZURE_TENANT_ID']
)
resource_client = ResourceManagementClient(credentials, subscription_id)
sql_client = SqlManagementClient(credentials, subscription_id)

# You MIGHT need to add SQL as a valid provider for these credentials
# If so, this operation has to be done only once for each credential
resource_client.providers.register('Microsoft.Sql')

# Create Resource group
resource_group_params = {'location':'westus'}
resource_client.resource_groups.create_or_update(GROUP_NAME, resource_group_params)

There are also a few supporting functions (print_item, print_metrics, and print_properties).

Create a SQL Server instance

server = sql_client.servers.create_or_update(
    GROUP_NAME,
    SERVER_NAME,
    {
        'location': REGION,
        'version': '12.0', # Required for create
        'administrator_login': 'mysecretname', # Required for create
        'administrator_login_password': 'HusH_Sec4et' # Required for create
    }
)

Get a server

server = sql_client.servers.get_by_resource_group(
    GROUP_NAME,
    SERVER_NAME,
)

List servers by resource group

sql_client.servers.list_by_resource_group(GROUP_NAME)

List servers by subscription

sql_client.servers.list()

List server usages

sql_client.servers.list_usages(GROUP_NAME, SERVER_NAME)

Create a database

async_db_create = sql_client.databases.create_or_update(
    GROUP_NAME,
    SERVER_NAME,
    DATABASE_NAME,
    {
        'location': REGION
    }
)
database = async_db_create.result() # Wait for completion and return created object

Get a database

database = sql_client.databases.get(
    GROUP_NAME,
    SERVER_NAME,
    DATABASE_NAME
)

Get a list of databases

sql_client.databases.list_by_server(GROUP_NAME, SERVER_NAME)

Get a list of database usages

sql_client.databases.list_usages(GROUP_NAME, SERVER_NAME, DATABASE_NAME)

Delete a database

sql_client.databases.delete(GROUP_NAME, SERVER_NAME, DATABASE_NAME)

Delete a SQL Server instance

sql_client.servers.delete(GROUP_NAME, SERVER_NAME)

More information