MySQL - access denied until I connect via Workbench or CLI - php

I'm having issues with intermittent connection issues from my localhost PHP site to my localhost MySQL DB. It gives me back Access Denied for 'user'#'localhost' (using password: YES) as is typical for a wrong password. I tested to make sure it wasn't anything particular, and I could get it with root, so I was confused. I even set up a 127.0.0.1, a localhost and a % account with the same username and password. None of them worked, but root worked just fine. That's been my biggest confusion - 'root' working with no issues, but this new user not working at all.
I've looked through so many of the problems (see below) and issues laid out on SE and other forums relating to Error 1045, Access Denied and all variants, and most boil down to a misspelling, or a typo, or something of that nature. I've copy and pasted every relevant string around all of my code, so that's not the issue. I don't have an anonymous user, so that's not the issue. There's no DNS issues, it's localhost and I've tried every iteration of that.
The issue comes from what solves the issue for me. If I open a connection to the DB using the new account via MySQL Workbench or CLI, suddenly the webpage will work with no issues, connecting via the new account with no problems. But if I run the script that changes the new account information whatsoever, even if I change nothing, and reload the page, it will deny the access again until I open that connection up again via MySQL Workbench or the CLI.
It works without fail, every single time. I have no idea what's going on - does the account need to be logged into via CLI or Workbench before they can be used via PHP? Or is there something more I am missing here?
PHP Code
$servername='localhost';
$username='website';
$password='password';
$database='db_1';
$conn=new mysqli($servername, $username, $password, $database);
if($conn->connect_error) die("connection failed");
MySQL Credentials Creation
create user website#'localhost' identified by 'password';
grant SELECT, INSERT, DELETE on db_1.* to 'website'#'localhost';
A few of the SO questions that didn't help:
MySQL - Access denied for user
access denied for user # 'localhost' to database ''
"Connect failed: Access denied for user 'root'#'localhost' (using password: YES)" from php function
EDIT 1:
I am running a WAMP stack on a local computer. I have my firewall open for port 3306 (the port MySQL is listening on). I am not using CPanel (I think that's what it's called anyways). And I just tested and it doesn't work with root either until I go ahead and recreate a new connection to the DB via the CLI or Workbench.
The error it's giving back also includes this, but I've never had this come up as I've never tried to install OpenSSL on either before: mysqli::__construct(): PHP was built without openssl extension, can't send password encrypted in [file]. But this goes away as soon as I make the connection via the CLI (no fancy tags or anything, so I don't think I'm using SSL) or via Workbench.
Edit 2: The rabbit hole went deeper and then stopped but I'm still confused as to how to configure my set up properly to avoid it in the future/fix it to be more secure. The solution (which I can't put up as my own, so if one of you wants to explain and then claim this, go for it - I'll accept it) was to go back to my MySQL server and reconfigure the Authentication Method to not use the Strong Password Encryption and instead opt for the Legacy Authentication Method. If someone could explain how to make my PHP compatible with the newer authentication method (I haven't looked yet but will be after I post this) I would be grateful.

The solution was to go back to my MySQL server and reconfigure the Authentication Method to not use the Strong Password Encryption and instead opt for the Legacy Authentication Method.

Related

PHP - Connect to different servers database

