0

Here is the PHP code which I have written :

<?php
$con=mysqli_connect("193.167.138.9","username","password","DatabaseName");
// Check connection
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

// escape variables for security
$t_name = mysqli_real_escape_string($con, $_POST['name']);
$t_email = mysqli_real_escape_string($con, $_POST['email']);
//$age = mysqli_real_escape_string($con, $_POST['age']);
$ip = getenv("REMOTE_ADDR") ;
$date_time = date("l j F Y g:ia", time()- date("Z")) ;

$sql="INSERT INTO IP_TIME (Name, MAIL_ADDRESS,IP_ADDRESS,DATE_TIME)
VALUES ('$t_name', '$t_email', '$ip', '$date_time')";

if (!mysqli_query($con,$sql)) {
  die('Error: ' . mysqli_error($con));
}
echo "Check the IP_TIME table";

mysqli_close($con);
?>

Here 193.167.138.9 is the IP of the server which I am using. Initially when I was testing it on my local machine, in the place of "193.167.138.9", it was "localhost.

When I try to run the code on the server, it displays the following error message.

Failed to connect to MySQL: Can't connect to MySQL server on '193.167.138.9' (111)Error: 

Any idea what has gone wrong? and how can I fix it?

I checked it in the mysql config file, especially the my.conf file , which looks like below :

[mysqld]
#
# * Basic Settings
#
user            = mysql
pid-file        = /var/run/mysqld/mysqld.pid
socket          = /var/run/mysqld/mysqld.sock
port            = 3306
basedir         = /usr
datadir         = /var/lib/mysql
tmpdir          = /tmp
lc-messages-dir = /usr/share/mysql
skip-external-locking
#
# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
#
bind-address           = 127.0.0.1

Not sure what is preventing the mysql to be connected. Any help/ suggestion would be helpful

kingmakerking
  • 2,017
  • 2
  • 28
  • 44

4 Answers4

0

You have to change the bind-address to

bind-address           = 193.167.138.9
Jens
  • 67,715
  • 15
  • 98
  • 113
  • Then it shows the following error "Failed to connect to MySQL: Lost connection to MySQL server at 'reading initial communication packet', system error: 0Error: " – kingmakerking Jun 30 '14 at 08:04
  • See this [Thread](http://stackoverflow.com/questions/5755819/lost-connection-to-mysql-server-at-reading-initial-communication-packet-syste) look like a problem with the firewall. – Jens Jun 30 '14 at 08:06
  • This is the correct answer, MySQL can only be bound to either 0, 1 or *all* ip addresses. You can bind it to ip **and** local unix socket, tho, using `bind-address` & `socket` option like you already have. – Daniel W. Jun 30 '14 at 09:50
0

You should comment the bind-address like this

# bind-address = 127.0.0.1

or change bind-address with your SERVER IP address

bind-address = 193.167.138.9
Ranjith
  • 2,779
  • 3
  • 22
  • 41
0

Just an idea, but have you configured your user to be able to connect remotely to the database? E.g. configure the host to be '%' for any host (since you mention that you tested it with localhost before)?

0

you need to create a user on mysql on your server machine with your requesting machine ip address,like below

grant all privileges on *.* to 'user'@'171.15.12.3' identified by 'password';
Ashok sri
  • 73
  • 7