Mysql command line tool

mysql> help

For information about MySQL products and services, visit:
   http://www.mysql.com/
For developer information, including the MySQL Reference Manual, visit:
   http://dev.mysql.com/
To buy MySQL Enterprise support, training, or other products, visit:
   https://shop.mysql.com/

List of all MySQL commands:
Note that all text commands must be first on line and end with ';'
?         (\?) Synonym for `help'. 
clear     (\c) Clear the current input statement.
connect (\ r) Reconnect to the server. Optional arguments are db and host.   Reconnect to the server. Optional parameters are db and host.
delimiter (\ d) Set statement delimiter. Set statement delimiter.
ego       (\G) Send command to mysql server, display result vertically.
exit      (\q) Exit mysql. Same as quit.
go        (\g) Send command to mysql server.
help      (\h) Display this help.
notee     (\t) Don't write into outfile.
print     (\p) Print current command.
prompt    (\R) Change your mysql prompt.
quit      (\q) Quit mysql.
rehash    (\#) Rebuild completion hash.
source    (\.) Execute an SQL script file. Takes a file name as an argument.
status    (\s) Get status information from the server.
system    (\!) Execute a system shell command.
tee       (\T) Set outfile [to_outfile]. Append everything into given outfile.
use       (\u) Use another database. Takes database name as argument.
charset   (\C) Switch to another charset. Might be needed for processing binlog with multi-byte charsets.
warnings  (\W) Show warnings after every statement.
nowarning (\w) Don't show warnings after every statement.
resetconnection(\x) Clean session context.

For server side help, type 'help contents'

The default mysql》 prompt can be reconfigured. The string defining the prompt can contain the following special sequence:

Options

description

\ v

Server version

\d

Current database

\h

Server host

\p

Current TCP / IP port or socket file

\ u

Your username

\ U

Your full user_name @ host_name account name

\\

'\' Backslash character

\n

New line character

\t

Tab character

\

Space (space after backslash)

\_

Space

\R

Current time, 24-hour military time (0-23)

\r

Current time, standard 12-hour (1-12)

\m

Minutes of current time

\and

Current year, two

\AND

Current year, four digits

\D

Current date

\s

Seconds of current time

\w

The day of the current week, in 3-character format (Mon, Tue, ...)

\P

am/pm

\The

Current month, number format

\O

Current month, 3-character format (Jan, Feb, ...)

\c

Count increments with each statement issued

\S

semicolon

\'

apostrophe

\"

Double quotes

The other letters following the '\' become that letter.

The mysql client program is generally used interactively:

shell> mysql db_name

You can also put SQL statements in a file and tell mysql to read input from the file. To achieve this, create a text file text_file and include the statements you want to execute. Then call mysql as follows:

shell> mysql db_name < text_file

You can also start a text file with a USE  db_name statement. In this case, you do not need to specify the database name on the command line:

shell> mysql < text_file

If you are running mysql, you can use the source or \. Command to execute the SQL script file:

mysql> source filename
mysql> \. filename

Sometimes you want to use a script to display progress information to the user; for this you can insert the following line:

SELECT '<info_to_display>' AS ' ';

<Info_to_display> will be output.

 

I understand the meaning of the script:

A file written in a certain language that meets a specific grammar for a certain purpose.

Display query results vertically:

Some query results are easier to read if they are displayed vertically instead of in the usual horizontal table format. Use \ G instead of semicolon

The query can be displayed vertically after ending the query. For example, longer text values ​​that include new lines are usually easier to read when output vertically:

mysql> SELECT * FROM mails WHERE LENGTH(txt) < 300 LIMIT 300,1\G
*************************** 1. row ***************************
  msg_nro: 3068
     date: 2000-03-01 23:29:50
time_zone: +0200
mail_from: Monty
    reply: monty@no.spam.com
  mail_to: "Thimble Smith" <tim@no.spam.com>
      sbj: UTF-8
      txt: >>>>> "Thimble" == Thimble Smith writes:

Thimble> Hi.  I think this is a good idea.  Is anyone familiar
Thimble> with UTF-8 or Unicode? Otherwise, I'll put this on my
Thimble> TODO list and see what happens.

Yes, please do that.

Regards,
Monty
     file: inbox-jani-1
     hash: 190402944
1 row in set (0.09 sec)

Use --safe-updates option

For novices, there is a useful startup option --safe-updates (or --i-am-a-dummy, with the same effect). This is useful when you have issued a DELETE FROM  tbl_name statement but forgot the WHERE clause. Normally, such a statement deletes all rows from the table. With --safe-updates, you can delete only certain rows by specifying key values ​​that can identify them. This can help prevent accidents.

If you use the --safe-updates option, mysql issues the following statement when connecting to the MySQL server:

SET SQL_SAFE_UPDATES=1,SQL_SELECT_LIMIT=1000,SQL_MAX_JOIN_SIZE=1000000;

The SET statement has the following effects:

· You are not allowed to execute UPDATE or DELETE statements unless you specify a key value constraint in the WHERE clause or provide a LIMIT clause (or both). E.g:

  UPDATE tbl_name SET not_key_column=val WHERE key_column=val;

  UPDATE tbl_name SET not_key_column=val LIMIT1;

All large SELECT results are automatically limited to 1,000 rows unless the statement includes a LIMIT clause.

Abandon the multi-table SELECT statement that may need to check more than 1,000,000 rows.

To specify limits other than 1,000 and 1,000,000, you can use the --select_limit and --max_join_size options to override the default values:

shell> mysql --safe-updates --select_limit=500 --max_join_size=10000

Disabling mysql Auto with access

If the mysql client program disconnects from the server when sending a query, it immediately and automatically attempts to reconnect to the server and send the query again. However, even if mysql reconnects successfully, your first connection has ended, and the previous session objects and settings are lost: including temporary tables, auto-commit mode, and user and session variables. This behavior is dangerous. As the following example shows, the server will shut down and restart without your knowledge:

 

Mysqladmin: client for managing MYSQL server

mysqladmin is a client program that performs administrative operations. You can use it to check the configuration and current status of the server, create and delete databases, and so on.

C:\Windows\system32>mysqladmin -u root -p --help
EnterPassword:
mysqladmin: [Warning] Using a password on the command line interface can be insecure. mysqladmin Ver 8.0.19 for Win64 on x86_64 (MySQL Community Server - GPL) Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Administration program for the mysqld daemon. Usage: mysqladmin [OPTIONS] command command.... --bind-address=name IP address to bind to. -c, --count=# Number of iterations to make. This works with -i (--sleep) only. -#, --debug[=#] This is a non-debug version. Catch this and exit. --debug-check This is a non-debug version. Catch this and exit. --debug-info This is a non-debug version. Catch this and exit. -f, --force Don't ask for confirmation on drop database; with multiple commands, continue even if an error occurs. -C, --compress Use compression in server/client protocol. --character-sets-dir=name Directory for character set files. --default-character-set=name Set the default character set. -?, --help Display this help and exit. -h, --host=name Connect to host. -b, --no-beep Turn off beep on error. -p, --password[=name] Password to use when connecting to server. If password is not given it's asked from the tty. -W, --pipe Use named pipes to connect to server. -P, --port=# Port number to use for connection or 0 for default to, in order of preference, my.cnf, $MYSQL_TCP_PORT, /etc/services, built-in default (3306). --protocol=name The protocol to use for connection (tcp, socket, pipe, memory). -r, --relative Show difference between current and previous values when used with -i. Currently only works with extended-status. --shared-memory-base-name=name Base name of shared memory. -s, --silent Silently exit if one can't connect to server. -S, --socket=name The socket file to use for connection. -i, --sleep=# Execute commands repeatedly with a sleep between. --ssl-mode=name SSL connection mode. --ssl-ca=name CA file in PEM format. --ssl-capath=name CA directory. --ssl-cert=name X509 cert in PEM format. --ssl-cipher=name SSL cipher to use. --ssl-key=name X509 key in PEM format. --ssl-crl=name Certificate revocation list. --ssl-crlpath=name Certificate revocation list path. --tls-version=name TLS version to use, permitted values are: TLSv1, TLSv1.1, TLSv1.2, TLSv1.3 --ssl-fips-mode=name SSL FIPS mode (applies only for OpenSSL); permitted values are: OFF, ON, STRICT --tls-ciphersuites=name TLS v1.3 cipher to use. --server-public-key-path=name File path to the server public RSA key in PEM format. --get-server-public-key Get server public key -u, --user=name User for login if not current user. -v, --verbose Write more information. -V, --version Output version information and exit. -E, --vertical Print output vertically. Is similar to --relative, but prints output vertically. -w, --wait[=#] Wait and retry if connection is down. --connect-timeout=# --shutdown-timeout=# --plugin-dir=name Directory for client-side plugins. --default-auth=name Default authentication client-side plugin to use. --enable-cleartext-plugin Enable/disable the clear text authentication plugin. --show-warnings Show warnings after execution --compression-algorithms=name Use compression algorithm in server/client protocol. Valid values are any combination of 'zstd','zlib','uncompressed'. --zstd-compression-level=# Use this compression level in the client/server protocol, in case --compression-algorithms=zstd. Valid range is between 1 and 22, inclusive. Default is 3. Variables (--variable-name=value) and boolean options {FALSE|TRUE} Value (after reading options) --------------------------------- ---------------------------------------- bind-address (No default value) count 0 force FALSE compress FALSE character-sets-dir (No default value) default-character-set auto host (No default value) no-beep FALSE port 0 relative FALSE shared-memory-base-name (No default value) socket (No default value) sleep 0 ssl-ca (No default value) ssl-capath (No default value) ssl-cert (No default value) ssl-cipher (No default value) ssl-key (No default value) ssl-crl (No default value) ssl-crlpath (No default value) tls-version (No default value) tls-ciphersuites (No default value) server-public-key-path (No default value) get-server-public-key FALSE user root verbose FALSE vertical FALSE connect-timeout 43200 shutdown-timeout 3600 plugin-dir (No default value) default-auth (No default value) enable-cleartext-plugin FALSE show-warnings FALSE compression-algorithms (No default value) zstd-compression-level 3 Default options are read from the following files in the given order: C:\Windows\my.ini C:\Windows\my.cnf C:\my.ini C:\my.cnf D:\D\Mysql\mysql-8.0.19-winx64\mysql-8.0.19-winx64\my.ini D:\D\Mysql\mysql-8.0.19-winx64\mysql-8.0.19-winx64\my.cnf D:\D\Mysql\mysql-8.0.19-winx64\mysql-8.0.19-winx64\bin\my.ini D:\D\Mysql\mysql-8.0.19-winx64\mysql-8.0.19-winx64\bin\my.cnf The following groups are read: mysqladmin client The following options may be given as the first argument: --print-defaults Print the program argument list and exit. --no-defaults Don't read default options from any option file, except for login file. --defaults-file=# Only read default options from the given file #. --defaults-extra-file=# Read this file after the global files are read. --defaults-group-suffix=# Also read groups with concat(group, suffix) --login-path=# Read this path from the login file. Where command is a one or more of: (Commands may be shortened) create databasename Create a new database debug Instruct server to write debug information to log drop databasename Delete a database and all its tables extended-status Gives an extended status message from the server flush-hosts Flush all cached hosts flush-logs Flush all logs flush-status Clear status variables flush-tables Flush all tables flush-threads Flush the thread cache flush-privileges Reload grant tables (same as reload) kill id,id,... Kill mysql threads password [new-password] Change old password to new-password in current format ping Check if mysqld is alive processlist Show list of active threads in server reload Reload grant tables refresh Flush all tables and close and open logfiles shutdown Take server down status Gives a short status message from the server start-slave Start slave stop-slave Stop slave variables Prints variables available version Get version info from server
C: \ Windows \ system32 > mysqladmin - u root - p123456 proc stat 
displays process related information and server status information.
mysqladmin:
[ Warning ] the Using A password ON . The Command Line interface CAN BE in the insecure + - - + + --------- ----------------- -------- + ---- + --------- + -------- + ----------------- ------- + ------------------ + | Id | User | Host | db | Command | Time | State | Info | +----+-----------------+-----------------+----+---------+--------+------------------------+------------------+ | 4 | event_scheduler | localhost | | Daemon | 363540 | Waiting on empty queue | | | 77 | root | localhost:52126 | | Sleep | 96 | | | | 80 | root | localhost:53183 | | Query | 0 | starting | show processlist | +----+-----------------+-----------------+----+---------+--------+------------------------+------------------+ Uptime: 363546 Threads: 3 Questions: 929 Slow queries: 0 Opens: 443 Flush tables: 6 Open tables: 106 Queries per second avg: 0.002

The result of the mysqladmin status command shows the following values:

Uptime: The number of seconds that the Mysql server has been running.

Threads The number of active threads (clients).

Questions The number of customer questions (inquiries) since the server was started.

Slow queries The number of queries that took longer than long_query_time seconds to execute.

The number of database tables that the Opens server has opened.

Flush tables The number of flush ..., refresh and reload commands that the server has executed.

Open tables The number of currently open tables.

 

Guess you like

Origin www.cnblogs.com/zykLove/p/12719498.html