274

Tried

mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'root' WITH GRANT OPTION;

Getting

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'IDENTIFIED BY 'root' WITH GRANT OPTION' at line 1.

Note: The same is working when tried in previous versions.

Also tried

mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION;

Getting

ERROR 1410 (42000): You are not allowed to create a user with GRANT

MySQL (8.0.11.0) username/password is root/root.

Praveen
  • 2,740
  • 2
  • 8
  • 8
  • are you connecting directly with mysql or do you use ssh? – Harald May 04 '18 at 14:29
  • directly from command prompt as root user – Praveen May 04 '18 at 15:03
  • 2
    See the differences between [13.7.1.6 GRANT Syntax](https://dev.mysql.com/doc/refman/8.0/en/grant.html) and [13.7.1.4 GRANT Syntax](https://dev.mysql.com/doc/refman/5.7/en/grant.html). – wchiquito May 04 '18 at 15:18
  • 25
    im too stuck with the same issue. I am launching the mysql shell using `mysql -u root -p`, then entering root password. Then I tried `GRANT GRANT OPTION ON *.* TO 'root'@'%';` and I get the error `ERROR 1410 (42000): You are not allowed to create a user with GRANT` – aiman May 05 '18 at 08:42
  • 5
    @Praveen are you ever going to accept an answer here? – Mike Lischke Nov 11 '20 at 16:37
  • What was the solution my man? I am receiving the same error! – Raul Chiarella Jan 31 '22 at 17:22
  • My issue was that I was trying to grant privileges to an account that did not exist. – late1 Sep 22 '22 at 09:23

24 Answers24

522

Starting with MySQL 8 you no longer can (implicitly) create a user using the GRANT command. Use CREATE USER instead, followed by the GRANT statement:

mysql> CREATE USER 'root'@'%' IDENTIFIED BY 'PASSWORD';
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION;
mysql> FLUSH PRIVILEGES;

Caution about the security risks about WITH GRANT OPTION, see:

BroodjeBE
  • 95
  • 10
Mike Lischke
  • 48,925
  • 16
  • 119
  • 181
  • 22
    This command will create a new user. But I want to grant privileges to existing root user. mysql> CREATE USER 'root'@'%' IDENTIFIED BY 'root'; Query OK, 0 rows affected (0.31 sec) mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION; Query OK, 0 rows affected (0.16 sec) mysql> SELECT User FROM mysql.user; +------------------+ | User | +------------------+ | root | | mysql.infoschema | | mysql.session | | mysql.sys | | root | +------------------+ 5 rows in set (0.00 sec) – Praveen May 07 '18 at 04:34
  • Now two users with name root are present in user table!! Also I am not able to connect remotely (root user). ------------- Details:    Type: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException    SQL State: 08001 – Praveen May 07 '18 at 04:39
  • Weird. The error message in your question indicates this user doesn't exist. This is the only reason why I suggested to create it first. And it's impossible to have the same user twice in single MySQL instance. They differ either in the name or in the host part. – Mike Lischke May 07 '18 at 06:39
  • 3
    These 2 users connect from different hosts. Run `SELECT User, Host FROM mysql.user;` instead. – Mike Lischke May 07 '18 at 06:46
  • U are right host is different for both root users. mysql> SELECT User, Host FROM mysql.user; +------------------+-----------+ | User | Host | +------------------+-----------+ | root | % | | mysql.infoschema | localhost | | mysql.session | localhost | | mysql.sys | localhost | | root | localhost | +------------------+-----------+ 5 rows in set (0.00 sec) The problem is why I am not ablr to connect remotely. Details: Type: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException SQL State: 08001 – Praveen May 08 '18 at 04:44
  • That's a different question and has nothing to do with this one here. Finish this one and then open a new question. – Mike Lischke May 08 '18 at 07:06
  • 13
    Got that error: `ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)` – Adil Sep 22 '18 at 10:24
  • Specifically, MySQL 8.0.11 removed the ability to use `GRANT` to create users. You can read about it here: https://dev.mysql.com/doc/relnotes/mysql/8.0/en/news-8-0-11.html – Adam Taylor May 22 '20 at 22:35
  • For people using DBeaver (you need two steps as @MikeLischke said): First create the user and save. Then grant access and save again. – Markus Zeller Jun 26 '20 at 14:51
  • its worked but without `@'%'` things – CodeToLife Nov 14 '21 at 06:24
  • In my case the mysql server was not open for remote access. – Ravi Soni Apr 05 '22 at 09:34
  • This gives me error: You are not allowed to create a user with GRANT – Raul Chiarella Jun 09 '22 at 14:37
  • `ERROR 1410 (42000): You are not allowed to create a user with GRANT` / CentOS 8 / MySQL 8 – mercury Jan 10 '23 at 07:23
  • I have found that \*.\* is very buggy too and sometimes doesn't work. You have to do DB_NAME.* – wbinky Jan 25 '23 at 10:19
  • @Adil did you guys find a solution? I am getting `ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)` as well. – DarkDead Jul 07 '23 at 11:49
  • @DarkDead I'm sorry, that's quite old comment (back in 2018) and I don't remember what had worked for me. – Adil Jul 07 '23 at 16:17
138

I see a lot of (wrong) answers, it is just as simple as this:

USE mysql;
CREATE USER 'user'@'localhost' IDENTIFIED BY 'P@ssW0rd';
GRANT ALL ON *.* TO 'user'@'localhost';
FLUSH PRIVILEGES;

Note: instead of a self-created user you can use root to connect to the database. However, using the default root account to let an application connect to the database is not the preferred way. Alternative privileges can be applied as follows (be careful and remember the least-privilege principle):

-- Grant user permissions to all tables in my_database from localhost --
GRANT ALL ON my_database.* TO 'user'@'localhost';

-- Grant user permissions to my_table in my_database from localhost --
GRANT ALL ON my_database.my_table TO 'user'@'localhost';

-- Grant user permissions to all tables and databases from all hosts --
GRANT ALL ON *.* TO 'user'@'*';

If you would somehow run into the following error:

ERROR 1130 (HY000): Host ‘1.2.3.4’ is not allowed to connect to this MySQL server

You need add/change the following two lines in /etc/mysql/my.cnf and restart mysql:

bind-address           = 0.0.0.0
skip-networking

You could run into the following error, which is a bit confusing:

ERROR 1410 (42000): You are not allowed to create a user with GRANT

This means that either the user does not exist at all OR that the user@host combination does not exist. You can easily check for this with the following command:

SELECT host, user FROM user
Cloudkollektiv
  • 11,852
  • 3
  • 44
  • 71
  • Could you elaborate on why it doesn't work instead of just complaining? That way I can help you out or post additional information. – Cloudkollektiv May 03 '20 at 19:46
  • 2
    This answer doesn't even sort of address the question of *root* user privilege. – Joseph8th May 22 '20 at 15:23
  • Updated the answer accordingly. – Cloudkollektiv May 25 '20 at 08:23
  • 3
    Best answer, now it's `GRANT ALL ON` for MySQL 8.0. Also I believe instead of disabling `bind-address` you can bind to `127.0.0.1` and include `--protocol=tcp` in any commands. The performance long-term is also better than with `localhost`. – Jesse Nickles Jul 12 '20 at 05:05
  • 2
    What all answers beside mine miss here is that the OP wondered why the user can no longer be created with the GRANT command. It was never asked how to use `GRANT` to grant privileges. But somehow people refuse to read what has been asked and answered before. – Mike Lischke Sep 17 '20 at 12:24
  • If you want to give privileges for a specific table, you can't specify it in quotes. You must write it directly. I don't know why they have removed this, but this is the truth. So don't miss.. – Suphi ÇEVİKER Feb 02 '21 at 07:00
  • I don't think changing the bind address to 0.0.0.0 is addressing the problem. This is dangerous as the "side effect" (which is not a side effect btw) will expose the VM/MySQL to the network. I think the issue is that `GRANT ALL ON *.* TO 'user'@'*';` should have been `GRANT ALL ON *.* TO 'user'@'%'` – Fnaxiom Aug 11 '22 at 16:59
  • This doesn't work. All it does is: `ERROR 1410 (42000): You are not allowed to create a user with GRANT` – AlexPi Dec 13 '22 at 00:14
  • For me, `use mysql`makes later command success. – HUA Di Feb 10 '23 at 10:15
42

1) This worked for me. First, create a new user. Example: User foo with password bar

