How do I properly connect to a mysql database? - php

Here is my code:
<?php
require_once('config.php');
$link = mysqli_connect($db_host, $db_user, $db_pass, $db_name) or die ('Your DB connection is misconfigured. Enter the correct values and try again.');
?>
I have stored my host, username, password and database name in the separate file config.php. I know that the information is correct because I can connect to my database via putty, but I keep getting the error:
Warning: mysqli_connect(): (HY000/1045): Access denied for user
'blank'#'T9AF3.WPA.blank.Ca' (using password: YES) in
C:\xampp\htdocs\temp2\index.php on line 3 Your DB connection is
misconfigured. Enter the correct values and try again.
Note: 'blank' is there to protect my identity and is not a typing error.
Edit: I only used putty to test that my login information was correct. Putty has very little to do with my actual question.
Edit2:
<?php
$db_host = 'host';
$db_user = 'user';
$db_pass = 'pass';
$db_name = 'dbname';
?>
These are filers.

if you can login from putty, by that i think you mean localhost, and you can't from php file, which is i think trying to connect remotely, the problem most probably lies on your user not defined for your servers ip. your server, which you are trying to connect from is 'T9AF3.WPA.blank.Ca'
in mysql a username has a password and a location and mysql uses both to authenticate the user. you can have permission to connect locally but not remotely i.e. from another server. check with your system admin to make sure you have proper access defined from 'T9AF3.WPA.blank.Ca'.
Or you have not specified an IP Address for the server to listen to. It may only be listening on localhost of your server, instead of allowing remote connections. Check your mysql configuration and which IP address it is listening on.

Your mysql user must have proper rights on the database you are using

Related

MYSQL Connection not allowing IP Address

When I run the following from my webserver it runs fine:
$ip = "localhost";
$uname = user
$pw = user password
$tb = table name
$dbconn = mysqli_connect($ip, $uname, $pw, $tb) or die("Couldn't connect");
However, when I make the following change, I get the "Couldn't connect" error:
$ip = "X.X.X.X";
Where X is the Public IP of my web server. Even when I change it to:
$ip = "127.0.0.1";
I get the couldn't connect error.
Can anybody think of a reason this would be refusing the connection?
Thanks
EDIT:
I have looked on the server logs and get the following (when I do 127.0.0.1):
[26-Nov-2015 23:51:38 Europe/Moscow] PHP Warning: mysqli_connect(): (28000/1045): Access denied for user '*USERNAME*'#'127.0.0.1' (using password: YES) in /filepath/
Where 'USERNAME' is my db username in the correct format, (cpname_dbuser)
If you are trying to access Mysql from remote location then you have to enable Remote Mysql or Whiltelist your ip
Use mysqli_connect_error() to return last connection error
You need to grant privileges from the IP address of your webserver, open port 3306 on the firewall to allow the IP you are connecting from access to the database and you will need to bind MySQL to the IP address you are connecting to. You can do this in the MySQL configuration file (search for bind-address).
To grant privileges you can log in as MySQL root and type the following...
grant all privileges on <database>.* to <username>#<ip address> identified by 'yourpassword'
Where ip address is the address of the machine you are accessing the database from.

Cannot connect to MySQL database on remote server using php mysqli

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?

MySql and PHPMyAdmin password

I was given an username, password, database name and an url for accessing the phpmyadmin console for a specific application. Now I want to connect to the database, which ip address I also know.
I tried running the following php snippet (from my local installation of xampp), which gave me an error, saying:
"Warning: mysql_connect(): Access denied for user 'xxxxxxx'#'localhost' "
<html>
<head>
</head>
<body>
<?php
$username = "given_username";
$password = "given_password";
$db_name = "given_database_name";
$hostname = "xxxxx:3306"; //remote address
//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
echo "Connected to MySQL<br>";
?>
</body>
</html>
My question is: Are the password and username for mysql and phpmyadmin the same, and, am I running this correctly?
Looking at your question, I assume the php code and the MySQL server run on different machines. In that case, you have to create a remote user in your MySQL server.
Login to your MySQL console on the MySQL server:
mysql -u root -p
Grant access to remote user
mysql> GRANT ALL ON given_database_name.* TO given_username#'<ip address of the php server>' IDENTIFIED BY 'given_password';
Now you should be able to connect MySQL from remote machine. If your MySQL server is behind a firewall, make sure you open incoming port of MySQL (default is 3306).

