How to resolve this MySQL error in Mac OS X Sierra? - php

I have been trying to reset the root password for MySQL on my Mac OS X Sierra and I cannot believe I can't find documentation for this anywhere that can resolve my problem.
The first error I got when I tried to access my database is this:
Error: Access denied for user 'root'#'localhost' (using password: NO)
I figured I would completely remove MySQL and install it again with brew but when I tried with mysql_secure_installation and I get this:
Securing the MySQL server deployment.
Enter password for user root:
Error: Access denied for user 'root'#'localhost' (using password: NO)
Also when I try to run:
mysql.server stop
I get this error:
ERROR! MySQL server PID file could not be found!

On mac, I think you have to use root as a password.

open mysqld_safe
mysql.server stop
mysqld_safe --skip-grant-tables
open a new terminal
mysql -uroot -p
use mysql;
update user set password=PASSWORD('123456') where user="root";
flush privileges;
quit;
close mysqld_safe terminal
restart mysql
mysql.server start
mysql -uroot -p
123456

Related

Could not create database for connection named default An exception occurred in the driver: SQLSTATE[HY000] [1045] Access denied 'root'#'localhost' [duplicate]

Consider:
./mysqladmin -u root -p** '_redacted_'
Output (including typing the password):
Enter password:
mysqladmin: connect to server at 'localhost' failed error:
'Access denied for user 'root'#'localhost' (using password: YES)'
How can I fix this?
All solutions I found were much more complex than necessary and none worked for me. Here is the solution that solved my problem. There isn't any need to restart mysqld or start it with special privileges.
sudo mysql
-- for MySQL
ALTER USER 'root'#'localhost' IDENTIFIED WITH mysql_native_password BY 'root';
-- for MariaDB
ALTER USER 'root'#'localhost' IDENTIFIED VIA mysql_native_password USING PASSWORD('root');
With a single query we are changing the auth_plugin to mysql_native_password and setting the root password to root (feel free to change it in the query).
Now you should be able to log in with root. More information can be found in MySQL documentation or MariaDB documentation.
(Exit the MySQL console with Ctrl + D or by typing exit.)
Open and edit /etc/my.cnf or /etc/mysql/my.cnf, depending on your distribution.
Add skip-grant-tables under [mysqld]
Restart MySQL
You should be able to log in to MySQL now using the below command mysql -u root -p
Run mysql> flush privileges;
Set new password by ALTER USER 'root'#'localhost' IDENTIFIED BY 'NewPassword';
Go back to /etc/my.cnf and remove/comment skip-grant-tables
Restart MySQL
Now you will be able to login with the new password mysql -u root -p
None of the previous answers helped me with this problem, so here's the solution I found.
The relevant part:
In Ubuntu systems running MySQL 5.7 (and later versions), the root MySQL user is set to authenticate using the auth_socket plugin by default rather than with a password. This allows for some greater security and usability in many cases, but it can also complicate things when you need to allow an external program (e.g., phpMyAdmin) to access the user.
In order to use a password to connect to MySQL as root, you will need to switch its authentication method from auth_socket to mysql_native_password. To do this, open up the MySQL prompt from your terminal:
sudo mysql
Next, check which authentication method each of your MySQL user accounts use with the following command:
SELECT user,authentication_string,plugin,host FROM mysql.user;
Output
+------------------+-------------------------------------------+-----------------------+-----------+
| user | authentication_string | plugin | host |
+------------------+-------------------------------------------+-----------------------+-----------+
| root | | auth_socket | localhost |
| mysql.session | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | mysql_native_password | localhost |
| mysql.sys | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | mysql_native_password | localhost |
| debian-sys-maint | *CC744277A401A7D25BE1CA89AFF17BF607F876FF | mysql_native_password | localhost |
+------------------+-------------------------------------------+-----------------------+-----------+
4 rows in set (0.00 sec)
In this example, you can see that the root user does in fact authenticate using the auth_socket plugin. To configure the root account to authenticate with a password, run the following ALTER USER command. Be sure to change password to a strong password of your choosing, and note that this command will change the root password you set in Step 2:
ALTER USER 'root'#'localhost' IDENTIFIED WITH mysql_native_password BY 'password';
Then, run FLUSH PRIVILEGES which tells the server to reload the grant tables and put your new changes into effect:
FLUSH PRIVILEGES;
Check the authentication methods employed by each of your users again to confirm that root no longer authenticates using the auth_socket plugin:
SELECT user,authentication_string,plugin,host FROM mysql.user;
Output
+------------------+-------------------------------------------+-----------------------+-----------+
| user | authentication_string | plugin | host |
+------------------+-------------------------------------------+-----------------------+-----------+
| root | *3636DACC8616D997782ADD0839F92C1571D6D78F | mysql_native_password | localhost |
| mysql.session | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | mysql_native_password | localhost |
| mysql.sys | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | mysql_native_password | localhost |
| debian-sys-maint | *CC744277A401A7D25BE1CA89AFF17BF607F876FF | mysql_native_password | localhost |
+------------------+-------------------------------------------+-----------------------+-----------+
4 rows in set (0.00 sec)
You can see in this example output that the root MySQL user now authenticates using a password. Once you confirm this on your own server, you can exit the MySQL shell:
exit
I tried many steps to get this issue corrected. There are so many sources for possible solutions to this issue that is is hard to filter out the sense from the nonsense. I finally found a good solution here:
Step 1: Identify the database version
mysql --version
You'll see some output like this with MySQL:
mysql Ver 14.14 Distrib 5.7.16, for Linux (x86_64) using EditLine wrapper
Or output like this for MariaDB:
mysql Ver 15.1 Distrib 5.5.52-MariaDB, for Linux (x86_64) using readline 5.1
Make note of which database and which version you're running, as you'll use them later. Next, you need to stop the database so you can access it manually.
Step 2: Stopping the database server
To change the root password, you have to shut down the database server beforehand.
You can do that for MySQL with:
sudo systemctl stop mysql
And for MariaDB with:
sudo systemctl stop mariadb
Step 3: Restarting the database server without permission checking
If you run MySQL and MariaDB without loading information about user privileges, it will allow you to access the database command line with root privileges without providing a password. This will allow you to gain access to the database without knowing it.
To do this, you need to stop the database from loading the grant tables, which store user privilege information. Because this is a bit of a security risk, you should also skip networking as well to prevent other clients from connecting.
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.
Now you can connect to the database as the root user, which should not ask for a password.
mysql -u root
You'll immediately see a database shell prompt instead.
MySQL Prompt
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
MariaDB Prompt
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]>
Now that you have root access, you can change the root password.
Step 4: Changing the root password
mysql> FLUSH PRIVILEGES;
Now we can actually change the root password.
For MySQL 5.7.6 and newer as well as MariaDB 10.1.20 and newer, use the following command:
mysql> ALTER USER 'root'#'localhost' IDENTIFIED BY 'new_password';
For MySQL 5.7.5 and older as well as MariaDB 10.1.20 and older, use:
mysql> SET PASSWORD FOR 'root'#'localhost' = PASSWORD('new_password');
Make sure to replace new_password with your new password of choice.
Note: If the ALTER USER command doesn't work, it's usually indicative of a bigger problem. However, you can try UPDATE ... SET to reset the root password instead.
[IMPORTANT] This is the specific line that fixed my particular issue:
mysql> UPDATE mysql.user SET authentication_string = PASSWORD('new_password') WHERE User = 'root' AND Host = 'localhost';
Remember to reload the grant tables after this.
In either case, you should see confirmation that the command has been successfully executed.
Query OK, 0 rows affected (0.00 sec)
The password has been changed, so you can now stop the manual instance of the database server and restart it as it was before.
Step 5: Restart the Database Server Normally
The tutorial goes into some further steps to restart the database, but the only piece I used was this:
For MySQL, use:
sudo systemctl start mysql
For MariaDB, use:
sudo systemctl start mariadb
Now you can confirm that the new password has been applied correctly by running:
mysql -u root -p
The command should now prompt for the newly assigned password. Enter it, and you should gain access to the database prompt as expected.
Conclusion
You now have administrative access to the MySQL or MariaDB server restored. Make sure the new root password you choose is strong and secure and keep it in safe place.
After trying all others answers, this it what finally worked for me:
sudo mysql -- It does not ask me for any password
-- Then in MariaDB/MySQL console:
update mysql.user set plugin = 'mysql_native_password' where User='root';
FLUSH PRIVILEGES;
exit;
I found the answer in the blog post Solved: Error “Access denied for user ‘root’#’localhost’” of MySQL — codementor.tech (Medium).
For Ubuntu/Debian users
(It may work on other distributions, especially Debian-based ones.)
Run the following to connect as root (without any password)
sudo /usr/bin/mysql --defaults-file=/etc/mysql/debian.cnf
If you don't want to add --defaults-file each time you want to connect as root, you can copy /etc/mysql/debian.cnf into your home directory:
sudo cp /etc/mysql/debian.cnf ~/.my.cnf
And then:
sudo mysql
In my experience, if you run without sudo it will not work. So make sure your command is;
sudo mysql -uroot -p
For new Linux users this could be a daunting task. Let me update this with MySQL 8 (the latest version available right now is 8.0.12 as on 2018-09-12)
Open "mysqld.cnf" configuration file at "/etc/mysql/mysql.conf.d/".
Add skip-grant-tables to the next line of [mysql] text and save.
Restart the MySQL service as "sudo service mysql restart". Now your MySQL is free of any authentication.
Connect to the MySQL client (also known as mysql-shell) as mysql -u root -p. There is no password to be keyed in as of now.
Run SQL command flush privileges;
Reset the password now as ALTER USER 'root'#'localhost' IDENTIFIED BY 'MyNewPassword';
Now let's get back to the normal state; remove that line "skip-grant-tables" from "mysqld.cnf" and restart the service.
That's it.
In my case under Debian 10, the error
ERROR 1698 (28000): Access denied for user 'root'#'localhost'
was solved by (good way)
sudo mysql -u root -p mysql
Bad way:
mysql -u root -p mysql
I did this to set my root password in the initial set up of MySQL in OS X. Open a terminal.
sudo sh -c 'echo /usr/local/mysql/bin > /etc/paths.d/mysql'
Close the terminal and open a new terminal.
And the following worked in Linux, to set the root password.
sudo /usr/local/mysql/support-files/mysql.server stop
sudo mysqld_safe --skip-grant-tables
(sudo mysqld_safe --skip-grant-tables: This did not work for me the first time. But on the second try, it was a success.)
Then log into MySQL:
mysql -u root
FLUSH PRIVILEGES;
Now change the password:
ALTER USER 'root'#'localhost' IDENTIFIED BY 'newpassword';
Restart MySQL:
sudo /usr/local/mysql/support-files/mysql.server stop
sudo /usr/local/mysql/support-files/mysql.server start
My Station here:
UBUNTU 21.04
PHP 5.6.40-57
MYSQL 5.7.37
let's config it
nano /etc/mysql/mysql.conf.d/mysqld.cnf
at the bottom, write this
skip-grant-tables
reload it
service mysql restart
In your MySQL Workbench, you can go to the left sidebar, under Management select "Users and Privileges", click root under User Accounts, in the right section click tab "Account Limits" to increase the maximum queries, updates, etc., and then click tab "Administrative Roles" and check the boxes to give the account access.
Ugh - nothing worked for me! I have a CentOS 7.4 machine running MariaDB 5.5.64.
I had to do this, right after installation of MariaDB from YUM;
systemctl restart mariadb
mysql_secure_installation
The mysql_secure_installation will take you through a number of steps, including "Set root password? [Y/n]". Just say "y" and give it a password. Answer the other questions as you wish.
Then you can get in with your password, using
mysql -u root -p
It will survive
systemctl restart mariadb
The Key
Then, I checked the /bin/mysql_secure_installation source code to find out how it was magically able to change the root password and none of the other answers here could. The import bit is:
do_query "UPDATE mysql.user SET Password=PASSWORD('$esc_pass') WHERE User='root';"
...It says SET Password=... and not SET authentication_string = PASSWORD.... So, the proper procedure for this version (5.5.64) is:
Log in using mysql -u root -p, using the password you already set.
Or, stop the database and start it with:
mysql_safe --skip-grant-tables --skip-networking &
From the mysql> prompt:
use mysql;
select host,user,password from user where user = 'root';
(observe your existing passwords for root).
UPDATE mysql.user set Password = PASSWORD('your_new_cleartext_password') where user = 'root' AND host = 'localhost';
select host,user,password from user where user = 'root';
flush privileges;
quit;
Kill the running mysqld_safe. Restart MariaDB. Log in as root: mysql -u -p. Use your new password.
If you want, you can set all the root passwords at once. I think this is wise:
mysql -u root -p
(login)
use mysql;
select host,user,password from user where user = 'root';
UPDATE mysql.user set Password = PASSWORD('your_new_cleartext_password') where user = 'root';
select host,user,password from user where user = 'root';
flush privileges;
quit;
This will perform updates on all the root passwords: i.e., for "localhost", "127.0.0.1", and "::1"
In the future, when I go to RHEL 8 or what have you, I will try to remember to check the /bin/mysql_secure_installation and see how the guys did it, who were the ones that configured MariaDB for this OS.
Use sudo to alter your password:
sudo mysql
ALTER USER 'root'#'localhost' IDENTIFIED WITH mysql_native_password BY 'insert_password';
Source: Phoenixnap - Access denied for user root localhost
Fix for macOS
Install MySQL from https://downloads.mysql.com/archives/community/ (8.x is the latest as on date, but ensure that the version is compatible with the macOS version)
Give password for root (let <root-password> be the password) during installation (don't forget to remember the password!)
Select Use Legacy Password Encryption option (that is what I had used and did not try for Use Strong Password Encryption option)
Search and open MySQL.prefPane (use search tool)
Select Configuration tab
Click Select option of Configuration File
Select /private/etc/my.cnf
From terminal open a new or existing file with name /etc/my.cnf (vi /etc/my.cnf) add the following content:
[mysqld]
skip-grant-tables
Restart mysqld as follows:
ps aux | grep mysql
kill -9 <pid1> <pid2> ... (grab pids of all MySQL related processes)
mysqld gets restarted automatically
Verify that the option is set by running the following from terminal:
ps aux | grep mysql
> mysql/bin/mysqld ... --defaults-file=/private/etc/my.cnf ... (output)
Run the following command to connect (let mysql-<version>-macos<version>-x86_64 be the folder where MySQL is installed. To grab the actual folder, run ls /usr/local/ and copy the folder name):
/usr/local/mysql-<version>-macos<version>-x86_64/bin/mysql -uroot -p<root-password>
If you are like me and all the information in previous answers failed, proceed to uninstall all versions of MySQL on your machine, search for all remaining MySQL files using the command sudo find / -name "mysql" and rm -rf every file or directory with the "mysql" name attached to it (you should skip files related to programming language libraries).
Now install a fresh version of MySQL and enjoy. NB: You will lose all your data so weigh your options first.
Sometimes a default password is set when you install it - as mentioned in the documentation. This can be confirmed by the following command.
sudo grep 'temporary password' /var/log/mysqld.log
It can happen if you don't have enough privileges.
Type su, enter the root password and try again.
After trying a lot with the following answer:
ALTER USER 'root'#'localhost' IDENTIFIED VIA mysql_native_password USING PASSWORD('root');
And similar answers, my terminal was still throwing me the following error:
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near...
So after researching on the web, this line solved my problem and let me change the root user password:
sudo mysqladmin --user=root password "[your password]"
windows :
cd \Ampps\mysql\bin :
mysql.exe -u root -pmysql
after mysql start (you can see shell like this mysql> )
use this query :
ALTER USER 'root'#'localhost' IDENTIFIED WITH mysql_native_password BY 'root';
try again access with root root
If you reached this page via Google like I did and none of the previous solutions worked, what turned out to be the error was 100% foolishness on my end. I didn't connect to the server. Once connected everything was smooth sailing.
In case it helps to know my setup, I'm using Sequel Pro and am trying to connect to it with Node using the NPM package, mysql. I didn't think I needed to actually connect (other than run Sequel Pro), because I was doing that from my application already.
I was getting the same error while setting up the mysql-8 zip version. Finally, switched to installer version which worked seamlessly. During installation, there is a prompt to set up the root password. Once set, it works for sure.
According to MariaDB official documentation, in MariaDB 10.4.3 and later, the unix_socket authentication plugin is installed by default.
In order to disable it, and revert to the previous mysql_native_password authentication method, add line below in [mysqld] section of my.cnf file:
[mysqld]
unix_socket=OFF
And then run:
mysql_install_db --auth-root-authentication-method=normal
And then start mysqld
This command will then work fine:
mysqladmin -u root password CHANGEME
For additional information, see Configuring mysql_install_db to Revert to the Previous Authentication Method.
I was trying to leverage Docker desktop on Mac to get 5.7.35 running and this docker-compose.yml configuration allowed it to work:
In particular it was the addition of the line...
command: --default-authentication-plugin=mysql_native_password
...that did the trick
version: '3.3'
services:
mysql_db:
image: mysql:5.7
command: --default-authentication-plugin=mysql_native_password
restart: always
environment:
MYSQL_ROOT_PASSWORD: 'your_password'
ports:
- '3306:3306'
expose:
- '3306'
volumes:
- ~/your/volume/path:/var/lib/mysql
One thing to check is the from-host filter. It may be "localhost" by default. Are you trying to connect from a remote client? Change this to "%".
On Arch Linux
Package: mysql 8.0.29-1
What worked for me:
Edit my.cnf file, normally can be found at /etc/mysql/my.cnf and append this skip-grant-tables at the bottom/end of the file.
Restart mysql service by invoking sudo systemctl restart mysqld
Ensuring mysql service has started properly by invoking sudo systemctl status mysqld
Login to mysql using 'root' by invoking mysql -u root -p
Flush privileges by invoking flush privileges;
Create new user by CREATE USER 'root'#'localhost' IDENTIFIED BY 'rootpassword';
(If you plan to use this db with PHP), you should instead use this CREATE USER 'root'#'localhost' IDENTIFIED WITH mysql_native_password BY 'rootpassword';
Check whether your changes have reflected in db by invoking the following in sequence:
use mysql;
SELECT User, password_last_changed FROM user;
Exit mysql console and comment/remove skip-grant-tables by editing my.cnf file (Refer to step 1 for the location)
Restart the mysql service (Refer to step 2 and step 3)
And that's all.
The '-p' argument doesn't expect a space between the argument name and value.
Instead of
./mysqladmin -u root -p 'redacted'
Use
./mysqladmin -u root -p'redacted'
Or just
./mysqladmin -u root -p
which will prompt you for a password.
Solution: Give up!
Hear me out. I spent about two whole days trying to make MySQL work to no avail, always stuck with permission errors, none of which were fixed by the answers to this question. It got to the point that I thought if I continued I'd go insane.
Out of patience for making it work, I sent the command to install SQLite, only using 450 KB, and it worked perfectly right from the word go.
If you don't have the patience of a saint, go with SQLite and save yourself a lot of time, effort, pain, and storage space..!

Access denied for user 'root'#'localhost' after installing phpmyadmin on ubuntu

I installed phpmyadmin on my ubuntu server following these steps here:
https://www.rosehosting.com/blog/install-phpmyadmin-on-ubuntu-16-04/
I installed apache, php, mysql following these steps:
https://vijayasankarn.wordpress.com/2017/01/17/setting-lamp-stack-in-ubuntu-16-04-using-aws-ec2/
and when I goto login to http://myserver.com/phpmyadmin I get this error:
mysqli_real_connect(): (HY000/1045): Access denied for user
'root'#'localhost' (using password: YES)
What am I doing wrong? Am I missing a step that is not in the links above?
I have tried this:
mysql> USE mysql;
mysql> UPDATE user SET plugin='mysql_native_password' WHERE User='root';
mysql> FLUSH PRIVILEGES;
mysql> exit;
$ service mysql restart
and this
mysql -u root -p
use mysql;
update user set plugin="" where user='root';
flush privilege;
This error can be fixed, the following command will reconfigure your phpMyAdmin credentials to gain access to the MYSQL DB:
sudo dpkg-reconfigure phpmyadmin
/!\ This will provide few interactive menus that will allow you to reconfigure the phpMyAdmin package with NEW credentials /!\
But you could also reconfigure it manually :
1 - Log into mysql as root
mysql -u root -p
2 - Make sure 'phpmyadmin' user exists :
SELECT User FROM mysql.user;
3 - Switch to the appropriate MYSQL DB :
use phpmyadmin;
4 - Set new password for the phpmyadmin user
UPDATE user SET password=PASSWORD('yourNewPassword') WHERE User='phpmyadmin';
sudo mysql -u root -p
USE mysql;
UPDATE user SET plugin='mysql_native_password' WHERE User ='root';
FLUSH PRIVILEGES;
exit;
sudo systemctl restart mysql
Check to see that you have actually set the root password in MySQL. You might find that when logged in as the root Linux user you can run
mysql -u root -p
and get access to your database no matter which password you specify, because the password hasn't actually been set. Use
ALTER USER 'root'#'localhost' IDENTIFIED BY 'PASSWORD'; FLUSH PRIVILEGES; EXIT;
to set your password, then try phpMyAdmin again.

SQLSTATE[HY000] [1698]

How can I overcome this error ?
Error: SQLSTATE[HY000] [1698] Access denied for user 'root'#'localhost' (SQL: select count(*) as aggregate from users
where email = email#gmail.com
I'm using Ubuntu
Please help
Login as root first:
$ sudo mysql -u root
Then CREATE or ALTER a non-root user (use '127.0.0.1' instead of 'localhost' if needed):
CREATE USER 'admin'#'localhost' IDENTIFIED BY 'adminspassword';
GRANT ALL PRIVILEGES ON *.* TO 'admin'#'localhost';
FLUSH PRIVILEGES;
Exit and restart:
exit
$sudo service mysql restart
$sudo service apache2 restart
And edit the .env file:
DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3630
DB_DATABASE=yourdbname
DB_USERNAME=admin
DB_PASSWORD=adminspassword
MySQL will make a difference between "localhost" and "127.0.0.1".
It might be possible that 'root'#'localhost' is not allowed because there is an entry in the user table that will only allow root login from 127.0.0.1.
This could also explain why some application on your server can connect to the database and some not because there are different ways of connecting to the database. And you currently do not allow it through "localhost".
I know its late however looking for answers, I couldn't find anything and at last I got this answer.
$sudo mysql -u root
[mysql] use mysql;
[mysql] update user set plugin='' where User='root';
[mysql] flush privileges;
[mysql] \q
Now you should be able to log in as root.
Thanks #Matematikisto in this thread
I encountered this problem in MySQL 8, Ubuntu 20. By default, the policy does not grant "GRANT" rights, including for root, but even after manipulations to obtain them, the application was able to gain access only after granting the mysql_native_password rights to the account. Maybe it will help someone:
UPDATE mysql.user SET authentication_string=CONCAT('*', UPPER(SHA1(UNHEX(SHA1('password'))))), plugin='mysql_native_password' WHERE User='root' AND Host='localhost';
the full sequence of steps to change the password and its format is here:
MySQL: How to reset or change the MySQL root password?

XAMPP phpmyadmin and MySQL databases not matching [duplicate]

Consider:
./mysqladmin -u root -p** '_redacted_'
Output (including typing the password):
Enter password:
mysqladmin: connect to server at 'localhost' failed error:
'Access denied for user 'root'#'localhost' (using password: YES)'
How can I fix this?
All solutions I found were much more complex than necessary and none worked for me. Here is the solution that solved my problem. There isn't any need to restart mysqld or start it with special privileges.
sudo mysql
-- for MySQL
ALTER USER 'root'#'localhost' IDENTIFIED WITH mysql_native_password BY 'root';
-- for MariaDB
ALTER USER 'root'#'localhost' IDENTIFIED VIA mysql_native_password USING PASSWORD('root');
With a single query we are changing the auth_plugin to mysql_native_password and setting the root password to root (feel free to change it in the query).
Now you should be able to log in with root. More information can be found in MySQL documentation or MariaDB documentation.
(Exit the MySQL console with Ctrl + D or by typing exit.)
Open and edit /etc/my.cnf or /etc/mysql/my.cnf, depending on your distribution.
Add skip-grant-tables under [mysqld]
Restart MySQL
You should be able to log in to MySQL now using the below command mysql -u root -p
Run mysql> flush privileges;
Set new password by ALTER USER 'root'#'localhost' IDENTIFIED BY 'NewPassword';
Go back to /etc/my.cnf and remove/comment skip-grant-tables
Restart MySQL
Now you will be able to login with the new password mysql -u root -p
None of the previous answers helped me with this problem, so here's the solution I found.
The relevant part:
In Ubuntu systems running MySQL 5.7 (and later versions), the root MySQL user is set to authenticate using the auth_socket plugin by default rather than with a password. This allows for some greater security and usability in many cases, but it can also complicate things when you need to allow an external program (e.g., phpMyAdmin) to access the user.
In order to use a password to connect to MySQL as root, you will need to switch its authentication method from auth_socket to mysql_native_password. To do this, open up the MySQL prompt from your terminal:
sudo mysql
Next, check which authentication method each of your MySQL user accounts use with the following command:
SELECT user,authentication_string,plugin,host FROM mysql.user;
Output
+------------------+-------------------------------------------+-----------------------+-----------+
| user | authentication_string | plugin | host |
+------------------+-------------------------------------------+-----------------------+-----------+
| root | | auth_socket | localhost |
| mysql.session | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | mysql_native_password | localhost |
| mysql.sys | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | mysql_native_password | localhost |
| debian-sys-maint | *CC744277A401A7D25BE1CA89AFF17BF607F876FF | mysql_native_password | localhost |
+------------------+-------------------------------------------+-----------------------+-----------+
4 rows in set (0.00 sec)
In this example, you can see that the root user does in fact authenticate using the auth_socket plugin. To configure the root account to authenticate with a password, run the following ALTER USER command. Be sure to change password to a strong password of your choosing, and note that this command will change the root password you set in Step 2:
ALTER USER 'root'#'localhost' IDENTIFIED WITH mysql_native_password BY 'password';
Then, run FLUSH PRIVILEGES which tells the server to reload the grant tables and put your new changes into effect:
FLUSH PRIVILEGES;
Check the authentication methods employed by each of your users again to confirm that root no longer authenticates using the auth_socket plugin:
SELECT user,authentication_string,plugin,host FROM mysql.user;
Output
+------------------+-------------------------------------------+-----------------------+-----------+
| user | authentication_string | plugin | host |
+------------------+-------------------------------------------+-----------------------+-----------+
| root | *3636DACC8616D997782ADD0839F92C1571D6D78F | mysql_native_password | localhost |
| mysql.session | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | mysql_native_password | localhost |
| mysql.sys | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | mysql_native_password | localhost |
| debian-sys-maint | *CC744277A401A7D25BE1CA89AFF17BF607F876FF | mysql_native_password | localhost |
+------------------+-------------------------------------------+-----------------------+-----------+
4 rows in set (0.00 sec)
You can see in this example output that the root MySQL user now authenticates using a password. Once you confirm this on your own server, you can exit the MySQL shell:
exit
I tried many steps to get this issue corrected. There are so many sources for possible solutions to this issue that is is hard to filter out the sense from the nonsense. I finally found a good solution here:
Step 1: Identify the database version
mysql --version
You'll see some output like this with MySQL:
mysql Ver 14.14 Distrib 5.7.16, for Linux (x86_64) using EditLine wrapper
Or output like this for MariaDB:
mysql Ver 15.1 Distrib 5.5.52-MariaDB, for Linux (x86_64) using readline 5.1
Make note of which database and which version you're running, as you'll use them later. Next, you need to stop the database so you can access it manually.
Step 2: Stopping the database server
To change the root password, you have to shut down the database server beforehand.
You can do that for MySQL with:
sudo systemctl stop mysql
And for MariaDB with:
sudo systemctl stop mariadb
Step 3: Restarting the database server without permission checking
If you run MySQL and MariaDB without loading information about user privileges, it will allow you to access the database command line with root privileges without providing a password. This will allow you to gain access to the database without knowing it.
To do this, you need to stop the database from loading the grant tables, which store user privilege information. Because this is a bit of a security risk, you should also skip networking as well to prevent other clients from connecting.
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.
Now you can connect to the database as the root user, which should not ask for a password.
mysql -u root
You'll immediately see a database shell prompt instead.
MySQL Prompt
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
MariaDB Prompt
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]>
Now that you have root access, you can change the root password.
Step 4: Changing the root password
mysql> FLUSH PRIVILEGES;
Now we can actually change the root password.
For MySQL 5.7.6 and newer as well as MariaDB 10.1.20 and newer, use the following command:
mysql> ALTER USER 'root'#'localhost' IDENTIFIED BY 'new_password';
For MySQL 5.7.5 and older as well as MariaDB 10.1.20 and older, use:
mysql> SET PASSWORD FOR 'root'#'localhost' = PASSWORD('new_password');
Make sure to replace new_password with your new password of choice.
Note: If the ALTER USER command doesn't work, it's usually indicative of a bigger problem. However, you can try UPDATE ... SET to reset the root password instead.
[IMPORTANT] This is the specific line that fixed my particular issue:
mysql> UPDATE mysql.user SET authentication_string = PASSWORD('new_password') WHERE User = 'root' AND Host = 'localhost';
Remember to reload the grant tables after this.
In either case, you should see confirmation that the command has been successfully executed.
Query OK, 0 rows affected (0.00 sec)
The password has been changed, so you can now stop the manual instance of the database server and restart it as it was before.
Step 5: Restart the Database Server Normally
The tutorial goes into some further steps to restart the database, but the only piece I used was this:
For MySQL, use:
sudo systemctl start mysql
For MariaDB, use:
sudo systemctl start mariadb
Now you can confirm that the new password has been applied correctly by running:
mysql -u root -p
The command should now prompt for the newly assigned password. Enter it, and you should gain access to the database prompt as expected.
Conclusion
You now have administrative access to the MySQL or MariaDB server restored. Make sure the new root password you choose is strong and secure and keep it in safe place.
After trying all others answers, this it what finally worked for me:
sudo mysql -- It does not ask me for any password
-- Then in MariaDB/MySQL console:
update mysql.user set plugin = 'mysql_native_password' where User='root';
FLUSH PRIVILEGES;
exit;
I found the answer in the blog post Solved: Error “Access denied for user ‘root’#’localhost’” of MySQL — codementor.tech (Medium).
For Ubuntu/Debian users
(It may work on other distributions, especially Debian-based ones.)
Run the following to connect as root (without any password)
sudo /usr/bin/mysql --defaults-file=/etc/mysql/debian.cnf
If you don't want to add --defaults-file each time you want to connect as root, you can copy /etc/mysql/debian.cnf into your home directory:
sudo cp /etc/mysql/debian.cnf ~/.my.cnf
And then:
sudo mysql
In my experience, if you run without sudo it will not work. So make sure your command is;
sudo mysql -uroot -p
For new Linux users this could be a daunting task. Let me update this with MySQL 8 (the latest version available right now is 8.0.12 as on 2018-09-12)
Open "mysqld.cnf" configuration file at "/etc/mysql/mysql.conf.d/".
Add skip-grant-tables to the next line of [mysql] text and save.
Restart the MySQL service as "sudo service mysql restart". Now your MySQL is free of any authentication.
Connect to the MySQL client (also known as mysql-shell) as mysql -u root -p. There is no password to be keyed in as of now.
Run SQL command flush privileges;
Reset the password now as ALTER USER 'root'#'localhost' IDENTIFIED BY 'MyNewPassword';
Now let's get back to the normal state; remove that line "skip-grant-tables" from "mysqld.cnf" and restart the service.
That's it.
In my case under Debian 10, the error
ERROR 1698 (28000): Access denied for user 'root'#'localhost'
was solved by (good way)
sudo mysql -u root -p mysql
Bad way:
mysql -u root -p mysql
I did this to set my root password in the initial set up of MySQL in OS X. Open a terminal.
sudo sh -c 'echo /usr/local/mysql/bin > /etc/paths.d/mysql'
Close the terminal and open a new terminal.
And the following worked in Linux, to set the root password.
sudo /usr/local/mysql/support-files/mysql.server stop
sudo mysqld_safe --skip-grant-tables
(sudo mysqld_safe --skip-grant-tables: This did not work for me the first time. But on the second try, it was a success.)
Then log into MySQL:
mysql -u root
FLUSH PRIVILEGES;
Now change the password:
ALTER USER 'root'#'localhost' IDENTIFIED BY 'newpassword';
Restart MySQL:
sudo /usr/local/mysql/support-files/mysql.server stop
sudo /usr/local/mysql/support-files/mysql.server start
My Station here:
UBUNTU 21.04
PHP 5.6.40-57
MYSQL 5.7.37
let's config it
nano /etc/mysql/mysql.conf.d/mysqld.cnf
at the bottom, write this
skip-grant-tables
reload it
service mysql restart
In your MySQL Workbench, you can go to the left sidebar, under Management select "Users and Privileges", click root under User Accounts, in the right section click tab "Account Limits" to increase the maximum queries, updates, etc., and then click tab "Administrative Roles" and check the boxes to give the account access.
Ugh - nothing worked for me! I have a CentOS 7.4 machine running MariaDB 5.5.64.
I had to do this, right after installation of MariaDB from YUM;
systemctl restart mariadb
mysql_secure_installation
The mysql_secure_installation will take you through a number of steps, including "Set root password? [Y/n]". Just say "y" and give it a password. Answer the other questions as you wish.
Then you can get in with your password, using
mysql -u root -p
It will survive
systemctl restart mariadb
The Key
Then, I checked the /bin/mysql_secure_installation source code to find out how it was magically able to change the root password and none of the other answers here could. The import bit is:
do_query "UPDATE mysql.user SET Password=PASSWORD('$esc_pass') WHERE User='root';"
...It says SET Password=... and not SET authentication_string = PASSWORD.... So, the proper procedure for this version (5.5.64) is:
Log in using mysql -u root -p, using the password you already set.
Or, stop the database and start it with:
mysql_safe --skip-grant-tables --skip-networking &
From the mysql> prompt:
use mysql;
select host,user,password from user where user = 'root';
(observe your existing passwords for root).
UPDATE mysql.user set Password = PASSWORD('your_new_cleartext_password') where user = 'root' AND host = 'localhost';
select host,user,password from user where user = 'root';
flush privileges;
quit;
Kill the running mysqld_safe. Restart MariaDB. Log in as root: mysql -u -p. Use your new password.
If you want, you can set all the root passwords at once. I think this is wise:
mysql -u root -p
(login)
use mysql;
select host,user,password from user where user = 'root';
UPDATE mysql.user set Password = PASSWORD('your_new_cleartext_password') where user = 'root';
select host,user,password from user where user = 'root';
flush privileges;
quit;
This will perform updates on all the root passwords: i.e., for "localhost", "127.0.0.1", and "::1"
In the future, when I go to RHEL 8 or what have you, I will try to remember to check the /bin/mysql_secure_installation and see how the guys did it, who were the ones that configured MariaDB for this OS.
Use sudo to alter your password:
sudo mysql
ALTER USER 'root'#'localhost' IDENTIFIED WITH mysql_native_password BY 'insert_password';
Source: Phoenixnap - Access denied for user root localhost
Fix for macOS
Install MySQL from https://downloads.mysql.com/archives/community/ (8.x is the latest as on date, but ensure that the version is compatible with the macOS version)
Give password for root (let <root-password> be the password) during installation (don't forget to remember the password!)
Select Use Legacy Password Encryption option (that is what I had used and did not try for Use Strong Password Encryption option)
Search and open MySQL.prefPane (use search tool)
Select Configuration tab
Click Select option of Configuration File
Select /private/etc/my.cnf
From terminal open a new or existing file with name /etc/my.cnf (vi /etc/my.cnf) add the following content:
[mysqld]
skip-grant-tables
Restart mysqld as follows:
ps aux | grep mysql
kill -9 <pid1> <pid2> ... (grab pids of all MySQL related processes)
mysqld gets restarted automatically
Verify that the option is set by running the following from terminal:
ps aux | grep mysql
> mysql/bin/mysqld ... --defaults-file=/private/etc/my.cnf ... (output)
Run the following command to connect (let mysql-<version>-macos<version>-x86_64 be the folder where MySQL is installed. To grab the actual folder, run ls /usr/local/ and copy the folder name):
/usr/local/mysql-<version>-macos<version>-x86_64/bin/mysql -uroot -p<root-password>
If you are like me and all the information in previous answers failed, proceed to uninstall all versions of MySQL on your machine, search for all remaining MySQL files using the command sudo find / -name "mysql" and rm -rf every file or directory with the "mysql" name attached to it (you should skip files related to programming language libraries).
Now install a fresh version of MySQL and enjoy. NB: You will lose all your data so weigh your options first.
Sometimes a default password is set when you install it - as mentioned in the documentation. This can be confirmed by the following command.
sudo grep 'temporary password' /var/log/mysqld.log
It can happen if you don't have enough privileges.
Type su, enter the root password and try again.
After trying a lot with the following answer:
ALTER USER 'root'#'localhost' IDENTIFIED VIA mysql_native_password USING PASSWORD('root');
And similar answers, my terminal was still throwing me the following error:
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near...
So after researching on the web, this line solved my problem and let me change the root user password:
sudo mysqladmin --user=root password "[your password]"
windows :
cd \Ampps\mysql\bin :
mysql.exe -u root -pmysql
after mysql start (you can see shell like this mysql> )
use this query :
ALTER USER 'root'#'localhost' IDENTIFIED WITH mysql_native_password BY 'root';
try again access with root root
If you reached this page via Google like I did and none of the previous solutions worked, what turned out to be the error was 100% foolishness on my end. I didn't connect to the server. Once connected everything was smooth sailing.
In case it helps to know my setup, I'm using Sequel Pro and am trying to connect to it with Node using the NPM package, mysql. I didn't think I needed to actually connect (other than run Sequel Pro), because I was doing that from my application already.
I was getting the same error while setting up the mysql-8 zip version. Finally, switched to installer version which worked seamlessly. During installation, there is a prompt to set up the root password. Once set, it works for sure.
According to MariaDB official documentation, in MariaDB 10.4.3 and later, the unix_socket authentication plugin is installed by default.
In order to disable it, and revert to the previous mysql_native_password authentication method, add line below in [mysqld] section of my.cnf file:
[mysqld]
unix_socket=OFF
And then run:
mysql_install_db --auth-root-authentication-method=normal
And then start mysqld
This command will then work fine:
mysqladmin -u root password CHANGEME
For additional information, see Configuring mysql_install_db to Revert to the Previous Authentication Method.
I was trying to leverage Docker desktop on Mac to get 5.7.35 running and this docker-compose.yml configuration allowed it to work:
In particular it was the addition of the line...
command: --default-authentication-plugin=mysql_native_password
...that did the trick
version: '3.3'
services:
mysql_db:
image: mysql:5.7
command: --default-authentication-plugin=mysql_native_password
restart: always
environment:
MYSQL_ROOT_PASSWORD: 'your_password'
ports:
- '3306:3306'
expose:
- '3306'
volumes:
- ~/your/volume/path:/var/lib/mysql
One thing to check is the from-host filter. It may be "localhost" by default. Are you trying to connect from a remote client? Change this to "%".
On Arch Linux
Package: mysql 8.0.29-1
What worked for me:
Edit my.cnf file, normally can be found at /etc/mysql/my.cnf and append this skip-grant-tables at the bottom/end of the file.
Restart mysql service by invoking sudo systemctl restart mysqld
Ensuring mysql service has started properly by invoking sudo systemctl status mysqld
Login to mysql using 'root' by invoking mysql -u root -p
Flush privileges by invoking flush privileges;
Create new user by CREATE USER 'root'#'localhost' IDENTIFIED BY 'rootpassword';
(If you plan to use this db with PHP), you should instead use this CREATE USER 'root'#'localhost' IDENTIFIED WITH mysql_native_password BY 'rootpassword';
Check whether your changes have reflected in db by invoking the following in sequence:
use mysql;
SELECT User, password_last_changed FROM user;
Exit mysql console and comment/remove skip-grant-tables by editing my.cnf file (Refer to step 1 for the location)
Restart the mysql service (Refer to step 2 and step 3)
And that's all.
The '-p' argument doesn't expect a space between the argument name and value.
Instead of
./mysqladmin -u root -p 'redacted'
Use
./mysqladmin -u root -p'redacted'
Or just
./mysqladmin -u root -p
which will prompt you for a password.
Solution: Give up!
Hear me out. I spent about two whole days trying to make MySQL work to no avail, always stuck with permission errors, none of which were fixed by the answers to this question. It got to the point that I thought if I continued I'd go insane.
Out of patience for making it work, I sent the command to install SQLite, only using 450 KB, and it worked perfectly right from the word go.
If you don't have the patience of a saint, go with SQLite and save yourself a lot of time, effort, pain, and storage space..!

'localhost' is not allowed to connect to this MySQL server

When I tried to connect to my http://localhost/phpmyadmin the following error appeared:
#1130 - Host 'localhost' is not allowed to connect to this MySQL server
I tried to connect to MySQL from command line and the same error appeared as well.
I used root as user, there are many suggestions that tell to do some scripts from MySQL, but I can't log in MySQL even though with root user. I have a root access but I can't log in because of that error, that error appears when I try to log in.
You could start MySQL with the --skip-grant-tables option
service mysqld stop
mysqld_safe --skip-grant-tables &
Then allow the user you're logging in as access from localhost:
grant all on <db>.<table> to '<user>'#'localhost' identified by password('<password>')
Then start MySQL normally:
service mysqld start
Replace all the <word> placeholders above with the actual settings. If you have root access you can just log in with the root user and run the grant line.

Categories