> mysql> CREATE USER 'foo'@'localhost' IDENTIFIED WITH mysql_native_password BY 'bar';

2) Replace the below code with a username with 'foo'.

> mysql> GRANT ALL PRIVILEGES ON database_name.* TO'foo'@'localhost';

Note: database_name is the database that you want to have privileges, . means all on all

3) Login as user foo

mysql> mysql -u foo -p

Password: bar

4) Make sure your initial connection from Sequelize is set to foo with pw bar.

shivangg
  • 521
  • 8
  • 15
J. Doe
  • 515
  • 4
  • 2
  • 9
    This does by far not answer the question. It's about global permissions, and not database specific permissions. – tmuecksch Nov 01 '18 at 14:46
  • 3
    The user root already exists! But get `ERROR 1410 (42000): You are not allowed to create a user with GRANT`. – mercury Jan 10 '23 at 07:24
  • `GRANT ALL PRIVILEGES` doesn't work. I keep getting `ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)` Any fix for this? – DarkDead Jul 07 '23 at 11:52
14

Just my 2 cents on the subject. I was having the exact same issue with trying to connect from MySQL Workbench. I'm running a bitnami-mysql virtual machine to set up a local sandbox for development.

Bitnami's tutorial said to run the 'Grant All Privileges' command:

/opt/bitnami/mysql/bin/mysql -u root -p -e "grant all privileges on *.* to 'root'@'%' identified by 'PASSWORD' with grant option";

