How to connect from windows command prompt to mysql command line / Start Shutdown

MySQL

If your \MYSQL\BIN is on drive C:, then your commands need to be:

C:\Users\prakash.singh> CD C:\Program Files\MySQL\MySQL Server 5.6\bin

C:\Program Files\MySQL\MySQL Server 5.6\bin>mysql -u root -p

Enter Password:****

mysql>

——————————————OR——————————————————————————————

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"

————————————————————————————————————————————————————-

(OR)

C:\>cd \MYSQL\Bin
C:\MYSQL\Bin>mysql -u root -p admin

If you’re not already on C: (which you’ll know by looking at the prompt in the cmd window), or your MySQL folder is on another drive (for instance, D:), change to that drive too:

C:\> cd /d D:\MYSQL\Bin
D:\MYSQL\Bin>mysql -u root -p admin

The .exe after mysql is optional, since .exe is an executable extension on Windows. If you type mysql, Windows will automatically look for an executable file with that name and run it if it finds it.

Note that in both my examples of running mysql, there are no = signs. You should just use -p with no password, and wait to be prompted for it instead.

———————————————————————————OR————————————————————————-

Starting MySQL from the Windows Command Line

The MySQL server can be started manually from the command line. This can be done on any version of Windows.

Note

The MySQL Notifier GUI can also be used to start/stop/restart the MySQL server.

To start the mysqld server from the command line, you should start a console window (or DOS window) and enter this command:

C:\> "C:\Program Files\MySQL\MySQL Server 5.5\bin\mysqld"

The path to mysqld may vary depending on the install location of MySQL on your system.

You can stop the MySQL server by executing this command:

C:\> "C:\Program Files\MySQL\MySQL Server 5.5\bin\mysqladmin" -u root shutdown
Note

If the MySQL root user account has a password, you need to invoke mysqladmin with the -p option and supply the password when prompted.

This command invokes the MySQL administrative utility mysqladmin to connect to the server and tell it to shut down. The command connects as the MySQL root user, which is the default administrative account in the MySQL grant system.

Note

Users in the MySQL grant system are wholly independent from any login users under Microsoft Windows.

If mysqld doesn’t start, check the error log to see whether the server wrote any messages there to indicate the cause of the problem. By default, the error log is located in the C:\Program Files\MySQL\MySQL Server 5.5\data directory. It is the file with a suffix of .err, or may be specified by passing in the --log-error option. Alternatively, you can try to start the server with the --console option; in this case, the server may display some useful information on the screen that will help solve the problem.

The last option is to start mysqld with the --standalone and --debug options. In this case, mysqld writes a log file C:\mysqld.trace that should contain the reason why mysqld doesn’t start. See The DBUG Package.

Use mysqld –verbose –help to display all the options that mysqld supports.