The command interpreter - mysql allows for interactive execution of SQL commands and for many routine and administrative tasks. At the launch of mysql, numerous options can be specified to manage formatting, outputs and other program controls. - The mysql command - Authentication parameters for user name and password - One can specify a name for an existing database, where the database becomes the default database to run commands on - same as saying USE databasename. If mysql immediately terminates, possibly with an error message such as Access denied for user sakilaadmin, then either access to MySQL is completely denied to user sakilaadmin, or it is protected by a password. In either case you must invoke mysql with the options -u name and -p:. Syntax mysql [options] [databasename] [ < commands.sql] Some Useful Options Command Alternate Form Description -e cmd --execute=cmd Executes the given command(s) in quotes sepa --tee=filename Redirect all input and output to the specified file; only allowed in interactive --no-tee Do not use logging (default). Here are a few examples of the commands using options above. These commands are stored in batch scripts and are intended to be executed on the OS command line. One can easily execute *.bat files on Unix/Linux as well: Code Sample: MySQLMonitor/Demos/Create-DB.bat mysql -u root -p -e "CREATE DATABASE sakilakubili" 1 / 7
The monitor executes a command to create a database after prompting the user for a password. Code Sample: MySQLMonitor/Demos/Select-Tee.bat mysql -B -u sakilauser -psakila sakila --tee=" sqllog/logfile1.log" -e "SELECT * FROM category" The monitor will not prompt for a password as it is specified on the command line itself. The monitor executes a SELECT command and dumps the session output into the given log file. Note: The directory to contain the file must already be created with right permissions. Formatting and Output Options Command Alternate Form Description -B --batch Separates columns in tables by tab characters -E --vertical Show query results with columns vertically, one co -N --skip-column-names Leaves off column titles in the output of tables. -s --silent Displays less status information than in normal mo Here are a few examples of the options above: Code Sample: MySQLMonitor/Demos/Select-Tee-Tabs.bat mysql -B -N -u sakilauser -psakila sakila --tee=" sqllog/tabs-notitle.log" -e "SELECT * FROM category" perror 2 The output will now be separated using tabs (-B option) and show no column titles (-N option). As before, the monitor will not prompt for a password as it is specified on the command line itself. The monitor executes a SELECT command and dumps the session output into the given log file. Code Sample: MySQLMonitor/Demos/Select-Vertical.bat 2 / 7
mysql --vertical -u sakilauser -psakila sakila --tee="sqllog/vertical.log" -e "SELECT * FROM category" The columns will be output one on a line (--vertical option). Warning: Be careful using this option with very large tables. As seen above, the --execute or -e is a very useful option with mysql as it can be used to pass SQL statements to the server. The statements must be enclosed by single or double quotation marks. If you wish to use quoted values within a statement, you should use double quotes for the statement, and single quotes for any quoted values within the statement. When this option is used, mysql executes the statements and exits. Tip: MySQL can be asked to generate XML output and even HTML tables. For example, you can use the following command to obtain a list of user accounts: Code Sample: MySQLMonitor/Demos/Show-Users.bat mysql -u root -p --execute="select User, Host FROM mysql.user" Show list of users from mysql database. Note: The long form (--execute) is followed by an equals sign (=). No default database is passed as a separate argument, as database is part of the query. Code Sample: MySQLMonitor/Demos/Execute-Multiple.bat mysql -u root -p -e "SELECT db FROM mysql.db;select VERSION();SELECT NOW()" 3 / 7
Show list of databases from mysql database, among other things. Multiple SQL statements may be passed on the command line, separated by semicolons Commands for Interactive Mode Unlike previous options which were used with the mysql at OS level, these commands are executed while using the monitor: Command Alternate Form Description \e edit Invoke external editor as EDITOR set via environment va En \g go Executes the command (equivalent to ; and Return \h help Displays a helpful list of available commands. \p print Displays the current command as it is in the buffer \c cancel Cancels input of current command and start all ove \q exit or quit \s status Displays status information about the MySQL serv \T [fn] tee [filename] Enter tee m Note :if the file already exists, then the input and output are appended to the end of th \t notee Ends tee m \u database USE database Makes the given database the default database. Executing a Script from the Monitor Use source command at the mysql command prompt to run SQL statements and mysql commands from a text file. The statements can include any statements that can be run inside of a MySQL Monitor. Executes the SQL commands contained in the file; the commands must be separated by semicolons. Create a script called " ClassFiles/MySQLMonitor/Dem os/select-category.sql " with the following SELECT statement: 4 / 7
Syntax SELECT * FROM category; At OS level, change to ClassFiles/MySQLMonitor and execute: Syntax root> mysql -uroot -p*** sakila mysql> source Demos/Select-Category.sql mysql> exit; We specify the source command followed the filename (and a file-path-prefix if file is not in the current directory). When you run the source command, the SQL statements and MySQL contained commands in the file are executed like a program. You can also use the following convention to use the source command: mysql>. Demos/Select-Category.sql The backslash and period (.) replace the word source. Execute the following SQL script logging in as sakilaadmin INTO sakila database: Code Sample: MySQLMonitor/Demos/Select-Tee.sql T sqllog/categories.log SELECT * FROM category; p t h USE mysql; SHOW TABLES; g q This SQL script will start a log, select output and then logout. Notice the similarity of name with a previous batch file. Warning: You will observe several failures, as is typical with quickly written scripts, of the errors are safe and self-explanatory. The errors will be discussed in the class. but most 5 / 7
Using the mysql Command to run a Script You can also use the mysql command at your operating system's command prompt to execute SQL statements and mysql commands in a text file, without actually launching the utility,. In addition to the regular mysql command, specify : - The (<) symbol to input STDIN to mysql command - The path and filename of the file that contains the statements. We will reuse our Select- Category.sql from before. The commands are input from a file with < file. All SQL commands can be used in this file. The commands must be followed by a semicolon. Comments are introduced with the character #. The following command is run at the operating system's command prompt: mysql -B -N -u sakilauser -psakila sakila < Select-Category.sql Note: Use double quotes to enclose the path and filename in if either name contains spaces. See Database Status The command STATUS displays of various status data information about the database: mysql> STATUS;... OR... mysql> s -------------- mysql Ver 14.12 Distrib 5.0.27, for Win32 (ia32) Connection id: 1 Current database: Current user: root@localhost SSL: Not in use Using delimiter: ; 6 / 7
Server version: 5.0.27-community-nt Protocol version: 10 Connection: localhost via TCP/IP Server characterset: latin1 Db characterset: latin1 Client characterset: latin1 Conn. characterset: latin1 TCP port: 3306 Uptime: 5 hours 28 min 52 sec Threads: 1 Questions: 3 Slow queries: 0 Opens: 12 Flush tables: 1 Open tables: 0 Queries per second avg: 0.000 -------------- MySQL Monitor Conclusion In this lesson of the MySQL tutorial, you learned about using mysql program - the MySQL client tool, also called the monitor. To continue to learn MySQL go to the top of this page and click on the next lesson in this MySQL Tutorial's Table of Contents. 7 / 7