mysql ERROR 1044 (42000): Access denied for user ''@'localhost' to database
1.问题描述:
在MySQL控制台下创建数据库出现以下信息:
mysql>CREATEDATABASEpython;
ERROR1044(42000):Accessdeniedforuser''@'localhost'todatabase'python'
2.解决方法:
执行以下命令进入控制台:
mysql--user=root-p
输入root用户的密码即可进入mysql控制台:
创建数据库:
createdatabasepython;
显示所有数据库:
showdatabases;
如下:
www.linuxidc.com@www.linuxidc.com:~$mysql--user=root-p
Enterpassword:
WelcometotheMySQLmonitor.Commandsendwith;or\g.
YourMySQLconnectionidis4
Serverversion:5.6.17MySQLCommunityServer(GPL)
Copyright(c)2000,2014,Oracleand/oritsaffiliates.Allrightsreserved.
OracleisaregisteredtrademarkofOracleCorporationand/orits
affiliates.Othernamesmaybetrademarksoftheirrespective
owners.
Type'help;'or'\h'forhelp.Type'\c'toclearthecurrentinputstatement.
mysql>createdatabasepython;
QueryOK,1rowaffected(0.00sec)
mysql>showdatabases;
+--------------------+
|Database|
+--------------------+
|information_schema|
|mysql|
|performance_schema|
|python|
|test|
+--------------------+
5rowsinset(0.02sec)
mysql>
3.OK,以上方法不是最好的,但却是简单可行的,Enjoyit!!!
4、方法四:
这几天用空密码登录mysql后,然后修改mysql默认密码,使用mysql表出现过这个问题,提示:ERROR1044(42000):Accessdeniedforuser''@'localhost'todatabase'mysql'。网上找了一些方法,终于搞定了。
我用的是xampp集成的mysql,之前空密码能登进去phpmyadmin,但怎么也进不去phpmyadmin的系统表
后来解决成功发现是因为mysql数据库的user表里,存在用户名为空的账户即匿名账户,导致登录的时候是虽然用的是root,但实际是匿名登录的,通过错误提示里的''@'localhost'可以看出来。我用方法一解决了问题,
方法一:
在my.ini的[mysqld]字段加入:
skip-grant-tables
重启mysql服务,这时的mysql不需要密码即可登录数据库
然后进入mysql
mysql>usemysql;
mysql>updateusersetpassword=password('新密码')WHEREUser='root';
mysql>flushprivileges;
运行之后最后去掉my.ini中的skip-grant-tables,重启mysqld即可。
修改mysql密码方法二:
不使用修改my.ini重启服务的方法,通过非服务方式加skip-grant-tables运行mysql来修改mysql密码
停止mysql服务
打开命令行窗口,在bin目录下使用mysqld-nt.exe启动,即在命令行窗口执行:mysqld-nt--skip-grant-tables
然后另外打开一个命令行窗口,登录mysql,此时无需输入mysql密码即可进入。
按以上方法修改好密码后,关闭命令行运行mysql的那个窗口,此时即关闭了mysql,如果发现mysql仍在运行的话可以结束掉对应进程来关闭。
启动mysql服务。
linux下的处理方法:
mysql>usemysql ERROR1044(42000):Accessdeniedforuser'root'@'localhost'todatabase'mysql' mysql>exit Bye [root@testtest~]#servicemysqldstop Stoppingmysqld:[OK] [root@testtest~]#mysqld_safe--user=mysql--skip-grant-tables--skip-networking& [root@testtest~]#mysql-uroot-p-hlocalhost Enterpassword: mysql>usemysql mysql>SELECThost,user,password,Grant_priv,Super_privFROMmysql.user; mysql>UPDATEmysql.userSETGrant_priv='Y',Super_priv='Y'WHEREUser='root'; mysql>FLUSHPRIVILEGES; mysql>GRANTALLON*.*TO'root'@'localhost'; mysql>GRANTALLON*.*TO'root'@'cn.cn.cn.cn'; mysql>GRANTALLON*.*TO'root'@'245.245.245.245'; mysql>GRANTALLON*.*TO'root'@'127.0.0.1'; mysql>FLUSHPRIVILEGES; mysql>quit Bye [root@testtest~]#servicemysqldstart restartLinux/OS