
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
Connect to MySQL database from command line
Let us understand how MySQL can be connected to the database using the command-line. This is done for clients like mysql or mysqldump.
The below command invokes mysql without specifying any explicit connection parameters −
mysql
Since there are no parameter options, the default values will be applied −
The default host name is localhost.
The default user name is ODBC on Windows.
No password is sent because neither --password nor -p has been mentioned.
For mysql, the first non-option argument is considered the name of the default database. Since there is no such argument, mysql selects no default database.
To specify the host name, user name and password specifically, provide appropriate options on the command line. To select a default database, add a database-name argument. Let us see this with the command −
mysql --host=localhost --user=myname --password=password mydb mysql -h localhost -u myname -ppassword mydb
Client programs determine the type of connection that needs to be made −
If the host is not specified or it is a localhost, a connection to the local host occurs
Else, the connection will use TCP/IP.
To ensure that the client makes a TCP/IP connection to the local server, ‘—host’ or ‘-h’ needs to be specified, with a host name value of 127.0.0.1 (instead of localhost). Instead of this, the IP address or name of the local server can also be mentioned. The transport protocol can be explicitly mentioned using the --protocol=TCP option. Let us see with the example −
Example
mysql --host=127.0.0.1 mysql --protocol=TCP
On Windows, MySQL client can be forced to use a named-pipe connection. This can be done by specifying the --pipe or --protocol=PIPE option. Otherwise, . (period) can also be mentioned as the host name.
Connections to remote servers will use TCP/IP. The command would connect to the server running on remote.example.com using the default port number (3306). Let us see it in action −
mysql --host=remote.example.com
To specify a port number explicitly, the --port or -P option can be used. Lets see how it can be done −
mysql --host=remote.example.com --port=13306
To cause the port number to be used, force a TCP/IP connection. It can be done using any one of the way −
mysql --port=13306 --host=127.0.0.1 (or) mysql --port=13306 --protocol=TCP
It is possible to specify connection parameters without entering them on the command line every time a client program is invoked.
- Related Articles
- Connecting to MySQL database from the command line?
- How to create a database on command line in MySQL?
- How to connect to my MongoDB table by command line?
- How to connect hibernate with MySQL Database?
- How to upgrade MySQL server from command line?
- MySQL query to discover current default database collation (via command line client)?
- How can we analyze the tables of a particular database from MySQL Server command line?
- How to repair MySQL tables from the command line?
- How can we return to windows command shell from MySQL command line tool?
- How do I drop a MongoDB database from the command line?
- How can we get the list of tables in a particular database from MySQL Server command line?
- The MySQL Command-Line Client
- How to find the MySQL data directory from command line in Windows?
- Oracle DataBase – Grant Privileges to a User in SQL Command Line
- How to connect Database in Python?
