15

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)?

1 Answer 1

33

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
Sign up to request clarification or add additional context in comments.

2 Comments

But it requires a running MySQL doesn't it?
@Qwe Any modification to a MySQL database including simply setting the root password requires a running MySQL instance.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.