MySQL

MySQL IntroductionComment MySQLInstall MySQL container with Docker-ComposeMySQL AdminMySQL ALTER TABLEMySQL ArithmeticMySQL BackticksMySQL Backup using mysqldumpMySQL Change PasswordMySQL Character Sets and CollationsMySQL clientMySQL ClusteringMySQL Configuration and tuningMySQL Connecting with UTF-8 Using Various Programming language.MySQL Converting from MyISAM to InnoDBMySQL Create New UserMySQL Creating databasesMySQL Customize PS1MySQL Data TypesMySQL Date and Time OperationsMySQL Dealing with sparse or missing dataMySQL DELETEMySQL Drop TableMySQL Dynamic Un-Pivot Table using Prepared StatementMySQL ENUMMySQL Error 1055: ONLY FULL GROUP BY: something is not in GROUP by clause ...MySQL Error codesMySQL EventsMySQL Extract values from JSON typeMySQL Full-Text searchMySQL Group byMySQL Handling Time ZonesMySQL Indexes and KeysMySQL INSERTMySQL JoinsMySQL JOINS: Join 3 table with the same name of id.MySQL JSONMySQL Limit and OffsetMySQL LOAD DATA INFILEMySQL LOCK TABLEMySQL Log filesMySQL Many-to-many Mapping tableMySQL MyISAM EngineMySQL mysqlimportMySQL NULLMySQL One to ManyMySQL ORDER byMySQL PartitioningMySQL Performance TipsMySQL Performance TuningMySQL Pivot queriesMySQL PREPARE StatementsMySQL Recover from lost root passwordMySQL Regular ExpressionsMySQL ReplicationMySQL Reserved WordsMySQL Security via GRANTsMySQL SELECTMySQL Server InformationMySQL SSL Connection SetupMySQL Stored routines (procedures and functions)MySQL String operationsMySQL Table CreationMySQL Temporary TablesMySQL Time with subsecond precisionMySQL TransactionMySQL TRIGGERSMySQL UNIONMySQL UnionsMySQL UPDATEMySQL Using VariablesMySQL VIEWRecover and reset the default root password for MySQL 5.7+



MySQL client

From WikiOD

Syntax[edit | edit source]

  • mysql [OPTIONS] [database_name]

Parameters[edit | edit source]

Parameter Description
-D --database=name name of the database
--delimiter=str set the statement delimiter. The default one is ';'
-e --execute='command' execute command
-h --host=name hostname to connect to
-p --password=name password Note: there is no space between -p and the password
-p (without password) the password will be prompted for
-P --port=# port number
-s --silent silent mode, produce less output. Use \t as column separator
-ss like -s, but omit column names
-S --socket=path specify the socket (Unix) or named pipe (Windows) to use when connecting to a local instance
--skip-column-names omit column names
-u --user=name username
-U --safe-updates --i-am-a-dummy login with the variable sql_safe_updates=ON. This will allow only DELETE and UPDATE that explicitly use keys
-V --version print the version and exit

Base login[edit | edit source]

To access MySQL from the command line:

mysql --user=username --password=pwd --host=hostname test_db

This can be shortened to:

mysql -u username -p password -h hostname test_db

By omitting the password value MySQL will ask for any required password as the first input. If you specify password the client will give you an 'insecure' warning:

mysql -u=username -p -h=hostname test_db

For local connections --socket can be used to point to the socket file:

mysql --user=username --password=pwd --host=localhost --socket=/path/to/mysqld.sock test_db

Omitting the socket parameter will cause the client to attempt to attach to a server on the local machine. The server must be running to connect to it.

Execute commands[edit | edit source]

This set of example show how to execute commands stored in strings or script files, without the need of the interactive prompt. This is especially useful to when a shell script needs to interact with a database.

Execute command from a string[edit | edit source]

$ mysql -uroot -proot test -e'select * from people'

+----+-------+--------+
| id | name  | gender |
+----+-------+--------+
|  1 | Kathy | f      |
|  2 | John  | m      |
+----+-------+--------+

To format the output as a tab-separated grid, use the --silent parameter:

$ mysql -uroot -proot test -s -e'select * from people'

id      name    gender
1       Kathy   f
2       John    m

To omit the headers:

$ mysql -uroot -proot test -ss -e'select * from people'

1       Kathy   f
2       John    m

Execute from script file:[edit | edit source]

$ mysql -uroot -proot test < my_script.sql
$ mysql -uroot -proot test -e'source my_script.sql'

Write the output on a file[edit | edit source]

$ mysql -uroot -proot test < my_script.sql > out.txt

$ mysql -uroot -proot test -s -e'select * from people' > out.txt

Credit:Stack_Overflow_Documentation