Accessing MYSQL through terminal

34,298

Solution 1

To access mysql from CLI:

 mysql -u <username> -p<password> <database_name>

Make sure no space between -p and password. Of course, this password will be visible in history.

To overcome this:

  1. Leave it blank and it will ask you for it.
  2. start the command with a space in front of mysql

Solution 2

With ...

mysql -u {user} -p {database}

The database is optional. {user} is the user you created when installing. You will get prompted for the password you supplied when installing. By the way: this is assuming you installed our native LAMP stack.

If you not yet have a user and password

mysql

will work too. If it does do make a user and password with correct privileges.

This is assuming MySQL is running.

Solution 3

Here is the official documentation about MySQL command line Tool

And a basic tutorial

Hope this helps

Solution 4

You may want to make sure the service has been started with:

sudo service mysql start

After that, if you have not set up any users or databases, then you could run:

/usr/bin/mysql -u root -p

to access the command line as root. If you set up a root password when installing LAMP then enter that. Otherwise just press enter and set the password later.

Share:
34,298

Related videos on Youtube

Parvej Islam
Author by

Parvej Islam

Updated on September 18, 2022

Comments

  • Parvej Islam
    Parvej Islam over 1 year

    I just installed LAMP in my computer through commandline.Now how can I access MYSQL prompt from commandline?I need to use MYSQL to create tables,views and then access them from my Perl script.

    • D-unit
      D-unit over 8 years
      You dont have to access MySQL from command line if you dont want to. You can use a tool like phpmyadmin, which is a tool that lets you manage your database in your browser.
    • ThunderBird
      ThunderBird about 8 years
      You can still have to access MySQL from command line whether you want it or not. You can also use tools like phpmyadmin, mysql workbench, which let you manage your database in a GUI.
  • Rinzwind
    Rinzwind over 8 years
    Are you sure it is not "--password={pwd}". If seem to remember the next word after -p is considered the database. But more important: this will echo the password into the process list and if you do not want this password stored in "history" add a space in from of "mysql" ;)
  • Aizuddin Zali
    Aizuddin Zali over 8 years
    -p<password> no space in between. Edited the answer.
  • Rinzwind
    Rinzwind over 8 years
    Oh yes! That was it indeed. It needs to be against the -p
  • ThunderBird
    ThunderBird almost 6 years
    You must not necessarily be root to invoke MySQL.
  • kimxons
    kimxons almost 6 years
    that is true, but if the problem persists, it is always better to login as root...mine works best that way.
  • RodrikTheReader
    RodrikTheReader over 4 years
    Why on earth would they require the condition that there's no space between -p and password? I spent half an hour on this believing I was a victim of some black magic.