目录
1.第一种创建用户并授权(也可用于改密码)
第一步:先查看基本用户与主机
select user,host from mysql.user; +---------------+-----------------+ | user | host | +---------------+-----------------+ | repl | % | | root | % | | system | % | | tapdapp | % | | tapdjob | % | | test | % | | asd | 172.17.0.98 | | mysql.session | localhost | | mysql.sys | localhost | | system | localhost | | test | localhost | | test | ‘localhost’ | +---------------+-----------------+ 12 rows in set (0.00 sec)
第二步:修改已有的用户密码
grant all privileges on *.* to 'root'@'%' identified by 'root';
根据第一步可以看的用户对应的主机在@这里分别写入最后by出要修该的密码
2.直接alter改
alter user ‘root'@'localhost' identified by '123456'; flush privileges;//刷新系统权限表
3.用UPDATE直接编辑user表
首先登录MySQL。
进入表改
mysql> use mysql; mysql> update user set password=password(‘123’) where user=’root’ and host=’localhost’; mysql> flush privileges;
4.忘记数据库root密码
1)添加skip-grant-tables参数到my.cnf文件中
vim /etc/my.cnf [mysqld] skip-grant-tables
2)重启mysql服务,并登录
systemctl restart mysqld mysql -u root -p #直接回车登陆即可
3)修改root用户密码
show databases; use mysqld;
---以下两个命令都可修改用户密码
(1)update mysql.user set authenticationstring=password('123456') where user='root' and Host = 'localhost'; (2)alter user 'root'@'localhost' identified by 'cy7m0ypu8CpLFperzI45'; 退出mysql命令模式输入 exit;
4)回到步骤1注释掉skip-grant-tables这个参数
vim /etc/my.cnf [mysqld] #skip-grant-tables
退出编辑后,重启mysql服务
systemctl restart mysqld