Windwos10 重置MySQL密码

# 1. 确保在正确目录
cd "C:\Program Files\MySQL\MySQL Server 8.0\bin"

# 2. 停止服务
net stop MySQL80
Get-Process mysqld -ErrorAction SilentlyContinue | Stop-Process -Force

# 3. 清理所有可能的数据目录
Write-Host "清理数据目录..." -ForegroundColor Yellow
Remove-Item "C:\Program Files\MySQL\MySQL Server 8.0\data" -Recurse -Force -ErrorAction SilentlyContinue
Remove-Item "C:\ProgramData\MySQL\MySQL Server 8.0\Data" -Recurse -Force -ErrorAction SilentlyContinue

# 4. 创建正确的配置文件
Write-Host "创建配置文件..." -ForegroundColor Yellow
@"
[mysqld]
basedir=C:/Program Files/MySQL/MySQL Server 8.0
datadir=C:/ProgramData/MySQL/MySQL Server 8.0/Data
port=3306
character-set-server=utf8mb4
default-storage-engine=INNODB
default_authentication_plugin=mysql_native_password

[mysql]
default-character-set=utf8mb4

[client]
default-character-set=utf8mb4
"@ | Out-File "C:\ProgramData\MySQL\MySQL Server 8.0\my.ini" -Encoding UTF8

# 5. 重新创建数据目录
New-Item -ItemType Directory -Path "C:\ProgramData\MySQL\MySQL Server 8.0\Data" -Force
icacls "C:\ProgramData\MySQL\MySQL Server 8.0\Data" /grant "Everyone:(OI)(CI)F" /T

# 6. 无密码初始化
Write-Host "初始化MySQL..." -ForegroundColor Yellow
.\mysqld --initialize-insecure --console

# 7. 启动服务
Write-Host "启动服务..." -ForegroundColor Yellow
net start MySQL80

# 8. 测试登录
Write-Host "测试无密码登录..." -ForegroundColor Yellow
.\mysql -u root
-- 切换到mysql数据库
USE mysql;

-- 重置root密码和使用旧认证方式
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'MyNewPassword123!';

-- 或者如果上面失败,使用传统的更新方法
UPDATE user SET authentication_string='', plugin='mysql_native_password' WHERE user='root';

-- 刷新权限
FLUSH PRIVILEGES;

-- 退出
EXIT;

留下评论

您的邮箱地址不会被公开。 必填项已用 * 标注