Friday, November 15, 2024
Google search engine
HomeData Modelling & AIHow to Connect to MySQL from Windows Command Line

How to Connect to MySQL from Windows Command Line

Introduction

MySQL is a popular and open-source relational database application. Therefore, many servers make use of MySQL. The way you access the database depends on the operating system from which you are working.

This guide walks you through using the Windows Command line to connect to a MySQL database.

how to connect to mysql from windows command linehow to connect to mysql from windows command line

Prerequisites

  • A Windows-based system with an administrator account
  • A local installation of MySQL
  • Notepad text editor (optional)

Starting MySQL from the Windows Command Line

Step 1: How to Open a Windows Command Prompt

Start by opening the Run command box in Windows. Use the keyboard shortcut – hold the Windows (super) key and press the letter R (Win+R).

Then, type in cmd and press Enter. This command opens the Windows command line.

open windows command promptopen windows command prompt

A black command line interface should launch, with white text and a cursor for you to type.

windows command line interfacewindows command line interface

Step 2: Verify MySQL is Running on Windows

Next, run the command to display a list of all the services that are currently running. Enter the following in the command prompt:

net start
net start windows command linenet start windows command line

If MySQL is not on the list, you can start it using the Services panel. Enter the following command:

services.msc

A new window will launch and display the list of services available on your system. Scroll down to find MySQL, and check the status column. Left-click the MySQL service to highlight it, then right-click to open a context menu. Finally, left-click on start.

Step 3: Connect to a Local MySQL Server

First, start MySQL in Windows using the following command:

mysql.exe -u[username] -p

Replace [username] with the username for your MySQL installation.

Enter mysql.exe -uroot -p, and MySQL will launch using the root user.

MySQL will prompt you for your password. Enter the password from the user account you specified with the –u tag, and you’ll connect to the MySQL server.

Welcome to the MySQL monitor.  Commands end with ; or g.
Your MySQL connection id is 2
Server version: 5.7.11-log MySQL Community Server (GPL)

The command prompt changes to look like this:

mysql>

Change to the MySQL folder, use the cd command:

cd c:\Program Files\MySQL

The command prompt should change to mysql> letting you know you’re currently in the MySQL folder.

To list the contents of this folder:

dir

This lists the contents of the current folder. One of the folders will display the version number of your MySQL installation.

For example, if you’ve installed MySQL 5.5, you should see a folder named “MySQL Server 5.5”.

Step: 4 Create Windows Shortcut to Login to MySQL

  1. To create a shortcut in Windows, enter the following in the command line:
notepad
  1. Open the Windows Notepad text editor with the command:
cmd /K “C:\Program Files\MySQL\MySQL Server X.X\bin\mysql.exe” -uroot -ppassword

Replace -uroot with -u[username] if you have a different username, and -ppassword with -p[YourActualPassword].

Also, make sure you change the path from MySQL Server X.X to the actual folder location.

  1. Now, press Ctrl-S to launch a Save dialog.
  1. Then, type “mysql.bat” in the name field, and change the location to your desktop (or another place that’s easy to find).

This creates a small Windows executable file that you can double-click to log into MySQL using the specified username and password.

Conclusion

With the help of this simple guide, you should be able to connect to a MySQL database using the Windows command prompt. Once you’ve logged into the MySQL server, the commands will be the same regardless of what kind of system you’re running on.

If you are searching for an alternative, terminal-based solution, try out using PostgreSQL from command line.

Was this article helpful?
YesNo

RELATED ARTICLES

Most Popular

Recent Comments