Cannot set password for MySQL root user - mysql

I am trying to get MySQL running on my localhost. This never happened to me before, but upon installation sudo apt-get install mysql-server did not ask me for the initial password.
After searching for answers to "reset" my password and battling with directory permissions, i am currently at this state:
The mysqld_safe solution:
After rebooting my computer, mysql -u root -p will ask me for my password, and after i enter it, it will complain with ERROR 1698 (28000): Access denied for user 'root'#'localhost'
If i instead try mysql -u root (no password), it will say ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
If i run the advice that other people have posted about resetting the password via mysqld_safe --skip-grant-tables, then i run across some hiccups along the way:
First i run sudo service mysql stop
Then sudo mysql_safe --skip-grant-tables will say mysqld_safe Directory '/var/run/mysqld' for UNIX socket file don't exists.
I can fix that if i do sudo mkdir -p /var/run/mysqld and sudo chown mysql /var/run/mysqld
After this, running mysqld_safe will run with the message Starting mysqld daemon with databases from /var/lib/mysql and then hang. And Ctrl+C doesn't get me out of that.
In a new terminal window i can now actually connect with mysql -u root and get the mysql> prompt.
I can set a new password with use mysql; update user set authentication_string=password('0000') where user='root';flush privileges; which will tell me it successfully updated the row with the message Rows matched: 1 Changed: 1 Warnings: 1
The warning only states: 'PASSWORD' is deprecated and will be removed in a future release.
And after all this, i'm back at square one: The process still hangs, so i have to reboot because killall mysqld_safe says no process found. After rebooting and entering my new password 0000, i will get access denied.
The system is Linux 4.15.0-48-generic #51-Ubuntu SMP.
The mysqladmin solution
If i run mysqladmin -u root password '0000' (without sudo), i get:
mysqladmin: connect to server at 'localhost' failed
error: 'Access denied for user 'root'#'localhost''
If i run it with sudo, i get:
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.
This last one doesn't really tell me anything in regards of whether the password change happened or not; has it been refused or not (since it's a warning, not an error, i would assume it should go through), but in either case, trying to connect with 0000 still says "access denied".
Any advice on how to fix this is greatly appreciated.

