1) service stoped
sudo /etc/init.d/mysqld stop
2) started in safe mode
sudo mysqld_safe --skip-grant-tables &
3) user created
mysql -u root
4) setting passwd
UPDATE user SET Password=PASSWORD('my_password') where USER='root';
exit from the mysql console,
and restart the service and trying to access mysql console with
mysql -u root -p my_password
[vikram@VoltyLinux ~]$ mysql -u root -p
Enter password: *******
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using
password: YES)
解决方案
Go in as before in Step 2, that is, restart mysql daemon in safe-mode.
Issue the command
select user,host,password from mysql.user where user='root';
cut and paste output into a text editor for comparison (of the hashed password column values) before and after the following.
For each row notice the host column. In the following example I am assuming I have two rows. One row has % as host, the next has localhost
For each row that had a host variation, you will issue one command. So in my example above, I would be issuing 2 commands in total, such as:
SET PASSWORD FOR 'root'@'%' = PASSWORD('MyNewPassword');
SET PASSWORD FOR 'root'@'localhost' = PASSWORD('MyNewPassword');
run
select user,host,password from mysql.user where user='root';
cut and paste that into text editor. Notice the change to the password hashes.
Shutdown the daemon from safe mode, and restart normally.
Try to login as root with MyNewPassword
Here are a few links. The one for SET PASSWORD Syntax, and an often next step for GRANT Syntax.
For the narrow scope of this question, that is to login, the grants would not be necessary. But without proper grants to databases, one would be sandboxed, not able to do much, other than simple commands like select now();
I am not suggesting that your root user does not have privileges once the password is changed. The Grants are necessary for normal users to be sure.