Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking if mysql user exists

Tags:

mysql

How can I check if a user exists?

Im doing an installer for a mysql database, and I need to check if a user exits, if not create user, if yes delete user and create it again.

this so i can execute the script without worries.

thanks.

like image 663
sergiogx Avatar asked Jun 16 '10 00:06

sergiogx


People also ask

How do you check is user created or not in MySQL?

Use the following query to show MySQL users created in the database server: SELECT user FROM mysql. user; As a result, you will see the list of all the users that have been created in MySQL.

How do you check if username already exists in php MySQL?

$query = mysql_query("SELECT username FROM Users WHERE username=$username", $con); if (mysql_num_rows($query) != 0) { echo "Username already exists"; } else { ... }

What is user @% in MySQL?

User@% would allow access from all locations. User@localhost would only allow access from localhost. They are two different users with two different passwords (though you can set them to the same password, but if you update one password the other will not auto-update)


1 Answers

MySQL stores user data in a table called user in a database named mysql (by default). The following query will return 1 if a user with the specified username exists, 0 otherwise.

SELECT EXISTS(SELECT 1 FROM mysql.user WHERE user = 'username') 
like image 144
Lauri Lehtinen Avatar answered Oct 04 '22 11:10

Lauri Lehtinen