MySQL connection fails when database name has Chinese characters

I’m working on a C# application that needs to connect to MySQL databases. Everything works fine with regular database names, but I keep getting errors when the database name contains Chinese characters.

Here’s my connection code:

string connectionString = "server=127.0.0.1;uid=root;pwd=mypassword;database='中文数据库名'";
MySqlConnection dbConnection = new MySqlConnection(connectionString);

When I try to open the connection, I get this exception: MySql.Data.MySqlClientException(0x80004005)

The error says something like: Authentication failed - Unknown database ‘???’. It looks like all the Chinese characters got replaced with question marks.

I think this might be an encoding problem. Maybe I need to specify UTF-8 or some other character set in my connection string? Has anyone dealt with this before? I’m not sure what parameter to add or how to handle Chinese characters properly in MySQL connection strings.

did u try URL encoding the db name in ur connection string? special chars sometimes need encoding even when charset’s set right. also check ur mysql connector version - i had chinese character issues until i updated to the latest mysql.data package.

Had this exact problem last month! Add CharSet=utf8mb4 to your connection string, but first check if the database actually exists with those Chinese characters. Sometimes it’s not an encoding issue - the db name got corrupted when it was created. Run show databases; in MySQL Workbench to make sure the actual name matches what you think it is.

I faced a similar issue recently with a client’s database that utilized Chinese characters. It’s indeed related to encoding—specifically, MySQL requires the connection string to specify that you’re using UTF-8 encoding. You should include charset=utf8mb4 in your connection string and eliminate the single quotes around the database name, which aren’t necessary. Your revised connection string will look like this: csharp string connectionString = "server=127.0.0.1;uid=root;pwd=mypassword;database=中文数据库名;charset=utf8mb4"; Additionally, ensure that your C# source file is saved with UTF-8 encoding to avoid any character discrepancies before it reaches the MySQL connector. In Visual Studio, navigate to File > Advanced Save Options and confirm it’s set to Unicode (UTF-8 with signature). This adjustment resolved my authentication issues, as utf8mb4 supports the complete range of Unicode characters that regular utf8 does not.

Check your MySQL server config first - this might not be just a connection string problem. Your MySQL server needs to actually support UTF-8. Run SHOW VARIABLES LIKE 'character_set%'; in your MySQL console to check. If character_set_server isn’t utf8mb4, that’s probably why your Chinese database names aren’t working. Did you specify the character set when you created the database? If not, fixing your connection string won’t help. You’ll need to recreate the database with CREATE DATABASE 中文数据库名 CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; so it can actually store and recognize Chinese characters.

The problem’s likely in how your app handles Unicode strings before they hit MySQL. Sure, charset parameters matter, but there’s another sneaky issue - C# string literals can get corrupted during compilation. I ran into this with Japanese database names and found that Visual Studio sometimes messes up Unicode characters in string constants based on your project settings. Instead of hardcoding the database name in your connection string, try building it dynamically from a UTF-8 source like a config file or resource. Also check your MySQL client library version - older MySql.Data versions had Unicode bugs even when the server was configured right.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.