How to grant all privileges to a user in MySql - mysql

I am connected as a root user in MySql.
Root user has GRANT and
grant all privileges on *.* to 'root'#'localhost' with grant option;
went through last time I did it without error code returned.
When I however change root to sqluser there is: "Error Code: 1410. You are not allowed to create a user with GRANT
"
Earlier action:
grant grant option on *.* to 'sqluser'#'%'
and it didn't return error code. However as I ran query with localhost there is error code with number 1410.
I have MySql 8.0 installed.
How can I make grant all privileges go through?

I had the same problem when setting up a new mysql database, at the point of adding the first user.
I have found my problem was resolved by removing the #'localhost' from the GRANT.
This works:
mysql> grant all privileges on *.* to 'laravel_user';
Query OK, 0 rows affected (0.10 sec)
This does not:
mysql> grant all privileges on *.* to 'laravel_user'#'localhost';
ERROR 1410 (42000): You are not allowed to create a user with GRANT
mysql version:
$ mysql -V
mysql Ver 8.0.12 for osx10.14 on x86_64 (Homebrew)
I was following this guide to set up the database:
https://www.a2hosting.co.uk/kb/developer-corner/mysql/managing-mysql-databases-and-users-from-the-command-line

test this code.
GRANT ALL PRIVILEGES ON *.* TO 'user'#'localhost' REQUIRE NONE WITH GRANT OPTION MAX_QUERIES_PER_HOUR 0 MAX_CONNECTIONS_PER_HOUR 0 MAX_UPDATES_PER_HOUR 0 MAX_USER_CONNECTIONS 0;

Related

Grant all privileges to root user in MySQL 8.0.21

I am aware that this question has been asked already on stackoverflow but I don't see any answer for the actual question posted. Hence posting again.
GRANT ALL PRIVILEGES ON *.* TO 'root'#'localhost' IDENTIFIED BY PASSWORD 'abcd' WITH GRANT OPTION;
GRANT ALL PRIVILEGES ON *.* TO 'root'#'::1' IDENTIFIED BY PASSWORD 'abcd' WITH GRANT OPTION;
Above lines throw
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 PASSWORD 'abcd' WITH GRANT OP' at line 1
I am trying to upgrade mysql from 5.7 to latest 8.0.21
Thanks!
For GRANT ALL privileges to root user use the following syntax:
GRANT ALL PRIVILEGES ON database_name.* TO 'root'#'localhost';
FLUSH PRIVILEGES;
The above grant mysql command defines that:
GRANT the PRIVILEGES of type ALL
These privileges are for a particular database named dbname and it applies to all tables of that database indicated by the .* that follows the dbname.
These privileges are assigned to username when that username is connected through locally, as specified by #'localhost'.
You can change to specify any valid host, replace 'localhost' with '%'.

Access denied for user 'root'#'localhost' to database 'drupal'

I'm working on a MySQL server I haven't configured my self and I'm a bit of a noob when it comes to SQL.
I've created a database like this:
root#localhost:~# mysqladmin -u root -p create drupal
And then tried to grant privileges like this:
root#localhost:~# mysql -u root -p
MariaDB [(none)]> GRANT ALL PRIVILEGES ON drupal.* TO root IDENTIFIED BY ...;
But get this error:
ERROR 1044 (42000): Access denied for user 'root'#'localhost' to database 'drupal'
[Update] I get this error with all forms of GRANT commands.
But according to SHOW GRANTS I should have privileges:
MariaDB [(none)]> SHOW GRANTS;
+---------------------------------------------------------------------------
-------------------------------------------+
| Grants for root#localhost
|
+---------------------------------------------------------------------------
------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'root'#'localhost' IDENTIFIED BY PASSWORD
'*...' |
+---------------------------------------------------------------------------
-------------------------------------------+
1 row in set (0.00 sec)
MySQL version is: mysql Ver 15.1 Distrib 10.1.26-MariaDB
On Debian 9.5
The problem was that the root user didn't have Grant privileges.
SELECT host,user,password,Grant_priv,Super_priv FROM mysql.user;
I fixed it with this command:
UPDATE mysql.user SET Grant_priv='Y', Super_priv='Y' WHERE User='root';
FLUSH PRIVILEGES;
GRANT ALL ON *.* TO 'root'#'localhost';
You have to log out and in again for it to work.

MySQL "mysql there is no such grant defined for user"

