How to create a new user in MariaDB in one command line in Debian

Issue

I want to create a new database access user, but I need to pass the entire command on the same line:

mysql -u root; CREATE USER 'test' @ '%' IDENTIFIED BY '123456'; GRANT ALL PRIVILEGES ON *. * TO 'test' @ '%'; FLUSH PRIVILEGES;

How do I make it work?

Solution

You can use the echo command like:

echo "CREATE USER 'test' @ '%' IDENTIFIED BY '123456'; GRANT ALL PRIVILEGES ON *. * TO 'test' @ '%'; FLUSH PRIVILEGES; " | mysql -u root

Or use the -e option:

mysql -u root -e "CREATE USER 'test' @ '%' IDENTIFIED BY '123456'; GRANT ALL PRIVILEGES ON *. * TO 'test' @ '%'; FLUSH PRIVILEGES;"

Answered By – Bernd Buffen

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published