I want to test if a user exists. If it does I should do nothing. If it doesn't I should create it and grant privileges.
The concept code that I came up with is this:
IF EXISTS(SELECT 1 FROM mysql.user WHERE user = 'web_service_user' AND host = '%') THEN
\! echo "EXISTS";
ELSE
\! echo "DOES NOT";
END IF;
Hower when I run this I get this output:
EXISTS
DOES NOT
ERROR 1064 (42000) at line 1 in file: 'test_conditional.sql': You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'ELSE
END IF' at line 3
So how do I test (in a conditional) if a user exists?
I know of this: mysql create user if not exists
However I would need still need to check if the user "was just created" in order to know If I need to execute the grant statements.
Related
i'm trying to do a forward engineering and at the end it gaves me this error
"ERROR: 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 'IF EXISTS Amministrazione' at line 1"
SQL Code: DROP USER IF EXISTS Amministrazione
This is the code:
SET SQL_MODE = '';
DROP USER IF EXISTS Amministrazione;
SET SQL_MODE='ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION';
CREATE USER 'Amministrazione' IDENTIFIED BY 'amministrazione';
GRANT EXECUTE ON procedure `GestioneCorsiDiLingue`.`Generare report Insegnante Mensile` TO 'Amministrazione';
GRANT EXECUTE ON procedure `GestioneCorsiDiLingue`.`Elimina Corso` TO 'Amministrazione';
GRANT EXECUTE ON procedure `GestioneCorsiDiLingue`.`Aggiunta Insegnante` TO 'Amministrazione';
GRANT EXECUTE ON procedure `GestioneCorsiDiLingue`.`Elimina Insegnante` TO 'Amministrazione';
GRANT EXECUTE ON procedure `GestioneCorsiDiLingue`.`Creazione corso` TO 'Amministrazione';
GRANT EXECUTE ON procedure `GestioneCorsiDiLingue`.`Assegnazione insegnanti a corso` TO 'Amministrazione';
if you can please help me it would be great because i can't really figure out what's the problem, if you need further details just ask me!
The best solution to solve this error is to upgrade to MySQL 5.7 (or later).
The "IF EXISTS" option for DROP USER was not supported before MySQL 5.7. If your MySQL Server is an earlier version, that syntax will return error 1064.
You can use DROP USER in older versions of MySQL, but not DROP USER IF EXISTS.
I'm talking about the version of MySQL Server, regardless of the version of the client like MySQL Workbench. The SQL syntax you can use is that which is supported by the server.
You can verify your MySQL Server version:
SELECT ##version;
P.S. MySQL 5.6 reached its end-of-life as of 2021-02-05 (that is, yesterday as I post this). So you should upgrade anyway.
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.
I've copied-pasted alter user "root"#"localhost" identified by "NEW PASSWORD"; into mysql>. The message is:
Query OK, 0 rows affected (0,00 sec)
But if I enter exactly the same command (tested several times!), the message 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 'indentified by "NEW PASSWORD"' at line 1
Why?
Please take a closer at the error the server returns; you're typing indentified instead of identified!
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;
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;