connecting to MySQL from the command line

Mysql

Mysql Problem Overview


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.

Mysql Solutions


Solution 1 - Mysql

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

Solution 2 - Mysql

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

Solution 3 - Mysql

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:*********

Solution 4 - Mysql

Use the following command to get connected to your MySQL database

mysql -u USERNAME -h HOSTNAME -p

Solution 5 - Mysql

Short, sweet, and complete: (and also secure)

mysql -u <username> -h <hostname> -P <port> <database> -p

This will

  1. Connect you to a remote database (including port)
  2. Not store your password in your .bash_history

Solution 6 - Mysql

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=

Solution 7 - Mysql

Sometimes you may need to add -P for port:

mysql -u USERNAME -pPASSWORD -h HOSTNAME -P PORTNUMBER DATABASE;

Solution 8 - Mysql

This worked for me ::-

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

or

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

Solution 9 - Mysql

In my case, it worked with the following command on Mac.

After you run MySQL Shell and you have seen the following:

mysql-js>

Firstly, you should:

mysql-js>\sql

Second step:

MySQL  SQL > \c --mysql username@host

Then finally provide the password as prompted

Solution 10 - Mysql

Those steps worked for me with Windows 10

  1. go to MySQL installation directory then access to bin directory (mysql.exe must be showed in list of files)
  2. open cmd in the same location
  3. run mysql -u [username] -p (don't need to add -p if you didn't have set a password) then press enter

Solution 11 - Mysql

Oddly enough, despite there being a lot of (similar) answers, no one suggested this:

You can create a .my.cnf file in your $HOME folder, which contains:

[client]
host=127.0.0.1
port=3306
database=google
user=root
password=root

And you'll only have to do

$> mysql

To connect to that database.

A few key notes to take into consideration :

  • Storing the password in that file is not a good idea. At worst, please do a chmod 400 .my.cnf. But best is to store the password elsewhere. Other threads on StackOverflow offer great answers for that.
  • You can customize the data in that file, and leave the rest to you. For instance, removing the database line allow you to do mysql another-db-name

Solution 12 - Mysql

Use below command to do the login to remote mysql server

mysql -u property_wlive  -h 127.0.0.1 -P 3306 property_plive -p

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionLeahcimView Question on Stackoverflow
Solution 1 - MysqlNishantView Answer on Stackoverflow
Solution 2 - MysqlRickView Answer on Stackoverflow
Solution 3 - MysqlName SurnameView Answer on Stackoverflow
Solution 4 - MysqlSwamyView Answer on Stackoverflow
Solution 5 - MysqlNoah GaryView Answer on Stackoverflow
Solution 6 - MysqlarshoView Answer on Stackoverflow
Solution 7 - MysqlScorpioooooon21View Answer on Stackoverflow
Solution 8 - MysqlAnand TiwariView Answer on Stackoverflow
Solution 9 - MysqlSandeep KumarView Answer on Stackoverflow
Solution 10 - MysqlAnis KCHAOUView Answer on Stackoverflow
Solution 11 - MysqlCyril N.View Answer on Stackoverflow
Solution 12 - MysqlNaveen KumarView Answer on Stackoverflow