12

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,

mommomonthewind
  • 4,390
  • 11
  • 46
  • 74
  • 1
    you already have it. you didn't provide a password, (`-p` option), so your login was denied. If you DIDN'T have the mysql client, you'd get "no such command" or whatever. – Marc B Jul 05 '16 at 15:46
  • Hi, I have this message ``2016-07-05T15:36:38.772955Z 1 [Note] A temporary password is generated for root@localhost: 6_fQx:dfS9H; If you lose this password, please consult the section How to Reset the Root Password in the MySQL reference manual.`` – mommomonthewind Jul 05 '16 at 15:50
  • However, when I tried with the password ``6_fQx:dfS9H``, it still reported ``Access denied`` – mommomonthewind Jul 05 '16 at 15:50
  • http://askubuntu.com/a/406612 , restart mysql daemon, login without password, change that password ---> `SET PASSWORD FOR 'root'@'localhost' = PASSWORD('MyNewPass');`, Go back and ditch that info from your cnf and save. That cnf can be your best friend, a problem that gets in the way of login, or a major security risk, Restart mysql daemon – Drew Jul 05 '16 at 16:24

1 Answers1

17

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.

Jason
  • 15,017
  • 23
  • 85
  • 116
  • Hi, could you take a look at my previous comment. I provided password but it does not work. – mommomonthewind Jul 05 '16 at 15:56
  • Hi, it's great. Could I set null password, i.e. allows to run ``mysql`` command itself without any interaction? – mommomonthewind Jul 11 '16 at 19:10
  • I think the tutorial is outdated for MySQL 5.7 http://stackoverflow.com/questions/30692812/mysql-user-db-does-not-have-password-columns-installing-mysql-on-osx – mommomonthewind Jul 11 '16 at 19:20
  • You are correct about the tutorial being out of date but my summary should be correct. As for running mysql without a password and as a commend without interaction...no password is never a good idea but there are options including my.conf. What mysql command are you trying to run and from where? That would help determine the best route. – Jason Jul 13 '16 at 00:54