How to have "mysql" command on Mac OS? - mysql

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,

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:
sudo /usr/local/mysql/support-files/mysql.server stop
sudo mysqld_safe --skip-grant-tables
mysql -u root
ALTER USER 'root'#'localhost' IDENTIFIED by 'password';
FLUSH PRIVILEGES;
exit
sudo /usr/local/mysql/support-files/mysql.server start
Which
Stops mysql
Sets mysql to run without bothering with privileges
Opens a mysql prompt
Updates the root password to 'password' - you should use something else here.
"Cleans" passwords (some might say this is unnecessary)
Exits the mysql prompt
Starts mysql
That should allow you to run mysql -u root -p and use the new password set in #4.

Related

Resetting mysql root password

I understand that this is the safest way to change or reset mysql root password. I'm doing it because 8 times out of 10 the installation program won't prompt me for a root password.
So I run commands:
sudo /etc/init.d/mysql stop
sudo mysqld_safe --skip-grant-tables &
sudo mysql -u root
use mysql;
Then, depending on your version of mysql or maria you're using you type:
update user set password=PASSWORD("mynewpassword") where User='root';
or
ALTER USER 'root'#'localhost' IDENTIFIED BY 'MyNewPass';
In my case I typed the first one. Then I refresh privileges by:
flush privileges; and then I exit out of mysql by typing: quit.
At this point everything went well. Then I restart mysql server by:
sudo /etc/init.d/mysql start
I get "OK" from the terminal. However, when I attempt to log in as root
I get pleasant error message saying:
ERROR 1698 (28000): Access denied for user 'root'#'localhost'
I'm 100% confident that I'm typing correct password. Why is this happening ?
Problem resolved by backing up all important data and reinstalling the operating system. As turned out I had some broken packages that affected how mysql authentication worked. Standard steps to fix broken packages was no use so I wiped out HDD and started over.
MySQL was installed without any hiccups and was even prompted for a root password to be set. Now MySQL authentication works like a charm.
Try this out :- It Works for me..!!
Stop the MySQL server process.
service mysql stop
Start the MySQL (mysqld) server/daemon process with the –skip-grant-tables option so that it will not prompt for password.
mysqld_safe --skip-grant-tables > /dev/null 2>&1 &
Connect to mysql server as the root user. And Setup new mysql root account password.
mysql -u root -e "use mysql; update user set password=PASSWORD('NEW-PASSWORD') where User='root'; flush privileges;"
Exit and restart the MySQL server.
service mysql restart
Note: You may need to wait after mysqld_safe command, before you can run subsequent mysql command.
Now run :- mysql -u root -p

How can I successfully login with root after running mysql_secure_installation?

