Azure Function Connection to third party mysql database

Jovin Risch 1 Reputation point
2021-08-04T15:59:47.513+00:00

Hello

We want to create an API which pulls data from an third party database.

Is there an possible Way to create an connection with python to MySql Database hosted by a local Hoster?

It would be excellent if there is an way, else we have to migrate to an other serverless provider.

Thanks and kind regards,
Jovin

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
5,909 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. MayankBargali-MSFT 70,936 Reputation points Moderator
    2021-08-05T04:07:48.13+00:00

    @Jovin Risch You can connect to any third party database with azure function if the service/database is accessible over internet.
    Based on your requirement you can create function app with any of the available triggers (Timer, HTTP etc.) and write your own code to connect to the third party database.

    I do see mysql-connector for python which you can configure in requirements.txt so the packages can be installs when publishing to Azure. To start with azure python function you can refer to this document.

    1 person found this answer helpful.

  2. Jovin Risch 1 Reputation point
    2021-08-05T08:52:29.193+00:00

    @MayankBargali-MSFT

    Thanks for your replay

    I will send you the information which confirms that my statment ist true.

    *With following config we control the host:
    120784-bildschirmfoto-2021-08-05-um-103948.png

    *Here is the code:

    import os
    import mysql.connector

    RCPDB_CONNECTION = None

    def get_rcpdb_connection():
    global RCPDB_CONNECTION
    if RCPDB_CONNECTION is None or not RCPDB_CONNECTION.is_connected():
    RCPDB_CONNECTION = mysql.connector.connect(
    host=os.environ['RCPDB_HOST'],
    user=os.environ['RCPDB_USER'],
    password=os.environ['RCPDB_PASS'],
    database=os.environ['RCPDB_DB']
    )
    return RCPDB_CONNECTION

    *But we still got the Error (from Livemetrics):

    Exception while executing function: Functions.CoverLetterData <--- Result: Failure Exception: ProgrammingError: 1045 (28000): Access denied for user 'mysmarth_it'@'20.203.129.224' (using password: YES) Stack: File "/azure-functions-host/workers/python/3.9/LINUX/X64/azure_functions_worker/dispatcher.py", line 398, in _handle__invocation_request call_result = await self._loop.run_in_executor( File "/usr/local/lib/python3.9/concurrent/futures/thread.py", line 52, in run result = self.fn(*self.args, **self.kwargs) File "/azure-functions-host/workers/python/3.9/LINUX/X64/azure_functions_worker/dispatcher.py", line 602, in _run_sync_func return ExtensionManager.get_sync_invocation_wrapper(context, File "/azure-functions-host/workers/python/3.9/LINUX/X64/azure_functions_worker/extension.py", line 215, in _raw_invocation_wrapper result = function(**args) File "/home/site/wwwroot/CoverLetterData/init.py", line 121, in main data = get_coverletter_data_by_guid(guid) File "/home/site/wwwroot/CoverLetterData/init.py", line 59, in get_coverletter_data_by_guid connection = get_rcpdb_connection() File "/home/site/wwwroot/shared/rcpdb.py", line 10, in get_rcpdb_connection RCPDB_CONNECTION = mysql.connector.connect( File "/home/site/wwwroot/.python_packages/lib/site-packages/mysql/connector/init.py", line 179, in connect return MySQLConnection(*args, **kwargs) File "/home/site/wwwroot/.python_packages/lib/site-packages/mysql/connector/connection.py", line 95, in init self.connect(**kwargs) File "/home/site/wwwroot/.python_packages/lib/site-packages/mysql/connector/abstracts.py", line 716, in connect self._open_connection() File "/home/site/wwwroot/.python_packages/lib/site-packages/mysql/connector/connection.py", line 208, in _open_connection self._do_auth(self._user, self._password, File "/home/site/wwwroot/.python_packages/lib/site-packages/mysql/connector/connection.py", line 144, in _do_auth self._auth_switch_request(username, password) File "/home/site/wwwroot/.python_packages/lib/site-packages/mysql/connector/connection.py", line 177, in _auth_switch_request raise errors.get_exception(packet)


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.