352

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.

Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129
Leahcim
  • 40,649
  • 59
  • 195
  • 334

14 Answers14

610

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

HoldOffHunger
  • 18,769
  • 10
  • 104
  • 133
Nishant
  • 54,584
  • 13
  • 112
  • 127
  • 1
    thank you, 2 questions 1) do you have to include the "- u" "- p" etc, or are those replaced by the username etc 2) if there's no database created do you just leave that blank? – Leahcim Feb 27 '11 at 07:13
  • 2
    It is possible to set up MySQL authentication based on the user you logged in as, or not authenticate at all, but neither is a good idea, Specifying the password on the command line is even a slight security risk because it ends up in your command history and process tables. If you leave out the password it will ask you for it. – dj_segfault Feb 27 '11 at 07:16
  • @Michael 1) updated. the ALL-CAPS needs to be replaced with actual values. leave the -u, -p, -h as it is 2) yes. After logging-in type `show databases` to see the list of databases. – Nishant Feb 27 '11 at 07:16
  • 3
    @dj_segfault right. So, if you leave, `-p` without specifying the password, it will prompt you for password. – Nishant Feb 27 '11 at 07:17
  • 1
    If you don't currently have a database created, you need to use the *mysqladmin* command to create one. But I believe when you install the server, the default "mysql" database is created, that holds all the schema and authentication information. You can log into that one (if you have sufficient privileges) and create other databases from there. – dj_segfault Feb 27 '11 at 07:19
  • 1
    -P to use a port other than the default – Santa Jun 13 '19 at 11:57
  • Is there any way to pass in key value pair parameters along the schema name ? Say ones like `useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull` – Stephane Sep 19 '20 at 16:59
  • I do not get a password prompt when I leave out the password parameter – TheRealChx101 Jul 30 '21 at 10:30
  • Tip: Omit the database name argument if don't have a target database in mind. Then either run `SHOW DATABASES ;` to see a list, or run `CREATE DATABASE bogus_ ;`. – Basil Bourque Aug 14 '23 at 01:04
183

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

Melebius
  • 6,183
  • 4
  • 39
  • 52
Rick
  • 1,831
  • 1
  • 11
  • 2
  • 32
    To elaborate, this keeps your password from showing up in `.bash_history`. Using the current top-voted method, if someone gains access to `$HOME` (and thus `.bash_history`), they also have your authentication details -- scary! This kind of intrusion can (and [does](http://arstechnica.com/security/2015/08/0-day-attack-on-firefox-users-stole-password-and-key-data-patch-now/)) happen... – Jeremy Aug 10 '15 at 16:37
  • You could execute a bash command without without it showing up in the .bash_profie. Read more here: http://www.commandlinefu.com/commands/view/1512/execute-a-command-without-saving-it-in-the-history#comment – Skid Kadda Sep 06 '15 at 13:41
  • @Jeremy Not only in `.bash_history` but in `top` and more tools too! You can even have your files secure, just share the computer with another legitimate user. – Melebius Jul 14 '16 at 13:12
  • +1 because my password has special symbols in it and it won't parse when part of the command, it has to be done separately. – Jacksonkr Dec 20 '16 at 17:44
24

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:*********
Name Surname
  • 359
  • 2
  • 2
  • How can import a mysql file if I only have the shell? It will not see my local file system. Do I need a client or IS the Shell a client? – Timo Aug 13 '20 at 10:55
15

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
Noah Gary
  • 916
  • 12
  • 25
13

Use the following command to get connected to your MySQL database

mysql -u USERNAME -h HOSTNAME -p

Swamy
  • 400
  • 1
  • 3
  • 15
6

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=
arshovon
  • 13,270
  • 9
  • 51
  • 69
6

With this command below, everyone can log in MySQL:

mysql -u root -p

You need to put a password after running the command above:

Enter password: *****
Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129
5

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
Naveen Kumar
  • 987
  • 11
  • 11
4

Sometimes you may need to add -P for port:

mysql -u USERNAME -pPASSWORD -h HOSTNAME -P PORTNUMBER DATABASE;
Scorpioooooon21
  • 491
  • 5
  • 17
3

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
Cyril N.
  • 38,875
  • 36
  • 142
  • 243
  • Exactly what I was looking for! As you alluded to, If you don't want to hardcode the password you can omit it and then just run `mysql -p` to be prompted for the password interactively. – Tim Feb 25 '22 at 04:50
2

This worked for me ::-

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

or

mysql --host=hostNameorIp --user=username --password=password database_name
vs97
  • 5,765
  • 3
  • 28
  • 41
2

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

Oleksandr Savchenko
  • 642
  • 3
  • 10
  • 35
1

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
Anis KCHAOU
  • 830
  • 1
  • 11
  • 11
0

if you use no password for entering the mysql.server

mysql -u root -h 127.0.0.1 -P 'port'
toyota Supra
  • 3,181
  • 4
  • 15
  • 19