Open In App

Connecting to MySQL Using Command Options

Last Updated : 02 Oct, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will learn to connect the MySQL database with a command line interface using command line options. To connect the MySQL database the community provides a command line tool called mysql which comes up with some command line arguments.

To connect the MySQL we need to make sure that the mysqld is running on the system. you can run this daemon process using the command

mysqld [options]

where the options are the optional command line arguments with respect to the MySQL server. 

Now, we can use mysql command line program along with its options to connect to this running server as follows,

Syntax :

mysql –host <hostname> –port<portnumber> –user <username> –password=<password>

where,

--host / -h : host where the mysqld is running
--port / -p : port where the daemon is running
--user : this is done for authorization and by default root 
 acts as the first user when you install mysql for the first time.
--password : password set for the user.

Let us understand the complete process with examples,

Steps:

Step 1: Have a MySQL server running with accurate configuration. You can find this configuration in mysql workbench > user and privileges tab

 

The above image shows that there are two users root and pma who can access this database with their credentials user and password

Step 2: Open your favorite command line tool like windows command prompt and type the following,

mysql --host localhost --user 
root --password=<your password>

 

In the above image, we are connecting as pma as specified earlier and localhost where my server is running. Even if you do not specify the password it prompts for the password and also it is unsafe to specify the password as a command line argument

Now you are connected to the server and that is indicated with a welcome message as shown in the above image.

Facts:

 1.  –host and –user is the mandatory arguments if you do not specify then it shows the error like this,

 

2. You can ignore the port option in which case it automatically finds it. You can find the port of the daemon in the workbench administration tab.

3. You can find multiple other command options with this,

mysql --help

4. Within the client we have the commands like quit, exit, connect, print, and many more which we can use to perform appropriate operations.


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads