How to connect from windows command prompt to mysql command line
Asked Answered
T

17

58

I'm trying to connect to mysql server command line from my windows prompt

I write the next line in cmd but i get an error.

cd C:\MYSQL\bin\

And then i execute

mysql.exe -u=root -p=admin

but i getting this error

ERROR 1045: <28000>: Access denied for user 'root'@'localhost' <using password:YES>

Thanks,

Treponema answered 6/12, 2012 at 20:57 Comment(1)
I would have done it differently... 1. Go to environment variables, 2. add the path of your MySQL Bin directory (suppose if it is "C:\MySQL\Bin", add this path to "PATH" in environment variable). 3. click ok ok and save all. 4. open command prompt and write 5. mysql -u root -p.. hit enter and it should work. it worked for me.Vetavetch
J
88

The cd in your question is invalid (quoting it here because you've removed it once, and it was there when this answer was posted):

cd CD:\MYSQL\bin\

You can't cd to CD:\ anything, because CD:\ isn't a valid directory in Windows. CD: would indicate a drive, except that drives are restricted to a single letter between A and Z.

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

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.

Journalistic answered 6/12, 2012 at 21:7 Comment(0)
A
19

Use this :

mysql -u user_name -p  then press_enter_key

then type password

i.e.

line-1 : mysql -u root -p

line-2 : admin
Arrowhead answered 6/12, 2012 at 21:15 Comment(1)
This worked for me : line-1 : mysql -u root -p line-2 : adminBacchius
G
9

C:\Program Files\MySQL\MySQL Server 5.7\bin> mysql -u username -p

Then it will ask for the password. Enter the password you set for the username during installation while adding db Users. In the given image depp is my username

Gylys answered 3/7, 2018 at 18:17 Comment(0)
R
7

first type cmd then the windows command prompt will appear:

PATH C:\XAMPP\MYSQL\BIN;%PATH%;
mysql -u root -p

where:

  • -u is the user id
  • -p is the password, if you will not using a password just leave it blank.
Racine answered 31/1, 2015 at 15:49 Comment(1)
Finally...a lot of people simply says to type commands in CMD and its just wrong.Trimetallic
M
3

You are logging in incorrectly; you should not include = in your login. So to log in, type:

mysql.exe -uroot -padmin

If that doesn't work, then you may not have your system configured. If so, then here's a good tutorial on getting started with the MySQL prompt: http://breakdesign.blogspot.com/2007/11/getting-started-with-php-and-mysql-in_11.html

Mensa answered 6/12, 2012 at 20:59 Comment(4)
That's not the problem (well, not the basic problem, anyway). cd CD:` is not a valid CMD expression; there can't be a directory CD:` to cd into, as : is the drive separator in Windows.Journalistic
Well, according to the OP question s/he is getting into MySQL, so that's not the problem. After all, the OP got far enough to get the password rejection from MySQL.Mensa
Looks like both are the problem (as I said). The first (read the question, where it says "I write the next line in cmd but i get an error"); the second happens because the MYSQL\Bin folder (wherever it's located) is on the system PATH and the executable is found anyway, but the login fails.Journalistic
Don't include the password in the command, just use mysql.exe -u root -p The password is asked later by the server.Oeildeboeuf
M
3
  1. Start your MySQL server service from MySQL home directory. Your one is C:\MYSQL\bin\ so choose this directory in command line and type: NET START MySQL
    (After that you can open Windows Task Manager and verify in Processes tab is mysqld.exe process running. Maybe your problem is here.)
  2. Type: mysql -u user -p [pressEnter]
  3. Type your password [pressEnter]

or make a start.bat file:

  1. add C:\MYSQL\bin\ to your PATH
  2. write a start.bat file
  3. My start.bat file has only two lines like below:
    net start MySQL
    mysql -u root -p

Good luck!

Mazman answered 30/5, 2014 at 17:44 Comment(0)
D
3

Please perform the following steps

  1. First, open your command prompt with Administrator.
  2. Go to MySQL installed directory and copy path and past on command prompt like:- C:\Program Files\MySQL\MySQL Server 5.7\bin>

  3. C:\Program Files\MySQL\MySQL Server 5.7\bin>mysql -uroot -p [-u for username -p for password]

  4. C:\Program Files\MySQL\MySQL Server 5.7\bin>mysql -uroot -p Enter password: **** [enter your password here]

1 first image

Dugan answered 29/5, 2019 at 15:10 Comment(0)
R
3

To make it easier to invoke MySQL programs, you can add the path name of the MySQL bin directory to your Windows system PATH environment variable:

On the Windows desktop, right-click the My Computer icon, and select Properties.

Next select the Advanced tab from the System Properties menu that appears, and click the Environment Variables button.

Under System Variables, select Path, and then click the Edit button. The Edit System Variable dialogue should appear.

Place your cursor at the end of the text appearing in the space marked Variable Value. (Use the End key to ensure that your cursor is positioned at the very end of the text in this space.) Then enter the complete path name of your MySQL bin directory (for example, C:\Program Files\MySQL\MySQL Server 8.0\bin).

Open a different terminal and if you are using root as user run mysql -u root -p else use the a different username you created.

Riggle answered 16/9, 2020 at 13:55 Comment(0)
R
2

Simply to login mysql in windows, if you know your username and password.

Open your command prompt, for me the username is root and password is password

mysql -u root -p
Enter password: ********

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)

Hope it would help many one.

Rusty answered 15/4, 2018 at 14:43 Comment(7)
Did you ran this on Windows? Doesn't work (I'm on 7 pro and works in linux only).Trimetallic
@MarceloAgimóvel there must be some issue with your installation, this is working fine in windows 7, 8.1, 10 and linux distro ubuntu for Hadoop database, and this complete command I have posted as I used to login, working with mysql over 8+ years. I suggest you to uninstall and later install it again as officially guided or you can contact some mentor over this.Rusty
Ok. I cannot upvote to remove my downvote, unless you change your answer. Can you change it so I can upvote ?Trimetallic
@MarceloAgimóvel The answer is already correct and I am using this command to login mysql for more than 8 years either it were windows, ubuntu, mint etc. and there is nothing to change, I already suggested you, that contact a mentor for you or watch videos to login mysql, if something is there to change, that is your wrong practice!Rusty
You got it wrong Arif! I am trying to upvote your answer (to remove my downvote) but StackOverflow wont let me do it until you change your answer. I'm not saying is wrong, I'm just saying change anything so I can upvote your answer ?!Trimetallic
@MarceloAgimóvel Oops! sometimes, it is very hard to understand tiny things, I apologies...Rusty
thanks for update. I'v updated your answer, I also apologies for downvote.Trimetallic
C
1

I have used following command to connect MySQL Server 8.0 in Windows command prompt.

C:\Program Files\MySQL\MySQL Server 8.0\bin>mysql -u root -p my_db
Enter password: ****

or

C:\Program Files\MySQL\MySQL Server 8.0\bin>mysql -u root -p my_db -h localhost
Enter password: ****

Here my_db is schema name.

enter image description here

Craal answered 1/5, 2018 at 13:20 Comment(0)
D
1

First jump to the mysql bin directory. You have two ways to connect

1) mysql -u root -p1234 (without space for password flag) 2) mysql -u root -p and enter the password when it is asking

Delafuente answered 21/8, 2019 at 6:39 Comment(0)
C
1

If you're not on exact root of C:/ drive then first of all use cd../.. and then use query mysql -u username -p passsword

using the above lines you can access to mysql database.

Cuttler answered 23/12, 2020 at 10:2 Comment(0)
P
0

Following commands will connect to any MySQL database

shell> mysql --host=localhost --user=myname --password=mypass mydb

or

shell> mysql -h localhost -u myname -pmypass mydb

Since it shows the password in plain text, you can type password later as prompted. So, the command will be as follows

shell> mysql --host=localhost --user=myname --password mydb
shell> mysql -h localhost -u myname -p mydb
Pancake answered 27/12, 2016 at 7:12 Comment(0)
M
0

Your have to go on mysql installed path is on drive C:, then your commands need to be:

C:\Program Files\MySQL\MySQL Server 5.7\bin>mysql.exe -u root -p

OR

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

That will ask your MySql password over command prompt:

Enter password: ******

Put the password and you will get mysql dashboard.

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

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective

owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>
Marque answered 29/4, 2017 at 10:31 Comment(0)
U
0

Go to your MySQL directory. As in my case its...

cd C:\Program Files\MySQL\MySQL Server 8.0\bin
mysql -uroot -p

root can be changed to your user whatever MySQL user you've set.

It will ask for your password. If you have a password, Type your password and press "Enter", If no password set just press Enter without typing. You will be connected to MySQL.

There is another way to directly connect to MySQL without every time, going to the directory and typing down the commands.

Create a .bat file. First, add your path to MySQL. In my case it was,

cd C:\Program Files\MySQL\MySQL Server 8.0\bin

Then add these two lines

net start MySQL 
mysql -u root -p

If you don't want to type password every time you can simply add password with -p e.g. -proot (in case root was the password) but that is not recommended.

Also, If you want to connect to other host than local (staging/production server). You can also add -h22.345.80.09 E.g. 22.345.80.09 is your server ip.

net start MySQL 
mysql -u root -p -h22.345.80.0

Save the file. Just double click to open and connect directly to MySQL.

U answered 2/6, 2018 at 21:12 Comment(0)
Y
-1

syntax to open mysql on window terminal as:

mysql -u -p

e.g. mysql -uroot -proot

where: -u followed by username of your database , which you provided at the time of installatin and -p followed by password

Assumption: Assuming that mysql bin already included in path environment variable. if not included in path you can go till mysql bin folder and then run above command. if you want to know how to set path environment variable

Yulandayule answered 20/7, 2017 at 8:51 Comment(0)
E
-3

If you don't want to go to bin folder of MySQL then another option is to put a shortcut of mysql.exe to your default path of command prompt (C:\Users\"your user name">) with the contents of:

mysql -u(username) -p(password)
Elene answered 11/12, 2014 at 10:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.