Movatterモバイル変換


[0]ホーム

URL:


Connect to Azure Database for MySQL using Python

This tutorial will show you how to use Python to connect to an Azure Database for MySQL. To complete the exercise, you will need to install the following tools:

  • Python 3 (make sure to add Python to your PATH)
  • Visual Studio Code or other code editor

If you need help with installing these tools, follow the instructions in theSet up your Python beginner development environment with Visual Studio Code Microsoft Learn Module.

Install MySQL connector

  1. Open the command prompt and run the following command to install the MySQL connector for Python.

    1
    pip install mysql-connector-python
  2. Open Visual Studio Code and create a new Python file. Add the following command and selectRun.

    1
    importmysql.connector

    If MySQL Connector is installed successfully, the above code will be executed with no errors.

Get connection information

The next step is to collect the information that you need to connect to the Azure Database for MySQL. You need the server name, the database name, and the login credentials (username and password).

  1. Sign in toAzure Portal, expand the left navigation panel and selectAll resources.

    Select All resources in Azure
  2. Then, select the Database that you created in thepart 1.

  3. In theOverview tab, you can see theServer name andServer admin login name.

    Database for MySQL information

Create Connection

Open Visual Studio Code and create a new Python file nameddb1.py. Add the following code to connect to your database:

123456789
importmysql.connector# Establish the connectionconn= mysql.connector.connect(  user='username@server',  password='password',  host='server.mysql.database.azure.com',  database="demodb")print(conn)

where:

  • username is the admin login name,
  • server refers to the server name,
  • password is the password of the admin and
  • demodb is the name of the database that you created inpart 1.

Next steps

Now you are ready to start working with MySQL in Python. In the following tutorials, you will use Pyhton and SQL statements to create tables, manipulate and query data in the database.

You May Also Like

Insert data from file in Azure Database for MySQL

Insert data from file in Azure Database for MySQL

In this tutorial, you will use SQL statements to manipulate data in Azure Database for MySQL and learn how to open files in Python.

Use Python to manipulate data in Azure Database for MySQL

Use Python to manipulate data in Azure Database for MySQL

This tutorial will show you how to use Python to create tables and manipulate data in Azure Database for MySQL.

Create an Azure Database for MySQL server via the Azure portal

Create an Azure Database for MySQL server via the Azure portal

In this tutorial, you will learn how to create an Azure Database for MySQL single server, create a new database and configure firewall rules.

Manipulate data in Azure SQL Database using Python and Jupyter Notebook

Manipulate data in Azure SQL Database using Python and Jupyter Notebook

This tutorial will show you how to manipulate and query data in an Azure SQL database using Python and Jupyter Notebooks.


[8]ページ先頭

©2009-2025 Movatter.jp