How to connect from windows command prompt to mysql command line

后端 未结 16 1832
南旧
南旧 2020-12-12 23:30

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\\


        
相关标签:
16条回答
  • 2020-12-13 00:21

    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.

    0 讨论(0)
  • 2020-12-13 00:23

    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.

    0 讨论(0)
  • 2020-12-13 00:24

    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
    
    0 讨论(0)
  • 2020-12-13 00:25

    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.

    0 讨论(0)
提交回复
热议问题