Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to have "mysql" command on Mac OS?

Tags:

mysql

macos

I was given a code repo which requires mysql in command line.

I am using Mac OS 10.11

First, I installed MySQL Community Server from http://dev.mysql.com/downloads/mysql/ and install it by running the PKG file.

After that, I opened System Preferences to start MySQL Server.

Then, I tried to execute

/usr/local/mysql/bin/mysql -uroot

and there is an error:

ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

How could I have mysql in command line?

Thanks,

like image 990
mommomonthewind Avatar asked Jul 05 '16 15:07

mommomonthewind


People also ask

How do I enable MySQL on my Mac?

Open macOS system preferences and select the MySQL preference panel, and then execute Start MySQL Server. The Instances page includes an option to start or stop MySQL, and Initialize Database recreates the data/ directory.

Why is MySQL command not found Mac?

The mysql Command not found error occurs because your computer can't find a program associated with the command on your computer's PATH environment variable. The PATH environment variable is a list of directories specifying where your computer should look for a program to run from the Terminal.


1 Answers

Typically the command is:

/usr/local/mysql/bin/mysql -u root -p

which will prompt you for your root password (which might be blank unless you changed it)

You can also use:

/usr/local/mysql/bin/mysql -u root -p[password] 

but keep in mind that password will be visible onscreen as you are typing it unlike the straight -p option that will hide your password as you type it when prompted.

Take a look at the options for mysql: https://dev.mysql.com/doc/refman/5.6/en/mysql-command-options.html

In your case, I'd try /usr/local/mysql/bin/mysql -u root -p then hit enter. mysql will prompt you for your password - type in in and hit enter again. If it's wrong mysql will let you know and then you'll have to go about resetting the mysql root password.

https://coolestguidesontheplanet.com/how-to-change-the-mysql-root-password/ is a reasonable set of instructions for doing that in OS X (may be out of date for your version of MySQL but the comments will help) but YMMV depending on where mysql was installed, etc...

Basically those instructions are:

  1. sudo /usr/local/mysql/support-files/mysql.server stop
  2. sudo mysqld_safe --skip-grant-tables
  3. mysql -u root
  4. ALTER USER 'root'@'localhost' IDENTIFIED by 'password';
  5. FLUSH PRIVILEGES;
  6. exit
  7. sudo /usr/local/mysql/support-files/mysql.server start

Which

  1. Stops mysql
  2. Sets mysql to run without bothering with privileges
  3. Opens a mysql prompt
  4. Updates the root password to 'password' - you should use something else here.
  5. "Cleans" passwords (some might say this is unnecessary)
  6. Exits the mysql prompt
  7. Starts mysql

That should allow you to run mysql -u root -p and use the new password set in #4.

like image 133
Jason Avatar answered Nov 12 '22 23:11

Jason