简体   繁体   中英

how to connect to database on another server

Could I have my php scripts on server A and connect to the MySQL database on server B?

If yes, how it would be done? Thanks in advance

its simple all thise above techniques are quite complicated

suppose you have database on server B and website on server A(say it has IP 192.234.12.1)

on cpanel whitelist the IP of server B

and create a new user having sufficient privileges in database (say this user is test)

then create this user as test@192.234.12.1

Yes.

The same way you access the localhost on the same server, you change the database host to the external one. This is more a configuration issue, you need to grant your database user remote access to your MySQL, you also need to make sure your firewall allows connections on the MySQL port.

Example on Debian: http://www.debianhelp.co.uk/remotemysql.htm

Yes it can be done.

Find out the IP address of the server A where your scripts will be uploaded. Do not forget to change the localhost to the ip address of the Server B in mysql_connect() or mysqli_connect() method.

Now go the control panel of the Server B where your Database is.

In the control panel's Homepage go the databases section and click the Remote MYSQL option.

Then add the Ip address of the Server A and click on add host.

Now you can access to the database in Server B while your scripts are running in Server A. Mind you the fetched result will be slow cause it is getting data from database that is located on another server.

Your welcome

Just don't the hostname of the other box for the connection. Details depend on the extension you're using:

$mysql = mysql_connect($host, $user, $pass);
$mysqli = new mysqli($host, $user, $password, $schema);
$pdo = new PDO("mysql:host=$host", $user, $pass);

Make sure that the user is allowed to access by the MySQL server ( CREATE USER ) and check that there's no firewall in the way.

That is all what you need .

(Even you can have your scripts on server A, your web server on server B and your database on server C ...)

Have a look here:

http://us2.php.net/manual/en/function.mysql-connect.php

You can either pass in the server hostname as an argument, or configure in php.ini.

I was having similar challenges but here is what work for me: To connect to server B from server A, First, you need to allow remote MySQL access hosts in cPanel (Server B), Home -> Databases -> Remote MySQL and also whitelist the IP in the firewall (That is IP Address of B server). Then the following php db connection should work.

$db_connect =  mysqli_connect("serverB.com", "dbuser", "dbpassword", "dbname");
// Evaluate the connection
if (mysqli_connect_errno()) {
    echo mysqli_connect_error();
    exit();
}else{
   //successful connection
    echo "Yes, successful";
}

Its a perfect solution for connecting another database from other servers.

$dbserverName = "191.238.0.2";    
$dbUsername = "lauranco_L2L";
$dbPassword = "SL92TIW5T96L";
$dbName = "lauranco_siteBits";

Good old thread.

Still - of all the answers appearing here, nothing addresses about the security .

It is highly insecure to open up the mysql port to outside the server.

The most secure option is to keep the mysql port open to one and only localhost in all servers.

And have another php running inside the second server, make it create the desired output and deliver the same to your php (running in the first server).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM