How to change MySQL root password via a file? - mysql

How do I change the MySQL root password in ubuntu server via a file?
The file may be any shell script or normal text file.
My try:
Normally the below command, in the terminal, changes the password for newly installed mysql.
mysqladmin -u root password 'newpass'
But while doing it via file it's showing the bellow error.
mysqladmin: connect to server at 'localhost' failed
error: 'Access denied for user 'root'#'localhost' (using password: NO)'
So can someone help me to do so.

Stop the MySQL Server: sudo /etc/init.d/mysql stop
Start the mysqld configuration: sudo mysqld --skip-grant-tables
Login to MySQL as root: mysql -u root mysql
Replace YOURNEWPASSWORD with your new password:
UPDATE
mysql.user
SET
Password = PASSWORD('YOURNEWPASSWORD')
WHERE
User = 'root';
FLUSH PRIVILEGES;
exit;
Note: This method is not regarded as the most secure way of resetting the password, however, it works.

If you have never assigned a root password for MySQL, the server does not require a password at all for connecting as root follow :
Resetting Permissions

Related

Can't connect to mysql, get this error: ERROR 1045 (28000): Access denied for user 'root'#'localhost' (using password: YES)

I enter mysql -u root -p and put in my password and get this error, I have uninstalled and reinstalled mysql and changing the password for root but still get this message.
I have looked and looked for solutions but nothing seems to work.
I am trying to enter mysql through my terminal, instead I get get the error above and can't get into mysql even after uninstalling and reinstalling it.
Usually this means that you are using the wrong credentials to connect, try to consult consult the general log file /var/log/mysqld.log to copy the initialization password.
if that doesn't work you can try this approach:
check if MySQL is installed and running on your system sudo service mysql status
Reset the root password:
stop the server: sudo /etc/init.d/mysql stop
start the server in safe mode: sudo mysqld_safe --skip-grant-tables &
try to connect: mysql -u root
reset the root password: UPDATE mysql.user SET authentication_string=PASSWORD('newpassword'), password_expired='N' WHERE User='root';
flush privileges: FLUSH PRIVILEGES;
quit the server: quit
stop the safe mode server: sudo /etc/init.d/mysql stop
now you can start the MySQL server again: sudo /etc/init.d/mysql start
Use the correct credentials to connect (with the new password)
check out previous answers to this problem here and here.

Bitnami server: Reset the MySQL root password

I don’t remember my MySQL root password used in 'Bitnami WordPress Stack' (wordpress-5.4.2-0) in Linuy-Mint and tried the steps described here:
https://docs.bitnami.com/installer/apps/wordpress/administration/change-reset-password/
explicitly:
nano installdir/mysql/tmp/mysql-init
and saved it with:
ALTER USER 'root'#'127.0.0.1' IDENTIFIED BY 'NEW_PASSWORD';
then i restarted the MySQL server via Bitname App installdir/manager-linux-x64.run
i expected to be able in:
http://127.0.0.1:8080/phpmyadmin/index.php
with user root and NEW_PASSWORD but get then this error-message:
mysqli::real_connect(): (HY000/1045): Access denied for user 'root'#'localhost' (using password: YES)
This way is different to suggestions on: Bitnami. reset mysql root pwd
i tried also:
~/wordpress-5.4.2-0/mysql$ service mysql reset-password
mysql: unrecognized service
~/wordpress-5.4.2-0/mysql$ sudo dpkg-reconfigure mysql-server-x.x
and get the same errors in phpmyadmin.
if i use:
sudo /usr/bin/mysqld_safe --skip-grant-tables & mysql -h localhost
it got
Command 'mysql' not found, but can be installed
but of course its installed.

How to reset root password when denied access to user in ubuntu ec2 instance

I have installed phpmyadmin on ubuntu ec2 instance. Phpmyadmin page is showing up in front page, but I am receiving error denying access to the root user. I had already set up password for the root during installation, but when I try to login using mysql -u root -p then I am getting error access denied for user 'root#localhost' (using password: YES). How can I setup root password so that I can login into phpmyadmin?
You can change the password of the root user as follows:
1.- Stop the Mysql service
service mysqld stop
O well:
/etc/init.d/mysqld stop
2.- Start the MySQL service in open mode with the following command:
mysqld_safe --skip-grant-tables --skip-networking &
3.- Once MySQL is started, you must access it with the user “root”. For this you can do it simply with:
mysql -u root
4.- Once inside MySQL, you will have to access the MySQL database, which is where you will have to change the password:
mysql> use MySQL;
5.- Within the MySQL database, you can launch the following query that will make the password change:
mysql> UPDATE user SET password = PASSWORD ('password') WHERE user = 'root';
6.- And when it finishes you will be able to leave the MySQL server with:
mysql> exit
7.- Finally you will have to restart the MySQL service so that it starts in normal mode:
service mysqld restart
O well:
/etc/init.d/mysqld restart
After completing the previous steps, you will have already modified the password of the MySQL “root” user.

Unable to login to MySQL

I installed MySQL using the following command:
sudo apt-get install mysql-server mysql-client mysql-common
It asks me for a root password (On package configuration), I enter one and confirm it.
sudo /etc/init.d/mysql start
However when I try logging in to mysql with user root and password which I supplied in the previous step, it denies me access:
root#mbilwebh02:/etc# > mysql --user root --password
Enter password:
ERROR 1045 (28000): Access denied for user 'root'#'localhost' (using password: YES).
Another attempt that I made was to uninstall and reinstall mysql without supplying a password but that fails as well.
I then switched the user on the server to mysql
root#mbilwebh02:/etc# sudo -u mysql -s
root#mbilwebh02:/etc# sudo -u mysql -s
bash: /root/.bashrc: Permission denied
mysql#mbilwebh02:/etc$ mysql -u root -p
Enter password:
ERROR 1045 (28000): Access denied for user 'root'#'localhost' (using password: YES)
At this point I am able to only start and stop mysql. Any ideas where I might be going wrong?
[EDIT]
After searching a lot I found that one can log in to mysql using debian-sys-maint as user and password which is in the debian.cnf file. There was no 'root' user in the mysql database hence everything I tried denied me access.
I do not know if this is a safe way to do it though.
Taken from: http://dev.mysql.com/doc/refman/5.0/en/resetting-permissions.html
Stop mysqld and restart it with the --skip-grant-tables option. This enables anyone to connect without a password and with all privileges. Because this is insecure, you might want to use --skip-grant-tables in conjunction with --skip-networking to prevent remote clients from connecting.
Connect to the mysqld server with this command:
shell> mysql
Issue the following statements in the mysql client. Replace the password with the password that you want to use.
mysql> UPDATE mysql.user SET Password=PASSWORD('MyNewPass')
-> WHERE User='root';
mysql> FLUSH PRIVILEGES;
The FLUSH statement tells the server to reload the grant tables into memory so that it notices the password change.
You should now be able to connect to the MySQL server as root using the new password. Stop the server, then restart it normally (without the --skip-grant-tables and --skip-networking options).

Can't Connect to LocalHost Mysql Server

I recently installed mysql server and client, but I can't connect to the server. I get this message:
Access denied for user 'root'#'localhost' (using password: YES/NO)
Whatever I do i will always get the same message.
I tried commenting bind-address but still the same thing.
If you set up a password for the root user during installation, then start the mysql client with the -p option, mysql -u root -p, and it should prompt you for a password.
If there's no password set, leave the -p off and it should log right in.
If you've somehow forgotten what the root password is, you can fix it by following these instructions... http://dev.mysql.com/doc/refman/5.0/en/resetting-permissions.html