Cannot run GRANT as root: access denied for user 'root'@'localhost'
ERROR 1044 (42000): Access denied for user 'root'@'localhost' to database 'mydb'Short answer
GRANT works only for an account that has the GRANT OPTION privilege and holds every privilege it hands out. When it fails, you are usually not the account you think you are: an anonymous row matched first, or the account was created without grant rights. Check CURRENT_USER() and SHOW GRANTS first.
Fix it fast
1Find out which cause you have
Ask the server which account your session really matched, and what that account may do.
mysql -u root -p -e "SELECT USER(), CURRENT_USER(); SHOW GRANTS;"2Match what you see, then jump to its fix
- 1
CURRENT_USER() shows an empty name, such as @localhostAn anonymous account matched the connection firstFix - 2
SHOW GRANTS lists privileges but no WITH GRANT OPTIONThe account has no GRANT OPTION, or lacks the privilege it is grantingFix - 3
Plain SELECTs work, GRANT is refused, and read_only is ONThe server is read-onlyFix - 4
ERROR 1045 on every connection as root, with or without a passwordYou no longer have a working root loginFix
- 1
What does the error look like?
ERROR 1044 (42000): Access denied for user 'root'@'localhost' to database 'mydb'ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)Access denied for user 'root'@'localhost' while attempting to grant privileges
Cause 1 of 4
An anonymous account matched the connection first
1Check
The server uses the first matching row in its sorted account list. A row with a blank user name and host localhost can come before the row for the user you typed, and then you are treated as an anonymous user with no privileges.
SELECT CURRENT_USER();
SELECT user, host FROM mysql.user ORDER BY user, host;2Fix
From an administrator account, drop the anonymous row. If you must keep it, connect with a name and host that match the account you want.
DROP USER ''@'localhost';3Verify
Reconnect and check which account you are.
mysql -u root -p -e "SELECT CURRENT_USER();"Confirmed by dev.mysql.com
Cause 2 of 4
The account has no GRANT OPTION, or lacks the privilege it is granting
1Check
To grant a privilege you need the GRANT OPTION privilege and you must hold the privileges you are granting. You cannot hand out something you do not have.
SHOW GRANTS FOR 'root'@'localhost';2Fix
Connect as an administrator that has the privilege, and give the account the grant option without changing its other privileges.
GRANT USAGE ON *.* TO 'someuser'@'somehost' WITH GRANT OPTION;3Verify
Read the grants back.
SHOW GRANTS FOR 'someuser'@'somehost';Confirmed by dev.mysql.com
Cause 3 of 4
The server is read-only
1Check
While read_only is enabled, GRANT also requires CONNECTION_ADMIN (or the deprecated SUPER privilege).
SHOW VARIABLES LIKE 'read_only';2Fix
Run the statement on the writable primary, or grant the account CONNECTION_ADMIN from an administrator.
GRANT CONNECTION_ADMIN ON *.* TO 'someuser'@'somehost';3Verify
Repeat the failed GRANT.
SHOW GRANTS FOR 'someuser'@'somehost';Confirmed by dev.mysql.com
Cause 4 of 4
You no longer have a working root login
1Check
Try the client with an explicit user and password prompt. If that fails too, the root password is unknown.
mysql -u root -p2Fix
Reset the password with an init file that contains one ALTER USER line, start the server with it, then restart normally and delete the file.
# /home/me/mysql-init contains:
# ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass';
mysqld --init-file=/home/me/mysql-init &3Verify
Stop the server, start it normally and log in with the new password.
mysql -u root -p -e "SELECT CURRENT_USER();"Confirmed by dev.mysql.com
How do I stop it coming back?
- Create each application account with CREATE USER, then grant on one database, such as GRANT ALL ON mydb.* TO 'app'@'localhost'.
- Remove anonymous accounts after installing the server.
- Keep one administrator account with GRANT OPTION and use it only for account management.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- ShopGoAn e-commerce aggregator with a Python scraping engine behind its catalog, past 10K Google Play downloads.
Related fixes
Databases
ERROR 1044: Access denied for user to database
ERROR 1044 (42000): Access denied for user 'app'@'localhost' to database 'shop'
Error 1044 is different from 1045: the login worked, but the account MySQL matched has no privileges on the database you named.
3 causes, each with a fixDatabases
MySQL ERROR 1227: you need the CREATE USER privilege
Access denied; you need (at least one of) the %s privilege(s) for this operation
Error 1227 means the account running CREATE USER lacks the privilege for it.
3 causes, each with a fixDatabases
Installing the Airflow MySQL provider fails on pkg-config or mysqlclient
Building the mysqlclient wheel fails during the install
The Airflow MySQL provider depends on mysqlclient, which pip compiles against the MySQL C client library.
2 causes, each with a fixDatabases
A query on Aurora MySQL dies with 'table /rdsdbdata/tmp/#sql... is full'
ERROR 1114 (HY000): The table '/rdsdbdata/tmp/#sqlfd_1586_2' is full
In Aurora MySQL 3, internal temporary tables share one pool, and when that pool and the memory-mapped overflow file are used up, a reader instance ends the query with error 1114.
3 causes, each with a fixDatabases
Azure Database for MySQL refuses a connection that does not use SSL
ERROR 3159 (HY000): Connections using insecure transport are prohibited while --require_secure_transport=ON.
Azure Database for MySQL Flexible Server requires encrypted connections by default (require_secure_transport = ON, TLS 1.2).
4 causes, each with a fixDatabases
Compiling a C or C++ program that includes mysql.h fails
undefined reference to `mysql_init'
The compiler only finds mysql.h when the MySQL client development files are installed and their folder is on the include path.
3 causes, each with a fix