≡ Menu

Login to MySQL server from command line

In Windows, MySQL, by default, is installed in the folder %systemdrive%\Program Files\MySQL.  For example, if the system drive is C:, then the installation directory for MySQL server 5.5 would be C:\Program Files\MySQL\MySQL Server 5.5\

We can connect to MySQL server by running the mysql.exe located in the bin folder in the mysql installation directory.

If you just run mysql without passing any arguments, you can connect to the server with anonymous account. This works only if the anonymous account is not disabled.  When you install MySQL, by default, it enables anonymous access.

Connect to MySQL as root

You can connect to MySQL server by specifying the user name and password in the command line arguments to mysql command. To connect as root, the command would be as below.

mysql.exe -uroot -ppassword

You will be prompted for password if -p option is skipped. If MySQL service is not running then the command would fail with the below error

C:\>mysql.exe -u root -ppassword
ERROR 2003 (HY000): Can't connect to MySQL server on 'localhost' (10061)

You can create a desktop batch file to launch MySQL client as follows.

  • Create a text file and name it as mysql.bat.
  • Add the following command to the mysql.bat file.
  • cmd /K “C:\Program Files\MySQL\MySQL Server 5.5\bin\mysql.exe” -uroot -ppassword
  • Change the mysql.exe path as per your MySQL server version and install directory.
  • Save the file. Now onwards you can connect to server by just double clicking on the batch file.

Alternative way is to create a desktop shortcut with the target as C:\Program Files\MySQL\MySQL Server 5.5\bin\mysql.exe.  After creating the shortcut, open the properties and add -uroot -ppassword to the target  field.

The default installation of MySQL creates a shortcut in  Start menu-> All Programs with the target as below. You can use it as the target for your desktop shortcut.

"C:\Program Files\MySQL\MySQL Server 5.5\bin\mysql.exe" "--defaults-file=C:\Program Files\MySQL\MySQL Server 5.5\my.ini" "-uroot" "-ppassword"

Related Posts:
How to install MySql server in Windows 7

0 comments… add one

Leave a Comment