Cry MySQL How to...
Run a database script from the command line
To run a database script from the command line you will need to use the mysql command line tool. These notes assume familiarity with the notes on how to "Connect to a MySQL database using the mysql command line tool".
The general format of the command line to take and run a script is:
For example, assuming there is a script containing various SQL statements and that script is called "myscript.sql" then
will connect to the mysql database on the local computer using the account "root" and password "secret". Since this example does not specify the database to use it is assumed that the script specifies the database, but in general it may be better style to specify the database explicitly, for example:
which does the same as the previous example, but explicitly specifies that the script is to be run against the database called "census".
These notes have been tested against MySQL version 4.1 and 5.0
About the author: Brian Cryer is a dedicated software developer and webmaster. For his day job he develops websites and desktop applications as well as providing IT services. He moonlights as a technical author and consultant.