CREATE USER MySQL - 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.

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.

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

DROP USER IF EXISTS triggers syntax error in MySQL

I have a brand new mysql installation. I want to delete a user only if exists and I have only root user. I'm trying to run from shell:
DROP USER IF EXISTS foo;
or
DROP USER IF EXISTS 'foo'#'localhost';
But mysql returns this kind of messages:
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 'IF EXISTS foo' at line 1
What am I doing wrong?
I read this post but does not explain why IF EXISTS statement does not work
From your own link:
As of MySQL 5.7.8, the IF EXISTS clause can be used, which causes the
statement to produce a warning for each named account that does not
exist, rather than an error.
You can find out your server version with e.g.:
select ##version;

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;

How can I create a MySQL-database

I tried to create a database by the following command:
mysql> GRANT SELECT,INSERT,UPDATE,DELETE,CREATE,ALTER,INDEX,DROP,
-> CREATE TEMPORARY TABLES,SHOW VIEW,CREATE ROUTINE,ALTER ROUTINE,
-> EXECUTE,CREATE VIEW,EVENT,TRIGGER
-> ON {projekti_db}.*
-> TO '{asiakas}'#'localhost';
and got the following error message
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 '{projekti_db}.*
TO '{asiakas}'#'localhost'' at line 4
I have MySQL 5.1.37. What am I doing wrong?
You are trying to assign roles to a user on a certain database. To create a database, use
CREATE DATABASE NAME;
That syntax doesn't create a database, it grants db privileges to a user.
To create a database, you'd use:
CREATE DATABASE projekti_db;
See here for a quick cheat sheet for MySQL.
#
# First create the Database
#
CREATE DATABASE projekti_db;
#
# Then Create the User that will access that database
#
GRANT SELECT,INSERT,UPDATE,DELETE,CREATE,ALTER,INDEX,DROP,
CREATE TEMPORARY TABLES,SHOW VIEW,CREATE ROUTINE,ALTER ROUTINE,
EXECUTE,CREATE VIEW,EVENT,TRIGGER
ON projekti_db.*
TO 'asiakas'#'localhost';
#
# Now verify that the user asiaskas can access the projekti_db only from localhost
#
SHOW GRANTS FOR 'asiakas'#'localhost';
Give it a Try !!!