2016-04-28 81 views
0

我正在用Phonegap進行我的第一個實驗。我想構建一個從MySql服務器檢索數據的應用程序,但似乎問題在於我無法連接到我的dB。Phonegap。無法訪問遠程mysql數據庫

當我建立網站很容易。我使用PHP和下面的代碼:

$conn = new mysqli($servername, $username, $password); 

其中$servernamelocalhost

但隨着PhoneGap的localhost會當然不是工作,所以我必須使用hostnameIP address

這就是問題所在。我有一個IP的VPS和主機名,假設,vps.my-domain.com。兩者都不會讓我訪問MySql數據庫,我不明白爲什麼。

下列串報告:

'SQLSTATE [HY000] [2005]未知MySQL服務器主機 'vps.my-domain.com:3306'(20)'

$conn = new mysqli("xxx.yy.kkk.qqq", $username, $password); 
$conn = new mysqli("vps.my-domain.com", $username, $password); 

我代碼(HTML + Jquery + Ajax + PHP)在我的VPS上運行時運行良好,我使用localhost,但在使用IP addresshostname時失敗。

我也試圖與mySQLjs

MySql.Execute(
    "http://xxx.yy.kkk.qqq", 
    "username", 
    "password", 
    "phonegap", 
    "select * from test", 
    function (data) { 
     console.log(data) 
    }); 

,但仍然沒有成功。

我在mySQLjs找到的演示代碼工作正常,所以我很確定我錯過了關於我的連接的一些事情。

如何使用IP addresshostname而不是使用localhost訪問我的MySql db?是否有我應該在我的VPS上設置的配置?

+1

請不要設計從互聯網上的隨機設備直接連接到MySQL的應用程序。這是非常危險的。 MySQL沒有針對攻擊強化,並且很容易被某人破壞。訪問控制幾乎完全不可能,因爲如果有人需要寫入權限,則不能將其鎖定到不同的記錄組。您至少需要使用應用程序與之交互的一些精簡API。 JSON是一個非常流行的選擇,因爲它非常輕便,HTTPS是連接它的最普遍的方法。 – tadman

回答

0

解決

許多temptatives後,我的工作了。

由於安全原因,默認情況下遠程訪問MySQL數據庫服務器被禁用。

1-您需要編輯MySQL配置文件並允許從不同於本地主機的源訪問。

nano /etc/mysql/my.cnf 

2-更改的行bind-address到:

bind-address = your_ip_number   i.e. bind-address = xxx.yy.qqq.tt 

3-重啓MySQL:

/etc/init.d/mysql restart 

4-與phpMyAdmin創建的主機是你的IP地址,這樣用戶就看起來如下:

[email protected]_ip_number 

下面有代碼(HTML + PHP + CONFIG.XML)

HTML

<!DOCTYPE html> 
<html lang="en"> 
    <head> 
     <meta charset="UTF-8"> 
     <title>Fetch MySQL data into Phonegap app</title> 

     <script src="jquery.min.js"></script> 
     <script src="phonegap.js"></script>  <!-- When you build the app with build.phonegap.com remove the phonegap.js file from the package you are going to upload but keep its reference --> 

    </head> 
    <body> 
     HELLO WORLD! 

     <div id="output"></div> 

     <script> 
      $(document).ready(function($){   

       var output = $('#output'); 

       $.ajax({ 
        url: 'http://your_domain.com/get.php',  // FULL PATH! 
        dataType: 'jsonp', 
        jsonp: 'jsoncallback', 
        timeout: 3000, 
        success: function(data, status){ 
         $.each(data, function(i,item){ 
         var landmark = '<h1>'+item.title+'</h1>' 
         + '<p>'+item.description+'<br>' 
         + item.url+'</p>'; 

         output.append(landmark); 
         }); 
        }, 
       error: function(){ 
        output.text('There was an error loading the data.'); 
       } 
      }); 
     }); 
    </script> 
</body> 

PHP

<?php 
    header("Access-Control-Allow-Origin: *"); 

    $db_username = 'the_user_you_created'; 
    $db_password = 'the_password'; 
    $db_name  = 'the_db_name'; 
    $db_host  = 'your_ip_number'; 

    $mysqli = new mysqli($db_host, $db_username, $db_password, $db_name); 
    if ($mysqli->connect_error) { 
     die('Error : ('. $mysqli->connect_errno .') '. $mysqli->connect_error); 
    } 

    // Run the query and fetch the data. $html contains the result 

    // Convert it to JSON and remember to add the 'jsoncallback' string 
    echo $_GET['jsoncallback'] . '(' . json_encode($html) . ');'; 

?> 

CONFIG.XML這是配置Phonegap的文件

<?xml version="1.0" encoding="UTF-8" ?> 
<widget xmlns = "http://www.w3.org/ns/widgets" 
    xmlns:gap = "http://phonegap.com/ns/1.0" 
    id   = "com.phonegap.example" 
    versionCode = "10" 
    version  = "1.0.0" > 

    <!-- versionCode is optional and Android only --> 

    <name>Your app</name> 

    <description> 
     My first MySql connection with Phonegap 
    </description> 

    <author href="http://www.my_domain.com"> 
     Its me 
    </author> 

    <!-- The two following lines make the difference! Important --> 
    <gap:plugin name="cordova-plugin-whitelist" source="npm"/> 

    <!-- In my esperience only * worked. Using the IP address (http://xxx.yy.qqq.tt or simply xxx.yy.qqq.tt) did not work --> 
    <access origin="*" subdomains="true" /> 

</widget> 

使用您的帳戶將HTML文件,XML文件和JQuery.min.js壓縮並上傳到build.phonegap.com。

希望我幫助別人!