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!
I’m newbie to R with RStudio using Mac(OS X).
I successfully use sequel Pro to see DB with like this.
I use dbConnect with RMySQL and DBI(below code) with RStudio.
library(DBI)
library(RMySQL)
con <- dbConnect(RMySQL::MySQL(),
username = "username",
password = "password",
host = "hostname-xxx.ap-northeast-1.rds.amazonaws.com",
port = 3306,
dbname = "dbname"
)
but I've got below error.
Error in .local(drv, ...)
Failed to connect to database: Error: Access denied for user 'username'#'yyyyyyyyyyymarunouchi.tokyo.ocn.ne.jp' (using password: YES)
So ssh tunnel like this using terminal.
ssh -f sshuser#xx.xxx.xx.xx -i ~/.ssh/ssh_key -L 3306:hostname-xxx.ap-northeast-1.rds.amazonaws.com:3306 -N
and successfully logined.
after that, in order to confirm, executed below command(in terminal) but failed after entering correct password.
mysql -h 127.0.0.1 -p -u username dbname
with error code
ERROR 1044 (42000): Access denied for user 'username'#'localhost' to database 'dbname'
(other machine with same username successfully login. I didn't know why...)
and dbConnect(RMySQL with RStudio) is showing same above error.
Anyone same situation? Please tell me what to do.
Thank you.
This is just a matter of database credentials. You need to set up a user in your DBMS who has permission to access the DB that you login to and then use that account + password to login. This may not be the same as the account that you use to login to the server.
CREATE USER 'newuser'#'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON * . * TO 'newuser'#'localhost';
FLUSH PRIVILEGES;
If this gives you any trouble then instead of granting ALL PRIVILEGES grant each permission one at a time. This is rare but it happens, especially with other DB's like Hive.
I had written a following perl script to connect with the mysql workbench database, but it is giving the error access denied for user 'root'#'localhost' (using password yes).
Error after running the code:
C:\Users\1053130\Desktop>perl mysql.pl
DBI connect('database=dvd_collection','root',...) failed: Access denied for user 'root'#'localhost' (using password: YES) at mysql.pl line 9.
Access denied for user 'root'#'localhost' (using password: YES) at mysql.pl line 9.
I have gone through the similar questions also, but according the these I already have username and password. I even changed the root password also. But the error is still the same.
code:
#!/usr/bin/perl
use DBI;
use strict;
my $driver = "mysql";
my $database = "dvd_collection";
my $dsn = "DBI:$driver:database=$database";
my $userid = "root";
my $password = "123";
my $dbh = DBI->connect($dsn, $userid, $password ) or die $DBI::errstr;
Shouldnt my $dsn = "DBI:$driver:database=$database"; rather be
my $dsn = "DBI:$driver:database=$database;host=$hostname;port=$port"; ?
Missing the host and port?
Edit:
It says Access denied for... #localhost. Is it a local server? If not, you should declare the server-details as shown above.
Edit2:
According your comment you should check your password, as simple as it sounds. You can go for it with mysql -u root -p newPasswordHere. Choose a new one and retry.
Edit3:
As you already changed your password, try the following change of your connect:
my $dbh = DBI->connect('dbi:mysql:dbname=dvd_collection;host=localhost','root', '123') || die "Could not connect to database: $DBI::errstr";
I've seen some ppl having some issues with the different formats of the connection.
if the error says "the access is denied" then there are chances the password is wrong and in xampp server the dafault password to sql will be ' ' .hence just try the below line
my $dbh=DBI->connect("DBI:mysql:perldb",'root','') or die;
I've been having some issues trying to log in as non-root users from a .php file onto phpMyAdmin.
I've set up a login form that uses the root user to check if the login that someone uses matches an entry in the mysql.User table and then stores the username and password as session variables which I'm using to try and log in to MySQL and perform user specific tasks later.
This is the code I'm having issues with:
$host = "localhost"; // Host name
$username = $_SESSION['myusername']; // Mysql username
$password = $_SESSION['mypassword']; // Mysql password
$db_name = "gcim"; // Database name
$tbl_name = "permissions"; // Table name
// Create connection
$conn = new mysqli($host, $username, $password, $db_name);
myusername and mypassword are just the session variables I've stored and they're set to the values exactly how they should be, "gcim" is the database I'm trying to access and "permissions" is the table
Trying to execute the code results in the following:
Warning: mysqli::mysqli(): (HY000/1045): Access denied for user 'BUT03'#'localhost' (using password: YES) in C:\xampp\htdocs\gcim\permissions\permissions.php on line 12
Connection failed: Access denied for user 'BUT03'#'localhost' (using password: YES)
I'm pretty sure that the issue isn't a wrong hostname, username or password because even if I change the connection line to:
$conn = new mysqli("localhost", "BUT03", "test", "gcim");
it still gives me the same error
Here's the setup of my mysql.User table:
http://puu.sh/dDxd3/5e25f315f4.png
Anyway, any help would be very much appreciated,
Thanks
EDIT: I've been experimenting and I've come to the conclusion that the ONLY way to get a successful login to MySQL is using the root account with no password, any attempt to log in with a password will give me the same error, as well as removing the password from a non-root account and trying to log in with no password which gives me the following error: Connection failed: Access denied for user ''#'localhost' to database 'gcim'
I have a feeling that there's some issue with the server settings, was I meant to change anything from the default phpMyAdmin settings?
EDIT: I just tried to log in to phpMyAdmin itself as 'BUT03'#'localhost' and I appear to have no privileges at all even though everywhere I can see I've granted all possible privileges to that account, could this be affecting anything and does anyone know what could be causing it?
I assume passowrds are saved not in plain text, but hashed. That would mean your hashed password is 'test', and you actual password is something unknown. Change your password to test with this function
SET PASSWORD FOR 'BUT03'#'localhost' = PASSWORD('test');
see:
http://dev.mysql.com/doc/refman/5.0/en/set-password.html
I'm working on a MAC in a MAMP environment.
so when i try to connect to the database i get this error
$this->db = mysqli_connect('localhost', 'root', '');
I can log in with PhpMyAdmin as the root user.
mysqli_connect(): (28000/1045): Access denied for user
'root'#'localhost' (using password: YES)
my root user is set like this
user: root
machine: localhost
password: no
global rights: ALL PRIVILEGES
grant: yes
Does anyone know how to resolve this problem?
The PHP Manual here says about the password field:
If not provided or NULL, the MySQL server will attempt to authenticate
the user against those user records which have no password only. This
allows one username to be used with different permissions (depending
on if a password as provided or not).
So, either do mysqli_connect($host, $user); or mysqli_connect($host, $user, null);
you have to call mysqli_connect without a password parameter, else php tries to authenticate the user using the empty string as password. try
mysqli_connect($host, $user);
only, withouth the ''.