Fixed: "Option not supported (Parameter ...)" from a Semicolon in a MySQL Password in .NET C#
Option not supported (Parameter 'def;sslmode') looked like a typo in an option name, but def was the end of my password. A ; ends a value in a connection string, so the driver read the rest of the password as a new option, and put it in the exception message. That message can end up in your logs.
What I Was Doing
I gave a MySQL account the password abc;def, as a test of special characters, and used it as it is in a connection string, on MySql.Data 26.10.0 and .NET 10:
Server=127.0.0.1;Port=3306;Database=inventory;User ID=quote_demo;Password=abc;def;SslMode=Required
dotnet run test-connection.cs
What I Expected
A connection: the password was correct.
What I Got
Unhandled exception. System.ArgumentException: Option not supported (Parameter 'def;sslmode')

What I Checked
What's in the parameter. def;sslmode is the tail of the password, joined to the next option's name. So the parser saw Password=abc, then an option called def;sslmode, with the value Required.
Other characters. I tried each with its own account:
| Password | In the connection string | Result |
|---|---|---|
abc;def | Password=abc;def | Option not supported |
abc;def | Password='abc;def' | Connects |
abc;def | Password="abc;def" | Connects |
a=b | Password=a=b | Connects |
a b | Password=a b | Connects |
| A space at each end | No quotes | Access denied: the spaces are dropped |
| A space at each end | Password=' ab ' | Connects |
a'b | Password='a''b' | Connects: the ' is doubled |
a'b | Password="a'b" | Connects |
a"b | Password="a""b" | Connects: the " is doubled |
a"b | Password='a"b' | Connects |
Why it matters. MySQL's IDENTIFIED BY RANDOM PASSWORD can produce ; and quotes, so this isn't rare. And because the exception is thrown where the string is read, in an app it's the first thing in the log. That line holds part of your password.
Why It Happens
A connection string is a list of name=value pairs split by ;. It works the same way as a CSV line: a value holding the separator has to be quoted.
The Fix
Quote the value. Use single quotes, and double any ' inside:
Server=127.0.0.1;Port=3306;Database=inventory;User ID=quote_demo;Password='abc;def';SslMode=Required
Or let the driver write the string, which gets every case right:
// The builder quotes every value for you, whatever characters it holds.
var builder = new MySqlConnectionStringBuilder
{
Server = "127.0.0.1",
Port = 3306,
Database = "inventory",
UserID = "inventory_app",
Password = "a'b\"c;d",
SslMode = MySqlSslMode.Required,
};
Console.WriteLine(builder.ConnectionString);
server=127.0.0.1;port=3306;database=inventory;user id=inventory_app;password="a'b""c;d";sslmode=Required
Or generate passwords without these characters. Hex from openssl rand -hex 24 has none, as How to Generate a Strong Random Password in the Terminal shows.
If that message reached your logs, treat the password as exposed, and change it.
After the Fix
dotnet run test-connection.cs
Connected as quote_demo@% to MySQL 9.7.4
TLS: TLSv1.2

Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Escaping with a backslash: abc\;def | The ; still ends the value, and you get the same error | Quote the value |
| Quoting the value in the shell only | The shell removes those quotes | Quote inside the string: "…Password='abc;def';…" |
Assuming a password with no ; is safe | Spaces at the ends are dropped too | Generate hex passwords |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
Access denied once it's quoted | The account's password has other characters than you think | The fix |
| It works locally, and fails in a container | The tool that sets the variable handles quotes differently | Print the length of the value, not the value |
Incorrect value in Connection String near 'Password' | An opening quote with no closing quote | Check the quotes match |
Conclusion
Option not supported with a strange parameter usually means a ; in a value. Quote it, or use MySqlConnectionStringBuilder, and check whether that message reached your logs.
Additional Resources
- Creating a Connector/NET Connection String — the format.
- Connector/NET API reference — the
MySqlConnectionStringBuilderclass.
