1

I am trying to create a batch file to create a new user and then assign permissions to the new user. I want to do something like this:

c:\mysql\bin\mysql -uroot -ppassword < CREATE USER 'username_here'@'localhost' IDENTIFIED BY 'password_here';
c:\mysql\bin\mysql -uroot -ppassword < GRANT ALL ON myDB.* TO 'username_here'@'localhost';

Problem is I get an error saying the System can not find the file specified. I know I am calling the mysql exe from the right place because if I just do:

c:\mysql\bin\mysql -uroot -ppassword

it logs in and gives me the mysql prompt. Am I doing something wrong?

3 Answers 3

4

You can use the --execute option to pass one statement to the database:

c:\mysql\bin\mysql -uroot -ppassword --execute="CREATE USER 'username_here'@'localhost' IDENTIFIED BY 'password_here';"
c:\mysql\bin\mysql -uroot -ppassword --execute="GRANT ALL ON myDB.* TO 'username_here'@'localhost';"
Sign up to request clarification or add additional context in comments.

Comments

1

You need to puts quotes around the command, otherwise the shell thinks you are giving it a file name:

c:\mysql\bin\mysql -uroot -ppassword < "CREATE USER 'username_here'@'localhost' IDENTIFIED BY 'password_here';"
c:\mysql\bin\mysql -uroot -ppassword < "GRANT ALL ON myDB.* TO 'username_here'@'localhost';"

1 Comment

That's because < is used to redirect input from a file, not input verbatim text to a program.
0

You can simplify things slightly using just a single GRANT statement:

mysql mysql -uroot -ppassword --execute "GRANT ALL ON myDB.* TO username_here@localhost IDENTIFIED BY 'password_here';"

You do seem to have to specify the mysql database when you run it this way, and if you don't use reserved words for your username, you can avoid a few quotes.

Comments

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.