Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question

migrated from Nov 22 '12 at 4:08

This question came from our site for professional and enthusiast programmers.

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
up vote 5 down vote accepted

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
share|improve this answer
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

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...


share|improve this answer
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

Run mysql with -e option:

mysql -h host -u user -p -e 'SHOW TABLES FROM test'
share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .