Unable to login to MySQL - 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).

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.

Unable to log into mysql using password - Ubuntu

I installed mysql in Ubuntu 20.04.
Problem -
Case 1: I have default root user. I can login using sudo mysql but cannot login using mysql -u root -p.
Case 2: I created another user 'local'. I can login using mysql -u local -p and by giving password I set. But problem is if I give wrong password, it still logs me in. I don't know why.
I followed this ERROR 1698 (28000): Access denied for user 'root'#'localhost'
but this didn't help me. I followed other topics too like creating new user for mysql etc.
I cannot access from workbench or from python script too.
Error : ERROR 1698 (28000): Access denied for user 'root'#'localhost'
Login to mysql using sudo mysql -u root
Give your su password and it will login without mysql password.
Run following commands:
USE mysql;
UPDATE user SET plugin='mysql_native_password' WHERE User='root';
FLUSH PRIVILEGES;
exit;
Restart:
sudo systemctl restart mysql.service
After that, run commands to secure MySQL server and create a new root password.
sudo mysql_secure_installation
When prompted, answer the following questions :
New password: Enter password
Re-enter new password:
Repeat password
Remove anonymous users? [Y/n]: Y
Disallow root login remotely? [Y/n]: Y
Remove test database and access to it? [Y/n]: Y
Reload privilege tables now? [Y/n]: Y
Next time, type following command in terminal to login:
sudo mysql -u root -p
Then type mysql password you set recently to sign in.
Good Luck, this worked for after continuous wasting several hours on this thing and following tens of tutorials.
By doing this, I was finally able to establish connection in workbench and in my python scripts.

Show mysql databases as Linux root user

