mysql ERROR 1044 (42000): Access denied for user ‘‘@‘localhost‘ to database

当尝试创建数据库时遇到ERROR 1044 (42000)错误,本文提供了解决方案。包括通过以root用户登录并创建数据库,以及通过修改MySQL配置文件skip-grant-tables来重置root密码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. 問題描述:

在MySQL控制台下建立資料庫出現以下資訊:

mysql> CREATE DATABASE python; 
ERROR 1044 (42000): Access denied for user ‘‘@‘localhost‘ to database ‘python‘

2. 解決方案:

執行以下命令進入控制台:(在MySQL bin檔案下)

mysql --user=root -p

輸入root使用者的密碼即可進入mysql控制台:

建立資料庫:

create database python;

顯示所有資料庫:

show databases;

如下:

www.linuxidc.com @www.linuxidc.com:~$ mysql --user=root -p 
Enter password: 
Welcome to the MySQL monitor. Commands end with ; or \g. 
Your MySQL connection id is 4 
Server version: 5.6.17 MySQL Community Server (GPL)

Copyright (c) 2000, 2014,Oracleand/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its 
affiliates. Other names may be trademarks of their respective 
owners.

Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.

mysql> create database python; 
Query OK, 1 row affected (0.00 sec)

mysql> show databases; 
+--------------------+ 
| Database | 
+--------------------+ 
| information_schema | 
| mysql | 
| performance_schema | 
| python | 
| test | 
+--------------------+ 
5 rows in set (0.02 sec)

mysql>

3. OK, 以上方法不是最好的, 但卻是簡單可行的,Enjoy it!!!

4、方法四:

這幾天用空密碼登入mysql後,然後修改mysql預設密碼,使用mysql表出現過這個問題,提示:ERROR 1044 (42000): Access denied for user ‘‘@‘localhost‘ to database ‘mysql‘。網上找了一些方法,終於搞定了。

我用的是xampp整合的mysql,之前空密碼能登進去phpmyadmin,但怎麼也進不去phpmyadmin的系統資料表

後來解決成功發現是因為mysql資料庫的user表裡,存在使用者名稱為空白的賬戶即匿名賬戶,導致登入的時候是雖然用的是root,但實際是匿名登入的,通過錯誤提示裡的‘‘@‘localhost‘可以看出來。我用方法一解決了問題,

方法一:
在my.ini的[mysqld]欄位加入:

skip-grant-tables

重啟mysql服務,這時的mysql不要求輸入密碼即可登入資料庫
然後進入mysql

mysql>use mysql;
mysql>update user set password=password(‘新密碼‘) WHERE User=‘root‘; 
mysql>flush privileges;

運行之後最後去掉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> use mysqlERROR 1044 (42000): Access denied for user ‘root‘@‘localhost‘ to database ‘mysql‘mysql> exitBye[[email protected] ~]# service mysqld stop Stopping mysqld:[ OK ][[email protected] ~]# mysqld_safe --user=mysql --skip-grant-tables --skip-networking &  [[email protected] ~]# mysql -u root -p -hlocalhostEnter password:  mysql> use mysql mysql> SELECT host,user,password,Grant_priv,Super_priv FROM mysql.user; mysql> UPDATE mysql.user SET Grant_priv=‘Y‘, Super_priv=‘Y‘ WHERE User=‘root‘; mysql> FLUSH PRIVILEGES; mysql> GRANT ALL ON *.* TO ‘root‘@‘localhost‘; mysql> GRANT ALL ON *.* TO ‘root‘@‘cn.cn.cn.cn‘; mysql> GRANT ALL ON *.* TO ‘root‘@‘245.245.245.245‘; mysql> GRANT ALL ON *.* TO ‘root‘@‘127.0.0.1‘; mysql> FLUSH PRIVILEGES;  mysql> quitBye[[email protected] ~]# service mysqld start  restart Linux/OS

Related Article:MySql 執行計畫

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值