This was clearly not working, I finally got it to work using Mike Lischke's answer.

What I think happened was that the root@% user had the wrong credentials associated to it. So if you've tried to modify the user's privileges and with no luck try:

  1. Dropping the user.
  2. Create the user again.
  3. Make sure you have the correct binding on your MySQL config file. In my case I've commented the line out since it's just for a sandbox environment.

1. Dropping the user.

From Mysql Console:

List Users (helpful to see all your users):

select user, host from mysql.user;

Drop Desired User:

drop user '{{ username }}'@'%';

2. Create the user again.

Create User and Grant Permissions:

CREATE USER '{{ username }}'@'%' IDENTIFIED BY '{{ password }}';
GRANT ALL PRIVILEGES ON *.* TO '{{ username }}'@'%' WITH GRANT OPTION;

Run this command:

FLUSH PRIVILEGES;

3. Make sure you have the correct binding on your MySQL config file.

Locate your MySQL config file (additional notes at the end). If you want to have MySQL listen for connections on more than one network find the following line on the config file:

bind-address=127.0.0.1

and comment it using a '#':

#bind-address=127.0.0.1

For production environments you might want to use limit the network access (additional notes at the end).

Then restart your MySQL service.

Hope this helps someone having the same issue!


Binding: If you want to know more about this I suggest looking at the following solution How to bind MySQL server to more than one IP address. It basically says you can leave MySQL open and limit connections by using a firewall, or natively if you have MySQL version 8.0.13 and above.

MySQL Config File The file could have different locations depending on your Linux distribution and installation. On my system it was located at '/etc/my.cnf'. Here are other suggested locations:

  • /etc/mysql/mysql.conf.d
  • /etc/mysql/my.cnf

You can also search for the config locations as shown in this website: How to find locations of MySQL config files.

Armando Juarez
  • 173
  • 1
  • 8
12

For those who've been confused by CREATE USER 'root'@'localhost' when you already have a root account on the server machine, keep in mind that your 'root'@'localhost' and 'root'@'your_remote_ip' are two different users (same user name, yet different scope) in mysql server. Hence, creating a new user with your_remote_ip postfix will actually create a new valid root user that you can use to access the mysql server from a remote machine.

For example, if you're using root to connect to your mysql server from a remote machine whose IP is 10.154.10.241 and you want to set a password for the remote root account which is 'Abcdef123!@#', here are steps you would want to follow:

  1. On your mysql server machine, do mysql -u root -p, then enter your password for root to login.

  2. Once in mysql> session, do this to create root user for the remote scope:

    mysql> CREATE USER 'root'@'10.154.10.241' IDENTIFIED BY 'Abcdef123!@#';
    
  3. After the Query OK message, do this to grant the newly created root user all privileges:

    mysql> GRANT ALL ON *.* TO 'root'@'10.154.10.241';
    
  4. And then:

    FLUSH PRIVILEGES;
    
  5. Restart the mysqld service:

    sudo service mysqld restart
    
  6. Confirm that the server has successfully restarted:

    sudo service mysqld status
    

If the steps above were executed without any error, you can now access to the mysql server from a remote machine using root.

Dan C. Vo
  • 151
  • 1
  • 4
9

My Specs:

mysql --version
mysql  Ver 8.0.16 for Linux on x86_64 (MySQL Community Server - GPL)

What worked for me:

mysql> CREATE USER 'username'@'localhost' IDENTIFIED BY 'desired_password';
mysql> GRANT ALL PRIVILEGES ON db_name.* TO 'username'@'localhost' WITH GRANT OPTION;

Response in both queries:

Query OK, O rows affected (0.10 sec*)

