Mysql – How to drop user@% on the MySQL CLI

command lineMySQL

How to drop 'user'@'%' on the MySQL CLI using the mysql -e command?

mysql -uroot  -p -e "'myuser'@'%'";

This does not work.

Best Answer

You have to surround the % character with single quotes

DROP USER user@'%';

or put the user in single quotes as well

DROP USER 'user'@'%';

From the CLI, you would run something like this

MYSQL_USER=root
MYSQL_PASS=rootpassword
MYSQL_CONN="-u${MYSQL_USER} -p${MYSQL_PASS}"
SQL="DROP USER 'user'@'%'"
mysql ${MYSQL_CONN} -ANe"${SQL}"

An alternative method would be delete it from mysql.user

MYSQL_USER=root
MYSQL_PASS=rootpassword
MYSQL_CONN="-u${MYSQL_USER} -p${MYSQL_PASS}"
SQL="DELETE FROM mysql.user"
SQL="${SQL} WHERE user='user'"
SQL="${SQL} AND host='%';"
SQL="${SQL} FLUSH PRIVILEGES;"
mysql ${MYSQL_CONN} -ANe"${SQL}"