① 输入命令mysql –uroot –p,然后输入密码直接回车
② 然后再输入use mysql
③ 输入SQL语句select host,user from mysql.user; 附图:
3. 修改MySQL的登录密码
1. 修改my.cnf文件
① 输入命令vim /etc/my.cnf
② 然后在[mysqld]后面添加skip-grant-tables
③ 保存退出(Esc + :wq) 附图: 2. 重启MySQL服务
① 输入命令sudo systemctl restart mysqld 3. 选择数据库
① 输入命令mysql
② 然后再输入use mysql
③ 最后输入exit退出mysql 附图:
4. 修改密码
① 输入命令mysql –uroot –p,不输入密码直接回车
② 输入SQL语句set password for root@localhost = password('自己要更改的密码');
如果出现该错误:
ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement
解决方法:
1) 输入命令flush privileges;
2) 再次输入命令set password for root@localhost = password('自己要更改的密码');
③ 设置远程访问,输入SQL语句GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'password' WITH GRANT OPTION;
④ 输入命令flush privileges;再次刷新
⑤ 最后输入exit退出mysql 附图: 5. 还原my.cnf文件
① 输入命令vim /etc/my.cnf
② 去掉(或者注释掉)[mysqld]后面添加的skip-grant-tables
③ 保存退出(Esc + :wq)