how to connect to database on another server

前端 未结 8 1167
孤独总比滥情好
孤独总比滥情好 2020-12-03 02:05

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

相关标签:
8条回答
  • 2020-12-03 02:25

    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";
    }
    
    0 讨论(0)
  • 2020-12-03 02:28

    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.

    0 讨论(0)
  • 2020-12-03 02:30

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

    $dbserverName = "191.238.0.2";    
    $dbUsername = "lauranco_L2L";
    $dbPassword = "SL92TIW5T96L";
    $dbName = "lauranco_siteBits";
    
    0 讨论(0)
  • 2020-12-03 02:34

    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

    0 讨论(0)
  • 2020-12-03 02:34

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

    0 讨论(0)
  • 2020-12-03 02:37

    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

    0 讨论(0)
提交回复
热议问题