Google Apps 脚本 JDBC 连接问题

发布于 2024-10-29 03:01:49 字数 417 浏览 4 评论 0原文

我在 google apps 脚本中使用 jdbc 连接器连接到任何 mysql 数据库时遇到问题,我正在使用教程代码:

var conn = Jdbc.getConnection("jdbc:mysql://host(or ip):3306/database", "username", "password");

但在每种情况下(我在 4 个不同的主机名上测试了 4 个不同的数据库)我都会得到相同的错误:

建立数据库连接失败。检查连接字符串、用户名和密码。 (第 2 行)

我正在寻求一些帮助,我不知道可能是什么问题;-(

ps.用户名/密码没问题。 pps。在每个数据库中,远程访问都可以正常工作(我已经使用 telnet 进行了测试)。

I have problem to connect to any mysql database using jdbc connector in google apps scripts, I'm using tutorial code:

var conn = Jdbc.getConnection("jdbc:mysql://host(or ip):3306/database", "username", "password");

But in each case ( I've tested 4 different databases on 4 different host names) i get the same error:

Failed to establish a database connection. Check connection string, username and password. (line 2)

I'm looking for some help, I have no idea what could be the problem ;-(

ps. usernames/passwords are ok.
pps. In each database remote access is working ( I've tested by using telnet).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

神回复 2024-11-05 03:01:49

由于您的问题是远程访问 MySQL 数据库,我将发布一些相关文档,以便像 TonyMiao 这样的未来查看者将有一个途径来解决他们自己的相关问题。

步骤#1:使用 SSH 登录(如果服务器位于数据中心之外)

首先,通过 ssh 登录到远程 MySQL 数据库服务器。您可能需要以 root 用户身份登录 MySQL 服务器:

ssh [email protected]

使用 su 或 sudo 以 root 身份登录

su

,或使用 sudo

sudo -i

或直接以 root 用户身份登录(如果允许):

ssh [email protected]

步骤 # 2:编辑 my.cnf 文件

连接后,您需要编辑使用文本编辑器(例如 vi)创建 MySQL 服务器配置文件 my.cnf:

如果您使用的是 Debian/Ubuntu Linux,则该文件位于 /etc/mysql/my.cnf 位置。

如果您使用的是 Red Hat Linux/Fedora/Centos Linux,文件位于 /etc/my.cnf 位置。

如果您使用 FreeBSD,则需要创建一个文件 /var/db/mysql/my.cnf 位置。

编辑 /etc/my.cnf,运行:

# vi /etc/my.cnf

步骤 # 3:文件打开后,找到如下行

[mysqld] 
Make sure line skip-networking is commented (or remove line) and add following line

bind-address=YOUR-SERVER-IP
For example, if your MySQL server IP is 65.55.55.2 then entire block should be look like as follows:

[mysqld]
user            = mysql
pid-file        = /var/run/mysqld/mysqld.pid
socket          = /var/run/mysqld/mysqld.sock
port            = 3306
basedir         = /usr
datadir         = /var/lib/mysql
tmpdir          = /tmp
language        = /usr/share/mysql/English
bind-address    = 65.55.55.2

# skip-networking
....
..
....

其中,

  • bind-address:要绑定的 IP 地址。
  • Skip-networking :根本不监听 TCP/IP 连接。全部
    与 mysqld 的交互必须通过 Unix 套接字进行。这个选项是
    强烈推荐用于仅允许本地请求的系统。
    由于您需要允许远程连接,因此应删除此行
    来自 my.cnf 或将其置于注释状态。

步骤# 4 保存并关闭文件

如果您使用 Debian / Ubuntu Linux,请键入以下命令重新启动 mysql 服务器:

# /etc/init.d/mysql restart

或者

# systemctl restart mysql

如果您使用 RHEL / CentOS / Fedora / Scientific Linux,请键入以下命令重新启动 mysql 服务器:

# /etc/init.d/mysqld restart

或者

# systemctl restart mysqld

如果您使用的是 FreeBSD,请键入以下命令来重新启动 mysql 服务器:

# /usr/local/etc/rc.d/mysql-server restart

或者

# service mysql-server restart

步骤 # 5 授予对远程 IP 地址的访问权限

连接到 mysql 服务器:

$ mysql -u root -p mysql

授予对新数据库的访问权限

如果要添加名为 foo 的新数据库用户 bar 和远程 IP 202.54.10.20 那么你需要在 mysql> 输入以下命令提示:

mysql> CREATE DATABASE foo;
mysql> GRANT ALL ON foo.* TO bar@'202.54.10.20' IDENTIFIED BY 'PASSWORD';

如何授予对现有数据库的访问权限?

假设您始终从名为 202.54.10.20 的远程 IP 为用户 webadmin 的名为 webdb 的数据库建立连接,要授予对此 IP 地址的访问权限,请键入以下命令:提示现有数据库,输入:

mysql> update db set Host='202.54.10.20' where Db='webdb';
mysql> update user set Host='202.54.10.20' where user='webadmin';

步骤#6:注销 MySQL

键入 exit 命令注销 mysql:

mysql> exit

步骤#7:打开端口 3306

您需要使用 iptables 或 BSD pf 防火墙打开 TCP 端口 3306。

打开 LINUX IPTABLES 防火墙的示例 IPTABLES 规则

/sbin/iptables -A INPUT -i eth0 -p tcp --destination-port 3306 -j ACCEPT

或仅允许来自位于 10.5.1.3 的 Web 服务器的远程连接:

/sbin/iptables -A INPUT -i eth0 -s 10.5.1.3 -p tcp --destination-port 3306 -j ACCEPT

或仅允许来自 lan 子网 192.168.1.0/24 的远程连接:

/sbin/iptables -A INPUT -i eth0 -s 192.168.1.0/24 -p tcp --destination-port 3306 -j ACCEPT

最后保存所有规则(RHEL / CentOS 特定命令) :

# service iptables save

FREEBSD / OpenBSD / NETBSD PF 防火墙规则示例 (/ETC/PF.CONF)

使用以下命令在基于 BSD 的系统上打开端口 # 3306:

pass in on $ext_if proto tcp from any to any port 3306

或仅允许来自位于 10.5.1.3 的 Web 服务器的访问:

pass in on $ext_if proto tcp from 10.5.1.3 to any port 3306  flags S/SA synproxy state

步骤 # 8 :测试它

从远程系统或桌面键入以下命令:

$ mysql -u webadmin -h 65.55.55.2 -p

其中,

-u webadmin:webadmin 是 MySQL 用户名
-h IP或主机名:65.55.55.2是MySQL服务器IP地址或主机名(FQDN)
-p :提示输入密码
您还可以使用 telnet 或 nc 命令连接到端口 3306 进行测试:

$ echo X | telnet -e X 65.55.55.2 3306

$ nc -z -w1 65.55.55.2 3306

输出示例:

Connection to 65.55.55.2 3306 port [tcp/mysql] succeeded!

资源信息:点击此处!

Since your issue was Remote access to a MySQL Database I will post some related documentation so that future viewers like TonyMiao will have a avenue to fix their own related issues.

Step # 1: Login Using SSH (if server is outside your data center)

First, login over ssh to remote MySQL database server. You may need to login to your MySQL server as the root user:

ssh [email protected]

login as the root using su or sudo

su

or use sudo

sudo -i

OR directly login as root user if allowed:

ssh [email protected]

Step # 2: Edit the my.cnf file

Once connected you need to edit the MySQL server configuration file my.cnf using a text editor such as vi:

If you are using Debian/Ubuntu Linux file is located at /etc/mysql/my.cnf location.

If you are using Red Hat Linux/Fedora/Centos Linux file is located at /etc/my.cnf location.

If you are using FreeBSD you need to create a file /var/db/mysql/my.cnf location.

Edit the /etc/my.cnf, run:

# vi /etc/my.cnf

Step # 3: Once file opened, locate line that read as follows

[mysqld] 
Make sure line skip-networking is commented (or remove line) and add following line

bind-address=YOUR-SERVER-IP
For example, if your MySQL server IP is 65.55.55.2 then entire block should be look like as follows:

[mysqld]
user            = mysql
pid-file        = /var/run/mysqld/mysqld.pid
socket          = /var/run/mysqld/mysqld.sock
port            = 3306
basedir         = /usr
datadir         = /var/lib/mysql
tmpdir          = /tmp
language        = /usr/share/mysql/English
bind-address    = 65.55.55.2

# skip-networking
....
..
....

Where,

  • bind-address: IP address to bind to.
  • skip-networking : Don’t listen for TCP/IP connections at all. All
    interaction with mysqld must be made via Unix sockets. This option is
    highly recommended for systems where only local requests are allowed.
    Since you need to allow remote connection this line should be removed
    from my.cnf or put it in comment state.

Step# 4 Save and Close the file

If you are using Debian / Ubuntu Linux, type the following command to restart the mysql server:

# /etc/init.d/mysql restart

OR

# systemctl restart mysql

If you are using RHEL / CentOS / Fedora / Scientific Linux, type the following command to restart the mysql server:

# /etc/init.d/mysqld restart

OR

# systemctl restart mysqld

If you are using FreeBSD, type the following command to restart the mysql server:

# /usr/local/etc/rc.d/mysql-server restart

OR

# service mysql-server restart

Step # 5 Grant access to remote IP address

Connect to mysql server:

$ mysql -u root -p mysql

GRANT ACCESS TO A NEW DATABASE

If you want to add a new database called foo for user bar and remote IP 202.54.10.20 then you need to type the following commands at mysql> prompt:

mysql> CREATE DATABASE foo;
mysql> GRANT ALL ON foo.* TO bar@'202.54.10.20' IDENTIFIED BY 'PASSWORD';

HOW DO I GRANT ACCESS TO AN EXISTING DATABASE?

Let us assume that you are always making connection from remote IP called 202.54.10.20 for database called webdb for user webadmin, To grant access to this IP address type the following command At mysql> prompt for existing database, enter:

mysql> update db set Host='202.54.10.20' where Db='webdb';
mysql> update user set Host='202.54.10.20' where user='webadmin';

Step # 6: Logout of MySQL

Type exit command to logout mysql:

mysql> exit

Step # 7: Open port 3306

You need to open TCP port 3306 using iptables or BSD pf firewall.

A SAMPLE IPTABLES RULE TO OPEN LINUX IPTABLES FIREWALL

/sbin/iptables -A INPUT -i eth0 -p tcp --destination-port 3306 -j ACCEPT

OR only allow remote connection from your web server located at 10.5.1.3:

/sbin/iptables -A INPUT -i eth0 -s 10.5.1.3 -p tcp --destination-port 3306 -j ACCEPT

OR only allow remote connection from your lan subnet 192.168.1.0/24:

/sbin/iptables -A INPUT -i eth0 -s 192.168.1.0/24 -p tcp --destination-port 3306 -j ACCEPT

Finally save all rules (RHEL / CentOS specific command):

# service iptables save

A SAMPLE FREEBSD / OPENBSD / NETBSD PF FIREWALL RULE ( /ETC/PF.CONF)

Use the following to open port # 3306 on a BSD based systems:

pass in on $ext_if proto tcp from any to any port 3306

OR allow only access from your web server located at 10.5.1.3:

pass in on $ext_if proto tcp from 10.5.1.3 to any port 3306  flags S/SA synproxy state

Step # 8: Test it

From your remote system or your desktop type the following command:

$ mysql -u webadmin -h 65.55.55.2 -p

Where,

-u webadmin: webadmin is MySQL username
-h IP or hostname: 65.55.55.2 is MySQL server IP address or hostname (FQDN)
-p : Prompt for password
You can also use the telnet or nc command to connect to port 3306 for testing purpose:

$ echo X | telnet -e X 65.55.55.2 3306

OR

$ nc -z -w1 65.55.55.2 3306

Sample outputs:

Connection to 65.55.55.2 3306 port [tcp/mysql] succeeded!

Resource information: Click Here!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文