user10198975
user10198975

Reputation:

Access MySql database on the Server A from PHP mysqli on Server B with different PHP version

I'm trying to figure out, how is possible to retrieve records from MySql database tables, located on the server A, which is PHP Version 5.3.29 from server B php mysqli PHP Version 7.2.8

I found this example How to Connect to the Remote MySQL Database using PHP, but I don't understand, how it works in relation to what I'm trying to do, doubt if it is what I'm looking for

I'm new with php, and all that I can imagine, I should be to use connection data from server A for php mysqli on server B, and add access host IP of server B in Remote MySQL of server A, and seems like I have to add database user and password in MySQL databases on server A, but I'm not sure, where do I have to get it on server B

Server A:

Version by phpinfo();: PHP Version 5.3.29

Database server

Server: Localhost via UNIX socket
Server type: MySQL
Server version: 5.6.39 - MySQL Community Server (GPL)
Protocol version: 10
Server charset: UTF-8 Unicode (utf8)

Web server

cpsrvd 11.70.0.51
Database client version: libmysql - 5.1.73
PHP extension: mysqliDocumentation curlDocumentation mbstringDocumentation
PHP version: 5.6.30

phpMyAdmin

Version information: 4.7.7

Server B:

Version by phpinfo();: PHP Version 7.2.8

Database server

Server: Localhost via UNIX socket
Server type: MariaDB
Server version: 5.5.56-MariaDB - MariaDB Server
Protocol version: 10
Server charset: UTF-8 Unicode (utf8)

Web server

nginx/1.11.10
Database client version: libmysql - mysqlnd 5.0.11-dev - $Id: 
PHP extension: mysqliDocumentation curlDocumentation mbstringDocumentation
PHP version: 5.6.30

phpMyAdmin

Version information: 4.6.6

Any advice, guide or example would be very helpful

Edit:

My attempt by answer, first of all I've added IP of server B in Remote MySQL on server A. My conn.php, located on Server B to connect to Server A:

<?php
$servername = "Server_A_IP";
$username = "Server_A_database_user";
$password = "Server_A_database_user_password";
$db = "Server_A_database";

$conn = new mysqli($servername, $username, $password, $db);
$conn->set_charset('utf8');

if(!$conn)
{
    die ("Error on the Connection" . $conn->connect_error);
}
?>

and dbtab.php also located on Server B with conn.php in same folder:

<?php
 include 'conn.php';
$sql = "SELECT * FROM tab";
$result = $conn->query($sql);
?>
<!DOCTYPE html>
<html>
    <h1 align="center">Database</h1>
    <table border="1" align="center" style="line-height:25px;">
    <tr>
    <th colspan="2">Records</th>
    </tr>
    <tr>
    <th bgcolor="#ff897f">id</td> 
    <th bgcolor="#ff897f">name</td>   
    </tr>
<?php
if($result->num_rows > 0)
{
   while($row = $result->fetch_assoc())
   {
    ?>
     <tr>
        <td bgcolor="#e5cac8"><?php echo $row['id']; ?></td>
        <td bgcolor="#e5cac8"><?php echo $row['name']; ?></td>
    <?php
   }
}
else
{
    ?>
    <tr>
    <th colspan="2">There's No data found!!!</th>
    </tr>
    <?php 
}
?>
</body>
</html>

but in result, when I'm tying to open www.site.com/folder/dbtab.php or www.site.com/folder/conn.php, page does not load, just freezes

ping of Server A:

enter image description here

Upvotes: 1

Views: 2242

Answers (1)

M. Biegert
M. Biegert

Reputation: 36

You don't need php on Server A for this. You just need to configure your MySQL Server (Server A) to

  1. allow network access (via IP-addresses) and
  2. create a MySQL Account for the IP-address of Server B

If you have set up this you can create a connection from php on Server B by using

$mysqli = new mysqli("<IP of Server A>", "user", "password", "database");

see the excellent php website on this: mysqli Connections

The steps 1, 2 depend on your MySQL Server. If you have direct root access you need to edit the configuration files and allow networking, otherwise it depends on the hosting service.

To create a user account with remote access issue the following SQL statement (or use a web GUI):

GRANT <Rights> ON <db-name>.* TO 'username'@'<IP of Server B>';

see the MySql manual on granting privileges to users: https://dev.mysql.com/doc/refman/5.5/en/grant.html

Upvotes: 2

Related Questions