MYSQL Connection not allowing IP Address - php

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.

Related

what is error when connecting database with localhost and i can connect database with host as my local IP address? [duplicate]

This question already has answers here:
MYSQL Access denied for user 'root'#'localhost'
(3 answers)
Closed 5 years ago.
My php page and mysql server are in same server.
I can connect database using local IP , it works.
but, when trying to connect with localhost it show the following error
Connection failed: Access denied for user 'root'#'localhost'
changed mysqld.cnf file
as BIND ADDRESS = 0.0.0.0
I gave the following permission
GRANT ALL PRIVILEGES ON . TO root#localhost IDENTIFIED BY 'password' WITH GRANT OPTION;
GRANT ALL PRIVILEGES ON . TO root#my local IP IDENTIFIED BY 'password' WITH GRANT OPTION;
I don't know what is problem in my code and configuration ...
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$database="db";
// Create connection
$conn = new mysqli($servername, $username, $password, $database);
// Check connection
if ($conn->connect_error){
die("Connection failed: " . mysqli_connect_error());
} else {
echo 'connected successfully';die;
}
mysqli_close($conn);
?>
Have you tried servername 127.0.0.1?
From the mysql manual:
On Unix, MySQL programs treat the host name localhost specially, in a way that is likely different from what you expect compared to other network-based programs. For connections to localhost, MySQL programs attempt to connect to the local server by using a Unix socket file. This occurs even if a --port or -P option is given to specify a port number. To ensure that the client makes a TCP/IP connection to the local server, use --host or -h to specify a host name value of 127.0.0.1, or the IP address or name of the local server.1

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

Can't connect remote MySql server

I have some troubles to connect to a remote mysql server.
This is the situation :
I have 1 MySql server which is working (Installed phpMyAdmin, and can do actions on it).
I have 1 webserver
I try to access to my mysql server with the mysql_connect(); function :
$host = 'my_ip:ext_port';
$user = 'leUser';
$pass = 'lePassword';
$mydb = 'leDb';
$db = mysql_connect($host, $user, $pass);
Where ext_port is the port I open in my MySql server and which redirect to the port 3306.
I also give to my user the access with :
grant all privileges on *.* to 'leUser'#'%' IDENTIFIED BY PASSWORD '*lePasswordEncrypted' WITH GRANT OPTION
Tried a lot of things in vain (As comment the line bind-address in my.cnf, etc ...) but still get the error :
Can't connect to MySQL server on 'xxx.xxx.xx.xx' (4)

Can't connect to MySQL server with php but can through HeidiSQL

When I try to connect to a website database using the code:
$server = "domain";
$login = "username";
$password = "password";
$database = "databasename";
$port = 3307;
$con = mysqli_connect($server, $login, $password, $database, $port);
and have all warnings and errors enabled I get a message saying
Warning: mysqli_connect(): (HY000/2003): Can't connect to MySQL server on 'domain' (111) in 'path'
however when I change $server to localhost it works fine.
If it is blocking me through php unless I use localhost does anyone know why the HeidiSQL or MySQL Administrator clients can connect using the domain?
What I understand from your problem statement, you are unable to connect to MySQL server using domain or IP address.
To resolve it, first you need to give database access permission to user. Checkout below sql which will do work for you.
GRANT ALL PRIVILEGES ON `databasename`.* TO 'username'#'domain' IDENTIFIED BY 'password' WITH GRANT OPTION;
Once permissions are granted to user check your MySQL configuration settings. Edit your MySQL confifuration file my.cnf and set nbelow mentioned settings.
Set the bind-address to 0.0.0.0 or comment it out:
bind-address = 0.0.0.0
OR
# bind-address = 127.0.0.1

How do I properly connect to a mysql database?

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

Categories