Resetting mysql root password - mysql

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

Related

Cannot login to MySQL using sudo as root

I'm having a weird problem where I cannot login to my MySQL server with sudo as root on a ubuntu 19.10 server. I want to be able to reset my root password.
The error message I got was:
ERROR 1045 (28000): Access denied for user 'root'#'localhost' (using password: NO)
Anyone had similar problems? Thanks!
Ok - so I solved this problem myself.
The correct way of resetting the root password for mysql is to:
Stop all mysql server sessions.
Start mysqld manually using - this basically tells MySQL to skip any credentials check and allow anyone to login, hence you want to skip-networking to avoid network users.
sudo mysqld --skip-grant-tables --skip-networking
Use this to login as root:
sudo mysql -u root
In the mysql prompt, do:
FLUSH PRIVILEGES;
SET PASSWORD FOR root#'localhost' = PASSWORD('password');
FLUSH PRIVILEGES;
Stop mysqld and restart mysql normally (you may need to kill the mysqld process).
In my case, there was some problems with the installation and the /var/run/mysqld folder was not correctly set up. It may be a bug with MySQL and it was not properly reported.
After creating the folder with the correct setmod (user:group is mysql:mysql) mysqld runs without any problem.

how to reset my mysql password in mac os 10.13.3

I have a problem with MySQL. I forgot the password I used when I installed it
so, I can not access to the server now.
I tried deleting the MySQL and install it again but it didn't show the password again.
So I tried to do it by the terminal and this is the result ...
first i stopped the MySQL server
then i put sudo /usr/local/mysql/bin/mysqld_safe –skip-grant-tables in the terminal
after that in new terminal window i wrote sudo /usr/local/mysql/bin/mysql -u root
UPDATE mysql.user SET Password=PASSWORD('root') WHERE User='root';
FLUSH PRIVILEGES;
\q
the result was "ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)"
these are all the commands
Try this command. I believe you should have mysql running. If that doesn't work try with mysql stopped.
sudo mysql_secure_installation
Hopefully should get you to prompt a password change.
Also, for the socket error, you can try following this link.
Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
You can try reseting the root password by running MySQL in Safe Mode.
Here are the steps:
Stop MySQL:
sudo /usr/local/mysql/support-files/mysql.server stop
Start it in safe mode:
sudo mysqld_safe --skip-grant-tables
This will be an ongoing command until the process is finished so open another shell/terminal window, and..
Log in without a password as root:
mysql -u root
Update root (and any other user's) password)
FLUSH PRIVILEGES;
ALTER USER 'root'#'localhost' IDENTIFIED BY 'MyNewPass';
\q
Restart MySQL in normal mode
sudo /usr/local/mysql/support-files/mysql.server start
Reference: https://coolestguidesontheplanet.com/how-to-change-the-mysql-root-password/
Note: this is pretty standard reset procedure, but just documented better in the above guide compared to mysql reference docs.

Resetting mysql server root password on OS X

First of all, I know there are several threads, but I have tried so many solutions and I cant get anything to work.
I dont have any experience with mysql server and Terminal.
I downloaded mysql server 5.7.19
Following the answer from redtek, here: Setting the MySQL root user password on OS X
I open mysql from system setting, click stop server. Then I open the terminal and write
sudo mysqld_safe --skip-grant-tables
I asks me for my password (I assume this is the same when I start my computer). I get a message that command not found.
MacBook-Pro:~ XXXXXX$ sudo mysqld_safe --skip-grant-tables
Password:
sudo: mysqld_safe: command not found
MacBook-Pro:~ XXXXXX$
UPDATE: When I run the solution below, after opening a new window I get the following errors:
Last login: Sun Aug 13 16:51:49 on ttys002
MacBook-Pro:~ XXXXX$ mysql -u root
-bash: mysql: command not found
MacBook-Pro:~ XXXXX$ UPDATE mysql.user SET Password=PASSWORD('my-new-password') WHERE User='root';
-bash: syntax error near unexpected token `('
MacBook-Pro:~ XXXXX$ FLUSH PRIVILEGES;
-bash: FLUSH: command not found
MacBook-Pro:~ XXXXX$ \q
Stop the MySQL server.
sudo /usr/local/mysql/support-files/mysql.server stop
Restart it with the --skip-grant-tables option.
sudo /usr/local/mysql/bin/mysqld_safe --skip-grant-tables
Open another terminal to connect to the MySQL server using the mysql client.
cd /usr/local/mysql/bin
mysql
Tell the server to reload the grant tables so that account-management statements work.
FLUSH PRIVILEGES;
Now reset the password for root user
MySQL 5.7.6 and later:
ALTER USER 'root'#'localhost' IDENTIFIED BY 'MyNewPass';
MySQL 5.7.5 and earlier:
SET PASSWORD FOR 'root'#'localhost' = PASSWORD('MyNewPass');
Stop the server and restart it normally
sudo /usr/local/mysql/support-files/mysql.server stop
sudo /usr/local/mysql/support-files/mysql.server start
First step is to stop MySQL service.
sudo /usr/local/mysql/support-files/mysql.server stop
Then you need to start it in safe mode
sudo /usr/local/mysql/bin/mysqld_safe --skip-grant-tables
secondly: let's open another shell/terminal window, log in with no password
mysql -u root
UPDATE mysql.user SET Password=PASSWORD('my-new-password') WHERE User='root';
FLUSH PRIVILEGES;
\q
Because in MySQL 5.7, the password field in mysql.user table is removed, now the field name is 'authentication_string'.
mysql -u root
UPDATE mysql.user SET authentication_string=PASSWORD('my-new-password') WHERE User='root';
FLUSH PRIVILEGES;
\q
Now again yu need to start the MySQL server
sudo /usr/local/mysql/support-files/mysql.server start
The command is not found because MySQL installation folder ( /usr/local/mysql/ ) is not included in the system variable PATH.
You can add to PATH
OR you can use full path /usr/local/mysql/bin/mysqld_safe
It took me a while in resolving this, considering most solutions around are for versions lower than MySQL version 5.7
Follow this below and it could help get you sorted as well.
For Safely ensuring process:
- Turn off the tick on "Automatically Start MySQL Server on Startup" inside System Preferences of MySQL (spotlight - mysql)
Open Terminal and type sudo /usr/local/mysql/support-files/mysql.server start -PS: This is for ensuring its in-line, times were that the next processes were breaking on me.
Now shut the MySQL service: sudo /usr/local/mysql/support-files/mysql.server stop
Type sudo mysqld_safe --skip-grant-tables
This will have now bypassed the security for MySQL - not safe for operations and not a permanent solution to always allow you to use MySQL.
Currently, as you would see, its in a process... This will allow us to do following steps. Leave this tab of Terminal OPEN throughout remaining process!!
Now Cmd+N (new terminal window), and in the new terminal:
- sudo /usr/local/mysql/support-files/mysql.server start
- update user set authentication_string=password(‘jj’) where user='root'
This on older version would have been as update user set password=PASSWORD(“jj”) where user='root’;
- FLUSH PRIVILEGES; //This is essential (updates disk instead of cache) to ensuring the next time around when you close mysql and get back it stays accessible as you setup.
- \q or quit
Close it all down - All terminals, give your computer a restart, and ensure everything is in order (ofcourse this entails - restart - open terminal - mysql -u root -p (enter) - respond with password you gave on steps above).
In my answer: jj was the password set
Cool-Stuff for General knowledge of fairly new (this somehow immediately worked for me after saying Password is not a field or something of sorts, on going in this new Terminal at update user set authentication_string=password(‘jj’) where user='root', so if you had the same, go at it in following steps - in >mysql itself where you are..):
- use mysql;
- show tables;
- describe user;
and then continue as steps above from the point of update user set authentication_string=password(‘jj’) where user='root'

Unable to reset the MySQL root password on a Mac

I just reinstalled MySQL using brew on my mac.
As far as I can see everything works fine, but I cannot change my root password.
When trying I'm getting this error message:
mysql -u root
ERROR 1045 (28000): Access denied for user 'root'#'localhost' (using password: NO)
The common way to solve this seems to use (several answers on stack overflow suggest using this):
sudo mysqld_safe --skip-grant-tables
2017-05-19T11:54:35.6NZ mysqld_safe Logging to '/usr/local/var/mysql/mac.err'.
2017-05-19T11:54:35.6NZ mysqld_safe Starting mysqld daemon with databases from /usr/local/var/mysql
2017-05-19T11:54:35.6NZ mysqld_safe mysqld from pid file /usr/local/var/mysql/mac.pid ended
I stopped the server before executing the command. But this solution does not work for me.
There is also no entry in the error log:
/usr/local/var/mysql/mac.err
I read also about using mysql_secure_installation but there I get just the same error message as when using mysql -u root.
As I mentioned above, I tried already to just uninstall and reinstall it again, but obviously some data remained. So is there maybe any way to manually delete or reset these files?
Solution by OP.
It turned out that:
mysql.server start --skip-grant-tables
(not sure if sudo is necessary)
works for me.
The access denied is because you're not specifying the -p switch
If you know the old password then maybe login to MySql and change it as:
1).login to MySql 2).change to the correct db 3) change the passwd and flush
mysql -u root -p
mysql> use mysql;
mysql> update user set password=PASSWORD("newpass") where User='ENTER-USER-NAME-HERE';
mysql> flush privileges;
mysql> quit
Also, the following works in Linux/Unix systems but i haven't tested it on Mac (may work if you have the clients installed)
mysqladmin -u root -p oldpassword newpass
Reference: https://dev.mysql.com/doc/refman/5.7/en/resetting-permissions.html
//If you don't remember the password, to recover it check this out(bottom).
https://www.howtoforge.com/setting-changing-resetting-mysql-root-passwords
https://coolestguidesontheplanet.com/how-to-change-the-mysql-root-password/

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