问题描述
我在连接到公共托管的静态IP mysql服务器时遇到了一个奇怪的问题.
I have a strange problem connecting to publicly hosted static IP mysql server.
我能够使用MySQL Workbench成功连接到服务器,也可以通过SSH连接到本地远程服务器.但是,尝试从jdbc连接器连接到mysql服务器(通过NetBeans和通过本地部署的代码均失败).在查看日志时,代码正在尝试与远程服务器连接,因为用户凭据是本地用户而不是远程用户.因此,该用户位于远程上,但是Java在尝试连接时会考虑使用user @ localhost.确切的错误显示为:
I am able to connect to the server successfully with MySQL Workbench and also on the local remote server through SSH. However the connection attempt to the mysql server from jdbc connecter(both through NetBeans and through the locally deployed code fails). On looking into the logs, the code is trying to connect with remote server considering the user credentials are of a local user instead of remote user. So the user exists on remote but java considers user@localhost while trying connection. The exact error appears as:
我的连接字符串是:
有人可以建议如何提及用户,以便java认为它是我计算机上的远程用户,而不是本地用户吗?
Can someone please suggest how to mention the user so java considers it to be a remote user instead of local user on my machine?
推荐答案
这里的问题是:
在连接MySQL Workbench时,连接成功,因为用户是对数据库具有完全访问权限的[email protected].但是,当我使用Java进行连接时,它尝试连接的用户是userName @ JavaServerIP,而不是[email protected].由于userName @ JavaServerIP没有访问权限,因此连接尝试失败.向此类用户授予对mysql服务器上数据库的访问权限,为我解决了该问题.这是帮助我解决此问题的命令:
When MySQL Workbench was connecting, the connection was successful since the user was [email protected] which had full access to DB. However when I was using java to connect, the user it was trying to connect with was userName@JavaServerIP, instead of [email protected]. So the connection attempt was failing since the userName@JavaServerIP had no access. Granting such user access to the DB on mysql server resolved the issue for me. Here's the command that helped me resolve this:
GRANT ALL PRIVILEGES ON my_database.* TO 'my_user'@'%' IDENTIFIED BY 'password' WITH GRANT OPTION;
这篇关于与远程MySQL数据库的JDBC连接失败;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!