mysql grant sql script to user not working - mysql

the title says it all, here was my code
GRANT SELECT, INSERT, UPDATE, DELETE ON 'database_name' TO 'mysqluser'#'111.111.111.111';
it says
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 near ''database_name' TO 'mysqluser'#'111.111.111.111'' at line 1

This:
GRANT SELECT, INSERT, UPDATE, DELETE ON 'database_name' TO 'mysqluser'#'111.111.111.111';
Should be:
GRANT ALL ON 'database_name'.* TO 'mysqluser'#'111.111.111.111';
In your specific case(tested on my mysql server):
GRANT SELECT, INSERT, UPDATE, DELETE ON database_name.* TO 'mysqluser'#'111.111.111.111';

You quoted the DB name with ', which turns it into a string. Once it's a string, it's no longer a table name. it's just a string that CONTAINS something that looks like a table name.
It should be EITHER
GRANT ... ON database_name
or
GRANT ... ON `database_name`
The backticks are only necessary if the db name happens to be a reserved word.

Related

'Learning SQL: Master SQL Fundamentals' syntax error

Please I'm a newbie and i'm using the alain Beaulieu book 'Learning SQL' and on the first tutorial we need to add this function after creating the database 'bank'.
I have created it and typed the code but the shell returns the error after this code :
mysql> grant all privileges on bank.* to 'lrngsql'#'localhost' identified by 'xyz';
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 'xyz'' at line 1
I didn't find anyone speaking about this problem, so I hope someone could help
In older versions of MySQL you could use IDENTIFIED BY in a GRANT statement to change passwords or event create accounts. Newer versions do not have this option. Adding a user and granting it privileges are two separate things and therefore have to be done in two different statements.
Try
CREATE USER 'lrngsql'#'localhost' identified by 'xyz';
GRANT ALL PRIVILEGES ON bank.* TO 'lrngsql'#'localhost';
For more information have a look at the documentation for GRANT and CREATE USER.

CREATE USER MySQL

Error with dropping tables:
SQL query:
DROP user IF EXISTS 's01'#'%';
MySQL said: Documentation
#1064 - 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 'if exists 's01'#'%'' at line 1
Error without dropping tables:
SQL query:
/*Students*/ CREATE User 's01'#'%' IDENTIFIED BY 'pw1';
MySQL said: Documentation
#1396 - Operation CREATE USER failed for 's01'#'%'
Example of some of my code:
Drop user if exists 's01'#'%';
flush privileges;
/*Students*/
Create User 's01'#'%' Identified by 'pw1';
I don't exactly know what is wrong. I looked it up and it said add flush privileges, but I still get the same two errors.
Check your version of mysql with select version();. The IF EXISTS option for DROP USER was added in 5.7. If you're not using 5.7, then that likely explains your first error.
Check if the user exists by querying mysql.user table.
select user, host from mysql.user where user = 's01';
Also since you are not specifying a hostname, just execute
DROP user IF EXISTS 's01';
Create User 's01' Identified by 'pw1';
The wildcard host (#'%') is implied.
Also execute SHOW GRANTS to verify that your user has the correct privileges to create and drop users.

REVOKE insert on a specific table form a user in my sql

i want to revoke insert on a specific table form a user in mysql
and I try this code before:
use varian_db;
REVOKE INSERT ON 'gevhm_users' FROM 'varian_user';
but mysql returned this error :
#1064 - 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 ''gevhm_users' FROM 'varian_user'' at line 1
Please help.
You have to specify full path of database name and table name when granting or revoking privileges to/from such user. And also write user name in correct format. Your statement should like this:
REVOKE INSERT ON varian_db.gevhm_users FROM 'varian_user'#'user_ip_address';
FLUSH PRIVILEGES;
More information: MySQL Revoke Syntax

SQL database with column in the tablename

I have an error in my MySQL statement (using shell file), which I can't seem to resolve. Tried to put quotes etc but no luck. When I created it using phpmyadmin it works, but I want to have it scripted. Must be something small... Any ideas?
--- Create mySQL database ---
CREATE DATABASE IF NOT EXISTS _test-123;
CREATE USER 'test-123'#localhost IDENTIFIED BY '***';
GRANT ALL PRIVILEGES ON _test-123.* TO 'test-123'#'localhost';
FLUSH PRIVILEGES;
Enter password:
ERROR 1064 (42000) at line 1: 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 '-123' at line 1
Try removing the "-" and do this one instead-
CREATE DATABASE IF NOT EXISTS _test_123;

mysql error when using "IDENFIFIED BY " at end of statement

I am following instruction to set permissions on a db in MySQL-Client-Version: 5.1.61
This is the statement I should enter:
GRANT SELECT, INSERT, DELETE, UPDATE, CREATE, DROP, ALTER, INDEX on 'databasename'.*
TO 'username'#'localhost' IDENFIFIED BY 'password';
Obviously I change the names but keep the quotes.
The error is -
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 ''databasename'.* TO 'username'#'localhost' IDENFIFIED BY 'password'' at line 1
What is going wrong here?
Use IDENTIFIED BY instead of IDENFIFIED BY.