I am currently unable to login as root on mysql and I am not quite sure about what's going on. Here's what happened:
I ran mysql_secure_installation as recommended, to secure my mysql installation. Afterwards, I typed the default root password that was asked and then I entered:
No to setting a password for root
Yes on removing anonymous users
Yes on disallowing remote root login
Yes on removing the test database and access to it
Yes on reloading privilege tables
After completing this process, I tried accessing mysql with mysql -u root -p (entered the default password) and received this message:
ERROR 1045 (28000): Access denied for user 'root'#'localhost' (using password: YES)
Do you have any ideas on what might have went wrong?
I also tried resetting the root password by starting mysql with --skip-grant-tables, but I am still not able to login.
I am using Ubuntu 14.04 and mysql 14.14 (LAMP stack).
I know this is an old post but the main answer is outdated and did not solve my issue.
Below my steps for future reference if anybody is having similar problems.
Stop mysql if it's running
$ sudo service mysql stop
Start mysql in safe mode
$ sudo mysqld_safe --skip-grant-tables --skip-syslog --skip-networking
If you get the error
"mysqld_safe Directory '/var/run/mysqld' for UNIX socket file don't exists"
Just create that folder:
$ mkdir -p /var/run/mysqld
$ chown mysql:mysql /var/run/mysqld
Open a new terminal window and log into mysql service + select mysql database:
$ mysql -u root
mysql> use mysql;
Set new password for root user:
mysql> update user set authentication_string=password('new-password') where user='root';
Flush privileges and exit mysql:
mysql> FLUSH PRIVILEGES;
mysql> exit;
Stop the safemode mysql (from the second terminal, you will see it stop in the first terminal)
$ mysqladmin -u root -p shutdown
Restart mysql
$ sudo service mysql start
You should be able to use the root user with password now to login to mysql/phpmyadmin
CentOS/Redhat:
From what I read in docs, when you run mysql_secure_installation, a temporary root password is generated and is stored in some log file.
sudo grep 'temporary password' /var/log/mysqld.log
Debian/Ubuntu:
During the packages installation, you get a prompt asking for the root password. If you don’t set it up, MySQL’s root user is created without a password. We can read the following line in package installation output:
Shell
2016-05-16T07:27:21.532619Z 1 [Warning] root#localhost is created with
an empty password ! Please consider switching off the
--initialize-insecure option.
but it is configured with the auth_socket plugin. You will only be able to connect using the UNIX socket, therefore any attempt to connect using your local IP or the network fails. Later on, you can change the password to allow connections from the network (as explained in this blog post).
Source
All we can do now is to see the root password. Lets change the root password since you cannot understand hashed password even if we can see it:
sudo service mysql stop
sudo mysqld_safe --skip-grant-tables --skip-syslog --skip-networking
then run mysql in a new terminal
mysql -u root
and run the following query, after changing the password
UPDATE mysql.user SET Password=PASSWORD('password') WHERE User='root';
FLUSH PRIVILEGES;
quit the mysql safe mode and start mysql service by
mysqladmin shutdown
sudo service mysql start
just run this script by root , you need custormize you password
mysqlpassword=password
/usr/bin/mysqladmin -u root password "$mysqlpassword"
#configure mysql login privileges
echo "grant all privileges on *.* to root#\"localhost\" identified by \"$mysqlpassword\";show databases;" |mysql -u root -p$mysqlpassword

How do I set mysql password?

I was getting an error trying to log in to mysql with mysql -u root -p , it always gave me the error "ERROR 1045 (28000): Access denied for user 'root'#'localhost' (using password: YES)". I'm on a Macbook Air running Yosemite.
So, I figured that I'd forgotten my password. It got so frustrating trying to reset it that I uninstalled mysql completely by sudo rming /usr/local/bin/mysql, and then reinstalling the dmg again from scratch. The installer gave me the gibberish temporary password, I tried logging in with it, and I got the same 1045 error that I've been getting all along. So maybe my first password wasn't wiped somehow . . . ?
I've tried this both with and without running mysqld in another terminal window, did not make a difference unfortunately. Any help on how I can just set the root password to whatever I want and log into mysql?
If you want to update a root password, then you need to use the following command:
$ mysqladmin -u root -p oldpassword newpassword
If you are trying to update the password for another user use:
$ mysqladmin -u user-name -p oldpassword newpassword
If you get a Access denied error it may be because you are not running the command as root.
To run a command as root add sudo to the beginning of the command.
For Example:
To update the password for the current, as root use:
$ sudo mysqladmin -u root -p oldpassword newpassword
To update the password for another user, as root use:
$ sudo mysqladmin -u user-name -p oldpassword newpassword
Also you might want to look at mysql_secure_installation, it will make your mysql installation more secure, it is recommended that you use it for production servers.
You first need to enter in your MySQL database : then copy and paste the following
SET PASSWORD FOR 'root'#'localhost' = PASSWORD('SETYOURPASSWORDHERE');
Be sure your user is root

ERROR 1045 (28000): Access denied for user 'root'#'localhost' (using password: YES)

