I have tried various options I got from Google but unable to set password for root. I can login without any password, but the Java drivers require a password, so I have to set it.
In my last attempt, I tried following command in the MySQL console:
SET PASSWORD FOR root#localhost=PASSWORD('abc123');
But I got following error:
ERROR 1133 <42000>: Can't find any matching row in the user table
Enter the following lines in your terminal.
Stop the MySQL Server.
sudo /etc/init.d/mysql stop
Start the mysqld configuration. (in safe mode)
sudo mysqld_safe --skip-grant-tables &
Login to MySQL as root.
mysql -u root mysql
Replace YOURNEWPASSWORD with your new password!
UPDATE user SET Password=PASSWORD('YOURNEWPASSWORD') WHERE User='root';
FLUSH PRIVILEGES;
exit;
sudo /etc/init.d/mysql start
REFERENCES :
http://www.rackspace.com/knowledge_center/article/mysql-resetting-a-lost-mysql-root-password
https://help.ubuntu.com/community/MysqlPasswordReset
It could be that the user root for localhost does not exists. Adding this account can be done by:
CREATE USER 'root'#'localhost' IDENTIFIED BY 'abc123';
In MySQL, each user is uniquely identified by both the username and the host, meaning that root#localhost is not the same user as root#127.0.0.1 or root#% (for example). It could be that you don't have the root#localhost user that you're trying to set the password for.
Double-check which users you actually have in the mysql.user table:
SELECT * FROM mysql.user WHERE user = 'root';
If you want create the missing user, use these statements instead:
CREATE USER root#localhost IDENTIFIED BY 'abc123';
GRANT ALL PRIVILEGES ON *.* TO root#localhost;
Related
I just installed Ubuntu 16.04 (Xenial Xerus) and installed web server on it. Everything works well, but I cannot access database.
Even if I create new user and grant all privileges, I can't create database
In PHP I'm getting this error:
SQLSTATE[HY000] [1698] Access denied for user 'root'#'localhost'
When I try to login in terminal, it works, but in PHP and phpMyAdmin don't.
PHP Code:
protected $host = '127.0.0.1';
protected $db = 'dbname';
protected $name = 'root';
protected $pass = 'root';
protected $conn;
private static $settings = array(
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'
);
public function __construct() {
try {
$this->conn = new PDO("mysql:host=$this->host;dbname=$this->db", $this->name, $this->pass, self::$settings);
} catch (PDOException $e) {
echo $e->getMessage();
}
}
It turns out you can't use the root user in 5.7 anymore without becoming a sudo'er. That means you can't just run mysql -u root anymore and have to do sudo mysql -u root instead.
That also means that it will no longer work if you're using the root user in a GUI (or supposedly any non-command line application). To make it work you'll have to create a new user with the required privileges and use that instead.
See this answer for more details.
These steps worked for me on several systems using Ubuntu 16.04 (Xenial Xerus), Apache 2.4, MariaDB, and PDO:
Log into MYSQL as root
mysql -u root
Grant privileges. For a new user, execute:
CREATE USER 'newuser'#'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON *.* TO 'newuser'#'localhost';
FLUSH PRIVILEGES;
UPDATE for Google Cloud Instances
MySQL on Google Cloud seem to require an alternate command (mind the backticks).
GRANT ALL PRIVILEGES ON `%`.* TO 'newuser'#'localhost';
NOTE:
Depending on wether your new user should be able to grant all privileges to other users as well you could extend the command by the GRANT WITH option. Please be aware that this exposes your user to be sudoer and hence become a higher security risk.
GRANT ALL PRIVILEGES ON `%`.* TO 'newuser'#'localhost' GRANT WITH OPTION;
Bind to all addresses:
The easiest way is to comment out the line in your
/etc/mysql/mariadb.conf.d/50-server.cnf or /etc/mysql/mysql.conf.d/mysqld.cnf file, depending on what system you are running:
#bind-address = 127.0.0.1
Exit MySQL and restart MySQL
exit
service mysql restart
By default it binds only to localhost, but if you comment the line it binds to all interfaces it finds. Commenting out the line is equivalent to bind-address=*.
To check the binding of the MySQL service, execute as root:
netstat -tupan | grep mysql
Use:
sudo mysql -u root
And now in the MySQL client:
use mysql;
update user set plugin='' where User='root';
flush privileges;
\q
Now you should be able to log in as root in phpMyAdmin.
(It was found here.)
To create a user for phpMyAdmin:
sudo mysql -p -u root
Now you can add a new MySQL user with the username of your choice.
CREATE USER 'USERNAME'#'%' IDENTIFIED BY 'PASSWORD';
And finally grant superuser privileges to the user you just created.
GRANT ALL PRIVILEGES ON *.* TO 'USERNAME'#'%' WITH GRANT OPTION;
In short, in MariaDB:
sudo mysql -u root;
use mysql;
UPDATE mysql.user SET plugin = 'mysql_native_password',
Password = PASSWORD('pass1234') WHERE User = 'root';
FLUSH PRIVILEGES;
exit;
ALTER USER or DROP the user and create again works perfectly.
DROP USER root#localhost;
CREATE USER root#localhost IDENTIFIED BY 'root_password';
GRANT ALL PRIVILEGES ON *.* TO 'root'#'localhost';
FLUSH PRIVILEGES;`
MySQL makes 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".
Just create a new user for MySQL; do not use root. There is a problem with its security issues:
sudo mysql -p -u root
Log in into MySQL or MariaDB with root privileges
CREATE USER 'troy121'#'%' IDENTIFIED BY 'mypassword123';
Log in and create a new user:
GRANT ALL PRIVILEGES ON *.* TO 'magento121121'#'%' WITH GRANT OPTION;
And grant privileges to access "." and "#" "%" any location, not just only 'localhost'.
exit;
If you want to see your privilege table, SHOW GRANTS; and enjoy.
With MySQL client version 14.14 and Distrib 5.7.22, the update statement is now:
update user set authentication_string=password('1111') where user='root';
If you are receiving that error even after creating a new user and assigning them the database privileges, then the one last thing to look at is to check if the users have been assigned the privileges in the database.
To do this, log into to your MySQL client (this is presumably the application that has restricted access to the database, but you as a root can be able to access your database table via mysql -u user -p).
Commands to apply
mysql -u root -p
password: (provide your database credentials)
On successful login, type
use mysql;
from this point, check each user's privileges if it is enabled from the database table as follows:
select User,Grant_priv,Host from db;
If the values of the Grant_priv col for the created user is N, update that value to Y with the following command:
UPDATE db SET Grant_priv = "Y" WHERE User= "your user";
With that, now try accessing the application and making a transaction with the database.
sudo mysql -u root
mysql> USE mysql;
mysql> UPDATE user SET plugin='mysql_native_password' WHERE User='root';
mysql> FLUSH PRIVILEGES;
mysql> exit;
service mysql restart
After restarting mysql server reload the app please.
None from this question reply that solving my problem but i got super easy to solving that problem!
Just open file DEBIAN.CNF :
/etc/mysql/debian.cnf
You will find default sys admin user and pass! login with this account on your PhpMyAdmin then create new user etc whatever you want!
# Automatically generated for Debian scripts. DO NOT TOUCH!
[client]
host = localhost
user = debian-sys-maint
password = 8pTMhYuRMW6jmMG1
socket = /var/run/mysqld/mysqld.sock
[mysql_upgrade]
host = localhost
user = debian-sys-maint
password = 8pTMhYuRMW6jmMG1
socket = /var/run/mysqld/mysqld.sock
Users for MySQL and for server are two different things. Look how to add a user to the database and log in with these credentials.
I had the same problem in my Ubuntu 20.04 (Focal Fossa) and MySQL 8.0 and I do these steps:
log in to MySQL
sudo mysql -p -u root
Show the users added to MySQL
SELECT user,plugin,host FROM mysql.user
Change the root user plugin from auth_socket to mysql_native_password
ALTER USER 'root'#'localhost' IDENTIFIED WITH mysql_native_password BY 'root';
Flush the privileges
FLUSH PRIVILEGES;
Ctrl + z to exit from MySQL
Restart your MySQL service
sudo service MySQL restart
Check your phpMyAdmin page and try to log in.
Use:
sudo mysql -u root
mysql> CREATE USER 'sample'#'localhost' IDENTIFIED BY 'Secure1pass!';
mysql> CREATE DATABASE testdb;
mysql> GRANT ALL PRIVILEGES ON testdb . * TO 'sample'#'localhost';
In case you just want to use your MySQL server on Ubuntu locally and want to connect with your application to a database.
I had 'user'#'%' with all privileges when getting the same error mentioning 'user'#'localhost' denied access.
So I create 'user'#'localhost' with all privileges, and then flush, and even restart services to no avail.
At last I changed $host = '127.0.0.1'; to $host = 'localhost';.
Now it works!
1) service stoped
sudo /etc/init.d/mysqld stop
2) started in safe mode
sudo mysqld_safe --skip-grant-tables &
3) user created
mysql -u root
4) setting passwd
UPDATE user SET Password=PASSWORD('my_password') where USER='root';
exit from the mysql console,
and restart the service and trying to access mysql console with
mysql -u root -p my_password
[vikram#VoltyLinux ~]$ mysql -u root -p
Enter password: *******
ERROR 1045 (28000): Access denied for user 'root'#'localhost' (using
password: YES)
Go in as before in Step 2, that is, restart mysql daemon in safe-mode.
Issue the command
select user,host,password from mysql.user where user='root';
cut and paste output into a text editor for comparison (of the hashed password column values) before and after the following.
For each row notice the host column. In the following example I am assuming I have two rows. One row has % as host, the next has localhost
For each row that had a host variation, you will issue one command. So in my example above, I would be issuing 2 commands in total, such as:
SET PASSWORD FOR 'root'#'%' = PASSWORD('MyNewPassword');
SET PASSWORD FOR 'root'#'localhost' = PASSWORD('MyNewPassword');
run
select user,host,password from mysql.user where user='root';
cut and paste that into text editor. Notice the change to the password hashes.
Shutdown the daemon from safe mode, and restart normally.
Try to login as root with MyNewPassword
Here are a few links. The one for SET PASSWORD Syntax, and an often next step for GRANT Syntax.
For the narrow scope of this question, that is to login, the grants would not be necessary. But without proper grants to databases, one would be sandboxed, not able to do much, other than simple commands like select now();
I am not suggesting that your root user does not have privileges once the password is changed. The Grants are necessary for normal users to be sure.
I am facing problem with mysql non root/admin user, I am following the below steps for creating user and its privileges, correct me if i am doing wrong,
i am installing mysql on RHEL 5.7 64bit, packages are mentioned below, once i done the rpm install we are
creating mysql db using mysql_install_db, then
starting the mysql service then
using mysql_upgrade also we are doing to the server.
After this process i can login as root but with a non-root user I am not able to log into the server:
[root#clustertest3 ~]# rpm -qa | grep MySQL
MySQL-client-advanced-5.5.21-1.rhel5
MySQL-server-advanced-5.5.21-1.rhel5
[root#clustertest3 ~]# cat /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
# Default to using old password format for compatibility with mysql 3.x
# clients (those using the mysqlclient10 compatibility package).
old_passwords=1
# Disabling symbolic-links is recommended to prevent assorted security risks;
# to do so, uncomment this line:
# symbolic-links=0
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
[root#clustertest3 ~]# ls -ld /var/lib/mysql/mysql.sock
srwxrwxrwx 1 mysql mysql 0 Nov 30 11:09 /var/lib/mysql/mysql.sock
mysql> CREATE USER 'golden'#'%' IDENTIFIED BY 'password';
Query OK, 0 rows affected (0.00 sec)
mysql> GRANT ALL PRIVILEGES ON * . * TO 'golden'#'%';
Query OK, 0 rows affected (0.00 sec)
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT USER(),CURRENT_USER();
+----------------+----------------+
| USER() | CURRENT_USER() |
+----------------+----------------+
| root#localhost | root#localhost |
+----------------+----------------+
1 row in set (0.00 sec)
[root#clustertest3 ~]# mysql -ugolden -p
Enter password:
ERROR 1045 (28000): Access denied for user 'golden'#'localhost' (using password: YES)
This is the problem I am facing, is there any solution to this?
Do not grant all privileges over all databases to a non-root user, it is not safe (and you already have "root" with that role)
GRANT <privileges> ON database.* TO 'user'#'localhost' IDENTIFIED BY 'password';
This statement creates a new user and grants selected privileges to it.
I.E.:
GRANT INSERT, SELECT, DELETE, UPDATE ON database.* TO 'user'#'localhost' IDENTIFIED BY 'password';
Take a look at the docs to see all privileges detailed
EDIT: you can look for more info with this query (log in as "root"):
select Host, User from mysql.user;
To see what happened
If you are connecting to the MySQL using remote machine(Example workbench) etc., use following steps to eliminate this error on OS where MySQL is installed
mysql -u root -p
CREATE USER '<<username>>'#'%%' IDENTIFIED BY '<<password>>';
GRANT ALL PRIVILEGES ON * . * TO '<<username>>'#'%%';
FLUSH PRIVILEGES;
Try logging into the MYSQL instance.
This worked for me to eliminate this error.
Try:
CREATE USER 'golden'#'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON * . * TO 'golden'#'localhost';
FLUSH PRIVILEGES;
Or even better use: mysql_setpermission to create the user
It looks like you're trying to make a user 'golden'#'%' but a different user by the name of 'golden'#'localhost' is getting in the way/has precedence.
Do this command to see the users:
SELECT user,host FROM mysql.user;
You should see two entries:
1) user= golden, host=%
2) user= golden, host=localhost
Do these Command:
DROP User 'golden'#'localhost';
DROP User 'golden'#'%';
Restart MySQL Workbench.
Then do your original commands again:
CREATE USER 'golden'#'%' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON * . * TO 'golden'#'%';
Then when you go to try to sign in to MySQL, type it in like this:
Hit 'Test Connection' and enter your password 'password'.
First I created the user using :
CREATE user user#localhost IDENTIFIED BY 'password_txt';
After Googling and seeing this, I updated user's password using :
SET PASSWORD FOR 'user'#'localhost' = PASSWORD('password_txt');
and I could connect afterward.
For anyone else who did all the advice but the problem still persists.
Check for stored procedure and view DEFINERS. Those definers may no longer exists.
My problem showed up when we changed the wildcard host (%) to IP specific, making the database more secure. Unfortunately there are some views that are still using 'user'#'%' even though 'user'#'172....' is technically correct.
I also have the similar problem, and later on I found it is because I changed my hostname (not localhost).
Therefore I get it resolved by specifying the --host=127.0.0.1
mysql -p mydatabase --host=127.0.0.1
According way you create your user, MySQL interprets a different manner. For instance, if you create a user like this:
create user user01 identified by 'test01';
MySQL expects you give some privilege using grant all on <your_db>.* to user01;
Don't forget to flush privileges;
But, if you create user like that (by passing an IP address), you have to change it to:
create user 'user02'#'localhost' identified by 'teste02';
so, to give some privileges you have to do that:
grant all on <your_db>.* to user02#localhost;
flush privileges;
Make sure the user has a localhost entry in the users table. That was the problem I was having. EX:
CREATE USER 'username'#'localhost' IDENTIFIED BY 'password';
For annoying searching getting here after searching for this error message:
Access denied for user 'someuser#somewhere' (using password: YES)
The issue for me was not enclosing the password in quotes. eg. I needed to use -p'password' instead of -ppassword
Try this:
If you have already created your user, you might have created your user with the wrong password.
So drop that user and create another user by doing this.
To see your current users.
SELECT Host,User FROM mysql.user;
To drop the user
DROP User '<your-username>'#'localhost';
After this you can create the user again with the correct password
CREATE USER '<your-username>'#'localhost' IDENTIFIED WITH mysql_native_password BY '<correct password>';
then
FLUSH PRIVILEGES;
You might still run into some more errors with getting access to the database, if you have that error run this.
GRANT ALL PRIVILEGES ON *.* to '<your-username>'#'localhost';
In my case the same error happen because I was trying to use mysql by just typing "mysql" instead of "mysql -u root -p"
connect your server from mysqlworkbench and run this command->
ALTER USER 'root'#'localhost' IDENTIFIED BY 'yourpassword';
The error of ERROR 1045 (28000): Access denied for user might not be always related to privilages problems but to the fact that there is a missing -p at the end of the command:
# Will prompt us a mysql terminal in case there are no privilages issues
mysql -u root -p
# Will fail with the mentioned ERROR 1045
mysql -u root
sometimes,it can just be a wrong password.Kindly remember your passwords including their sensitivity.
I had this issue and something dummy ended up solving.
For some reason "locahost" was not resolving for anything, so using its local IP made it work.
So you would change
mysql -h localhost -P 33061
to:
mysql -h 127.0.0.1 -P 33061
Had a similar issue when trying to grant privileges to an already existing user using the command:
use my-db;
GRANT ALL PRIVILEGES ON my-database.* TO 'my-user'#'%' IDENTIFIED BY 'my-password';
Here's how I solved it:
It had to do with 2 issues:
The password of the already exiting user was different from the password that provided in the GRANT ALL PRIVILEGES command. I had to rerun the GRANT ALL PRIVILEGES with the correct password for the already existing user.
The host name of the database server that I provided when connecting to the database was incorrect. I had created the database and the user on a particular database server and I was trying to connect to another database server different from the database server where the database and the user were created. I had to get the correct database server hostname, and I used it for the connection.
After all this were sorted, I was able to connect to the database using the credentials.
The issue was that my-user already had the privileges I wanted to grant it.
You can check to see the privileges that you've granted your user using:
SHOW GRANTS FOR 'your-user'#'%';
OR
SHOW GRANTS FOR 'your-user'#'localhost';
That's all.
Just add computer name instead of 'localhost' in hostname or MySQL Host address.
I did the following steps to use MySQL in Ubuntu:
sudo aptitude install php5-mysql mysql-server
sudo service mysql stop
sudo mysqld_safe --skip-grant-tables &
sudo mysql -u root mysql
Change root password:
mysql> UPDATE mysql.user SET Password=PASSWORD('SecurePassword') WHERE
User='root';
mysql> FLUSH PRIVILEGES;
mysql> EXIT
Modify /etc/mysql/my.cnf:
[client]
user=root
password=SecurePassword
[mysqld]
...
default-time-zone = '+0:00'
Then:
sudo service mysql start
mysql -u root
mysql> SHOW GRANTS FOR root#localhost
+--------------------------------------------------------------------------------------------------+
| Grants for root#localhost |
+--------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'root'#'localhost' IDENTIFIED BY PASSWORD '[here is the Securepassword]' |
+-------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql>
GRANT ALL PRIVILEGES ON *.* TO 'root'#'localhost' WITH GRANT OPTION;
and I receive an error:
Access denied for user 'root'#'localhost' (using password: YES)
It was absolutely correct what you did, but I guess it's not working for one small reason.
You should use identified by password when you are going to grant privileges like this:
mysql> GRANT ALL PRIVILEGES ONE `*`.`*` TO 'root'#'localhost' IDENTIFIED BY PASSWORD
'*A4B6157319038724E3560894F7F932C8886EBFCF' WITH GRANT OPTION;
If you get an error message like this:
Warning: mysql_connect(): Access denied for user: ....
then the problem is that your application can not access the database. This can have several causes:
First of all, make sure the database settings in your db-config.php (connection file for your application) are correct, specifically the name and password of your MySQL user, the name of your database, and the name of your MySQL server.
If you're running your own server, you may need to give your MySQL user proper permissions. Log in to MySQL as the MySQL root user and issue these commands:
GRANT ALL PRIVILEGES ON database_name TO user#host IDENTIFIED BY 'password';
FLUSH PRIVILEGES;
System like Ubuntu prefers to use auth_socket plugin for root account by default. 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 you checking this out instead.
I am able to login into mysql using a username & password that I have created.
I ran SELECT user FROM mysql.user;
and it shows my created account and 3 instances of root.
1.) How do I see the passwords for any of the root users?
2.) If that cannot be down, how do remove those root users, and create a new root user with no password?
You cannot see the plain text passwords.
DELETE FROM mysql.user WHERE user='root' AND host <> 'localhost';
UPDATE mysql.user SET password = '' WHERE user='root';
FLUSH PRIVILEGES;
Before doing this, you should run mysql_secure_installation.
If you cannot get back in with root#localhost and no password, do the following:
SQLSTMT="GRANT ALL PRIVILEGES ON *.* to root#localhost"
SQLSTMT="${SQLSTMT} IDENTIFIED BY 'resetpwd' WITH GRANT OPTION;"
echo ${SQLSTMT} > /var/lib/mysql/init.sql
service mysql restart --init-file=/var/lib/mysql/init.sql
rm -f /var/lib/mysql/init.sql
SQLSTMT="UPDATE mysql.user SET password = '' WHERE user='root'"
SQLSTMT="${SQLSTMT} AND host='localhost'; FLUSH PRIVILEGES;"
mysql -uroot -presetpwd -e"${SQLSTMT}"
mysql -uroot
It is not recommended to have a root mysql user with blank password!!
However, here is how to do it
SET PASSWORD FOR root#127.0.0.1=PASSWORD('');
please not that you must be logged in to mysql as root!
The 3 instances of root will likely be for the host variations. In other words, there might be one for root#localhost and one for root#%.
If you're using phpMyAdmin you could delete the users you don't want, or reset the passwords by clicking the Users tab.
You can check the users you don't want, and click Go on Remove selected users.
Or to reset the password for a specific user, you can click Edit privileges, scroll down to the Change password section, select No password, and hit Go.
You can download phpMyAdmin here, and you'll of course need to be logged in as root to do this.