N.B: I created a database (db_name) earlier and was creating a user credential with all privileges granted to all tables in the DB in place of using the default root user which I read somewhere is a best practice.

User
  • 31,811
  • 40
  • 131
  • 232
Nuel Makara
  • 99
  • 1
  • 3
8

The specified user just doesn't exist on your MySQL (so, MySQL is trying to create it with GRANT as it did before version 8, but fails with the limitations, introduced in this version).

MySQL's pretty dumb at this point, so if you have 'root'@'localhost' and trying to grant privileges to 'root'@'%' it treats them as different users, rather than generalized notion for root user on any host, including localhost.

The error message is also misleading.

So, if you're getting the error message, check your existing users with something like this

SELECT CONCAT("'", user, "'@'", host, "'") FROM mysql.user;

and then create missing user (as Mike advised) or adjust your GRANT command to the actual exisiting user specificaion.

  • 2
    This was a valid solution for me! I simply had failed to type the user@ line precisely. Be sure to check this before trying the other solutions suggested. For me it was: CREATE USER 'user'@'%domain.com' and then when I issued the failing GRANT statement, my syntax was off by one '.'. It looked like this GRANT .... TO 'user'@'%.domain.com'. I forgot to put the '.' in my create statement and it made all the difference in fixing this issue. – wallisds Aug 22 '19 at 21:20
  • This should be accepted answer! Same here, it's what after @ that matters and it must be correct. Stupid Mysql – Andrew Jul 26 '21 at 18:20
  • I too had same problem - do double check if your create user actually created a user as expected – Yogesh Devi Jul 04 '22 at 15:39
5

You will get this error

ERROR 1410 (42000): You are not allowed to create a user with GRANT

If you are trying to run a GRANT on a user that doesn't exist!

Therefore, first run this to make sure the user you use in your GRANT matches exactly to what you have:

select User, Host from user;

In particular pay attention whether the user you created is at localhost but the one you are trying to grant to is %

ACV
  • 9,964
  • 5
  • 76
  • 81
5

Copy this and use it at once:

CREATE USER 'username'@'localhost' IDENTIFIED BY 'password';
GRANT ALL ON *.* TO 'username'@'localhost';
FLUSH PRIVILEGES;

Instead of using single lines of code such as:

CREATE USER 'username'@'localhost' IDENTIFIED BY 'password';

Then:

GRANT ALL ON *.* TO 'username'@'localhost';

Then:

FLUSH PRIVILEGES;
Toni
  • 1,555
  • 4
  • 15
  • 23
Edd Michira
  • 101
  • 1
  • 3
5

Many thanks @Nebulastic If you want to only allow remote IP using following command

CREATE USER 'user_test'@'113.yy.xx.94' IDENTIFIED BY 'YOUR_PWD';
GRANT ALL ON *.* TO 'user_test'@'113.yy.xx.94';
FLUSH PRIVILEGES;
Đọc truyện hay
  • 1,913
  • 21
  • 17
3

This worked for me:

mysql> FLUSH PRIVILEGES 
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%'WITH GRANT OPTION;
mysql> FLUSH PRIVILEGES
ZERO
  • 39
  • 2
2

Check out your username and domain is the same as created before. Mysql select account by the two colums in user table.If it is different, mysql may think you want to create a new account by grant,which is not supported after 8.0 version.

2

My Specs:

mysql --version
mysql  Ver 8.0.19 for Linux on x86_64 (MySQL Community Server - GPL)

What worked for me:

mysql> USE mysql;
mysql> UPDATE User SET Host='%' WHERE User='root' AND Host='localhost';
尹劍平
  • 184
  • 1
  • 1
  • 5
  • Although it would work in your specific situation. This pieces of sql syntax updates a user, and it does not set a user account. It also does not grant privileges. – Cloudkollektiv Apr 30 '20 at 19:15
  • Thanks a lot, your answer was sort of clarifying to me. mysql v8 deals 'host:localhost' only locally, but host:% is being dealt with for remote connection, so we need either a new user with host '%' in mysql.user table or modify one on the table to that host and grant all permissions to be able to use remotely. Although you better clarify it more and provide a full solution, you're on the stack, so you better get points – Mohamed RT May 04 '22 at 20:40
2

this commands work for me:

1-login to mysql and see all users

sudo mysql -u root

select user, host from mysql.user;

2-delete old user

drop user root@localhost;

3-create new user

CREATE USER 'root'@'localhost' IDENTIFIED BY 'mypassword'

4-add all privileges to it:

GRANT ALL ON *.* TO 'root'@'localhost'

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password
BY 'mypassword';

5-finally flush privileges

