Friday, February 5, 2016

How to run MySQL queries from Windows Command Prompt (cmd)

Warning: READ CAREFULLY

I am writing this post because I cannot find something like this over the internet. For some reasons that you do not want to use MySQL GUI and just wanted to run mysql cmd but you cannot seem to run its own command line, your other option is to run it through window's cmd. I just want to point out that there are two command prompts that you can use to manipulate your MySQL database. One is MySQL server's command line and the other is through Window's command prompt. BOTH OF THESE METHODS REQUIRE YOU TO HAVE A MySQL SERVER INSTALLED IN YOUR UNIT.

You can get MySQL Server through (but not limited to) the following:
1. Downloading and installing MySQL Server.
2. Using application server platforms like Wamp, Xampp, lamp or mamp, whichever works for your unit. These application servers installs MySQL Server too.

The point is, you have to have a MySQL Server installed in your unit!

To be able to run MySQL in Windows command line, do the following:

1. Make sure that your MySQL is turned on. 

  • In application servers (wamp, xampp, etc.), you may start the service of MySQL.
  • To force start of server, locate the MySQL files and run mysqld.exe.  

2. Locate your MySQL server executable file.
  • Look for your MySQL server exe file. Usually, it is installed in C:\Program Files\MySQL\MySQL Server x.x\bin. 
  • The MySQL Server x.x defines the version of MySQL in your unit. Example: C:\Program Files\MySQL\MySQL Server 5.6\bin
  • You may use Window's search to find this.
3. Open windows cmd.
  • You can either open this from your Start button or use Windows+r to run cmd to open the command prompt.
4. Go to the root directory in Windows cmd. 
Type cd.. then hit Enter button until you encounter the root directory of your unit (C:\)

5. Type the address of your MySQL Server

  • Your address should not contain the C:\ directory instead, replace it with cd. Example: cd Program Files\MySQL\MySQL Server 5.6\bin
  • The directory should change based on the address you typed. 



5. Login to MySQL Server

  • Type the login statement: mysql -u username -p
  • For example, mysql -u root -p
  • Enter your password

6. DONE! You've now successfully logged in into MySQL using Windows cmd.
You can now make your database, tables, queries, etc.