macOS安装MySQL v8.0.11后root@localhost访问被拒绝求助
Hey there, I’ve dealt with this exact issue when setting up MySQL 8.0 on High Sierra—let’s walk through the fix step by step. The key thing to know is MySQL 8.0 generates a temporary root password automatically during installation (you didn’t miss a step, it just doesn’t prompt you for it upfront!).
Step 1: Locate the temporary root password
MySQL stores the initial temporary password in its error log. Here’s how to grab it quickly:
- Open Terminal and run this command to search the log for the password:
You’ll see a line like:cat /usr/local/mysql/data/$(hostname).err | grep "temporary password"[Note] A temporary password is generated for root@localhost: XXXXXXXXXX - Copy that random string (it might include special characters like
!or#—don’t worry, just copy it as-is).
Step 2: Log in with the temporary password
Now use that password to access MySQL:
- Run this command in Terminal:
mysql -u root -p - When prompted, paste the temporary password you copied and hit Enter. You should now be inside the MySQL shell.
Step 3: Set a new root password
Once logged in, you’ll need to replace the temporary password with one you remember. MySQL 8.0 enforces password complexity by default, so pick a password with at least 8 characters, including uppercase, lowercase, numbers, and a special character.
- Run this SQL command (replace
your-new-passwordwith your chosen password):ALTER USER 'root'@'localhost' IDENTIFIED BY 'your-new-password'; - If you want a simpler password for local testing (not recommended for production!), first loosen the password policy:
Then run theSET GLOBAL validate_password.policy = LOW; SET GLOBAL validate_password.length = 6;ALTER USERcommand again with your simpler password.
Step 4: Verify the fix
Exit the MySQL shell with:
exit;
Then try logging in again with your new password:
mysql -u root -p
Enter your new password, and you should be logged in successfully.
Troubleshooting tip
If you run into issues with the MySQL service (like needing to restart it), you can:
- Use the MySQL preference pane in System Preferences to stop/start the service.
- Or use Terminal commands:
sudo /usr/local/mysql/support-files/mysql.server stop sudo /usr/local/mysql/support-files/mysql.server start
内容的提问来源于stack exchange,提问作者Faheem Hassan Zunjani

