connecting to MySQL from the command line

The Question :

245 people think this question is useful

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.

The Question Comments :

The Answer 1

439 people think this answer is useful

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

The Answer 2

137 people think this answer is useful

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

The Answer 3

10 people think this answer is useful

Use the following command to get connected to your MySQL database

mysql -u USERNAME -h HOSTNAME -p

The Answer 4

10 people think this answer is useful

After you run MySQL Shell and you have seen following:

mysql-js>

Firstly, you should:

mysql-js>\sql

Secondly:

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

And finally:

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

The Answer 5

5 people think this answer is useful

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

mysql --user=root --password=mypass

Here,

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=

The Answer 6

0 people think this answer is useful

This worked for me ::-

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

or

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

Tags:

Add a Comment