I didn't quite solve the problems with mysql directly; However for myself, the acceptable solution was switching to a different database software, but it's still a MySQL system, and it got me up and running. So if anyone gets stuck on existing solutions not working like i did, maybe this comes in handy to get you on the road and finally writing code again.
What i did was:
Purged mysql off my system
Manually deleted /etc/mysql and /var/lib/mysql (don't know if this step was necessary)
apt clean, apt autoremove, apt update
Installed mariadb
Followed these instructions to rid myself of the timeout bug
Used the mysqld_safe --skip-grant-tables method to create a new user with all privileges, since connecting with root always kept failing
PHP + MariaDB now working again.

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 "safely" terminate mysqld?

I am following mysql on Cygwin.
# To begin MySQL setup run the following:
mysql_install_db
# Run mysql - you'll get a firewall alert from windows if you have it active.
mysqld_safe &
# Immediately following that, it would be wise to run the following:
mysql_secure_installation
During the mysql_install_db, I was asked who the database (DB) would be for. I specified a non-administrator account nonadmin.
For the mysql_secure_installation process, I followed the similarly named MariaDB page. In contrast to the description, I was told that I had no root password, so I set it to the same as my cygwin and windows administrator account.
I lost track of whether which account was used for each of the three commands above, but here is what I remember:
mysql_install_db: Ran as nonadmin, but prompted for admin password
mysqld_safe: Run as nonadmin
mysql_secure_installation: Attempted as nonadmin, abandoned, and
completed as administrator account admin
It seems that I should restart the server after the final step. Using ps -ef, I noted that process mysqld belongs to nonadmin. Using nonadmin, I attempted the mysqladmin shutdown command at mysql on Cygwin. I get:
mysqladmin: connect to server at 'localhost' failed
error: 'Access denied for user 'nonadmin'#'localhost' (using password: NO)'
I tried the variation of the command that prompts the user for a password: mysqladmin -p shutdown. I did this twice, entering the cygwin/windows passwords for nonadmin and admin. Both times, I get:
mysqladmin: connect to server at 'localhost' failed
error: 'Access denied for user 'nonadmin'#'localhost' (using password: YES)'
This totalled 3 attempts. I then switched to the admin account and retried all 3 attempts. I got the above messages, but with the username admin:
mysqladmin: connect to server at 'localhost' failed
error: 'Access denied for user 'admin'#'localhost' (using password: NO)'
mysqladmin: connect to server at 'localhost' failed
error: 'Access denied for user 'admin'#'localhost' (using password: YES)'
So no joy connecting to the daemon to shut it down. As an alternative, I want to avoid clobbering mysqld with kill -9 because I like things to terminate cleanly (though I will probably do so soon).
As another alternative, I've come across references to /etc/init.d, but I have no such file.
Can anyone please explain why I can't connect to the daemon to shut it down?
Also, I'm new to MySQL, so if possible, it'd be nice to find a way back to the process at mysql on Cygwin rather than deviating from it too much.
Thanks.
What I ended up doing was kill -9 on all mysql looking processes shown by ps -ef. I then deleted /var/lib/mysql -- and accidentally deleted all of /var/lib/*. Subsequently, therefore, I reinstalled all of my Cygwin packages, thus starting from a clean slate.
I also found the following more detailed process at this alaya page for installing a MySQL/MariaDB database on Cygwin, after which I was able to shut down the MySQL server.
from the Administrator terminal run:
mysql_install_db
Start the mysql server:
mysqld_safe &
then run:
mysql_secure_installation
You should then be able to log in to MariaDB from a non-administrative shell with:
mysql -u root -p
The initial mysql_install_db step generates the following useful looking information, which I'm capturing here because I'm not sure if there is a way to get it again.
Installing MariaDB/MySQL system tables in '/var/lib/mysql' ...
2019-05-01 23:00:06 0 [Warning] InnoDB: innodb_open_files 300 should not be greaterthan the open_files_limit 256
OK
To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system
PLEASE REMEMBER TO SET A PASSWORD FOR THE MariaDB root USER !
To do so, start the server, then issue the following commands:
'/usr/bin/mysqladmin' -u root password 'new-password'
'/usr/bin/mysqladmin' -u root -h fmaPC password 'new-password'
Alternatively you can run:
'/usr/bin/mysql_secure_installation'
which will also give you the option of removing the test
databases and anonymous user created by default. This is
strongly recommended for production servers.
See the MariaDB Knowledgebase at http://mariadb.com/kb or the
MySQL manual for more instructions.
You can start the MariaDB daemon with:
cd '/usr' ; /usr/bin/mysqld_safe --datadir='/var/lib/mysql'
You can test the MariaDB daemon with mysql-test-run.pl
cd '/usr/mysql-test' ; perl mysql-test-run.pl
Please report any problems at http://mariadb.org/jira
The latest information about MariaDB is available at http://mariadb.org/.
You can find additional information about the MySQL part at:
http://dev.mysql.com
Consider joining MariaDB's strong and vibrant community:
https://mariadb.org/get-involved/
During the mysql_secure_installation step, I followed the recommendation to set a root password. Instead of the mysql -u root -p step, then, I could instead issue the following to stop the MySQL server:
mysqladmin -u root -p shutdown
I made an important discovery made about this process from the point of view of a user without administrator rights. Contrary to what is suggested on the alaya page cited above, the above process does not have to be done from an administrator account. Importantly, the user accounts with which one can connect to the MySQL server has nothing to do with the user accounts on the host system (Windows or *nix) (a fact that I got from perusing MariaDB knowledgebase). I suspect that any user on the host system can connect to any user account on the server via the socket that it presents [1], with the right password.
I also found that the above process works on a machine that has very restrictive Windows firewall policies. Even if the server (the mysqld_safe & step) and client (the mysql -u root -p step) are started from the same non-administrative account, the Windows firewall will issue a warning about curtailing some features. I suspect that the trigger is the creation of a socket for connecting to the server. Despite the warning, however, ps -ef shows the server to be running. Therefore, I suspect that the firewall restriction consists of allowing only loop-back connections. The mysql_secure_installation step apparently disallows external connections, and I found that after this step, stopping and relaunching the server no longer triggers the firewall warning. So the effect of mysql_secure_installation seems to persist into subsequent sqld launches, at least when they are started via mysqld_safe.
[1] "Present" might not be the right terminology here, as I'm more of an analyst rather than a developer or administrator.

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/

Mysql 5.7.17 root password forgotten / no access to mysql command line on Mac

I downloaded MySQL 5.7.17 for Mac and installed it. I can start and stop the SQL server from my Preferences > MySQL. It is shown as an instance on port 3306 from Workbench so all that seems fine. However, during install toward the end a pop-up window listed a password I was supposed to use. I assumed it would be promptly needed so copied on the clipboard. Few mins later having not been prompted for it yet, I copied something else in the clipboard thus losing it.
I have been trying to get some sort of client of mysql command line so I can enter sql commands, however there is no "Application" installed to use to access the server. I have scoured the MySQL documentation and Stack and beyond, and searched for how to get the password reset, and a mysql client/command line, but all the articles seem to assume you already have access to a "MySQL terminal"/"command line". I only have access to my normal Mac Terminal, and most commands in the normal Terminal window either fail or require the password I don't have, for example:
$ mysqld --skip-grant-tables
-bash: mysqld: command not found
$ mysql -u root -p
-bash: mysql: command not found
$ sudo sh -c 'echo /usr/local/mysql/bin > /etc/paths.d/mysql'
Password: (this is probably the password I was given that got overwritten? When I try the admin password of the machine it sends me back to the prompt)
$ sudo mysqld_safe --skip-grant-tables (after I'd stopped the server)
sudo: mysqld_safe: command not found
Can somebody please help me figure out how to get to a "MySql Terminal" and if I need this password that got overwritten, how to reset it? (it seems like uninstalling MySQL to repeat the steps and this time take better care of the password is actually horrendously difficult / no uninstall protocol?).
Thank you so very much.
--- Update: I used the How To Uninstall suggested by Josh M and then re-installed. This time I saved the password (so far, not sure how / where I need to use it). However, I must really be missing something b/c I still can't get access to mysql.
/usr/local/mysql/bin/mysql
ERROR 1045 (28000): Access denied for user 'Robert'#'localhost' (using password: NO)
Then I did $ export PATH=$PATH:/usr/local/mysql/bin but I still get ERROR 1045.
Try these set of commands
$ sudo /usr/local/mysql/support-files/mysql.server stop
$ sudo /usr/local/mysql/support-files/mysql.server start --skip-grant-tables
/usr/local/mysql/bin/mysql
FLUSH PRIVILEGES;
ALTER USER 'root'#'localhost' IDENTIFIED BY 'YOUR_NEW_PASSWORD';
EXIT;
$ sudo /usr/local/mysql/support-files/mysql.server stop
$ sudo /usr/local/mysql/support-files/mysql.server start
$ mysql -u root -p
$ Enter Password: 👉🏻 ENTER_YOUR_NEW_PASSWORD_HERE 👈🏻
And enjoy 🎉
There's a tutorial for resetting passwords here: https://www.howtoforge.com/setting-changing-resetting-mysql-root-passwords
Your MySQL command line interface (cli) is probably at:
/usr/local/mysql/bin/mysql
Honestly it might be better to reinstall. There's an SO on that: How do you uninstall MySQL from Mac OS X?

Cannot log in to mysql after default install with Macports

I've installed mysql5 using Macports and the installation appears to check out but I cannot login to the server at all.
This is what I did:
sudo port install mysql5-server
This builds and installs fine.
sudo /opt/local/lib/mysql5/bin/mysql_install_db --user=mysql
This runs fine as well. It outputs the following:
Installing MySQL system tables...
OK
Filling help tables...
OK
To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system
PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:
/opt/local/lib/mysql5/bin/mysqladmin -u root password 'new-password'
/opt/local/lib/mysql5/bin/mysqladmin -u root -h LeoMacBook.local password 'new-password'
Alternatively you can run:
/opt/local/lib/mysql5/bin/mysql_secure_installation
which will also give you the option of removing the test
databases and anonymous user created by default. This is
strongly recommended for production servers.
See the manual for more instructions.
You can start the MySQL daemon with:
cd /opt/local ; /opt/local/lib/mysql5/bin/mysqld_safe &
You can test the MySQL daemon with mysql-test-run.pl
cd /opt/local/mysql-test ; perl mysql-test-run.pl
Please report any problems with the /opt/local/lib/mysql5/bin/mysqlbug script!
Now...let's follow the instructions exactly. I start the server:
sudo /opt/local/lib/mysql5/bin/mysqld_safe &
Server is running fine.
When I try to change the root password, I CANNOT log in.
LeoMacBook:bin leonardteo$ /opt/local/lib/mysql5/bin/mysqladmin -u root password 'new-password'
/opt/local/lib/mysql5/bin/mysqladmin: connect to server at 'localhost' failed
error: 'Access denied for user 'root'#'localhost' (using password: NO)'
I'm at wits end.
I've started the server with --skip-grant-tables. With this, I can load up mysql fine from the command line and I'm connected to the server. When I run SELECT * FROM mysql.user;, it returns an empty set! With this, I've tried to create a new user by invoking the command:
CREATE USER 'root'#'localhost' IDENTIFIED BY 'root';
But this doesn't work. I get the error:
ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement
I cannot get this working. It seems I am so close, yet the root user seems to be missing.
Any ideas?
Leonard
Once you've started up mysql with --skip-grant-tables, issue a flush privileges; query. This'll re-enable the permissions system and allow you to run the usual grant and create user queries.