Creating a Secure Connection to a Server using PHP

I am trying to query a database that's on a server that I have a Remote Desktop connection to. What is required to be able to query a database on this server using my php web application.
You need:
Database server application configured to allow connections from external hosts
Server-side network configuration tuned to allow connections from external hosts (firewall, NAT, ...)
Database user, which is granted access to database you are going to use
PHP application, which connects to your database server under appropriate user
Details depend on what database server are you using.
$link = mysql_connect($host, $username, $password);
There is nothing secure at all by the way.
I use something like this, but it's far from secure. I keep it in a seperate "connection.php" that are required once by other files.
$db_host = 'INSERT IP HERE';
$db_user = 'User';
//My sql password for testing purposes
$db_pass = 'PASSWORD';
//name of the database
$db_name = 'dbname';
//connection
$conn = new mysqli($db_host, $db_user, $db_pass ,$db_name);
if (mysqli_connect_errno())
{
printf("Error connecting to DB, Errorcode: %s\n", mysqli_connect_error());
exit;
}
use answer by Merch member - the php code above .
You need to grant privileges and to create user on server you are retrieving the database data from.
Connect to mysql :
in shell type (if you are root type root or whatever username):
mysql -u root -p
enter password,
when you are logged in :
create user someusername#your-client-ip identified by 'password';
grant all privileges on *.* to someusername#your-client-ip with grant option;
now you can use php code above to connect to remote server database (ip you just used to create mysql user your-client-ip)
When you are creating php variable for mysql connect - host use mysql port in the variable if just ip not connects your-client-ip:3306
good luck

Connecting to MySQL on a different server

I have two servers (virtual machines - I can remotely connect to these) - server 1, and server 2.
On server 1 I keep my webpages, and on server 2, I keep the databases.
I am currently trying to connect to a database on server 2 from server 1.
Here is my php code:
<?php
$dbhost = 'xxx.xx.xx.xx:xxxx';
$dbuser = 'xxxxxx';
$dbpass = 'xxxxxx';
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die('Error connecting to mysql' . mysql_error());
$dbname = 'database';
mysql_select_db($dbname);
?>
This is the error message I get when I try to connect:
Access denied for user 'xxxxxx'#'server1' (using password: YES)
I found it a bit puzzling that it says #server1 considering I'm trying to connect to server 2. Can anyone offer any insights?
Thanks
PS: They're both on windows 2008
MySQL factors in the host name of the connecting client when determining whether or not to grant access. A given username may be allowed to log in from the same machine that is hosting the database (i.e. localhost) but not allowed to connect when used from a remote system.
In your case, it sounds like user 'xxxxxx' is not authorized to connect from 'server1'. You could grant user 'xxxxxx' login rights from all hosts ('%'). However, it would be more secure to limit your that account's login rights to the specific host ('server1') or range of hosts ('%.mydomain.com' or '144.155.166.0/255.255.255.0') from which it needs to be used.
For more information on this aspect of MySql's authentication process, see http://dev.mysql.com/doc/refman/5.0/en/connection-access.html.
"Access denied for user 'xxxxxx'#'server1'" means that server1 don't have access to server2. You need to add new user on server2 which can connect from server1 (hostname: server1);
You need to give rights for that machine
Check it here
Remove this user and create a new one with 'xxxxxx'#'%'. Do this on both servers.
#'%' indicates that the user can connect from anywhere.

Categories