Mac系统下MySQL安装后无法登录问题求助
Hey there, I totally get how frustrating this is—brew-installed MySQL has a quirk where the root user often uses socket authentication by default instead of password-based, even if you set a password initially. Let's walk through fixing this step by step:
Step 1: Stop the Running MySQL Service
First, make sure MySQL isn't running in the background:
brew services stop mysql
Step 2: Start MySQL in Safe Mode (Bypass Authentication)
We'll launch MySQL with a flag that skips permission checks, so we can log in without a password:
mysqld_safe --skip-grant-tables &
Step 3: Log Into MySQL Without Password
Now you can access the MySQL shell directly:
mysql -uroot
Step 4: Update Root User's Authentication & Password
Switch to the system mysql database, then update the root user to use password authentication and set a new password (replace your-secure-password with your desired password):
USE mysql; UPDATE user SET authentication_string=PASSWORD('your-secure-password'), plugin='mysql_native_password' WHERE User='root' AND Host='localhost'; FLUSH PRIVILEGES; EXIT;
Note: Using mysql_native_password ensures compatibility with most standard login methods, which avoids the "access denied" errors you're seeing.
Step 5: Restart MySQL Normally
First shut down the safe-mode instance, then start the service back up:
mysqladmin -uroot -p shutdown # Enter your new password when prompted brew services start mysql
Step 6: Test the Login
Now try logging in again—this should work:
mysql -uroot -p
You can also test the sudo variant if needed, and it should recognize your new password now.
内容的提问来源于stack exchange,提问作者sheetal

