忘记密码:
# vim /etc/my.cnf
skip-grant-tables
/etc/init.d/mysqld restart
或者
mysqld_safe --skip-grant-table &
# /etc/init.d/mysqld restart
# mysql -uroot
mysql5.5版本
> use mysql;
> UPDATE user SET Password = password ('R5GK8B2XdxGrx2dC') WHERE User = 'root' and Host = 'localhost';
mysql5.7版本
> use mysql;
> UPDATE user SET authentication_string=PASSWORD('zmt@123.com') where USER='root';
> flush privileges;
> alter user 'root'@'localhost' identified by 'zmt@123.com';
> SET PASSWORD = PASSWORD('zmt@123.com');
修改密码:
方法1:用mysqladmin (mysql5.7和mysql8.0以上都可以用)
格式:mysqladmin -u用户名 -p password 新密码
例子:
# mysqladmin -uroot -p password 123456
方法2: 用SET PASSWORD命令 (mysql5.7版本)
格式:mysql> set password for 用户名@localhost = password('新密码');
例子:mysql> set password for root@localhost = password('123456');
方法3: sql语句
mysql5.5版本
> use mysql;
> UPDATE user SET Password = password ('R5GK8B2XdxGrx2dC') WHERE User = 'root' and Host = 'localhost';
mysql5.7版本
> use mysql;
> update mysql.user set authentication_string=password('R5GK8B2XdxGrx2dC') where user='root' and Host = 'localhost';