- 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
Connecting to the MySQL Server Using Command Options
Let us see how command line options can be used to establish connection with the MySQL server for clients like mysql or mysqldump.
For a client program to be able to connect to the MySQL server, it must use proper connection parameters, like the name of the host where the server is running, the user name and password of the MySQL account. Every connection parameter has a default value, but it can be overridden when necessary using program options specified on the command line or in an option file.
Command to invoke mysql without specifying any explicit connection parameters is −
Since there are no parameter options, the default values are applied.
The default host name is localhost. On Unix, it has a special meaning.
The default user name is ODBC on Windows. On Unix, the login name on Unix.
No password is sent because neither --password nor -p is provided-.
For mysql, the first argument is considered as the name of the default database. There is no such argument, hence mysql doesn’t select any default database.
Imvoke - Specify host name, user name, and password
To specify the host name, user name, and password explicitly, appropriate options have to be provided on the command line.It has been shown below −
mysql --host=localhost --user=myname --password=password mydb mysql -h localhost -u myname -ppassword mydb
The password value is optional.
If a --password or -p option is present, and a password value is mentioned, there shouldn’t be any space between --password= or -p and the password that follows it.
If --password or -p doesn’t specify a password value, the client program prompts the user to enter the password. The password doesn’t get displayed when it is entered.
Type of Connection
Next step is for client programs to determine the type of connection that needs to be made. To ensure that the client makes a TCP/IP connection to the local server only, the --host or -h option is used to specify a host name with the value of 127.0.0.1 (instead of localhost). Instead of this, the IP address or name of the local server can also be provided. The transport protocol can be explicitly mentioned even for localhost using the --protocol=TCP option. Some examples have been shown below −
mysql --host=127.0.0.1 mysql --protocol=TCP
If connections need to be made to remote servers, then use TCP/IP. This command would help connect to the server that runs on remote.example.com using the default port number which is 3306. It has been shown below −
If the user wishes to display the port number specifically, the - -port or –P option needs to be mentioned −
mysql --host=remote.example.com --port=13306
- Related Articles
- Command Options for Connecting to the MySQL Server
- Connecting to and Disconnecting from the MySQL Server
- Connecting to MySQL database from the command line?
- Using Options on the Command Line for MySQL programs?
- Connecting to SAP HANA server on Cloud using PHP
- How to upgrade MySQL server from command line?
- After connecting to MySQL server how can we select a database from\ncommand prompt?
- MySQL Command-Line Options that Affect Option-File Handling
- Using Options to Set MySQL Program Variables
- Python Parser for command line options
- Connecting to SAP R/3 system via JCo client and JCo Server
- How can we analyze the tables of a particular database from MySQL Server command line?
- Connecting to a MySQL database with Java
- Getting error- Hard-coded logon parameters not allowed when using a Destination Configuration while connecting to SAP server dynamically
- mysqld - The MySQL Server