一 关于出现”ERROR 1045 (28000): Access denied for user ‘root’@‘localhost’ (using password: YES)“错误的解决:
1 查看版本和修改配置
mysql -V //查看对应的版本号,一般我们的服务器版本为mysql5.7.30
whereis my.cnf //查找mysql对应的配置文件,路径一般为/etc/my.cnf,mysql8.0以上在/etc/mysql/conf.d/mysql.cnf
vim /etc/my.cnf
在[mysqld]中添加以下内容:
skip-grant-tables
2 重启服务
service mysqld restart
3 修改密码
mysql -uroot -p //输入后直接按enter即可,无需输入密码
use mysql
select * from user; //查看是否有authentication_string字段
update user set authentication_string=password("你的新密码") where user='root'; //若数据库是5.7及以上的用该命令.因为5.7之前没有authentication_string字段,5.7之前的字段叫password
//update user set password=password("你的新密码") where user="root"; //5.7之前的版本用这个命令
flush privileges;
4 修改登录主机
//update user set host ='%' where user ='root'; //修改mysql只能在本地登录而无法远程登录(建议用下面的)
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '你刚刚改的密码'; //这里面*.*代表是所有库.所有表, root是用户名, %代表所有ip都可访问,也可指定ip访问,例如'root'@'172.17.5.90',123456代表root用户的密码;
flush privileges;select host,user from user; //查看host可以看到localhsot已经变成%。
quit;