Connecting to and Disconnecting from the MySQL Server


A MySQL user name needs to be provided when ‘mysql’ is invoked. Next a password has to be entered. If the server runs on a system which is not the same as that on which the user logs in, the host name also needs to be provided while trying to log in.

It is suggested to contact the administrator to find out the parameters that are required to connect to the server.

Once the parameters are determined, the below lines need to be sued to connect to the server −

shell> mysql −h host −u user −p
Enter the password: ***

Here, ‘host’ represents the name of the host where the MySQL server is running. The ‘user’ represents the user name of the MySQL account. The appropriate values are substituted in these places. The *** represents the password. This is entered when ‘mysql’ prompts ‘Enter the password’.

Once this is successful, some introductory information is displayed, and this is followed by ‘mysql>’ prompt.

shell> mysql −h host −u user −p
Enter password: ********
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 25338 to server version: 8.0.25-standard

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql>

The ‘mysql’ prompt tells that ‘mysql’ is ready for the user to enter their SQL commands and execute it.

Note: If the user is trying to log in from the same machine where MySQL is running, the host name can be omitted, and the below line can be run instead:

shell> mysql −u user −p

When trying to connect, if a message like ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2), shows up, it indicates that the MySQL server daemon (Unix) or service (Windows) is not running currently. When this happens, the administrator has to be contacted.

Once the connection is successful, and you wish to disconnect, run the below lines of code −

mysql> QUIT
Bye

Updated on: 08-Mar-2021

836 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements