1

I am trying to;

  1. Run the python code triggered by Cosmos DB when cosmos DB receives the data..
  2. The python code in Azure Functions has code to ingest data from Azure MySQL.

What I have done are;

Do you know how to install mysql module for Python to Azure Functions and connect to the database?

Thanks!

sauceishere
  • 326
  • 5
  • 15
  • Any progress now? – Jay Gong Nov 21 '17 at 01:26
  • Jay, Thanks. I will work on this in a couple of hours later. will post a feedback. – sauceishere Nov 21 '17 at 01:54
  • Run this "python -m pip install MySQLdb" and received the below. InsecurePlatformWarning Could not find a version that satisfies the requirement MySQLdb (from versions: ) No matching distribution found for MySQLdb – sauceishere Nov 21 '17 at 07:24
  • Please change the module to pyodbc and try again. Please refer to this thread about connecting mysql db via pyodbc. https://stackoverflow.com/questions/3982174/pyodbc-and-mysql – Jay Gong Nov 21 '17 at 07:32
  • Thanks. pyodbc has been successfully installed, but Functions log like "2017-11-21T10:16:53.927 Exception while executing function: Functions.alertemail. Microsoft.Azure.WebJobs.Script: ." – sauceishere Nov 21 '17 at 10:19

1 Answers1

1

According to your description ,I think your issue is about how to install the Python third-party module in the Azure function app.

Please refer to the steps as below :

Step 1 :

login kudu : https://Your_APP_NAME.scm.azurewebsites.net/DebugConsole.

Run Below command in d:/home/site/wwwroot/<your function name> folder.(will take some time)

python -m virtualenv myvenv

Step 2 :

Load the env via the below command in env/Scripts folder.

activate.bat

Step 3 :

Your shell should be now prefixed by (env).

Update pip

python -m pip install -U pip

Install what you need

python -m pip install MySQLdb

Step 4 :

In your code, update the sys.path to add this venv:

import sys, os.path
sys.path.append(os.path.abspath(os.path.join(os.path.dirname( __file__ ), 'env/Lib/site-packages')))

Then connect to mysql db via the snippet of code below

#!/usr/bin/python
import MySQLdb

# Connect
db = MySQLdb.connect(host="localhost",
                     user="appuser",
                     passwd="",
                     db="onco")

cursor = db.cursor()

# Execute SQL select statement
cursor.execute("SELECT * FROM location")

# Commit your changes if writing
# In this case, we are only reading data
# db.commit()

# Get the number of rows in the resultset
numrows = cursor.rowcount

# Get and display one row at a time
for x in range(0, numrows):
    row = cursor.fetchone()
    print row[0], "-->", row[1]

# Close the connection
db.close()

Hope it helps you.

Jay Gong
  • 23,163
  • 2
  • 27
  • 32