MAMP环境下phpMyAdmin数据库连接失败求助(错误码HY000/1045)
Hey there, let's work through this MySQL connection issue in MAMP—since you've already checked the basics like credentials and permissions, let's focus on MAMP-specific quirks that often trip people up:
1. Double-check MAMP's MySQL Port
MAMP doesn’t use the default 3306 port for MySQL by default—it usually uses 8889. When you tried localhost:3306, you might’ve been targeting a system-wide MySQL instance (if installed) instead of MAMP’s own database server.
- Open MAMP and look at the main interface: you’ll see the MySQL port listed right next to the "Start Servers" button.
- Update your connection code to use that port. For example, if the port is 8889:
$mysql_hostname = "localhost:8889"; $mysql_user = "root"; $mysql_password = "mypassword"; $mysql_database = "mydatabase"; $prefix = ""; // Combine connection and database selection into one step for simplicity $bd = mysqli_connect($mysql_hostname, $mysql_user, $mysql_password, $mysql_database) or die("Could not connect database");
2. Confirm Permissions Are Actually Applied
Sometimes granting permissions in phpMyAdmin doesn’t take effect right away. Let’s make sure the changes stick:
- Open phpMyAdmin, go to the User accounts tab.
- Find the
root@localhostuser, click Edit privileges. - Ensure all privileges are checked, then click Go to save.
- Restart MAMP’s MySQL service (stop and start servers again) to refresh the permission cache.
3. Test Credentials Directly via MAMP's MySQL Terminal
Let’s rule out password mismatches by testing directly in the terminal:
- In MAMP, click Open start page → navigate to Tools → MySQL Terminal.
- When prompted, run this command and enter your password when asked:
mysql -u root -p - If you can log in successfully, your credentials are correct—so the issue is in how your PHP code is connecting. If you can’t log in, reset the root password in phpMyAdmin:
- Go to User accounts → select
root@localhost→ Edit privileges. - Scroll to Change password, enter
mypassword(match what’s in your code), and save.
- Go to User accounts → select
4. Ensure You're Connecting to MAMP's MySQL Instance
If you have a system-wide MySQL installed, your PHP might be trying to connect to that instead of MAMP’s. To confirm:
- Check that MAMP’s MySQL process is running (look for the green light in the MAMP interface).
- Verify your PHP environment uses MAMP’s PHP (not the system’s): create a
phpinfo.phpfile with<?php phpinfo(); ?>, access it via MAMP’s local server (e.g.,http://localhost:8888/phpinfo.php), and check themysqlisection for the MySQL port—it should match MAMP’s port.
内容的提问来源于stack exchange,提问作者Manuel

