Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating mysql users via linux command line?

Tags:

linux

mysql

I'm working on a python script to setup servers quickly and it basically has a list of commands I want to execute on the linux commandline.

I can install all the software I need but not sure how to create a mysql user solely via command line? I can I can go into the mysql shell and do it but is there a way to do it solely from the linux shell(once the user is created I can do all the database setup remotely with a python script)?

like image 704
Lostsoul Avatar asked Feb 13 '12 15:02

Lostsoul


1 Answers

You do it with the same query you'd use in the client, but execute it on the command line with the -e flag:

mysql -uroot -prootpw -e "GRANT ALL PRIVILEGES ON dbname.* TO username@hostname IDENTIFIED BY 'userpassword'"

Or pipe in the command

echo "GRANT ALL PRIVILEGES ON dbname.* TO username@hostname IDENTIFIED BY 'userpassword'" | mysql -uroot -prootpw
like image 100
Michael Berkowski Avatar answered Nov 05 '22 18:11

Michael Berkowski