PHP script cannot connect to MYSQL database - php

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.

Related

MySQL - access denied until I connect via Workbench or CLI

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.

Could not connect to Google cloud SQL. Using PHP Google SDK

I did read this tutorial
https://developers.google.com/appengine/docs/php/cloud-sql/
I had created a Instance with name schoolnbd and my project id is myapplication-2013
After reading the tutorial I tried the code below
<?php
$dhost=":/cloudsql/myapplication-2013:schoolnbd";
$duser="root";
$dpassword="xxxyyy";
$database="schoolnbd";
$connection=mysql_connect($dhost, $duser, $dpassword) or die("Could not Connect to SQL Server Suleman");
$db=mysql_select_db($database, $connection) or die(" Check the Database Name from Config.php , wrong database entered ");
?>
But it is showing “Could not Connect to SQL Server Suleman”.
I had created one user by name suleman and made several changes , but still it is not connecting to SQL server, i am seeing same message.
I had tried doing many changes such as
try 1
$dhost=":/cloudsql/myapplication-2013:schoolnbd";
$duser="suleman";
try 2
$dhost="localhost";
$duser="root";
try 3
$dhost=":/myapplication-2013:schoolnbd";
$duser="root";
try 4
$dhost=":/cloudsql/myapplication-2013:myapplication-2013:schoolnbd";
$duser="root";
try 5
$dhost=":/cloudsql/myapplication-2013:myapplication-2013:schoolnbd";
$duser="suleman";
Error from the error log is
it is Showing mysql_connect(): Access denied for user 'root'#'localhost' (using password: YES) in /base/data/home/apps/s~myapplication-2013/1.371632998623546850/test.php on line 6
when I am using
$dhost=":/cloudsql/myapplication-2013:schoolnbd";
$duser="root";
i also faced the same problem, try without using password, it like this it will most probably work
$dhost=":/cloudsql/myapplication-2013:schoolnbd";
$duser="root";
$dpassword="";
but keep in mind , if you had set your root password then you should update root password, by typing this in console.
SET PASSWORD FOR 'root' = PASSWORD('');
If you are connecting using the root user, from AppEngine, you should not provide a password.

WAMP phpmyadmin error

I just installed WAMP server and for creating a new local database, when I clicked phpMyAdmin, i am getting this error,
Error
MySQL said: Documentation
1045 - Access denied for user 'root'#'localhost' (using password: YES)
phpMyAdmin tried to connect to the MySQL server, and the server rejected the connection. You should check the host, username and password in your configuration and make sure that they correspond to the information given by the administrator of the MySQL server.
Can some one please help me??
you are connecting to your local machine. If i am correct wamp does not set any credentials. So you should be fine to login with root and no password.
It's been a while since I have used WAMP (I only use Linux and OSX these days) I seem to remember the following...
In a command prompt enter in the mysql directory :
mysqladmin -u root password NEWPASSWORD
By default a mysql root password is not set yet the phpmyadmin pages are set to to connect with a password.
Dave
If you user has not password connect so. Try do it:
mysql_connect('server','user');
without thrid parameter. If all is right, you are connect to server.
else, if your user has password, you need provide the password correct
mysql_connect('server','user','password_correct');

Can't connect to database using my recently added user in MySQL

I'm trying to add a new user to a mysql database.
The original state of the file looks like this (which works):
$dconex=mysql_connect("200.xxx.xxx.xxx","abong1","XXXXXX");
mysql_select_db("abong1",$dconex);
I'm trying to add this line in between the mysql_select_db:
$dconex=mysql_connect("200.xxx.xxx.xxx","abong1_abongler","XXXXXXXX");
I configured the user in PhpMyAdmin with the correct IP, username, and password, but it doesn't seem to work. Is it something about the username? should it have a 'root' like "abong1.abong1_abongler" ?
Thanks in advance.
You may need to run
flush privileges;
What error are you getting, specifically? Permission denied? Can't connect? Change your connection line to the following:
$dconex=mysql_connect("200.xxx.xxx.xxx","abong1_abongler","XXXXXXXX") or die(mysql_error());
and see what error gets spit out. If it's permission denied, then your user isn't configured properly (could be username, hostname, or password is incorrect). Connection refused would be MySQL not allow connections over TCP.

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. =)

Categories