I'm developing on 'server1'.
I need to save the data processed on this server to the 'server1' database.
However, I also need to save some data to an external 'server2' database.
The problem is that when I connect to the database of the other server I get a fairly weird error.
Error code: 1045
Error message: Access denied for user 'server2-username'#'server1-name' (using password: YES)
Why does 'server1' appear in the error message?
To me, the message says this: "hey, I can't find the 'server2-username' on 'server1' database". Am I correct? Why does say that, when I'm connecting to 'server2'?
$connection = new mysqli('server2-ip', 'server2-username', 'server2-password', 'server2-database');
if($connection->connect_errno) {
echo $connection->connect_errno;
echo $connection->connect_error;
}
Any idea how should I connect to 'server2'? Or what should I check?
This is the first time I want to connect to another server database. I haven't done this yet, and I don't know what's wrong.
This error mean 'server2-username' try to connect from 'server1-name' and not succeed.
Please check permissions user2 on server2 for connect from remote host
It demands an account which matches the connecting user#hostname, which is server2-username#server1-name... it's just that the web-server runs on the same host server1-name as the mySQLd on server1-name. This may appear confusing, but the hostname comes from where the script runs.
You'd need to add user server2-username#server1-name to the mySQLd on server2-name... and if you can't get that account set up, most commonly there's a JSON API available as a web-service; exporting/importing data to any format would also be an option, if they wish to import themselves.
SSL tunneling could even connect through local loop-back interface 127.0.0.1 on :3307 (the problem isn't bind-address = 127.0.0.1, but that there is no such user#hostname available):
shell_exec("ssh -fNg -L 3307:server2-ip:3306 server2-username#server2-ip");
$connection = new mysqli('server2-ip', 'server2-username', 'server2-password', 'server2-database', 3307);
However, either getting that remote user account setup or using an API might rather be the suggested options, because the SSL tunnel created with shell_exec() might be closed at any time, which all needs to be tested & handled accordingly. The error message would at least be server2-username#server2-name or server2-username#localhost, when tunneling into it.

Valet not working for WordPress site: Error establishing a database connection

I am having a problem connecting to my WordPress site using Valet. I get the following error:
Warning: mysqli_real_connect(): (HY000/1045): Access denied for user
'db_username'#'localhost' (using password: YES) in
/Users/.../Documents/WordPress/.../wp-includes/wp-db.php
on line 1531
Error establishing a database connection
This either
means that the username and password information in your wp-config.php
file is incorrect or we can’t contact the database server at
localhost. This could mean your host’s database server is down.
Are you sure you have the correct username and password?
Are you sure that you have typed the correct hostname?
Are you sure that the database server is running?
If you’re unsure what these terms mean you
should probably contact your host. If you still need help you can
always visit the WordPress Support Forums.
This is odd, as I am able to connect the page that is online to this database. This makes me believe that all of the following settings should be correct:
DB_NAME
DB_USER
DB_PASSWORD
DB_HOST
DB_CHARSET
DB_COLLATE
I also wanted to note that I do not have any redirection issues. I had this at first and solved it by changing the "DOMAIN_CURRENT_SITE" to my Valet domain.
Example:
define('DOMAIN_CURRENT_SITE', 'valetdomain.test');
I am also able to connect to the database within Sequel Pro on my computer with the same login information.
Note: I can use Valet on my other pages locally as well and that works fine.
So, I have Valet working, Sequel should be working, and the site works online. I just can't seem to get them all working together :(
Is it possible that the wp-config settings or some other settings must be different when connecting a WordPress site locally, via Valet in my case, as opposed to online?
Things I have tried:
Creating a new user within cPanel, adding them to this database, and updating wp-config accordingly
using no password and connecting via Root
using my server's IP address as the hostname instead of localhost
using 127.0.0.1 as the hostname instead of localhost
changing the siteurl and home within wp_options table to the .test domain that I'm using with Valet (this just kills my online site, but doesn't help me connect the DB for the local site in Valet)
One thing I'd like to note here is that if I use...
define('DB_USER', 'root');
define('DB_PASSWORD', '');
... then the error changes from the one above to simply "Error establishing a database connection." Is this somehow a "better" error, meaning that I could be closer to solving this problem?
Final note, I am using WP Multisite. Not sure if this makes any difference.
I was able to solve the problem by closing everything and restarting WordPress from scratch. I think the problem was that I had two versions of SQL running on my computer, one from Brew and the other from the Mac. This may have caused the problem. I'm not sure if this is the best advice, but it is what helped me.

PHP script cannot connect to MYSQL database

I have a straight forward script that is trying to connect to a MySQL database.
When I run the script, I get the error:
Warning: mysqli_connect(): (HY000/1045): Access denied for user 'root'#'localhost' (using password: YES) in C:\xampp\htdocs\ShopSite\submitReg.php on line 16
Failed to connect.
The connection code is:
$server="127.0.0.1";
$db="shop";
$user="root";
$password="";
$conn=mysqli_connect($server,$user,$password,$db) or die("Failed to connect");
I can connect to phpMyAdmin and edit the database on there just fine. I am using XAMPP and running it locally.
When I go to user accounts, it says there is no password for root. I looked in the config file and there are no passwords set. I haven't set a password for anything.
I haven't went in and fiddled with settings, or created any new accounts. It was working fine (never used to get this problem), then it just started with this. I reinstalled XAMPP, still giving me this issue.
I don't get why it says using password "YES".
I've been at this for hours trying to fix it. I've looked through lots of threads. Not all necessarily apply to me, as I am running it locally, and haven't been going in and making new user accounts. Everything is default.
Before I reinstalled, I tried creating a new user account and giving it the necessary privileges. I gave it a password too. Didn't work.
I don't get why it is doing this, and am not knowledgeable enough to fix it myself. Any help is much appreciated, so that it can go back to connecting.
change
$server="127.0.0.1";
by
$server="localhost";
I used XAMPP for a while and it worked quite good for me,
i think the default password for your mysql server is not set to '', so i guess
if you change the root password and try accessing your database using the new password, it should work.
Updating the root password
To update the root use password, you have to go by these steps:
Open the XAMPP Control Panel
Click on the MySQL button
After the MySQL console has loaded, execute this query:
SET PASSWORD FOR 'root'#'localhost' = PASSWORD('pass');`
After that, change the $password variable to the new password which
is in this case pass
Adding a new user
You could also add a new user by executing this query:
GRANT ALL PRIVILEGES ON *.* TO 'new-user'#'localhost' IDENTIFIED BY 'password';
While the username is new-user and the password is password.
I hope this helps.

Reasons for MySQL authentication error: "Access denied for user 'xxx'#'yyy'"?

