mySQL Console

MySQL Console (mysql.exe) is a command-line sql interface. It allows you to enter any sql command - Connect, Create, Select, Insert, and the like - or to run a script containing those commands.

This program

Running mysql.exe via Windows Explorer | Comments | mysql.exe Commands | Debugging Scripts - Registry Mods


Running mysql.exe via Windows Explorer

The simplest way to run mysql.exe (IMHO) is to To make this work, I had to modify the registry

This is a possible *.reg file - on some systems, the .sql key may already exist.

Notice that If you do it manually, enter this


Comments

There are 2 methods to enter comments


mysql.exe Commands

In mysql.exe you can enter normal sql commands, terminated by a semicolon.

Several other "special" commands are supported

If you enter a command without a terminator (semicolon), the prompt changes from mysql> to -> (continuation). This allows you to type commands on several lines - to return to normal mode, enter the current terminator and press enter.

There are no commands to perform the following function.


Debugging Scripts

Using mysql in batch mode, it is a challenge to debug scripts - this should help.

When running a script, it is likely that an error will be displayed ... but the command that caused the error will not be displayed.

There are 2 ways to display where the error is

 


Registry Mods

These are several registry modifications that will simplify debug. Only the commands are discussed below - a reg file is provided to modify your registry. Please view and verify the content before merging the data ... fixing any paths as appropriate. (For instance, mysql.exe may be in a different directory on your system.) Note - if your system already has commands associated with *.sql files, this *.reg file will ignore and replace them. Please check first. In the first example, the code simply executes the sql file and displays the errors (if any). This simplifies checking for errors. The others display the sql text and associated errors - this simplifies debug.

For simple problems, I prefer to display the errors in the command window ... but for more complex errors, or large files where I want to search for the errors, then notepad is more convenient. The last command (actually entered on a single line, wrapped for readability) produces a file that contains the console output and then opens that file in notepad. Notice that cmd supports multiple commands separated by

Because a single ampersand does not wait for the previous command to complete a double ampersand is required before running notepad, otherwise notepad opens with only part of the data. There is an interesting problem - if a user name is not provided, the default from the config file is used. Even though there is no associated password, batch files seem to work fine. They are able to drop and create databases without authorization. When I open mysql.exe directly, I am prompted for a password. However, there is never a prompt with a script/sql file. You might assume that the first line of the file is treated as the password .. but that text does not work in interactive mode.

Reference - 4.5.1 - The MySQL Command-Line Tool

 


Registry Mods for *.sql files

This is a more complete registry definition. It includes notepad so that accidentally double clicking an sql file will not actually run the script. (That would be very bad :)

Using IE, you could just copy the above text to the clipboard and use that to make your own *.reg file. However, that won't work with Firefox (yet another reason I truly dislike that product.) Therefore, I have provided the following reg file you can download - be sure to modify it for your system before using it.


Author: Robert Clemenzi
URL: http:// mc-computing.com / Databases / MySQL / mySQL_Console.html