You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

MySQL安装后连接失败求助:ERROR 2005及Not Connected问题

解决MySQL连接"无此主机"(ERROR 2005)问题的分步方案

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) or service mysql status (older systems). If it's inactive, start it with sudo systemctl start mysql or sudo 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:

  1. Locate your MySQL configuration file:
    • Linux: Typically /etc/mysql/my.cnf or /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 hidden ProgramData\MySQL folder
  2. Open the file and find the bind-address line. Change it to:
    bind-address = 127.0.0.1
    
    (Alternatively, bind-address = localhost works too for most setups)
  3. 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:

  1. Stop the MySQL service first.
  2. 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 bin directory (e.g., cd C:\Program Files\MySQL\MySQL Server X.X\bin), then run mysqld --skip-grant-tables
  3. Open a new terminal/command prompt window and log into MySQL without a password:
    mysql -u root
    
  4. Execute these commands to create/update the root@localhost user with full privileges (replace your_secure_password with your actual root password):
    GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' IDENTIFIED BY 'your_secure_password' WITH GRANT OPTION;
    FLUSH PRIVILEGES;
    
  5. 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 11:47:41