LLI Wiki — My Digital Knowledge Base

Azure MySQL: Entra ID login for a managed identity, and HeidiSQL with a token

Let a managed identity log in to Azure Database for MySQL with an Entra ID token, create the mapped user, and open HeidiSQL with a token.

How to let a managed identity (or any Entra ID account) log in to an Azure Database for MySQL flexible server without a password, and how to open a graphical client such as HeidiSQL with a token. For the mysql and mysqlsh clients, see Azure MySQL: connect (password or Entra ID token) and import with mysqlsh.

How it works

Check the server

Three things must be in place on the server. All can be read without changing anything:

# Entra admin of the server, and the identity the server uses to read the directory
az mysql flexible-server ad-admin list -g <resource-group> -s <server>

# OFF = MySQL and Entra logins, ON = Entra only
az mysql flexible-server parameter show -g <resource-group> -s <server> -n aad_auth_only
What Expected
Authentication mode "MySQL and Microsoft Entra" or "Microsoft Entra only"
Server identity A user-assigned managed identity with the Directory Readers role (or the Graph permissions User.Read.All, GroupMember.Read.All, Application.Read.All)
Entra admin One user or group. A group is easier: every member can administer

Create the user for the identity

Only a session opened with an Entra login can create Entra users. Connect as the Entra admin: the user name is the admin user or group name, the password is your own token.

mysql -h mysql01.mysql.database.azure.com --enable-cleartext-plugin \
  --user grp-mysql-admins \
  --password=$(az account get-access-token --resource-type oss-rdbms --output tsv --query accessToken)

Then create the user under the display name of the identity and grant what it needs:

CREATE AADUSER 'uami-app-01';
GRANT SELECT ON appdb.* TO 'uami-app-01'@'%';
FLUSH PRIVILEGES;

-- Check
SELECT user, host, plugin FROM mysql.user WHERE user = 'uami-app-01';   -- plugin = aad_auth
SHOW GRANTS FOR 'uami-app-01'@'%';

Get a token

The token of the account signed in to the Azure CLI:

az account get-access-token --resource-type oss-rdbms --output tsv --query accessToken

The token of a managed identity, from a machine that carries it. This call to the instance metadata service does not touch the Azure CLI session:

$uri = 'http://169.254.169.254/metadata/identity/oauth2/token?api-version=2018-02-01' +
       '&resource=https://ossrdbms-aad.database.windows.net&client_id=<CLIENT_ID>'
(Invoke-RestMethod -Headers @{ Metadata = 'true' } -Uri $uri).access_token

az login --identity --client-id <CLIENT_ID> followed by the first command also works, but it replaces your account in the Azure CLI until the next az login.

HeidiSQL

HeidiSQL cannot ask for a token itself. By hand:

  1. Copy a token: az account get-access-token --resource-type oss-rdbms --query accessToken -o tsv | Set-Clipboard
  2. New session: host mysql01.mysql.database.azure.com, port 3306, user = the Entra user or group name, password = the token.
  3. SSL tab: use SSL. Advanced tab: enable cleartext authentication.

Or start it already connected. HeidiSQL (checked with 12.21) takes the same settings on its command line, and does not save a session started this way, so the token is never written to its settings:

$token = az account get-access-token --resource-type oss-rdbms --query accessToken -o tsv
Start-Process 'C:\Tools\HeidiSQL\heidisql.exe' -ArgumentList @(
    '-d="mysql01 (Entra)"', '-n=0',
    '-h=mysql01.mysql.database.azure.com', '-P=3306',
    '-u=grp-mysql-admins', "-p=$token",
    '-cte=1',   # cleartext authentication
    '-ssl=1'
)

To open the session as a managed identity, use the token of that identity and its MySQL user name in -u.

While HeidiSQL runs, the token is on its command line: the same Windows user and local administrators can read it. It expires after about an hour.

Errors

Error Meaning Fix
9010 Only connections established by Azure Active Directory accounts can create Azure Active Directory users CREATE AADUSER run from a session opened with a MySQL password Connect as the Entra admin
1396 Operation CREATE USER failed for '...'@'%' The user already exists Nothing to do, check with SHOW GRANTS
1045 Access denied ... (using password: YES) with a token No MySQL user for this identity, or cleartext authentication off in the client CREATE AADUSER, or enable cleartext authentication
Azure AD access token is not valid for user '...' with OID '...' The token belongs to another identity than the MySQL user Take the token of that identity
1044 Access denied for user ... to database Login accepted, no grant on that database GRANT as the Entra admin

Notes

Mastodon LLIMastodon SocialFosstodon