[mysql] connecting to MySQL from the command line

How can you connect to MySQL from the command line in a Mac? (i.e. show me the code)

I'm doing a PHP/SQL tutorial, but it starts by assuming you're already in MySQL.

This question is related to mysql

The answer is

After you run MySQL Shell and you have seen following:


Firstly, you should:



 mysql-sql>\connect username@servername (root@localhost)

And finally:

Enter password:*********

Best practice would be to mysql -u root -p. Then MySQL will prompt for password after you hit enter.

This worked for me ::-

mysql --host=hostNameorIp --user=username --password=password  


mysql --host=hostNameorIp --user=username --password=password database_name

One way to connect to MySQL directly using proper MySQL username and password is:

mysql --user=root --password=mypass


root is the MySQL username
mypass is the MySQL user password

This is useful if you have a blank password.

For example, if you have MySQL user called root with an empty password, just use

mysql --user=root --password=

Use the following command to get connected to your MySQL database

mysql -u USERNAME -h HOSTNAME -p