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

MySQL安装故障排查:root用户本地登录权限问题求助

MySQL 8.0.31 on Ubuntu 22.04: Fix Access Denied & Safe Mode Startup Errors

Step 1: Fix the Missing Socket Directory Error

The Directory '/var/run/mysqld' don't exists error occurs because the required socket directory is missing or has incorrect permissions. Resolve this first:

  • Stop the running MySQL service:
    sudo systemctl stop mysql
    
  • Create the missing directory:
    sudo mkdir -p /var/run/mysqld
    
  • Set correct ownership for the directory (must be owned by the mysql user):
    sudo chown mysql:mysql /var/run/mysqld
    

Step 2: Bypass Grant Tables to Reset Root Credentials

Now you can start MySQL in safe mode and fix the access denied issue:

  • Start MySQL with grant tables skipped:
    sudo mysqld_safe --skip-grant-tables &
    
  • Log into MySQL without a password:
    mysql -u root
    
  • Switch to the system database:
    USE mysql;
    
  • Critical for MySQL 8.0: The old PASSWORD() function is deprecated. Reset the root user with mysql_native_password (compatible with standard password login):
    ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'your-new-strong-password';
    FLUSH PRIVILEGES;
    
  • Exit MySQL:
    EXIT;
    
  • Kill the safe mode process and restart the regular MySQL service:
    sudo pkill mysqld
    sudo systemctl start mysql
    

Step 3: Verify Login & Complete Setup

  • Log in with your new password:
    sudo mysql -u root -p
    
    Enter the password you set earlier, and you should gain access to the MySQL command line.
  • Now you can run mysql_secure_installation to finish the security setup without errors.

Why Did the Original No-Password Login Fail?

On Ubuntu 22.04, MySQL 8.0 defaults to using the auth_socket plugin for the root user instead of password authentication. This plugin only allows system-level root users (via sudo) to connect without a password—but if the plugin was modified during installation or a previous reset, this behavior breaks, leading to the Access denied error even with sudo mysql.

内容的提问来源于stack exchange,提问作者Mandar Gite

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.05 14:20:28