MySQL数据库服务器连接失败问题求助
ERROR 1045 (28000): Access denied for user 'root'@'localhost' 的详细方案 Hey there, let's work through this frustrating MySQL access issue together — I’ve dealt with this exact problem more times than I can count, so I know the steps to get you back up and running.
First, let’s break down the error you’re seeing: (using password: NO) tells us MySQL isn’t receiving the password you think you’re entering, even if you’re sure it’s correct. Let’s tackle this step by step:
1. Fix the basic login command first
If you’re using the terminal to log in, don’t just run mysql -u root — this defaults to no password, which is why you get that "using password: NO" note. Instead:
- Recommended (secure) method:
When prompted, type your password (it won’t show on the screen — that’s normal) and hit enter.mysql -u root -p - If you need to include the password in the command (not ideal for security, but useful for testing):
Important: No space betweenmysql -u root -pYourPasswordHere-pand your password. If your password has special characters (like!@#$), wrap it in single quotes:mysql -u root -p'YourPasswordWithSpecialChars!'
2. Reset your root password (if login still fails)
If the above doesn’t work, we’ll reset the root password entirely:
Step 1: Stop the MySQL service
- Windows (Admin Command Prompt):
net stop mysql - macOS (Homebrew-installed MySQL):
brew services stop mysql - Linux (Ubuntu/Debian):
sudo systemctl stop mysql
Step 2: Start MySQL without permission checks
This lets you log in without a password to reset it:
- Windows:
Leave this window open (it will run the MySQL process), then open a new Command Prompt window.mysqld --skip-grant-tables - macOS:
mysqld_safe --skip-grant-tables & - Linux:
sudo mysqld_safe --skip-grant-tables &
Step 3: Reset the password
In the new terminal window, run:
mysql -u root
You’ll be dropped into the MySQL command line. Now run these commands (replace YourNewSecurePassword with your desired password):
- For MySQL 5.7 and older:
USE mysql; UPDATE user SET authentication_string = PASSWORD('YourNewSecurePassword') WHERE User = 'root' AND Host = 'localhost'; FLUSH PRIVILEGES; EXIT; - For MySQL 8.0+ (PASSWORD() function is deprecated):
USE mysql; ALTER USER 'root'@'localhost' IDENTIFIED BY 'YourNewSecurePassword'; FLUSH PRIVILEGES; EXIT;
Step 4: Restart MySQL normally
- Windows: Go back to the first command window, press
Ctrl+Cto stop the skip-grant-tables process, then run:net start mysql - macOS:
brew services start mysql - Linux:
sudo systemctl start mysql
Now test logging in with your new password: mysql -u root -p
3. Fix MySQL Workbench connection issues
If Workbench still throws errors:
- Double-check your connection settings: Ensure the username is
root, host islocalhost(or127.0.0.1), and password matches the one you just set. Try unchecking "Store in Vault" and retyping the password manually. - Verify the MySQL service is running (Workbench’s home screen will show service status at the bottom).
- Try creating a brand new connection in Workbench — sometimes old cached settings cause problems.
- If you’re on Windows, make sure you’re not trying to connect to a different MySQL service (e.g., one installed with XAMPP vs. a standalone MySQL instance).
4. Quick extra checks
- Log into MySQL and run
SELECT User, Host FROM mysql.user;to confirm therootuser haslocalhostlisted as its host. If not, runALTER USER 'root'@'localhost' IDENTIFIED BY 'YourPassword';to fix it. - Ensure you’re not accidentally trying to connect to a remote MySQL server instead of your local one.
内容的提问来源于stack exchange,提问作者coderoffuture




