Connect to Azure SQL Database¶
Not tested
tdslite's tests run against SQL Server 2017 to 2025, not against Azure SQL. These steps follow Microsoft's documentation and the TLS guide, which is tested against SQL Server. If you try them, please report what happened in a GitHub issue, whether it worked or not.
Azure SQL Database accepts only encrypted connections, so this is the TLS guide with Azure's settings. You need:
- A device or program that tdslite can encrypt the connection on: ESP32 or ESP8266 (Arduino core), ESP-IDF, or desktop with Boost.ASIO and OpenSSL. Boards without a TLS client, such as the Arduino Uno with an Ethernet shield, can't connect to Azure SQL.
- A database with SQL authentication on. tdslite logs in with a user name and a password; it doesn't support Microsoft Entra ID (Azure AD) logins.
Set up Azure¶
- Allow the device's address. In the Azure portal, open the SQL server (the logical server, not the database), then Security → Networking. Under Public access, select Selected networks, and add a firewall rule with the public IP address that your device's network uses. Your router's address is the one that counts, not the device's local one.
- Keep the connection policy at Default or Proxy, on the same page. With Redirect, the server tells the client to reconnect to another address, which tdslite doesn't do.
-
Note the server name and a SQL login. The server name is
<server>.database.windows.net, the login is the admin login you chose when creating the server, or one you create:-- In the master database CREATE LOGIN device WITH PASSWORD = '<a long random password>'; -- In your database CREATE USER device FOR LOGIN device; GRANT INSERT ON dbo.readings TO device;Give devices a login with only the permissions they need, as in Log sensor readings.
Get the CA certificates¶
Azure SQL's certificates are issued by public CAs. Trust the root CAs that Microsoft lists for Azure SQL Database; at the time of writing, they are DigiCert Global Root G2 and Microsoft RSA Root Certificate Authority 2017. Download both as PEM and put them, one after the other, into one string. Check Microsoft's Azure TLS certificate changes page for the current list, as Azure changes CAs from time to time.
Connect¶
Set server_name to the full host name, which the certificate lists, and
db_name to your database. In Azure SQL Database, a connection stays in the
database it logs in to (USE can't switch databases), so without db_name
you end up in master.
params.server_name = "<server>.database.windows.net";
params.port = 1433;
params.db_name = "<your database>";
params.user_name = "device";
params.password = "<password>";
Then set up TLS for your platform:
#include <WiFiClientSecure.h>
#include <tdslite.h>
// DigiCert Global Root G2, then Microsoft RSA Root Certificate Authority 2017
static const char azure_ca_pem [] = R"(-----BEGIN CERTIFICATE-----
...
-----END CERTIFICATE-----
-----BEGIN CERTIFICATE-----
...
-----END CERTIFICATE-----
)";
static tdsl::uint8_t net_buf [4096] = {};
static tdsl::arduino_driver<WiFiClientSecure> driver{net_buf};
// In setup(), after Wi-Fi is up and the clock is set over NTP:
auto & tls = driver.network().client();
tls.setCACert(azure_ca_pem);
static const char * alpn [] = {"tds/8.0", nullptr};
tls.setAlpnProtocols(alpn);
driver.network().set_tls_client(true);
#include <ESP8266WiFi.h>
#include <tdslite.h>
static const char azure_ca_pem [] = R"(...both certificates, as for the ESP32...)";
static BearSSL::X509List trusted_ca{azure_ca_pem}; // must outlive the connection
static tdsl::uint8_t net_buf [4096] = {};
static tdsl::arduino_driver<BearSSL::WiFiClientSecure> driver{net_buf};
// In setup(), after Wi-Fi is up and the clock is set over NTP:
auto & tls = driver.network().client();
tls.setTrustAnchors(&trusted_ca);
tls.setBufferSizes(16384, 2048);
driver.network().set_tls_client(true, 2048);
params.packet_size = 2048;
Connect an ESP8266 explains the buffer sizes.
ESP-IDF's certificate bundle includes the public root CAs, so there's no
PEM to embed. Enable CONFIG_MBEDTLS_CERTIFICATE_BUNDLE (on by default),
then:
The system's certificate store usually has the public root CAs:
Then call driver.connect(params) as usual.
Errors you may see¶
Azure reports these to the info callback, with connect returning
login_failed:
| Error | Meaning | What to do |
|---|---|---|
| 40615 | The firewall doesn't allow the device's address. The message names the address the server saw. | Add a firewall rule for that address. |
| 18456 | The user name or password is wrong, or the login has no user in db_name. |
Check the login, and create its user in your database. |
| 40613 | The database isn't available right now, e.g. a serverless database resuming from auto-pause. | Retry after a minute. |
| 40531 | The server name couldn't be determined. | Use the full <server>.database.windows.net name. If that doesn't help, try device@<server> as the user name. |
If connect returns connection_failed with no server message, the TLS
connection didn't come up: check the CA certificates, the clock, and the
host name, as in Troubleshoot.
Keep in mind¶
- Serverless databases pause when unused, and the first connection after a pause fails while the database resumes. A device that logs every few minutes keeps it from pausing, which costs compute time; if that matters, log less often or use a provisioned database.
- Idle connections are closed after a while by Azure's gateway. Expect
network_erroron the next request, and reconnect, as in Log sensor readings. - Transactions must start and end within one request on a TLS connection; see Connect with TLS.
Related¶
- Connect with TLS
- Direct connections and gateways: whether devices should connect to Azure SQL directly
- TDS versions and encryption