1、知道mysql旧密码,修改新密码

a.mysqladmin命令(回目录)

mysqladmin -u USER -p password PASSWORD

b.先用root帐户登入mysql,然后执行:

UPDATE user SET password=PASSWORD('123456') WHERE user='root';
FLUSH PRIVILEGES;

c.用root命令登入mysql,然后执行:

SET PASSWORD FOR root=PASSWORD('123456');

2、忘记mysql密码

修改配置文件/etc/my.cnf,在mysqld段下面增加如下参数,之后重启mysql

skip-grant-tables

此时登陆mysql无需密码,

UPDATE user SET password=PASSWORD('123456') WHERE user='root'; FLUSH PRIVILEGES;

用root命令登入mysql,然后执行: SET PASSWORD FOR root=PASSWORD('123456');

再次在配置文件中把增加的那个参数去掉,重启mysql即可


实践操作命令:

killall mysqld

mysqld_safe --skip-grant-tables --user=mysql &

update mysql.user set password=password("123456") where user='root' and host='localhost';

flush privileges;

mysqladmin -u root -p123456 shutdown

/etc/init.d/mysqld start

mysql -u root -p123456