无法通过Apps Script (JDBC)连接到本地MySQL服务器

6
请参见下面的编辑,我将保留原始问题以帮助其他人。
我正在尝试通过Apps Script和JDBC连接到本地MySQL服务器,但我一直收到两种错误之一。此代码:
function connectTest() {
  var conn = Jdbc.getConnection("jdbc:mysql://localhost", "root", "xxx");
}

出现错误:无法建立数据库连接。请检查连接字符串、用户名和密码。

这段代码:

function connectTest() {
  var conn = Jdbc.getConnection("jdbc:mysql://localhost:3306", "xxx", "pass");
}

出现了错误:无效参数:url

我已经尝试了数十种组合,但无法使其正常工作。尝试使用应用程序脚本登录时,在MySQL的访问日志中没有显示(即如果我在本地尝试使用错误信息登录,则会看到[Note] Access denied for user 'host'@'localhost' (using password: YES))。我已将适当的访问权限授予root:

mysql> show grants for 'root'@'%';
+-------------------------------------------------------------+
| Grants for root@%                                           |
+-------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION |
+-------------------------------------------------------------+
1 row in set (0.00 sec)

我需要做些什么才能将本地数据库暴露到互联网/Google Apps Script?编辑:我现在可以从MySQL日志中获得访问被拒绝的提示(例如“Access denied for user 'root'@'12.123.12.123' (using password: YES)”),但应用程序仍然无法连接。我尝试使用我的公共IP添加“bind-address”,但MySQL无法启动,我收到错误消息(“[ERROR] Can't start server: Bind on TCP/IP port: Can't assign requested address”)。
function connectTest() {
  var conn = Jdbc.getConnection("jdbc:mysql://12.123.12.123:3306", "root", "xxx");
}

编辑2:我更改了bind-address=0.0.0.0,这样可以在DB日志中看到被拒绝的访问错误,但仍然无法连接。我已成功连接到互联网上的其他开放MySQL数据库(例如ensembl.db.ensembl.org:3306),但仍然无法连接到我的本地数据库。


@Mr.Rebot,'root'@'%' 应该允许所有IP地址,不是吗? - howMuchCheeseIsTooMuchCheese
1
请不要公开发布您自己服务器的公共IP地址,也请千万不要使用root账户连接到它!同时,请不要将所有地址'%'加入白名单,只需加入来自谷歌服务器的地址(请关注@Mr.Rebot的链接)。这可能需要更多的工作,但是您会更安全! - mTorres
1
@mTorres,那不是我的IP地址,只是12和123。 - howMuchCheeseIsTooMuchCheese
是的...我太傻了。抱歉打扰了。 - mTorres
那么74.125....是什么? - Rick James
显示剩余3条评论
1个回答

7
最终成功了,下面是我的解决方法:

  1. Edit my.cnf with (this file can be located several different places and may not even exist, I put mine here /usr/local/etc/:

    [mysqld]
    bind-address=0.0.0.0
    skip-host-cache
    skip-name-resolve
    
  2. Open port 3306 to internet (for Xfinity, that meant going to http://10.0.0.1/ > clicking "Advanced" on left > "Port Forwarding" > "Add Device" on the bottom, find the device MySQL is running on and click add) open port

  3. Grant appropriate permission in MySQL and FLUSH PRIVILEGES;. You can do this with GRANT ALL PRIVILEGES ON *.* TO <USER>@'%' IDENTIFIED BY '<PASSWORD>', but that allows any IP, the Python script below will grant the Google IP's. You should check this IP list against Google's documentation for JDBC.

    from sqlalchemy import create_engine, MetaData
    
    conString = 'mysql+pymysql://<USER>:<PASSWORD>@localhost:3306/<DB>'
    mysql = create_engine(conString)
    
    ips = ['64.18.0.0 - 64.18.15.255',
    '64.233.160.0 - 64.233.191.255',
    '66.102.0.0 - 66.102.15.255',
    '66.249.80.0 - 66.249.95.255',
    '72.14.192.0 - 72.14.255.255',
    '74.125.0.0 - 74.125.255.255',
    '173.194.0.0 - 173.194.255.255',
    '207.126.144.0 - 207.126.159.255',
    '209.85.128.0 - 209.85.255.255',
    '216.239.32.0 - 216.239.63.255']
    
    for ip in ips:
        ip2 = ip.replace(' - ', '/')
        try:
            sql = "CREATE USER '<USER>'@'" + ip2 + "' identified by '<PASSWORD>';"
            mysql.execute(sql)
        except:
            print ip2
        sql = "GRANT ALL PRIVILEGES ON <DB>.* TO '<USER>'@'" + ip2 + "';"
        mysql.execute(sql)
    

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接