Unable to connect google apps script to mysql through localhost

一曲冷凌霜 提交于 2020-02-03 01:59:30

问题


Getting the error :"Failed to establish a database connection. Check connection string, username and password. " if I run the following script.I am able to connect locally through mysql workbench but unable to do so with googleapp script.Don't know where I am goin wrong .Also,do i need to set triggers if i write the script for a spreadsheet .

function test() { 
  var conn = Jdbc.getConnection("jdbc:mysql://127.0.0.1:3306/test", "root",""); 
  var stmt = conn.prepareStatement("SELECT * FROM info;"); 
  var dbList = stmt.executeQuery(); 
   dbList.next(); 
   var row = 0; 
  Logger.log("Start of Log:"); 
   while(dbList.next())
   { 
     Logger.log(dbList.getString(1)); 
     row++; 
   } 
  }

Thanks.


回答1:


Okay so if you are doing this from your home and want to run google app scripts your local MYsql database on your laptop follow the below steps:-

1)Expose port 3306 on your laptop to the internet.

Login to your router and port forward the 3306 port to your laptop:- Here is a short youtube video if you own a netgear router. Basically what you are doing here is; when the google script running on the google servers tries to locate your database it hits the router(public IP) on port 3306. Now from there this request gets routed to your specific laptop.Example port forwarding video:- Port Forwarding Video

2)Open up the MYSql data to anybody with a username and password from any computer:- run the following command on your MySQL database:-

GRANT ALL PRIVILEGES ON *.* TO 'username'@'%' IDENTIFIED BY 'password' WITH GRANT OPTION;

3)Use your public IP in the following line :- So instead of var conn = Jdbc.getConnection("jdbc:mysql://127.0.0.1:3306/test", "root",""); use the following

var conn = Jdbc.getConnection("jdbc:mysql://127.0.0.1:3306/test", "root","");

Just in case you face IPV6 and JDBC compatibility issues like most will. Get your public IP from the following address:- get my IPV4 address

So instead of 127.0.0.1 use the results from the above google query




回答2:


The IP address 127.0.0.1 represents your local computer. It cannot be used by other computers on the network to reach your computer. Remember that Google Apps Script runs on Google's servers and therefore, you need to provide an IP address that they can reach.

Another common mistake many folks make is to provide a 192.168.x.x or some similar internal IP address. So, provide an IP address that is accessible to external computers.



来源:https://stackoverflow.com/questions/17440179/unable-to-connect-google-apps-script-to-mysql-through-localhost

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!