MySQL安装后连接失败求助:ERROR 2005及Not Connected问题
Hey there, let's work through this MySQL connection issue step by step—those ERROR 2005 messages are almost always tied to basic service, configuration, or permission hiccups, so we can get this sorted out quickly.
First off, make sure the MySQL service is actually running. If the service isn't started, even connecting to localhost will fail with a "no such host" error:
- On Windows: Open the Run dialog (Win+R), type
services.msc, find the MySQL service in the list, and check if its status is "Running". If not, right-click and select "Start". - On Linux/macOS: Open a terminal and run
systemctl status mysql(for systemd systems) orservice mysql status(older systems). If it's inactive, start it withsudo systemctl start mysqlorsudo service mysql start.
Next up, double-check your connection command syntax. It's easy to slip up here, and a tiny typo can trigger that ERROR 2005. The correct command for local root login is:
mysql -u root -h localhost -p
Or, since local connections can skip the -h localhost flag (MySQL defaults to using a local socket/pipe), you can simplify it to:
mysql -u root -p
Note: When you use -p without a password immediately after it, MySQL will prompt you to enter the password securely—don't type your password directly in the command line (it's a security risk).
Another critical check: verify MySQL's bind-address configuration. If MySQL is set to bind to a non-local IP, it won't respond to localhost connections:
- Locate your MySQL configuration file:
- Linux: Typically
/etc/mysql/my.cnfor/etc/mysql/mysql.conf.d/mysqld.cnf - Windows: Look in your MySQL installation directory (e.g.,
C:\Program Files\MySQL\MySQL Server X.X) or the hiddenProgramData\MySQLfolder
- Linux: Typically
- Open the file and find the
bind-addressline. Change it to:
(Alternatively,bind-address = 127.0.0.1bind-address = localhostworks too for most setups) - Save the file and restart the MySQL service for changes to take effect.
If the above steps don't work, check the root user's host permissions. It's possible the root@localhost user doesn't exist in MySQL's privilege tables:
- Stop the MySQL service first.
- Start MySQL in skip-grant-tables mode to bypass authentication:
- Linux: Run
sudo mysqld_safe --skip-grant-tables & - Windows: Open an admin command prompt, navigate to MySQL's
bindirectory (e.g.,cd C:\Program Files\MySQL\MySQL Server X.X\bin), then runmysqld --skip-grant-tables
- Linux: Run
- Open a new terminal/command prompt window and log into MySQL without a password:
mysql -u root - Execute these commands to create/update the
root@localhostuser with full privileges (replaceyour_secure_passwordwith your actual root password):GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' IDENTIFIED BY 'your_secure_password' WITH GRANT OPTION; FLUSH PRIVILEGES; - Exit MySQL, stop the skip-grant-tables instance, and restart the regular MySQL service.
Finally, rule out firewall/security software interference. Local firewalls or antivirus tools sometimes block MySQL's default port (3306), even for localhost connections. Try temporarily disabling your firewall/antivirus and test the connection—if it works, add a rule to allow MySQL through port 3306.
As for the "ERROR: Not Connected" when trying to create a table, that's just a side effect of not having an active database connection. Once you fix the connection issues above, you'll be able to create tables normally.
内容的提问来源于stack exchange,提问作者CloudRookie

