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
- The password of an Entra login is an access token for the audience
https://ossrdbms-aad.database.windows.net. - The token is sent as it is, so the client needs TLS and the cleartext authentication plugin.
- The server compares the identity inside the token with the identity behind the MySQL user. A token never opens a session as someone else.
- A token lasts about one hour. A client that does not renew it must be reconnected with a new one.
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'@'%';
- Creating the user gives no right on any database: the grants are separate.
- Names are case sensitive. A name longer than 32 characters needs an alias:
CREATE AADUSER 'a-very-long-identity-name' AS 'short_name';
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:
- Copy a token:
az account get-access-token --resource-type oss-rdbms --query accessToken -o tsv | Set-Clipboard - New session: host
mysql01.mysql.database.azure.com, port 3306, user = the Entra user or group name, password = the token. - 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
- Server, group, identity and database names are generic examples.
- From January 2027 the service refuses tokens issued for an audience other than
ossrdbms: always ask forhttps://ossrdbms-aad.database.windows.net. - Reference: Set up Microsoft Entra authentication for Azure Database for MySQL - Flexible Server.