I have been following a manual to install a software suite on Ubuntu. I have no knowledge of MySQL at all. I have done the following installations on my Ubuntu.
sudo apt-get update
sudo apt-get install mysql-server-5.5
sudo apt-get install mysql-client-5.5
sudo apt-get install mysql-common
sudo apt-get install glade
sudo apt-get install ntp
Then I do
cd ~/Desktop/iPDC-v1.3.1/DBServer-1.1
mysql -uroot -proot <"Db.sql"
I ended up with the following error message.
ERROR 1045 (28000): Access denied for user 'root'#'localhost' (using password: YES)
How may I fix it and continue?
Note: For MySQL 5.7+, please see the answer from Lahiru to this question. That contains more current information.
For MySQL < 5.7:
The default root password is blank (i.e., an empty string), not root. So you can just log in as:
mysql -u root
You should obviously change your root password after installation:
mysqladmin -u root password [newpassword]
In most cases you should also set up individual user accounts before working extensively with the database as well.
I was recently faced with the same problem, but in my case, I remember my password quite alright, but it kept on giving me the same error. I tried so many solutions, but still none helped. Then I tried this:
mysql -u root -p
After which it asks you for a password like this
Enter password:
And then I typed in the password I used. That's all.
I was able to solve this problem by executing this statement
sudo dpkg-reconfigure mysql-server-5.5
Which will change the root password.
You have to reset the password! Steps for Mac OS X (tested and working) and Ubuntu:
Stop MySQL using
sudo service mysql stop
or
sudo /usr/local/mysql/support-files/mysql.server stop
Start it in safe mode:
sudo mysqld_safe --skip-grant-tables --skip-networking
(the above line is the whole command)
This will be an ongoing command until the process is finished, so open another shell/terminal window, log in without a password:
mysql -u root
mysql> UPDATE mysql.user SET Password=PASSWORD('password') WHERE User='root';
As per #IberoMedia's comment, for newer versions of MySQL, the field is called authentication_string:
mysql> UPDATE mysql.user SET authentication_string =PASSWORD('password') WHERE User='root';
Start MySQL using:
sudo service mysql start
or
sudo /usr/local/mysql/support-files/mysql.server start
Your new password is 'password'.
Note: for version of MySQL > 5.7 try this:
update mysql.user set authentication_string='password' where user='root';
It happens when your password is missing.
Steps to change the password when you have forgotten it:
Stop MySQL Server (on Linux):
sudo systemctl stop mysql
Start the database without loading the grant tables or enabling networking:
sudo mysqld_safe --skip-grant-tables --skip-networking &
The ampersand at the end of this command will make this process run in the background, so you can continue to use your terminal and run mysql -u root (as root). It will not ask for a password.
If you get error like as below:
2018-02-12T08:57:39.826071Z mysqld_safe Directory '/var/run/mysqld' for UNIX
socket file don't exists.
mysql -u root
ERROR 2002 (HY000): Can't connect to local MySQL server through socket
'/var/run/mysqld/mysqld.sock' (2)
[1]+ Exit 1
Make MySQL service directory.
sudo mkdir /var/run/mysqld
Give MySQL user permission to write to the service directory.
sudo chown mysql: /var/run/mysqld
Run the same command in step 2 to run MySQL in background.
Run mysql -u root. You will get the MySQL console without entering a password.
Run these commands
FLUSH PRIVILEGES;
For MySQL 5.7.6 and newer
ALTER USER 'root'#'localhost' IDENTIFIED BY 'new_password';
For MySQL 5.7.5 and older
SET PASSWORD FOR 'root'#'localhost' = PASSWORD('new_password');
If the ALTER USER command doesn't work use:
UPDATE mysql.user SET authentication_string = PASSWORD('new_password') WHERE User = 'root' AND Host = 'localhost';
Now exit
To stop the instance started manually:
sudo kill `cat /var/run/mysqld/mysqld.pid`
Restart MySQL
sudo systemctl start mysql
At the initial start up of the server the following happens, given that the data directory of the server is empty:
The server is initialized.
SSL certificate and key files are generated in the data directory.
The validate_password plugin is installed and enabled.
The superuser account 'root'#'localhost' is created. The password for the superuser is set and stored in the error log file.
To reveal it, use the following command:
shell> sudo grep 'temporary password' /var/log/mysqld.log
Change the root password as soon as possible by logging in with the generated temporary password and set a custom password for the superuser account:
shell> mysql -u root -p
mysql> ALTER USER 'root'#'localhost' IDENTIFIED BY 'MyNewPass5!';
If the problem still exists, try to force changing the password:
/etc/init.d/mysql stop
mysqld_safe --skip-grant-tables &
mysql -u root
Set up a new MySQL root user password:
use mysql;
update user set password=PASSWORD("NEW-ROOT-PASSWORD") where User='root';
flush privileges;
quit;
Stop the MySQL server:
/etc/init.d/mysql stop
Start the MySQL server and test it:
mysql -u root -p
If none of the other answers work for you, and you received this error:
mysqld_safe Logging to '/var/log/mysql/error.log'.
mysqld_safe Directory '/var/run/mysqld' for UNIX socket file don't exists.
[1]+ Exit 1 sudo mysqld_safe --skip-grant-tables
Follow the below commands step by step until you reset your password:
# Stop your server first
sudo service mysql stop
# Make the MySQL service directory.
sudo mkdir /var/run/mysqld
# Give MySQL permission to work with the created directory
sudo chown mysql: /var/run/mysqld
# Start MySQL, without permission and network checking
sudo mysqld_safe --skip-grant-tables --skip-networking &
# Log in to your server without any password.
mysql -u root mysql
# Update the password for the root user:
UPDATE mysql.user SET authentication_string=PASSWORD('YourNewPasswordBuddy'), plugin='mysql_native_password' WHERE User='root' AND Host='localhost';
# If you omit (AND Host='localhost') section, it updates
# the root password regardless of its host
FLUSH PRIVILEGES;
EXIT;
# Kill the mysqld_safe process
sudo service mysql restart
# Now you can use your new password to log in to your server
mysql -u root -p
# Take note for remote access. You should create a remote
# user and then grant all privileges to that remote user
I came across this very annoying problem and found many answers that did not work. The best solution I came across was to completely uninstall MySQL and reinstall it. On reinstall you set a root password and this fixed the problem.
sudo apt-get purge mysql-server mysql-client mysql-common mysql-server-core-5.5 mysql-client-core-5.5
sudo rm -rf /etc/mysql /var/lib/mysql
sudo apt-get autoremove
sudo apt-get autoclean
I found this code elsewhere, so I don't take any credit for it. But it works. To install MySQL after uninstalling it, I think DigitalOcean has a good tutorial on it. Checkout my gist for this.
How to install MySQL on Ubuntu (which works)
I am using Ubuntu 16.04 (Xenial Xerus) and installed MySQL 5.7.
I had the same issue
Login denied for root user.
I tried the below steps:
dpkg --get-selections | grep mysql (to get the version of MySQL).
dpkg-reconfigure mysql-server-5.7
mysql -u root -p
Without -p that doesn't prompt you to ask password. Once you are in, you can create a user with a password by following steps:
CREATE USER 'your_new_username'#'your-hostname' IDENTIFIED BY 'your-password';
GRANT ALL PRIVILEGES ON *.* to 'your_new_username'#'your-hostname' WITH GRANT OPTION;
Exit from the root and log in from the <name> you gave above.
mysql -u <your_new_username> -p
For some reason still just typing MySQL does not work. At all. I suggest to make it a habit to use mysql -u <name> -p.
In the terminal, just enter:
mysql -u root -p
Then it will ask the password from you.
I installed MySQL as root user ($SUDO) and got this same issue
Here is how I fixed it:
sudo cat /etc/mysql/debian.cnf
This will show details as:
# Automatically generated for Debian scripts. DO NOT TOUCH! [client] host = localhost user = debian-sys-maint password = GUx0RblkD3sPhHL5 socket = /var/run/mysqld/mysqld.sock [mysql_upgrade] host = localhost user = debian-sys-maint password = GUx0RblkD3sPhHL5 socket = /var/run/mysqld/mysqld.sock
Above we can see the password. But we are just going to use(GUx0RblkD3sPhHL5) that in the prompt.
`mysql -u debian-sys-maint -p
Enter password: `
Now provide the password (GUx0RblkD3sPhHL5).
Now exit from MySQL and log in again as:
`mysql -u root -p
Enter password: `
Now provide the new password. That's all. We have a new password for further uses.
It worked for me.
For those for whom the current answers didn't work can try this (tested on macOS):
mysql -h localhost -u root -p --protocol=TCP
After this, a password will be asked from you and you should use your OS user password. Then when you get into MySQL you can run:
select Host, User from mysql.user;
And you should see:
MySQL [(none)]> select Host, User from mysql.user;
+-----------+------------------+
| Host | User |
+-----------+------------------+
| localhost | mysql.infoschema |
| localhost | mysql.session |
| localhost | mysql.sys |
| localhost | root |
+-----------+------------------+
And from here you can change the configurations and edit the password or modify the grants.
Please read the official documentation: MySQL: How to Reset the Root Password
If you have access to a terminal:
MySQL 5.7.6 and later:
mysql
mysql> ALTER USER 'root'#'localhost' IDENTIFIED BY 'MyNewPass';
MySQL 5.7.5 and earlier:
mysql
mysql> SET PASSWORD FOR 'root'#'localhost' = PASSWORD('MyNewPass');
I am using mysql-5.7.12-osx10.11-x86_64.dmg on Mac OS X.
The installation process automatically sets up a temporary password for the root user. You should save the password. The password can not be recovered.
Follow the instructions:
Go to cd /usr/local/mysql/bin/
Enter the temporary password (which would look something like, "tsO07JF1=>3")
You should get the mysql> prompt.
Run, SET PASSWORD FOR 'root'#'localhost' = PASSWORD('{YOUR_PASSWORD}'); If you wish to set your password: "root" then the command would be, SET PASSWORD FOR 'root'#'localhost' = PASSWORD('root');
Run ALTER USER 'root'#'localhost' PASSWORD EXPIRE NEVER;
Run exit
Run ./mysql -u root -p
Type your password. In my case I would type, "root" (without quote)
That's all.
For convenience, you should add "/usr/local/mysql/bin" to your PATH environment variable.
Now from anywhere you can type ./mysql -u root -p and then type the password and you will get the mysql> prompt.
The answer may sound silly, but after wasting hours of time, this is how I got it to work:
mysql -u root -p
I got the error message
ERROR 1045 (28000): Access denied for user 'root'#'localhost' (using password: YES)
Even though I was typing the correct password (the temporary password you get when you first install MySQL).
I got it right when I typed in the password when the password prompt was blinking.
If you have MySQL as part of a Docker image (say on port 6606) and an Ubuntu install (on port 3306) specifying the port is not enough:
mysql -u root -p -P 6606
will throw:
ERROR 1045 (28000): Access denied for user 'root'#'localhost' (using password: YES)
as it's trying to connect to localhost by default, specifying your local IP address fixes the issue:
mysql -u root -p -P 6606 -h 127.0.0.1
Year 2021.
Answer for Ubuntu 20.04 (Focal Fossa) (maybe other distributions as well).
After days of wandering around... and having none of those answers working for me, I did this and it worked!
Always in a Bash shell:
sudo systemctl disable mysql
In order to stop the daemon from starting on boot.
sudo apt purge mysql-server
and
sudo apt purge mysql-community-server*
There, it warns you you'll erase configuration files... so it's working! Because those are the ones making trouble!
sudo autoremove
To delete all the left behind packages.
Then (maybe it's optional, but I did it) reboot.
Also, I downloaded mysql-server-8.0 from the official MySQL webpage:
sudo apt install mysql-server
A signal that it's working is that when you enter the command above, the system asks you to enter the root password.
Finally:
mysql -u root -p
And the password you entered before.
If the problem still exists, try to force changing the password.
Stop MySQL Server (on Linux):
/etc/init.d/mysql stop
Stop MySQL Server (on Mac OS X):
mysql.server stop
Start the mysqld_safe daemon with --skip-grant-tables:
mysqld_safe --skip-grant-tables &
mysql -u root
Set up a new MySQL root user password:
use mysql;
update user set password=PASSWORD("NEW-ROOT-PASSWORD") where User='root';
flush privileges;
quit;
Stop MySQL Server (on Linux):
/etc/init.d/mysql stop
Stop MySQL Server (on Mac OS X):
mysql.server stop
Start the MySQL server service and test to log in by root:
mysql -u root -p
I also came across the same problem. I did:
Open your cmd
Navigate to C:\Program Files\MySQL\MySQL Server 8.0\bin>
(where MySQL Server 8.0 may be different depending on the server you installed)
Then put the following command mysql -u root -p
It will prompt for the password... simply hit Enter, as sometimes the password you entered while installing is changed by to blank.
Now you can simply access the database.
This solution worked for me on the Windows platform.
By default, the password will be null, so you have to change the password by doing the below steps.
Connect to MySQL
root# mysql
Use mysql
mysql> update user set password=PASSWORD('root') where User='root';
Finally, reload the privileges:
mysql> flush privileges;
mysql> quit
Just one line and it solved my issue.
sudo dpkg-reconfigure mysql-server-5.5
In Ubuntu 16.04 (Xenial Xerus) and MySQL version 5.7.13, I was able to resolve the problem with the steps below:
Follow the instructions from section B.5.3.2.2 Resetting the Root Password: Unix and Unix-Like Systems
MySQL 5.7 reference manual
When I tried #sudo mysqld_safe --init-file=/home/me/mysql-init & it failed. The error was in /var/log/mysql/error.log:
2016-08-10T11:41:20.421946Z 0 [Note] Execution of init_file '/home/me/mysql/mysql-init' started.
2016-08-10T11:41:20.422070Z 0 [ERROR] /usr/sbin/mysqld: File '/home/me/mysql/mysql-init' not found (Errcode: 13 - Permission denied)
2016-08-10T11:41:20.422096Z 0 [ERROR] Aborting
The file permission of mysql-init was not the problem. We need to edit AppArmor permissions.
Edit by sudo vi /etc/apparmor.d/usr.sbin.mysqld
....
/var/log/mysql/ r,
/var/log/mysql/** rw,
# Allow user init file
/home/pranab/mysql/* r,
# Site-specific additions and overrides. See local/README for details.
#include <local/usr.sbin.mysqld>
}
Do sudo /etc/init.d/apparmor reload
Start mysqld_safe again. Try step 2 above. Check file /var/log/mysql/error.log. Make sure there is no error and the mysqld is successfully started.
Run mysql -u root -p
Enter password:
Enter the password that you specified in mysql-init. You should be able to log in as root now.
Shutdown mysqld_safe by sudo mysqladmin -u root -p shutdown
Start mysqld the normal way by sudo systemctl start mysql
While the top answer (with mysqladmin) worked on macOS v10.15 (Catalina), it did not work on Ubuntu. Then I tried many of the other options, including a safe start for MySQL, but none worked.
Here is one that does:
At least for the version I got 5.7.28-0ubuntu0.18.04.4 answers were lacking IDENTIFIED WITH mysql_native_password. 5.7.28 is the default on the current LTS and thus should be the default for most new new systems (till Ubuntu 20.04 (Focal Fossa) LTS comes out).
I found Can't set root password MySQL Server and now applied
ALTER USER 'root'#'localhost' IDENTIFIED WITH mysql_native_password BY 'your_pass_here';
which does work.
The error that I faced was:
ERROR 1045 (28000): Access denied for user 'root'#'localhost' (using password: NO)
It was a problem with the port running on.
By default, MySQL is running on port 3306.
You can check that on by running
in a 32-bit system:
sudo /opt/lampp/manager-linux.run
in a 64-bit system:
sudo /opt/lampp/manager-linux-x64.run
and click on the Configure button.
In my case the port was running on 3307, and I used the command
mysql -u root -p -P 3307 -h 127.0.0.1
Copied from this link, I had the same problem and this solved the problem. After we add a password for the database, we need to add -p (password-based login), and then enter the password. Otherwise, it will return this error:
mysql -u root -p
Because your error message says "PASSWORD: YES" this means you are are using the wrong password. This happened to me also. Luckily I remembered my correct password, and was able to make the DB connection work.
In recent MySQL versions there isn't any password in the mysql.user table.
So you need to execute ALTER USER. Put this one line command into the file.
ALTER USER 'root'#'localhost' IDENTIFIED BY 'MyNewPass';
And execute it as an init file (as the root or mysql user):
mysqld_safe --init-file=/home/me/mysql-init &
MySQL server need to be stopped to start mysqld_safe.
Also, there may be a problem with AppArmor permissions to load this init file. Read more in AppArmor and MySQL.
If you haven't set password yet, then run mysql -uroot. It works for me.
On Mac, if you have a problem in logging in with the first password you were given in installation, maybe you can just simply kill the MySQL process and then try.
So:
run the following command to find the PID of MySQL:
ps -aef | grep mysql | grep -v grep
kill the process:
kill -15 [process id]
Then you can log in with the initial password using this command:
mysql -uroot -p
Which asks you to enter your password. Just enter the initial password.

Access Denied for MYSQL ERROR 1045

I just got a new macbook pro (OS X 10.8.2) and am attempting to get mysql set up on it. So far I've been able to get it installed but I cannot get my root user access (or any user for that matter). I plan on using this for Python, on my other computer I only use MYSQL (no MAMP) and I prefer to keep it that way.
For reference, I did the following:
$ alias mysql=/usr/local/mysql/bin/mysql
$ sudo /Library/StartupItems/MySQLCOM/MySQLCOM start
$ alias mysqladmin=/usr/local/mysql/bin/mysqladmin
When i enter mysql or mysql -u root -p it gives me this:
ERROR 1045 (28000): Access denied for user 'root'#'localhost' (using password: YES)
or
ERROR 1045 (28000): Access denied for user 'jmitch'#'localhost' (using password: NO)
Depending on which phrasing I use
MYSQL is running in my system preferences. Thank you for your help.
Maybe updating the package the updater overwrote the root password.
To restore it:
Stop mysqld deamons.
$ sudo service mysqld stop
Go to mysql/bin directory
$ cd /usr/bin
Start a mysql deamon with this option:
$ sudo mysqld_safe --skip-grant-tables
Open another terminal and open a mysql session to execute this:
$ mysql
mysql> use mysql;
see Note1 below for next line.
mysql> UPDATE user SET password=PASSWORD('YOUR_NEW_PASSWORD_HERE') WHERE user = 'root';
mysql> exit;
Now kill the mysqld_safe process and restart mysqld normally:
$ sudo service mysqld start
Note1: password is the column name in table mysql.user prior to version 5.7. After which it became authentication_string. Change your update statement accordingly.
on Mac OSX 10.9 Mavericks I used the 'mysql.server' script in the support-files directory instead of the mysqld_safe and service script.
$sudo ./mysql.server stop
$sudo ./mysql.server start --skip-grant-tables
$ mysql
mysql> use mysql;
mysql> UPDATE user SET password=PASSWORD('YOUR_NEW_PASSWORD_HERE') WHERE user = 'root';
mysql> exit;
$sudo ./mysql.server stop
$sudo ./mysql.server start
I was having a similar issue trying to access MAMP's MySQL through the terminal on Mountain Lion.
The --no-defaults flag solved it for me.
/Applications/MAMP/Library/bin/mysql --no-defaults -u root -proot -h localhost
I want to add that for MySQL 5.7 simply changing the authentication_string column doesn't work. This is because MySQL never actually uses those values for root authentication, it uses a plugin. As far as I can tell this plugin verifies that you are also root on the host account (so you have to sudo mysql -u root).
The only way I was able to get this to work was to run this:
UPDATE mysql.user
SET authentication_string=PASSWORD(''), plugin=''
WHERE mysql.user = 'root';
It should also be noted that the official MySQL documentation for 5.7 never mentions this. Following this documentation to the letter gets you nowhere at all.