PhoneGap的。无法访问远程mysql数据库 [英] Phonegap. Cannot accessing remote mysql database

查看:440
本文介绍了PhoneGap的。无法访问远程mysql数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用Phonegap做我的第一个实验。我想构建一个从MySql服务器检索数据的应用程序,但似乎问题是我无法连接到我的dB。



当我建立网站很容易。我使用PHP和以下代码:

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

其中 $ servername localhost



但是使用Phonegap localhost 当然不行,所以我必须使用主机名 IP地址



这是问题。我有一个带有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地址主机名



我还尝试过 mySQLjs

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

但还没有成功。



我在 mySQLjs 中发现的演示代码工作正常,所以我很确定我错过了一些关于我的连接。



如何使用 IP访问MySql数据库地址主机名而不是使用 localhost ?有没有一个配置应该在我的VPS上设置?

解决方案

SOLVED



经过许多诱惑之后,我完成了它。



默认情况下,对MySQL数据库服务器的远程访问被禁用安全原因



1-您需要编辑MySQL配置文件,并允许来自与localhost不同的源代码访问。

  nano /etc/mysql/my.cnf 

2-更改行 bind-address to:

  bind-address = your_ip_number ie bind- address = xxx.yy.qqq.tt 

3-重启MySQL:

  /etc/init.d/mysql restart 

4-使用PHPMyAdmin创建一个用户哪个主机是您的IP地址,以便它似乎如下:

  phonegap @ your_ip_number 

下面是代码(HTML + PHP + CONFIG.XML)



HTML

 <! DOCTYPE html> 
< html lang =en>
< head>
< meta charset =UTF-8>
< title>将MySQL数据提取到Phonegap应用程序< / title>

< script src =jquery.min.js>< / script>
< script src =phonegap.js>< / script> <! - 当您使用build.phonegap.com构建应用程序时,将从要上传的包中删除phonegap.js文件,但请保留其参考 - >

< / 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);
});
},
错误:function(){
output.text('加载数据时出错。
}
});
});
< / script>
< / body>



strong>

 <?php 
头(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);
}

//运行查询并获取数据。 $ html包含结果

//将其转换为JSON,并记得添加'jsoncallback'字符串
echo $ _GET ['jsoncallback']。 '('。json_encode($ html)。');';

?>

CONFIG.XML 这是Phonegap的配置文件

 <?xml version =1.0encoding =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是可选的,只有Android - >

< name>您的应用< / name>

< description>
我的第一个MySql连接与Phonegap
< / description>

< author href =http://www.my_domain.com>
它的我
< / author>

<! - 以下两行有所不同!重要 - >
< gap:plugin name =cordova-plugin-whitelistsource =npm/>

<! - 在我的经验中只有*工作。使用IP地址(http://xxx.yy.qqq.tt或简单的xxx.yy.qqq.tt)无效 - >
< access origin =*subdomains =true/>

< / widget>

将HTML文件,XML文件和JQuery.min.js压缩并上传打包。 PhoneGap.com使用您的帐户。



希望我帮助某人!


I'm doing my first experiments with Phonegap. I would like to build an app that retrieves data from a MySql server, but it seems that the problem is that I'm not able to connect to my dB.

When I build web sites it's easy. I use PHP and the following code:

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

where $servername is localhost

But with Phonegap localhostwon't work of course so I have to use the hostname or the IP address.

And here's the problem. I have a VPS with a IP and the host name is, suppose, vps.my-domain.com. Both won't give me access to the MySql db and I don't understand why.

The following strings report:

'SQLSTATE[HY000] [2005] Unknown MySQL server host 'vps.my-domain.com:3306' (20)'

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

My code (HTML+Jquery+Ajax+PHP) works fine when I run it on my VPS and I use localhost but it fails when I use the IP address or the hostname.

I also tried with mySQLjs:

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

but still without success.

The demo code I found at mySQLjs works fine, so I'm pretty sure I'm missing something about my connection.

How do I access my MySql db using the IP address or the hostname rather than using localhost? Is there a configuration that should be I set on my VPS?

解决方案

SOLVED

After many temptatives I worked it out.

By default remote access to the MySQL database server is disabled for security reasons.

1- You need to edit MySQL configuration file and allow access from origins different than localhost.

nano /etc/mysql/my.cnf

2- change the line bind-addressto:

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

3- restart MySQL:

/etc/init.d/mysql restart

4- with PHPMyAdmin create a user which host is your ip address so that it seems as:

phonegap@your_ip_number

Below there's the code (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 This is the config file for 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>

ZIP the HTML file, the XML file and JQuery.min.js and upload the package to build.phonegap.com using your account.

Hope I helped someone!

这篇关于PhoneGap的。无法访问远程mysql数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