查看mysql是否拥有root用户
select host, user, authentication_string, plugin from user;
如果user表没有任何一个用户,包括root,所以一开始我要给权限,方便后续操作:
vim /etc/my.cnf
skip-grant-tables #在[mysqld]下面添加这一行,忽略权限表
重启mysql:/etc/init.d/mysqld restart
create user 'root'@'localhost' identified by '123457';
localhost表示本地,mysql登入的时候,不用指定ip登入
此步骤可能会报以下错误,没报错的跳过(直接到权限那一步),用一下方法解决:
ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement
flush privileges;
此时再次重新创建用户:
create user 'root'@'localhost' identified by '123457';
再次报错,这步没报错的也是直接跳到赋予权限那一步,报错的以下操作:
drop user 'root'@'localhost';
再次重新创建用户:
create user 'root'@'localhost' identified by '123456';
结果没有再报错,root用户创建成功。
下面赋予root权限:
GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' WITH GRANT OPTION; #赋予所有库所有表操作权限
flush privileges;
exit
到这一步没有报错,表明已经成功了,不过要把最开始的配置文件恢复:
vim /etc/my.cnf
删除配置文件中的:
skip-grant-tables
退出,重启mysql:
/etc/init.d/mysqld restart
最后,测试:
mysql -uroot -p123456 #可以登入的话,表示前面的操作没问题
navicat连接mysql出现2059
mysql -u root -p
ALTER USER 'root'@'%' IDENTIFIED BY 'password' PASSWORD EXPIRE NEVER;
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'password';
FLUSH PRIVILEGES;
重新连接,success