FLUSH PRIVILEGES;

fatemeh sadeghi
  • 1,757
  • 1
  • 11
  • 14
2

in select statement, changing 'user'@'%' to 'user'@'localhost' solved my problem

Ehab Reda
  • 555
  • 5
  • 9
2
  • ubuntu 22.04.1
  • Mysql Ver 8.0.31-0

My root had no GRANT privileges so I could not grant new users any previligies. Solution was to Drop current root user and create new one using 'mysql_native_password'.

Commands as follows Login to mysql with as root

mysql> DROP USER 'root'@'localhost' IDENTIFIED BY 'PASSWORD' FROM mysql.user;
mysql> CREATE USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'locahost' WITH GRANT OPTION;
mysql> FLUSH PRIVILEGES;
Marshall Fungai
  • 176
  • 1
  • 6
1

In my case I wanted to do something similar, I followed some steps from here but the best way was as @nebulasic mentioned:

USE mysql;
CREATE USER 'user'@'localhost' IDENTIFIED BY 'P@ssW0rd';
GRANT ALL ON *.* TO 'user'@'localhost';
FLUSH PRIVILEGES;

After this I encountered an error while trying to query the database or connect with SQLTools from VSCode.

Client does not support authentication protocol requested by server; consider upgrading MySQL client

Running this query will fix the problem:

ALTER USER 'user'@'localhost' IDENTIFIED WITH mysql_native_password BY 'Your_newP@s$w0Rd';

I also want to mention that these steps are ok to work in a local environment, when doing something in production is recommended to allocate each user to each database with generated password accordingly and different other security measures if necessary.

Philipos D.
  • 2,036
  • 1
  • 26
  • 33
0

Well, I just had the same problem. Even if route had '%' could not connect remotely. Now, having a look at my.ini file (config file in windows) the bind-address statement was missed.

So... I putted this bind-address = * after [mysqld] and restarted the service. Now it works!

Stephan Vierkant
  • 9,674
  • 8
  • 61
  • 97
Caos51
  • 11
0

1. grant privileges

mysql> GRANT ALL PRIVILEGES ON . TO 'root'@'%'WITH GRANT OPTION;

mysql> FLUSH PRIVILEGES

2. check user table:

mysql> use mysql

mysql> select host,user from user enter image description here

3.Modify the configuration file

mysql default bind ip:127.0.0.1, if we want to remote visit services,just delete config

#Modify the configuration file
vi /usr/local/etc/my.cnf

#Comment out the ip-address option
[mysqld]
# Only allow connections from localhost
#bind-address = 127.0.0.1

4.finally restart the services

brew services restart mysql

devo chen
  • 89
  • 1
  • 1
0

Try this, i had the same issue and i tried few options, but the below worked.

GRANT ALL ON . TO 'root'@'%';

Reference used - https://www.digitalocean.com/community/tutorials/how-to-install-linux-apache-mysql-php-lamp-stack-on-ubuntu-20-04#step-6-%E2%80%94-testing-database-connection-from-php-optional

rajeshk
  • 716
  • 1
  • 11
  • 21
-1

This may work:

grant all on dbtest.* to 'dbuser'@'%' identified by 'mysql_password';
Minding
  • 1,383
  • 1
  • 17
  • 29
Anup Joshi
  • 11
  • 1
  • 1
    Rather than toss out code, also explain why it's the appropriate solution. The goal is to educate so the OP knows what to do the next time, not merely solve the immediate problem. – the Tin Man Jan 05 '20 at 21:45
-1

I had this same issue, which led me here. In particular, for local development, I wanted to be able to do mysql -u root -p without sudo. I don't want to create a new user. I want to use root from a local PHP web app.

The error message is misleading, as there was nothing wrong with the default 'root'@'%' user privileges.

Instead, as several people mentioned in the other answers, the solution was simply to set bind-address=0.0.0.0 instead of bind-address=127.0.0.1 in my /etc/mysql/mysql.conf.d/mysqld.cnf config. No changes were otherwise required.

Joseph8th
  • 636
  • 5
  • 10
-2

I had the same problem on CentOS and this worked for me (version: 8.0.11):

mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%'
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
user3563950
  • 333
  • 2
  • 9
  • ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '. TO 'root'@'%'' at line 1 – GDefender Aug 25 '18 at 14:05
  • mysql> GRANT ALL PRIVILEGES ON . TO 'root'@'%'; ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '. TO 'root'@'%'' at line 1 mysql> – PGOEL Oct 30 '18 at 10:00
-3

Stary mysql with sudo

sudo mysql