I have a new situation. I had a javascript
running successfully on aArduino YUN
board that was basically querying a MySql server located on a server with an IP address (aaa.bbb.ccc.ddd). I am basically trying to replace Arduino YUN with Raspberry Pi 4
. This is what I did to run this javascript on the Raspebrry PI.Successfully installed all the modules that were there in the Arduino YUN board and all modules, packages have been successfully installed. Now I am running to issues when trying to access MySql server from the Raspberry PI. Looks like the script is giving null
output and I don't know how to connect the Raspberry PI to MySql server or did I make all configurations correctly as in my old board Arduino YUN.
Here is code:
connect2mysql = require('remote_mysql_connection.js');
var queryMySql = "SELECT product FROM catalog WHERE product_id =123;";
console.log(connect2mysql.query(mysqlQuery))
Here is the remote_mysql_connection.js
script that I am importing:
remote_mysql_connection.js
var shell = require('shelljs');
module.exports = {
user: 'test',
host: 'aa.bbb.cc.ddd',
password: 'blahblah',
database: 'geeko',
async: true,
/* Parses MySQL text output into an array of row objects. */
parseOutput: function(output)
{
var rows = [];
var outputLines = output.match(/[^\r\n]+/gm);
console.log(outputLines);
var columnNames = outputLines[0].split('\t'); //first line of output contains column headers
for(var n=1; n<outputLines.length; n++)
{
var row = {};
var fields = outputLines[n].split('\t'); //the values for
for(var c=0; c<columnNames.length; c++)
{
row[columnNames[c]] = fields[c];
}
rows.push(row);
}
return rows;
},
/* Executes a SQL query. After the query is finished, it calls the passed callback function with the result. */
query : function(query, callback)
{
var sql = "use "+this.database+"; "+query;
var options = {silent:true, async:this.async};
var command = 'mysql -u '+this.user+' -h '+this.host+' -p'+this.password+' -e "'+sql+'"';
//shell.exec() runs the command, the output data is processed by the function supplied below
shell.exec(command, options, function(code, output) {
var result = [];
console.log('Query:', query);
console.log(output);
//Parse the output if it is a select query
if((/^(\s+)?select/i).test(query)) result = module.exports.parseOutput(output);
if(callback) callback(result, output); //pass result back to user supplied callback function
});
}
};
Present output:
null
[ 2023-01-03T14:38:56.617Z ] 'Caught exception: TypeError: Cannot read property \'0\' of null'
TypeError: Cannot read property '0' of null
at Object.parseOutput (/home/pi/Documents/.../remote_mysql_connection.js:15:32)
at /home/pi/Documents/../remote_mysql_connection.js:44:63
at /home/pi/node_modules/shelljs/src/exec.js:145:9
at ChildProcess.exithandler (child_process.js:301:5)
at ChildProcess.emit (events.js:198:13)
at maybeClose (internal/child_process.js:982:16)
at Socket.stream.socket.on (internal/child_process.js:389:11)
at Socket.emit (events.js:198:13)
at Pipe._handle.close (net.js:607:12)
Compilation failed.
How to check and confirm connection between Raspberry Pi 4 and a remote MySql database located on a different server with an IP address?
UPDATE:
Interestingly, there seems to be a good connection between the RPi and remote MySql server (IP:'aa.bbb.cc.ddd'). How I know? I wrote a small Python script using the same credentials as above javascript
and MySql
query I was using in the original program (which is JavaScript). The program ran successfully and gave the correct response. It is just a question of having all the required packages, and dependencies that support the MySql connection through JavaScript. Still don't why my JavaScript
is not connecting to the remote MySql
server when my python
script connects successfully.