Cannot connect to MySQL database on remote server using php mysqli - php

I am trying to use mysqli to insert some data into a MySQL database (let's call the schema myDatabase), but cannot successfully connect. Here's the code snippet to connect:
...
$config = parse_ini_file('../includes/config.ini');
$username = $config['username'];
$password = $config['password'];
$dbname = $config['dbname'];
$server = $config['server'];
$conn = new mysqli($server, $username, $password, $dbname);
if (!$conn || $conn->connect_error) {
die( 'Connection Failed: ('.$conn->connect_errno.') '.$conn->connect_error);
}
...
I get the following result:
Connection Failed: (1045) Access denied for user 'myUser'#'my.laptop.ip.address' (using password: YES)
Here's some details on the set-up, in case they are relevant:
The code is on my laptop running Windows 7 and using PHP 5.3.5 that came with xammpp.
The database is hosted on a remote server with MySQL5.1.52. I created a user to which I granted all privileges on myDatabase.*. No host was specified for the user (e.g. 'myUser'#'%'), as I am still in development and don't know the ip address where the code for the live application will be hosted.
If I ssh onto the database server, I can connect to mysql using the credentials for myUser and access the tables in myDatabase. I have another schema on this same server which is accessed by a different user, and have been able to use mysqli to connect without any problems.
Just to be sure it wasn't a typo, I dropped the user, and created it again, copying and pasting the username and password from the config.ini file used in my php code (and flushed privileges, of course). I did this again, except this time the host was specified, e.g. CREATE USER 'myUser'#'my.laptop.ip.address' IDENTIFIED BY 'myPassword'. I keep getting the same error and now I'm completely stumped.
Help, please.

On your mysql machine hit:
GRANT ALL PRIVILEGES ON database.* TO 'myUser'#'%' IDENTIFIED BY 'newpassword';
FLUSH PRIVILEGES;
This will allow the user to connect from any host. Once it works, you can limit it to just a specific host and database.

Okay, this is strange, but it appears the problem had to do with the password I was using. The original one contained some special characters ($, & +). When I changed it so that it only contained numbers, letters and underscore, it worked.
Is this real, or did I accidentally do something else without realizing that turned out to be the actual solution?

Related

When trying to login to my hosted site using PHP, I get this 'Connection Failed' error message

This is my first time using this site so apologies if I break any sort of unspoken rules as a new user, but I have been plagued with a particular problem that I can't seem to find the answer to using Google.
So I am designing a website for a friend of the family and this site uses a login/account system with phpMyAdmin databases to store all of the users and check information when logging in. I am able to login perfectly fine when I host the website on localhost using XAMPP, however when I host the server on my domain and try to press the login button using the same credentials, I receive the following error message:
Connection failed: Access denied for user 'root'#'localhost' (using password: NO)
For reference, I am using Sentora to VPS host my website. I have exported the database I am using to store the login information from my Localhost phpMyAdmin into my Sentora phpMyAdmin. In order to connect to the database, I use the following code as an included PHP file that activates whenever a database needs to be accessed:
<?php
//Database Handler
$servername = "localhost";
$dBUsername = "root";
$dBPassword = "";
$dBName = "zadmin_barber";
$conn = mysqli_connect($servername, $dBUsername, $dBPassword, $dBName);
if(!$conn){
die("Connection failed: ".mysqli_connect_error());
}
Whenever I try to search up the cause of this problem on Google, I am only able to find tutorials for when this error appears when logging into phpMyAdmin. I, however, have no problem logging into my phpMyAdmin. I have tried changing the settings on both privileges and users in phpMyAdmin, but no combination allowed me to login with any credentials. For more reference, here is a picture of my phpMyAdmin users:
My phpmyadmin users:
And here is a picture of the privileges of the database I am trying to access:
Database prvileges:
If you look at your picture there is no access to the database for Any users from the host "localhost".
It is not recommended to connect to DB without any password. Simply create new user with any username, assign it a password, make sure it is "localhost" in the host settings.
Then open users click edit privileges and give all the privileges to the database zadmin_barber
And one thing, don't use root to connect to database from your scripts. Creating user in MySQL is very easy.

Access denied for user 'root'#'localhost' (using password: YES) using MAMP

I am new to php and can't seem to solve this issue that I have.
Basically I want to build a data base and access it on my website.
I downloaded MAMP on my mac to run the site locally until I publish my site.
I made a database using phpmyadmin.
The database is a table that consists of 5 rows and 4 columns (but will increase in the future).
The following is my code to connect to database called hello:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "hello";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
} else {
echo "connected to database";
}
?>
However I get this error as it seems that I cannot connect to the database.
Connection failed: Access denied for user 'root'#'localhost' (using password: YES)
I have tried different things like adding in a space for the password or setting it to root but they don't seem to work.
I dont understand how there is a password set on the database when I never set one.
I dont understand how there is a password set on the database when I never set one.
Because MAMP sets one by default.
Their documentation also tells you how to change it:
Open the Mac OS Terminal Application located in your /Applications/Utilities directory.
Enter the following command line: /Applications/MAMP/Library/bin/mysqladmin -u root -p password
[NewPassword]
Replace [NewPassword] with your new password.
Please bear in mind that you also have to change the phpMyAdmin configuration and probably your own php scripts to use the newly
created MySQL password. For changing the phpMyAdmin configuration edit
the following file and update the password. /Applications/MAMP/bin/phpMyAdmin/config.inc.php
… don't write applications that log in as root though. Create a new user with just as many permissions as they need.

Issue connecting to MySQL with mysqli in PHP [duplicate]

This question already has answers here:
Warning: mysqli_connect(): (HY000/1045): Access denied for user 'username'#'localhost' (using password: YES)
(25 answers)
Closed 6 years ago.
Been going through tons of answers but none have exactly matched my issue.
I have a MySQL database running and am attempting to connect in PHP with mysqli. The connection code is as follows:
<?php
if(!isset($_COOKIE["uid"])) {
$servername = "localhost";
$username = "study";
$password = "somepassword";
$dbname = "user_study";
$conn = new mysqli($servername, $username, $password, $dbname);
if($conn->connect_error){
die("aww");
}
}
However every time I get the error
mysqli::mysqli(): (28000/1045): Access denied for user
'study'#'localhost' (using password: YES)
I know that the user 'study'#'localhost' has permissions to access this database, because I am able to access it through the MySQL command line just fine. I have tried other accounts such as roots with the same result.
Is there anything else I should be checking?
New information (2/23)
It seems that even when I shut the database down I'm getting an access denied result, meaning it seems to be trying to connect to some other database on the server. How would I ensure it is connecting to the correct one?
I think the error message indicates that PHP was able to contact the MySQL Server using the socket file. (It would have been a different error otherwise.)
There's a couple of reasons you could get this error. If we can successfully connect to MySQL Server with a the mysql command line client like this:
> mysql --no-defaults -h localhost -u study -psomepassword user_study
That's going to rule out a lot of the possible reasons for the failure.
The most reasonable explanation for the error message from PHP is that the password being provided in the connection attempt from PHP does not match the password MySQL is expecting.
Some ideas we should be able to rule out. Privileges on the user_study database have been granted to 'study'#'localhost', e.g.
GRANT SELECT ON user_study.* TO 'user'#'localhost'
On a totally different tack, given the assignment statement:
$password = "somepassword";
And assuming that you wouldn't be supplying the actual password in the question... we're left wondering if the actual password contains characters that are subject to PHP string interpretation, such as backslash character, or a dollar sign.
For debugging, I suggest doing an echo $password; following the assignment, and verify that the string emitted is what is expected.
Another possibility is that there isn't an exact match in the mysql.user table, and the user 'study' is actually matching to a different mysql.user... an entry with an empty user ''#'localhost'.
I'd be taking a look at all of the entries in the mysql.user table where user='study' and user=''.
I also want to rule out the possibility that the mysql command line client using a .mylogin.cnf file.
I'm also tempted to suggest that changes were applied to the mysql.user table and a FLUSH PRIVILEGES statement wasn't executed... but that doesn't jive with the behavior (successful connection) observed in the mysql command line client.
We're assuming obviously that the MySQL Server is running local, on the same machine that PHP is executing on. And we're expecting to connect via the local socket file.
As a test, I'd suggest connecting via TCP. Specifying host as 127.0.0.1. That would require a different entry in the mysql.user table. We'd test connection from the mysql command line client:
> mysql --no-defaults -h 127.0.0.1 -u study -psomepassword user_study
But this gets into a whole host of other configuration issues with the MySQL Server, networking enabled, bind address, DNS name resolution, listening port, iptables, firewall, et al.
--
If the problem was an unsupported authentication protocol, I'd expect a different error. If the problem was the inability to connect to the socket file, I'd also expect a different error.
All of the usual causes for this error seem to be ruled out by a successful connection from the command line client, running on the local machine, connecting to using the same credentials.

PHP MySql (1045) Access Denied For User

I've tried to search for an existing answer to this problem, but the answers I find have not worked thus far.
I've been attempting to use PHP to connect to a MySql database. My web host uses cPanel on Linux. The code I'm using to do this seems standard enough:
$mysqli = new mysqli("localhost", "cPanelUsername_dbUsername", "dbPassword", "cPanelUsername_dbName");
I've been getting the following error:
Failed to connect to MySQL: (1045) Access denied for user 'cPanelUsername_dbUsername'#'localhost' (using password: YES)Access denied for user 'cPanelUsername'#'localhost' (using password: NO)
"localhost" is the host server where the MySql server is located (it seems like this works)
"cPanelUsername" is my cpanel username
"dbUsername" is the database user, which I added to the database with all permissions granted
"dbPassword" is the database password for dbUsername
"dbName" is the database name
I ended up adding my cPanel username before the dbName and dbUsername after searching for answers to this issue elsewhere.
It looks like I have everything set up correctly but it's not connecting (with the error above). I don't have any direct control over the server that I wouldn't have to ask my web host about, which may take a few days to get sorted out. Do I have something wrong with my connection code?
First check the database that you gave the proper user access to your database, which is given from Add User to databases from Mysql database section in cpanel.
after that check it again,
first try normal connection code in php,
$con = mysql_connect("localhost","cpanel_username","cpanel_password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
In cPanel, make sure that:
The database user cPanelUsername_dbName exists, with the password dbPassword
The database you want to use exists.
The user cPanelUsername_dbName is allowed to access the database.
The user cPanelUsername_dbName is allowed to access the database from localhost, 127.0.0.1, and the IP address of your server.
Your MySQL connections may use 127.0.0.1 or the IP address of your server, and MySQL will reject the connection if access isn't granted for the specific IP address used.
check the database name spelling at your phpMyAdmin. Usually the name is in format user_dbname.
For example:
cpanel username: jack,
database created: student
In your php script, the dbname should be jack_student
This worked for me:
Depending on what MySQL version you have, make sure you have matching password and hostname on your PHP file, config.inc.php file.
If you need to change the password for MySQL 5.7.6 and later:
ALTER USER 'root'#'localhost' IDENTIFIED BY 'MyNewPass';
MySQL 5.7.5 and earlier:
SET PASSWORD FOR 'root'#'localhost' = PASSWORD('MyNewPass');
If you are trying to access your localhost server from a different machine i.e. (emulator, another computer), make sure you are using the actual IP address of the the localhost, DO NOT USE localhost as the hostname. Because this is like telling the machine to connect to itself - but the server is on a different IP address.

PDO access denied to external host, but I can access to phpmyadmin cp

I'm trying to connect to an external database through this script:
$dsn = 'mysql:host=xxx.xxx.xxx.xxx;dbname=dbname';
$user = 'user';
$password = 'pass';
try {
$pdo = new PDO($dsn, $user, $password);
$pdo->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING );
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
die();
}
Regarding the data (host, username, database and password) everything is correct because when I tip xxx.xxx.xxx.xxx/phpmyadmin on the browser and I enter the user and password it let me in to the database with permissions to create, delete and such.
But when I try to connect through PDO it gives me the "Connection failed: SQLSTATE[28000] [1045] Acess denied for..."
The information displayed on the PHPMYADMIN panel is as follows:
MySQL
Server: Localhost via UNIX socket
Server version: 5.5.20
Protocol version: 10
User: user#localhost
MySQL charset: UTF-8 Unicode (utf8)
Web server
Apache
MySQL client version: 5.5.20
PHP extension: mysqli
Is it possible that I'm forced to use mysqli due to that "PHP extension: mysqli" property?
Thanks in advance!!!
Nothing seems wrong with your code except the setAttribute method is setting the error output to raise warnings.. but you're in a try/catch block.. so you may want to consider using PDO::ERRMODE_EXCEPTION.
Secondly, check your remote permissions for that username/password. There may be an IP limitation for that user account. You can use the wildcard % for the HOST on user accounts or (suggested) provide the server IP that is connecting TO the database.
I would venture a guess that phpmyadmin is working properly because phpmyadmin is on the host you're trying to connect to. My guess is that the server or user account is configured to only have access via 127.0.0.1/localhost - you can another record for the user you are trying to connect with and provide a wildcard or specific host for that user. Based on the information you've provided here, it seems like you'll just have to get MySQL setup to accept connections from other hosts.
Meanwhile I've tried to run the application on local but it seems that it can't connect either, so maybe this can be of some help when it comes to identify the problem above.
This time (in localhost), the error is:
Connection failed: SQLSTATE[HY000] [1045] Access denied for user 'xxxxxxx_xxxxxxx'#'localhost' (using password: YES)
I created the user on my own phpmyadmin granting him all the permissions over this database, so I can't understand why even in localhost I can't connect to it. Can it be related to the problem above or is it something completely different that should go on a different topic? Thank you!

Categories