mysql is a simple SQL shell (with GNU readline capabilities). It supports interactive and non-interactive use. When used interactively, query results are presented in an ASCII-table format. When used non-interactively (e.g., as a filter), the result is presented in tab-separated format. (The output format can be changed using command-line options.) You can run scripts simply like this:
shell> mysql database < script.sql > output.tab
If you have problems due to insufficient memory in the client, use the --quick option! This forces mysql to use mysql_use_result() rather than mysql_store_result() to retrieve the result set.
Using mysql is very easy; Just start it as follows mysql database or mysql --user=user_name --password=your_password database. Type a SQL statement, end it with `;', `\g' or `\G' and press return/enter.
mysql supports the following options:
-?, --help -A, --no-auto-rehash -B, --batch -C, --compress -#, --debug[=...] -D, --database=.. my.cnf file. -e, --execute=... -E, --vertical \G. -f, --force -i, --ignore-space -h, --host=... -H, --html -L, --skip-line-numbers -n, --unbuffered -N, --skip-column-names -O, --set-variable var=option --help lists variables. -o, --one-database -p[password], --password[=...] -p you can't have a space between the option and the password. -P --port=... -q, --quick -r, --raw --batch -s, --silent -S --socket=... -t --table -T, --exit-info -u, --user=# -U, --safe-updates[=#], --i-am-a-dummy[=#] UPDATE and DELETE that uses keys. See below for more information about this option. You can reset this option if you have it in your my.cnf file by using --safe-updates=0. -v, --verbose -V, --version -w, --wait If you type 'help' on the command line, mysql will print out the commands that it supports:
mysql> help MySQL commands: help (\h) Display this text ? (\h) Synonym for `help' clear (\c) Clear command connect (\r) Reconnect to the server. Optional arguments are db and host edit (\e) Edit command with $EDITOR exit (\q) Exit mysql. Same as quit go (\g) Send command to mysql server ego (\G) Send command to mysql server; Display result vertically print (\p) Print current command quit (\q) Quit mysql rehash (\#) Rebuild completion hash source (\.) Execute a SQL script file. Takes a file name as an argument status (\s) Get status information from the server use (\u) Use another database. Takes database name as argument
The status command gives you some information about the connection and the server you are using. If you are running in the --safe-updates mode, status will also print the values for the mysql variables that affects your queries.
A useful startup option for beginners (introduced in MySQL 3.23.11) is --safe-mode (or --i-am-a-dummy for users that has at some time done a DELETE FROM table_name but forgot the WHERE clause. When using this option, mysql sends the following command to the MySQL server when opening the connection:
SET SQL_SAFE_UPDATES=1,SQL_SELECT_LIMIT=#select_limit#,
SQL_MAX_JOIN_SIZE=#max_join_size#"
where #select_limit# and #max_join_size# are variables that can be set from the mysql command line. See section 7.28 SET syntax.
The effect of the above is:
UPDATE or DELETE statements if you don't have a key constraint in the WHERE part. One can however force an UPDATE/DELETE by using LIMIT: UPDATE table_name SET not_key_column=# WHERE not_key_column=# LIMIT 1;
#select_limit# rows. SELECT's that will probably need to examine more than #max_join_size row combinations will be aborted.