phpMyAdmin连接拒绝:ASP.NET MVC应用数据库连接问题求助
Hey! Let's work through this connection refused problem you're hitting with your ASP.NET MVC app, MAMP 4.4.1, and phpMyAdmin. I've dealt with similar issues on Mac, so here's what to check step by step:
- Fire up MAMP and double-check that the MySQL service shows a green indicator (meaning it's active). If it's gray, stop both Apache and MySQL, wait 10 seconds, then restart them—sometimes MySQL fails to start quietly.
- Note the port MySQL is using: MAMP defaults to either
3306(standard) or8889(non-standard). You can confirm this in MAMP's Preferences > Ports tab—don't skip this, port mismatches are a super common culprit.
web.config Connection String Your connection string was cut off, but let's make sure it's formatted correctly for MySQL (since phpMyAdmin is managing a MySQL database). A valid, complete connection string should look like this:
<connectionStrings> <add name="SchoolSystem" connectionString="server=localhost;port=YOUR_MYSQL_PORT;database=YOUR_DATABASE_NAME;uid=YOUR_DB_USERNAME;password=YOUR_DB_PASSWORD;" providerName="MySql.Data.MySqlClient" /> </connectionStrings>
- Replace
YOUR_MYSQL_PORTwith the exact port you found in MAMP (e.g.,8889if that's what's listed) - Double-check that
YOUR_DATABASE_NAMEmatches the name of the database you created in phpMyAdmin (case-sensitive sometimes!) - Use the right credentials: MAMP's default MySQL username and password are both
root, but if you changed them when setting up phpMyAdmin, use those custom values instead.
To rule out issues with your ASP.NET app, connect to MySQL via terminal:
mysql -h localhost -P YOUR_MYSQL_PORT -u root -p
When prompted, enter your MySQL password. If this command fails with "connection refused", the problem is with MAMP/MySQL, not your app—go back to step 1, or check if another service is using the same port (you can use lsof -i :PORT_NUMBER to see what's using a port on Mac).
- On macOS, head to System Preferences > Security & Privacy > Firewall and make sure MAMP or the MySQL process isn't being blocked. If you're unsure, temporarily disable the firewall (just for testing) to see if that fixes the issue.
- Also, ensure no other apps (like another MySQL instance, or a VPN) are hogging the port you're using for MAMP's MySQL.
- Open phpMyAdmin and scroll to the bottom of the page—look for the server info section. It should show the same host (
localhost) and port as you're using in your connection string. If phpMyAdmin is using a different socket or port, that means it's pointing to a different MySQL instance than the one your app is trying to connect to.
Make sure your ASP.NET MVC project has the latest MySql.Data NuGet package installed. Outdated drivers can cause compatibility issues with MAMP's MySQL version. In VS for Mac, right-click your project > Manage NuGet Packages, search for MySql.Data, and update it if there's a newer version available.
内容的提问来源于stack exchange,提问作者KWCham

