连接MySQL数据库遇UNKNOWN DATABASE报错,已存在数据库无法连接
dbmdgoserver2 When Connecting to MySQL Let’s walk through the most likely reasons you’re hitting this issue—even though the database shows up in phpMyAdmin—and how to fix them:
1. Case Sensitivity Mismatch
MySQL’s database name handling varies by OS:
- On Linux/Unix, database names are case-sensitive by default (since the underlying filesystem is case-sensitive). If you created
dbmdgoserver2with mixed case (likeDBMDGoServer2) in phpMyAdmin but your code uses all lowercase, that’ll trigger the error. - On Windows, names are case-insensitive, but it’s still worth double-checking the exact spelling.
To confirm the exact name, run this query in phpMyAdmin’s SQL tab:
SHOW DATABASES;
Compare the result directly with what’s in your connection string.
2. Missing User Permissions
Your root user (or the account you’re using to connect) might not have access to dbmdgoserver2, even though phpMyAdmin can see it. phpMyAdmin often uses a separate privileged account to list databases.
Check the user’s privileges with this query:
SHOW GRANTS FOR 'root'@'localhost';
Look for a line like GRANT ALL PRIVILEGES ON dbmdgoserver2.* TO 'root'@'localhost';. If it’s missing, grant the permissions:
GRANT ALL PRIVILEGES ON dbmdgoserver2.* TO 'root'@'localhost'; FLUSH PRIVILEGES;
3. Hidden Typographical Errors
Double-check your connection string for tiny typos—extra spaces, missing characters, or swapped letters can break things. Your current line is:
con.ConnectionString = String.Format("server=localhost;user=root;database=dbmdgoserver2")
Try hardcoding the string temporarily to rule out issues with String.Format:
con.ConnectionString = "server=localhost;user=root;database=dbmdgoserver2"
Also, make sure there’s no invisible trailing space after dbmdgoserver2.
4. Connecting to the Wrong MySQL Instance
It’s possible your app is targeting a different MySQL installation than phpMyAdmin. For example:
- You might have both XAMPP/WAMP and a standalone MySQL server running.
- phpMyAdmin could be using a non-default port (not 3306).
Add the port to your connection string to test:
con.ConnectionString = "server=localhost;port=3306;user=root;database=dbmdgoserver2"
If you’re on Unix, you might also need to specify the socket path if it’s non-standard.
5. Refresh MySQL’s Cache
Occasionally, MySQL caches its list of databases. Restart your MySQL server to refresh this cache—this can fix issues where the database was recently created but the server hasn’t registered it yet.
内容的提问来源于stack exchange,提问作者DatNoobPotatoPCGuy

