Home:ALL Converter>Why does MySQL connection string work in bash but not javascript?

Why does MySQL connection string work in bash but not javascript?

Ask Time:2016-08-23T04:42:33         Author:Michael Currin

Json Formatter

I've setup a physical mac computer to be a MySQL server. It works perfectly well if I access it remotely on the internet if I access it through the Terminal. Edit: I now realise this is on the local network only. So I'm now setting up a static IP and will have to post another question on that if I get stuck

But I'd love to solve why it someone doesn't work if I connect with javascript, such as in GoogleSpreadsheets or any related add-ons in GoogleSpreadsheets (which are also written on Javascript I think).

I can use any of those javascript approaches to connect to other databases which I have access to, but I'd like to find out why I can't connect to mine in particular and if there is anything I can change? Thanks.


I setup a MySQL server on computer A (Mac OS X Yosemite). I can connect fine if I use do the following in bash on computer B (Mac OS El Capitan) and then get whatever output I need from MySQL.

$ # in bash, change path to ensure SQL runs 
$ export PATH=$PATH:/usr/local/mysql/bin/ 
$ # start sql and run a query
$ mysql -u test -pXXXXX -h 192.168.XXX.XX sakila # replace X's with real credentials, where -p is for the password and -h is for the IP address.
$ mysql> SHOW TABLES; 
$ mysql> #... output is good
$ mysql> exit;
$

If I run the same credentials in Google Spreadsheets script editor, I get an error on the last line saying that either my connection string, user or password are not correct. See script below.

// attempt to access SQL data using javascript on https://script.google.com

function Drive() {
var ServerIP = '192.168.XXX.XX'; // IP address of the server. replace with real IP
var SQL_Port = '3306'; // port number
var SQL_Usr = 'test'; // name of user
var SQL_Pwd = 'xxxxx'; // password of user. replace with real password
var SQL_DB = 'sakila'; // name of sample database downloaded from MySQL Workbench documentation.

var connectorInstance = 'jdbc:mysql://' + ServerIP  +':' + SQL_Port;
var ConnectString = connectorInstance + '/' + SQL_DB;

var conn = Jdbc.getConnection(ConnectString, SQL_Usr, SQL_Pwd); // error on this line
}

My javascript, my syntax is definitely correct, since if I swop in the credentials for another online database then the error disappears. Similarly, I tried 4 different Google Spreadsheets add-ons which are available when you search "SQL" in the add-on store; those work with all databases I tried but not my own.

And my own server's credentials and authorisation should be working correctly, since I can connect using bash as shown at the top.

Is there some security option setup on the other databases in the server or in their internet connection which means javascript will connect on theirs and not mine? Is there something I missed?

My alternatives would be to only access the server with bash (not good when I am building a Spreadsheet-based front-end), or to host the server online, which would involve some costs.

Would an SSH connection help at all? I haven't used one before. Any help would be appreciated.


Steps I've tried

  • I started off setting up MySQL Workbench with a config file as -"etc/my.cnf".

  • I set "skip-networking" off.

  • I made sure "bind_address" was off and also tried variations since as "0.0.0.0", "*" and "%".

  • I followed a lesson on setting up javascript code for Google scripts and granted sufficent user account details on location "%" (yes I know that's not secure but this is a small test db).

  • I've tried playing around with other Options File preferences under Networking or other section, but they either have no effect or the server fails to restart successfully with those applied.

  • Before testing my DB connection, I make sure I restart my SQL server to ensure option and user changes are applied.

  • I've checked StackOverflow to see who else is asking about remote databases, javascript or connection strings. No one else seems to have my exact issue.

  • I've tried using the host's name "Servers-Macbook-Air.local" in place of IP, again that works in bash but NOT in Javascript still. Also if I use IP or hostname for another database, that works.

Author:Michael Currin,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/39088392/why-does-mysql-connection-string-work-in-bash-but-not-javascript
Michael Currin :

I did some research after Hardy's comment - I think it is the private IP restricting me. I had only tried with both laptops in Wifi range.\nhttp://www.gohacking.com/private-and-public-ip-addresses/\n\nSay for example, if a network X consists of 10 computers, each of them can be given an IP starting from 192.168.1.1 to 192.168.1.10. Unlike the public IP, the administrator of the private network is free to assign an IP address of his own choice (provided the IP number falls in the private IP address range as mentioned above).\nDevices with private IP addresses cannot connect directly to the Internet. Likewise, computers outside the local network cannot connect directly to a device with a private IP.\n",
2016-08-22T21:05:32
yy