I'm looking for a way to reduce the amount of typing I do to check mysql entries and other things. For example, I wanted to create a .bat file that will execute the following commands.

mysql -u user -p
*enter in the password*
USE databasename
SELCT * FROM table;

The problem is after the initial mysql -u user -p the rest of the commands written down do not get executed. Is it possible to continue running commands after calling something like mysql or other programs, that seem to add its prefix(?) to the beginning of commands (mysql>). I'm not very familiar with the terminology so forgive me if it sounds confusing. I'd like to use the same kind of concept for other things as well.

Help is much appreciated, Thanks.

  • they 'mysql>' in the beginning means that you're no longer working with 'cmd', but mysql instead. This is the root of your problem, as your batch file assumes you're using cmd. – hyleaus Nov 21 '12 at 1:33

You can run mysql in batch mode, as noted in the documentation.

mysql -h host -u user -p < batch-file

Basically you use a file containing all of your commands as an input parameter - mysql will execute the contents of that file.

Edit: If you want to build your query on the fly, you can always have your batch file write out a query to a temporary file that you can then load for execution by mysql. For example:

echo show tables from test > C:\path\to\file.sql
mysql -h host -u user -p < C:\path\to\file.sql
  • I seem to recall that while looking for a solution, but is that the only way? I would like to implement some sort of solution for multiple applications. – BCIT - JD Nov 21 '12 at 3:44
  • I don't believe that you can specify individual commands to execute because, as hyleaus mentioned, you're no longer running in cmd when you attempt to run mysql without specifying an input. I've updated my answer with an a solution that will still allow you to build on the fly, though... – Alec Sanger Nov 21 '12 at 3:54
  • This works, but its pretty much has the same results as the solution user Aacini posted below. – BCIT - JD Nov 21 '12 at 16:32

Run mysql with -e option:

mysql -h host -u user -p -e 'SHOW TABLES FROM test'

Perhaps you may want to try a small trick that emerged from other question in this forum (that was deleted unfortunately).

You may insert the input for a command directly in the lines below the command and then execute the file NOT as Batch file, but as input por cmd.exe (this is similar to a here document in Unix). For example:


@echo off
mysql -u user -p
*enter in the password*
USE databasename
SELCT * FROM table;
echo Exit from MYSQL...

Execute previous "script" this way:

cmd < script.TXT

If you perform this test, please report the result...


  • That seemed to have worked, although in a limited regard. It will run the script just fine, and display the table results but I wasn't able to continue writing SQL commands. It would return to cmd as soon as it finished the txt file commands. However it is still useful in many regards. – BCIT - JD Nov 21 '12 at 16:25
  • Of course, that is the intended result: execute SQL automatically with NO manual intervention from the user. Note that the EXIT command I put in the example is for SQL, otherwise is possible that SQL hangs waiting for input after the end of the input file... – Aacini Nov 21 '12 at 23:07

You could write something like this

mysql -u dbUsername yourDatabase -e "SELECT * FROM table;"

Or to run repeating tasks create a runtasks.bat file, save under the root of your project then write your cmd tasks inside

mysql -u dbUser -e "DROP DATABASE IF EXISTS testDatabase;"
mysql -u dbUser -e "CREATE DATABASE testDatabase;"

php index.php migration latest #

cd application\tests

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.