Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 702
Leahcim Avatar asked Feb 27 '11 07:02

Leahcim


2 Answers

See here http://dev.mysql.com/doc/refman/5.0/en/connecting.html

mysql -u USERNAME -pPASSWORD -h HOSTNAMEORIP DATABASENAME 

The options above means:

-u: username
-p: password (**no space between -p and the password text**)
-h: host
last one is name of the database that you wanted to connect. 

Look into the link, it's detailed there!


As already mentioned by Rick, you can avoid passing the password as the part of the command by not passing the password like this:

mysql -u USERNAME -h HOSTNAMEORIP DATABASENAME -p

People editing this answer: PLEASE DONOT ADD A SPACE between -p and PASSWORD

like image 135
Nishant Avatar answered Oct 31 '22 20:10

Nishant


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

like image 167
Rick Avatar answered Oct 31 '22 22:10

Rick