Mysql Access denied for user 'root'

Posted ssooking

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Mysql Access denied for user 'root'相关的知识,希望对你有一定的参考价值。

 

mysql Access denied for user \'root\'

ERROR 1045 (28000): Access denied for user \'root\'@\'localhost\' (using password: NO)

解决方法: 更改root密码

原密码为空,更改root密码

# service mysql stop
# mysqld_safe --user=mysql --skip-grant-tables --skip-networking

然后登陆mysql

sudo mysql -u root,设置密码

mysql> use mysql;
mysql> update user set password=password(\'新密码\') where user=\'root\';
mysql> flush privileges;
mysql> quit

重启数据库再次登陆即可

# service mysql restart
Enter password: <输入新设的密码>

 

 

 

增加一个数据库用户

mysql> insert into mysql.user (Host,User,Password) VALUES(\'%\',\'your username\',PASSWORD(\'your password\')); 
mysql> flush privileges;

 

创建数据库

create database DatabaseName;
grant select,insert,update,delete,create,drop,alter on DatabaseName.* to username@localhost identified by \'password\';

 

把数据库权限全部授予指定用户

grant all privileges on DatabaseName.* to \'username\'@\'%\';

 

远程登陆

授权用户root使用密码123456从任意主机连接到mysql服务器:

grant all privileges on *.* to \'root\'@\'%\' identified by \'123456\' with grant option;
flush privileges;

授权用户test使用密码123456,从指定ip为123.4.5.6的主机连接到mysql服务器:

grant all privileges on *.* to \'test\'@\'123.4.5.6\' identified by \'123456\' with grant option;
flush privileges;

 然后我们查询用户情况

mysql> use mysql;

mysql> select Host,User from user;

如果是类似与这样的输出,test前有%,代表从任意主机登陆

+----------------+------+
| Host           | User |
+----------------+------+
| %              | test |
| 123.4.5.6      | test |
| localhost      | root |
+----------------+------+

比如我想让root从任意ip远程访问,也可以在这里直接修改

mysql> update user set host = \'%\' where user = \'root\';

 

备份mysql数据库

mysqldump -u 用户名 -p 密码 数据库名 > back.sql  //备份指定数据库
mysqldump --all-databases > bak.sql    //备份所有数据库

还原mysql数据库

mysql -u 用户名 -p 密码 数据库名 < bak.sql

 

以上是关于Mysql Access denied for user 'root'的主要内容,如果未能解决你的问题,请参考以下文章

java访问数据库被拒绝,不能连接数据库ERROR 1045 (28000): Access denied for user 'root'@'localhost' (u

ERROR 1045 (28000): Access denied for user 'root'@'localhost'

mysql错误 Access denied for user 'root'@'xxx.xxx.xxx.xxx' (using password: YES)

mariadb mysql -u root -p 显示Access denied解决办法

MySql access denied for user错误

Mysql Access denied for user 'root'