LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   connecting php to remote mysql server (https://www.linuxquestions.org/questions/programming-9/connecting-php-to-remote-mysql-server-493562/)

habibno05 10-18-2006 12:17 PM

connecting php to remote mysql server
 
I saw there were similar posts but didn't answer my question.
I am running php and mysql on the same server and I have access to the local database just fine. As for connecting to the remote server, I can telnet port 3306 and can also connect to mysql on the remote machine through command line no problems. I restored a copy of my databases from my local mysql to my remote mysql server using command "mysql -u -h -p". So it does not appear to be a permissions issue.

I thought maybe this was a module that was not installed. So I installed php-mysql and mod_auth_mysql with yum on the remote server then restarted mysql service. Still to no avail.

The error I am receiving from mysql_error() is:

Can't connect to MySQL server on 'servername' (13)

Any assistance would be greatly appreciated!

acid_kewpie 10-18-2006 12:31 PM

please try and post in the correct forum... this thread will soon be moved to a more suitable location.

jeremy 10-18-2006 12:40 PM

//moved. Out of curiosity - is there something we could have done better to make clear that this forum was only for Website Suggestions & Feedback?

--jeremy

trickykid 10-18-2006 12:41 PM

You need to allow access by adding the host in the mysql database, by default it's only locally for user accounts unless otherwise specified when you create user accounts.

habibno05 10-19-2006 12:11 AM

Quote:

Originally Posted by jeremy
//moved. Out of curiosity - is there something we could have done better to make clear that this forum was only for Website Suggestions & Feedback?

--jeremy

My mistake I thought this for user websites not the linuxquestions.org.

habibno05 10-19-2006 12:23 AM

Quote:

Originally Posted by trickykid
You need to allow access by adding the host in the mysql database, by default it's only locally for user accounts unless otherwise specified when you create user accounts.

Hello trickykid,
I had those permissions. That is how I connect to the remote mysql server and performed a restore using "mysql -u username -p -h hostname".

smozgur 09-17-2007 06:11 PM

Can't connect to MySQL server on 'servername' (13)
 
Quote:

Originally Posted by habibno05 (Post 2468875)
Hello trickykid,
I had those permissions. That is how I connect to the remote mysql server and performed a restore using "mysql -u username -p -h hostname".

Hi habibno05,

I am having exactly the same problem including same error message from php script while I am perfectly able to connect by using mysql command prompt: Can't connect to MySQL server on 'serverip' (13).

I have other linux and windows (apache) webservers that can connect to the same mysql server with php but only this server.

The only significant difference is PHP is compiled as CGI in the new server where the other ones that can connect are installed as Apache Module.

Have you ever found a solution for this problem?

Thank you!

jeremy 09-17-2007 08:27 PM

Are you using a "servername" that PHP is able to resolve?

--jeremy

smozgur 09-18-2007 03:15 AM

Quote:

Originally Posted by jeremy (Post 2895250)
Are you using a "servername" that PHP is able to resolve?

--jeremy

Hi jeremy,

Thanks for your reply!

I am using "server_ip" and this is the same method that I can use in the other servers successfully.

Shouldn't PHP resolve an IP?

jeremy 09-18-2007 08:05 AM

What connection string are you using to connect?

--jeremy

smozgur 09-22-2007 10:56 AM

Quote:

Originally Posted by jeremy (Post 2895709)
What connection string are you using to connect?

--jeremy

Same method that I am able to connect from other servers via php code:


PHP Code:

$server "serverip";
$dbname "database";
$user "username"//Allowed for the ip
$password "password";
$link mysql_connect($server,$user,$password) or die(mysql_error());
mysql_select_db($dbname); 

Suat

smozgur 09-24-2007 10:25 PM

Solution
 
It is explained in the following link:
http://www.ehow.com/how_2090983_conn...r-selinux.html

Shortly;

Reason: SELinux

It restricts Apache remote database connection as default. So our scripts work in command line (# php sqltest.php), and mysql can connect to server but when Apache is involved (calling a php script as web page) then it doesn't work.

Setting:
setsebool -P httpd_can_network_connect=1

I hope it helps someone.

Thanks.

feri_yanto 10-07-2008 10:25 PM

thank you very much
it works

smozgur 10-08-2008 07:49 AM

Quote:

Originally Posted by feri_yanto (Post 3303634)
thank you very much
it works

Glad to hear it helps!

olidev 07-31-2018 05:04 AM

To establish remote mysql database connection for your PHP website, you must first whitelist the IP of server you will use to connect to the database remotely. You can use following code to configure the remote connection

Code:

<?php

function getdb(){

$servername = "46.101.5.233"; // put your cloudways server IP here
$username = "qxxfumxxxbd";
$password = "xxxxbQxxmM";
$db = "qxxfumxxxbd";

try {

    $conn = mysqli_connect($servername, $username, $password, $db);

    //echo "Connected successfully";

  }

catch(exception $e)

  {

  echo "Connection failed: " . $e->getMessage();

  }

  return $conn;

}

Next, you can use mysql client to connect to the database, like mysql workbench or sqlyog


Source: remote mysql connection setting


All times are GMT -5. The time now is 01:19 AM.