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
mysqluser):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 withmysql_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:
Enter the password you set earlier, and you should gain access to the MySQL command line.sudo mysql -u root -p - Now you can run
mysql_secure_installationto 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
相关产品推荐
相关产品推荐

