-1

I want to connect to my MySQL database on a local host which is not in IBM Cloud using IBM Cloud Functions. I am unable to do that.

I have written Node.js code in IBM Cloud Function.

var mysql = require('mysql');
//,cn="server=localhost;port=3306;database=chatbot;uid=root;password=oracle1#;";
    var connection = mysql.createConnection({
      server: 'localhost',
      user: 'root',
      port:3306,
     password: 'my_password'
    });
function main(params) {
 try {
     //var connection=mysql.createConnection(cn);
    connection.connect();
    //var s = JSON.stringify(params['user_input']);
   //var v = s.substring(1,11);
   //var check= conn.querySync("select count(distinct PHONE_NUMBER)  where PHONE_NUMBER='"+v+"'");
   var rows = connection.query(
   "select * from chatbot.customer_data");
   //console.log(rows);
   connection.end();
    return{message:"TRUE:"+rows[0]['PHONE']};
 }
 catch (e) {
   return { message:"error" };
  //return{message:"FALSE"}; 
 }
}

Expected result: TRUE:RESULTSET

Actual Result: error

data_henrik
  • 16,724
  • 2
  • 28
  • 49
  • Are you trying to use IBM Cloud Functions to reach to your local MySQL? Is this similar to what is done here, except your database in not in the cloud? https://cloud.ibm.com/docs/tutorials?topic=solution-tutorials-slack-chatbot-database-watson#build-a-database-driven-slackbot – data_henrik Apr 24 '19 at 08:51
  • yes trying to reach local Mysql through IBM Cloud function and this database is not in cloud. The doc attached has the databse in cloud itself, but in my case database is in local server and not in cloud. – Dipanjan Ray Apr 24 '19 at 09:09
  • Add details on how the cloud-based code is able to find your local server. Is it a public IP address and an open port? Are you using any gateway or VPN? – data_henrik Apr 24 '19 at 09:19
  • The mentioned port is closed now.That may be a reason for not fetching any records. I am currently not using any gateway. I have given the database details and hoping that the code access the database using those details. I am not using any gateway or VPN. Can you guide me on what else I need to add into my code so that the cloud based code can access my database. – Dipanjan Ray Apr 24 '19 at 10:48
  • Again, add details, including the error message. – data_henrik Apr 24 '19 at 10:58
  • if I return {message:"TRUE"}, there is no error but no data is fetched . Basically data is not fetched.The code is getting invoked without fetching any results. – Dipanjan Ray Apr 24 '19 at 12:18

2 Answers2

0

This is a pure network issue. The Cloud function running in IBM Cloud does not have a network route that directs to your local host. If you did want that connectivity then you would need to expose the address of your local host using something like a secure gateway service.

chughts
  • 4,210
  • 2
  • 14
  • 27
  • I am using ibm secure gateway now. I have added the secure gateway client,destination(on premise mysql databse) and the secure gateway server. Now can you guide me on how can I fetch records using cloud function through secure gateway service.Please help me with the code or the steps that are to be followed next. – Dipanjan Ray Apr 27 '19 at 07:33
  • If you have set up the destination on the gateway, then you will have a generated cloud port on the gateway server which directs to your local mysql server / port. Your cloud function should use the gateway server / generated port as part of its sql connection string. – chughts Apr 30 '19 at 10:07
0
var sql = require('mysql');

    var connection = sql.createConnection({
      host: 'host name',
    user: 'user name',
    password: 'passwprd',
    database: 'database_name'
    });
function main(params) {
 try {

    connection.connect();

   var sql="select * from table _name";

   connection.query(sql,function(err,result){
       return{message:result};
   })
   //console.log(rows);
   connection.end();

 }
 catch (e) {
   return { message:"error" };
  //return{message:"FALSE"}; 
 }
}