I'm logged into a CentOS server and I want to view all mysql databases. So I switched to the Linux root user and I tried logging into mysql. Normally (on other servers) I do this with a simple mysql command, but here I get an access denied:
[root#our.server.com kramer65]# mysql
ERROR 1045 (28000): Access denied for user 'root'#'localhost' (using password: NO)
I suppose that if I'm root on Linux I can do anything I want. Is there a way that I can show all Mysql databases as Linux root user?
From this you can log into your DB as linux root: if restarting servvices is an option:
https://www.digitalocean.com/community/tutorials/how-to-reset-your-mysql-or-mariadb-root-password
sudo systemctl stop mysql
sudo mysqld_safe --skip-grant-tables --skip-networking &
mysql -u root
Then the usual apply:
mysql>SHOW DATABASES;
The system root user is not the same as the root mysql user, and it's possible the system user has no access.
You can give the username to mysql with
mysql -u root -p
and enter the password for the mysql root user.
You can use the same command to connect with any mysql user account that is valid within mysql.

Access denied for user 'root'#'localhost' (using password: YES) after new installation on Ubuntu

Today I did a login as root into Ubuntu 14.04.1 LTS ll
and then apt-get install mariadb-server (without sudo but as root).
With mySQL -h localhost -u root --password=<PW> I got
Access denied for user 'root'#'localhost' (using password: YES)
With mySQL -u root -p I logged into the DB and did
GRANT ALL PRIVILEGES ON *.* TO 'root'#'localhost' IDENTIFIED BY '<PW>';
FLUSH ALL PRIVILEGES;
But this did not help. Have you got any idea?
I did not find the answer for the similar questions.
TL;DR: To access newer versions of mysql/mariadb as the root user, after a new install, you need to be in a root shell (ie sudo mysql -u root, or mysql -u root inside a shell started by su - or sudo -i first)
Having just done the same upgrade, on Ubuntu, I had the same issue.
What was odd was that
sudo /usr/bin/mysql_secure_installation
Would accept my password, and allow me to set it, but I couldn't log in as root via the mysql client
I had to start mariadb with
sudo mysqld_safe --skip-grant-tables
to get access as root, whilst all the other users could still access fine.
Looking at the mysql.user table I noticed for root the plugin column is set to unix_socket whereas all other users it is set to 'mysql_native_password'. A quick look at this page: https://mariadb.com/kb/en/mariadb/unix_socket-authentication-plugin/ explains that the Unix Socket enables logging in by matching uid of the process running the client with that of the user in the mysql.user table. In other words to access mariadb as root you have to be logged in as root.
Sure enough restarting my mariadb daemon with authentication required I can login as root with
sudo mysql -u root -p
or
sudo su -
mysql -u root -p
Having done this I thought about how to access without having to do the sudo, which is just a matter of running these mysql queries
GRANT ALL PRIVILEGES on *.* to 'root'#'localhost' IDENTIFIED BY '<password>';
FLUSH PRIVILEGES;
(replacing <password> with your desired mysql root password). This enabled password logins for the root user.
Alternatively running the mysql query:
UPDATE mysql.user SET plugin = 'mysql_native_password' WHERE user = 'root' AND plugin = 'unix_socket';
FLUSH PRIVILEGES;
Will change the root account to use password login without changing the password, but this may leave you with a mysql/mariadb install with no root password on it.
After either of these you need to restarting mysql/mariadb:
sudo service mysql restart
And voila I had access from my personal account via mysql -u root -p
PLEASE NOTE THAT DOING THIS IS REDUCING SECURITY Presumably the MariaDB developers have opted to have root access work like this for a good reason.
Thinking about it I'm quite happy to have to sudo mysql -u root -p so I'm switching back to that, but I thought I'd post my solution as I couldn't find one elsewhere.
In clean Ubuntu 16.04 LTS, MariaDB root login for localhost changed from password style to sudo login style...
so, just do
sudo mysql -u root
since we want to login with password, create another user 'user'
in MariaDB console... (you get in MariaDB console with 'sudo mysql -u root')
use mysql
CREATE USER 'user'#'localhost' IDENTIFIED BY 'yourpassword';
\q
then in bash shell prompt,
mysql-workbench
and you can login with 'user' with 'yourpassword' on localhost
from superuser accepted answer:
sudo mysql -u root
use mysql;
update user set plugin='' where User='root';
flush privileges;
exit;
Try the command
sudo mysql_secure_installation
press enter and assign a new password for root in mysql/mariadb.
If you get an error like
ERROR 2002 (HY000): Can't connect to local MySQL server through socket
'/var/run/mysqld/mysqld.sock'
enable the service with
service mysql start
now if you re-enter with
mysql -u root -p
if you follow the problem enter with sudo su and mysql -u root -p now apply permissions to root
GRANT ALL PRIVILEGES ON *.* TO 'root'#'localhost' IDENTIFIED BY '<password>';
this fixed my problem in MariaDB.
Good luck
I had to be logged into Ubuntu as root in order to access Mariadb as root. It may have something to do with that "Harden ..." that it prompts you to do when you first install. So:
$ sudo su
[sudo] password for user: yourubunturootpassword
# mysql -r root -p
Enter password: yourmariadbrootpassword
and you're in.
The new command to flush the privileges is:
FLUSH PRIVILEGES
The old command FLUSH ALL PRIVILEGES does not work any more.
You will get an error that looks like that:
MariaDB [(none)]> FLUSH ALL PRIVILEGES;
ERROR 1064 (42000): 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 'ALL PRIVILEGES' at line 1
Hope this helps :)
Run mysql_upgrade.
Check that
SHOW GRANTS FOR 'root'#'localhost';
says
GRANT ALL PRIVILEGES ON ... WITH GRANT OPTION
Check that the table exists _mysql.proxies_priv_.
Access denied for user 'root'#'localhost' while attempting to grant privileges. How do I grant privileges?
System like Ubuntu prefers to use auth_socket plugin. It will try to authenticate by comparing your username in DB and process which makes mysql request; it is described in here
The socket plugin checks whether the socket user name (the operating
system user name) matches the MySQL user name specified by the client
program to the server, and permits the connection only if the names
match.
Instead you may want to back with the mysql_native_password, which will require user/password to authenticate.
About the method to achieve that, I recommend this instead.
First of all close terminal to exit this cmd;
Then run: sudo mysql (Do not forgot sudo otherwise MySQL requires password which you don't know - use sudo to skip mysql password authentication)
Select mysql database by following cmd
mysql> use mysql
then set new password for your root user
mysql> alter user root#localhost identified with mysql_native_password by 'MyNewPassword#123';
then
mysql> flush privileges;
and quit to close mysql connection
mysql> quit
Now you can run sudo mysql_secure_installation without any error
Here you have to enter your new set password (MyNewPassword#123)