This error happened when I granted all privileges to a new root account I just created.
Steps to produce the problem:
CREATE USER 'root'#'localhost';
GRANT ALL PRIVILEGES ON *.* TO 'root'#'localhost' WITH GRANT OPTION;
FLUSH PRIVILEGES;
SHOW GRANTS for 'root'#'localhost';
After "show grants" I got the error "mysql there is no such grant defined for user 'root' on host 'localhost'". There were no errors after executing the first three commands. The new user was created successfully.
How do I solve this problem?
More info:
I'm running MySQL 5.7 on my MacOS laptop(OSX 10.10.5).
There is nothing wrong with your posted code but as guess try with wildcard symbol % like
SHOW GRANTS for 'root'#'%';
(OR)
As an alternative, login with your created user 'root'#'localhost' and just use SHOW GRANTS. See Documentation
I don't think mysql allows you to create another root account. So the create causes an error.
CREATE USER 'root'#'localhost';
ERROR 1396 (HY000): Operation CREATE USER failed for 'root'#'localhost'
You should check for the existing root account in the user table and you'll find the wildcard to be '%' which should mean you do not need to create a localhost root user.
select * from user where user = 'root';
Asking to show grants on root localhost should work, and does work for me.
show grants for 'root'#'localhost';
Step-1:
sudo mysql -u root -p
Step-2:
REVOKE ALL PRIVILEGES, GRANT OPTION FROM 'user name'#'localhost';
Ex.-
REVOKE ALL PRIVILEGES, GRANT OPTION FROM 'admin'#'localhost';
Step-3:
FLUSH PRIVILEGES;
I think it will work.

ERROR 1045 (28000): Access denied for user 'root'#'%'

I am trying to run
GRANT ALL PRIVILEGES ON * . * TO 'root'#'localhost';
from a remote server to localhost/phpmyadmin but getting ERROR 1045 (28000): Access denied for user 'root'#'%' (using password: NO).
mysql> show grants;
+----------------------------------+
| Grants for root#% |
+----------------------------------+
| GRANT USAGE ON *.* TO 'root'#'%' |
+----------------------------------+
How can I resolve this problem? I have gone through a lot of suggestions over internet but can't actually figure out.
I think, you forgot to flush privileges.
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'#'localhost' with GRANT OPTION;
mysql> FLUSH PRIVILEGES;
Note:
I have included with GRANT OPTION because as doc says:
The GRANT OPTION privilege enables you to give to other users or remove from other users those privileges that you yourself possess.
Edit 1:
Depending on comments, seems like you have forgotten the root password. So try to reset like this:
Stop the MySQL Server.
sudo /etc/init.d/mysql stop
Start the mysql without password
sudo mysqld_safe --skip-grant-tables &
Login to mysql as root:
mysql -u root
Set new password to root
UPDATE mysql.user SET Password=PASSWORD('password') WHERE User='root';
Flush privileges.
FLUSH PRIVILEGES;
Finally, restart mysql normally.
If you have root credential , then Set the privileges like mentioned below.
grant all privileges on db.* to user#'%' identified by 'pass';
Thanks

Why can't I login with newly created MySQL User?

I create a MySQL user as follows:
mysql> grant all privileges on *.* to mysql#'%' IDENTIFIED by 'myPassword' with grant option;
Query OK, 0 rows affected (0.00 sec)
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.02 sec)
mysql> exit;
Bye
Then I try to login with that newly created user but it doesn't work.
% mysql -u mysql -pmyPassword
ERROR 1045 (28000): Access denied for user 'mysql'#'localhost' (using password: YES)
%
Why?
I believe localhost is special and not covered by the '%'.
Issue
mysql> grant all privileges on *.* to mysql#'localhost' IDENTIFIED by 'myPassword' with grant option;
and it should work
It seems counter-intuitive, but 'localhost' is not matched by '%' in MySQL. The reason is that all '%' matches all TCP/IP connections, but connections from 'localhost' are mapped to the unix domain socket.
There are two possible solutions:
Do an extra grant for the user at 'localhost':
mysql> grant all privileges on *.* to mysql#'localhost' IDENTIFIED by 'myPassword' with grant option;
PS: you don't have to FLUSH PRIVILEGES after GRANT.
Force access via TCP/IP:
% mysql -u mysql -pmyPassword -h 127.0.0.1