MySQL connection string format
Server=localhost;Port=3306;Database=shop;User ID=app;Password=secret
Examples
| Case | Connection string |
|---|---|
| Local server | Server=localhost;Database=shop;User ID=app;Password=secret |
| Docker without TLS (development) | Server=localhost;Port=3306;Database=shop;User ID=root;Password=secret;SslMode=None;AllowPublicKeyRetrieval=True |
| Require TLS | Server=mydb.mysql.database.azure.com;Database=shop;User ID=app;Password=secret;SslMode=Required |
| Pool size and command timeout | Server=localhost;Database=shop;User ID=app;Password=secret;Maximum Pool Size=50;Default Command Timeout=60 |
Every example on this page is checked with MySqlConnector itself. Replace the passwords with your own, and keep real ones out of source control.
AllowPublicKeyRetrieval
MySQL 8 uses the caching_sha2_password plugin. Without TLS, the client needs the server's RSA public key to send the password, and MySqlConnector refuses to fetch it unless you add AllowPublicKeyRetrieval=True. It is fine for local development; in production, use TLS instead.
Where to keep it
Put it under ConnectionStrings in appsettings.json and read it with builder.Configuration.GetConnectionString("Default"). Keep passwords out of the file: user secrets in development, and an environment variable (ConnectionStrings__Default) or a secret store in production.
FAQ
MySqlConnector or MySql.Data?
Both read similar keywords. This page follows MySqlConnector, which Pomelo.EntityFrameworkCore.MySql uses.
What is the default port?
3306.
How do I use it with EF Core?
Add Pomelo.EntityFrameworkCore.MySql and call options.UseMySql(connectionString, ServerVersion.AutoDetect(connectionString)).
Does it work with MariaDB?
Yes. The connection string is the same.
Other databases
- PostgreSQL connection string
- SQL Server connection string
- SQLite connection string
- MongoDB connection string
- Redis connection string
- Connection string builder for every database