What possible reasons could exist for MySQL giving the error “Access denied for user 'xxx'#'yyy'” when trying to access a database using PHP-mysqli and working fine when using the command-line mysql tool with exactly the same username, password, socket, database and host?
Update:
There were indeed three users in the mysql.user table, each one with a different host (but with the same hashed password), one was set to localhost, one to 127.0.0.1 and one to the machine’s host name. Deleting two of them and changing the host of the third to “%” had only one effect: now the access is denied using the command-line tool also.
I did do a
select user();
before that in the command line and it yielded the same xxx#yyy that were denied in php.
Sometimes in php/mysql there is a difference between localhost and 127.0.0.1
In mysql you grant access based on the host name, for localusers this would be localhost.
I have seen php trying to connect with 'myservername' instead of localhost allthough in the config 'localhost' was defined.
Try to grant access in mysql for 127.0.0.1 and connect in php over 127.0.0.1 port 3306.
In case anyone’s still interested: I never did solve this particular problem. It really seems like the problem was with the hardware I was running MySQL on. I’ve never seen anything remotely like it since.
After I read your update I would suspect an error in/with the password.
Are you using "strange" characters in your PW (something likely to cause utf-8/iso encoding problems)?
Using % in the Host field would allow the user to connect from any host. So the only thing that could be wrong would be the password.
Can you create another user. whith the "grant all on all for '...'#'%' identiefied by 'somesimplepw'" syntax, and try to connect with that user?
Don't forget to 'flush privelidges'
For info on how to create a new user klick here
Today the FTP service of my web hosting provider is having some trouble, so I decide to realize a local virtual webserver for working on my website. I have installed EasyPHP 14.1 with phpMyAdmin and I have created my user and my database with tables.
First attempt: failed. I realized that the table I was looking for didn't exist. -> I solved creating the missing table.
Second attempt: failed. I realized that username that I set for my new user was diffrent from the username I used for my connection. -> I edited username.
Third attempt: failed. I realized that new database name was diffrent from database name I use for connection in my site. -> I edited db name.
Forth attempt: failed. I realized that between privileges of my new user there wasn't "Grant". I don't even know what "Grant" means, but let's try to enable it -> Added "Grant" privilege.
Fifth attempt: I win!
I hope my little adventure could help someone. =)

php access to remote database

Help!
I have a PHP (PHP 5.2.5) script on HOST1 trying to connect to an MySql database HOST2. Both hosts are in Shared Host environments controlled through CPanel.
HOST2 is set to allow remote database connections from HOST1.
The PHP connect I'm using is:-
$h2 = IPADDRESS;
$dbu = DBUSER;
$dbp = DBPASS;
$DBlink = mysql_connect($h2, $dbu, $dbp);
This always fails with:-
Access denied for user '<dbusername>'#'***SOMESTRING***' (using password: YES)
nb: SOMESTRING looks like it could be something to do with the shared host environment.
Any ideas???
BTW: I can make remote connections to HOST2 from my laptop using OpenOffice via ODBC, and SQLyog. The SQLyog and ODBC settings are exactly the same as the PHP script is trying to use.
somestring is probably the reverse-lookup for your web-server.
Can you modify privileges from your cPanel? Have you done anything to allow access from your workstation (ODBC)?
The error-message seems to indicate that you have network-access to the mysql-server, but not privileges for your username from that specific host.
If you're allowed to grant privileges for your database, invoking:
GRANT SELECT ON database.* TO username#ip.address.of.host1 IDENTIFIED BY 'password'
might work for you. I just wrote this out of my head, you might want to doublecheck the syntax in mysql-docs.
Have you read the MySQL documentation on Causes of Access denied Errors?
Have you contacted support for your hosting provider? They should have access to troubleshoot the database connection. People on the internet do not have access.
Do you need to specify the database name? Your account might have access to connect only to a specific database. The mysql_connect() function does not allow you do specify the database, but new mysqli() does. I'm not sure if this is relevant -- it might allow you to connect but give you errors when you try to query tables that aren't in your database.
Are you sure you're using the right password? MySQL allows each account to have a different password per client host. Admittedly, this is not a common configuration, but it's possible. Your hosting provider should be able to tell you.
Just some ideas:
HOST1 does not have remote access to HOST2 (shared host is disallowing)
MySQL account does not have access from HOST1 (IP address specified on account creation, or wildcard)
Edit:
In response to your comment, I meant that HOST1 cannot get to the MySQL port on HOST2. Web services will work, of course, because port 80 is open to the public. As another user pointed out though, you are getting a response, so you are reaching it. I would try specifying the DB, and double checking the account creation command you ran.
For the second piece, I meant this: http://dev.mysql.com/doc/refman/5.0/en/adding-users.html
You can specify what host the username can connect from. If it isn't set to HOST2's IP or the wildcard, HOST2 can't log in with those credentials.
The error message means that you can contact the mySql server, but the user you are trying to log in as, does not have access.
Either the user does not have access at all, or it has access locally, but not from the host you are connecting from.
You should try to use the hostname and port like $h2 = IPADDRESS:3307;

Categories