使用mysqladmin
# ./mysqladmin -u root password ‘newpassword‘ # ./mysqladmin -u root -h host_name password ‘newpassword‘
Usually mysqladmin‘s path is /usr/bin, host_name is your real host name, e.g. localhost.localdomain.
password后面的引号不是必须的,不过如果密码包含空格或者一些特殊的符号,需要用引号。
利用mysql SET PASSWORD命令
# ./mysql -u root mysql> SET PASSWORD FOR ‘root‘@‘localhost‘ = PASSWORD(‘newpassword‘); mysql> SET PASSWORD FOR ‘root‘@‘host_name‘ = PASSWORD(‘newpassword‘);
使用UPDATE语句更新user表重置ROOT密码
# ./mysql -u root mysql> UPDATE mysql.user SET Password = PASSWORD(‘newpassword‘) WHERE User = ‘root‘; mysql> FLUSH PRIVILEGES;
手动修改
1、停止MySQL服务
执行:/etc/init.d/mysql stop,你的机器上也不一定/etc/init.d/mysql也可能是/etc/init.d/mysqld
2、跳过验证启动MySQL
/usr/local/mysql/bin/mysqld_safe --skip-grant-tables >/dev/null 2>&1 &
注:如果mysqld_safe的位置如果和上面不一样需要修改成你的,如果不清楚可以用find命令查找。
3、重置密码
等一会儿,然后执行:/usr/local/mysql/bin/mysql -u root mysql
出现mysql提示符后输入:update user set password = Password(‘要设置的密码‘) where User = ‘root‘;
回车后执行:flush privileges; 刷新MySQL系统权限相关的表。再执行:exit; 退出。
4、重启MySQL
杀死MySQL进程:killall mysqld
重启MySQL:/etc/init.d/mysql start
时间: 2024-10-19 20:45:51