无法通过 localhost 将谷歌应用程序脚本连接到 mysql

收到错误:无法建立数据库连接.检查连接字符串、用户名和密码."如果我运行以下脚本.我可以通过 mysql 工作台在本地连接,但无法使用 googleapp 脚本连接.唐'不知道我哪里错了.另外,如果我为电子表格编写脚本,是否需要设置触发器.

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++; 
   } 
  }

谢谢.

推荐答案

好的,如果您在家中执行此操作并想在笔记本电脑上运行 google 应用脚本,请按照以下步骤操作您的本地 MYsql 数据库:-

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) 将笔记本电脑上的端口 3306 暴露在互联网上.

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

登录到您的路由器并将 3306 端口转发到您的笔记本电脑:-如果您拥有 netgear 路由器,这是一个简短的 youtube 视频.基本上你在这里做的是;当谷歌服务器上运行的谷歌脚本试图找到您的数据库时,它会在端口 3306 上访问路由器(公共 IP).现在,此请求从那里被路由到您的特定笔记本电脑.示例端口转发视频:-端口转发视频

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) 在任何计算机上向具有 用户名 和 密码 的任何人开放 MYSql 数据:-在您的 MySQL 数据库上运行以下命令:-

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) 在以下行中使用您的公共 IP:-所以而不是var conn = Jdbc.getConnection("jdbc:mysql://127.0.0.1:3306/test", "root","");使用以下

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","");

以防万一您像大多数人一样遇到 IPV6 和 JDBC 兼容性问题.从以下地址获取您的公共 IP:-获取我的 IPV4 地址

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

所以不要使用 127.0.0.1,而是使用上述谷歌查询的结果

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

相关文章