先停止mysql服務
/etc/init.d/mysqld stop
以安全模式啟動mysql
/usr/local/mysql/bin/mysqld_safe --skip-grant-tables --skip-networking &
/usr/local/mysql/bin/mysql
UPDATE user SET password=password('12345') WHERE user='root';
flush privileges;
授權
grant all privileges on *.* to root@'localhost' identified by '123456';
flush privileges;
有個地方要注意,/usr/local/mysql/bin/mysqld_safe 方式啟動的時侯默認是讀取/usr/local/mysql/data下的數據,如果你的data數據放在別的目錄就會出現密碼錯誤而被拒絕登陸的錯誤,
可以做個鏈接解決,(實際data目錄的地址/opt/mysql/data) ln -s /opt/mysql/data /usr/local/mysql/