使用Java连接多台计算机的MySQL连接
我和几个同学正在创建一个需要数据库的Java项目。我已经在 MySQL 中创建了一个连接,并使用以下 Connect 类成功将其连接到我的 Java 项目:
package com.example.javaworkoutgame.Model;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class Connect {
static Connection con;
public Connect() {
connect();
}
// attempt to connect to MySQL database
public static void connect() {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
System.out.println("Driver Loaded Successfully");
con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/lab3", "root",
"**********"); // not the actual password
System.out.println("Successful Connection");
} catch (ClassNotFoundException cnfe) {
System.err.println(cnfe);
} catch (SQLException sqle) {
System.err.println(sqle);
}
}
}
此代码在我的计算机上运行正常。
我将代码提交并推送到 Bitbucket,以便我的合作伙伴可以访问它。但是,当他们在计算机上运行代码时,会收到以下错误消息:
java.sql.SQLException: Access Denied for user 'root'@'localhost' (using password: YES)
我需要在 MySQL 中更改一些内容吗? workbench 以便其他人能够访问数据库?我找不到任何这方面的信息。
我唯一能尝试的是在这个线程中找到的:
java.sql.SQLException: 用户 'root'@'localhost' 的访问被拒绝(使用密码:YES)
我打开了一个新的 .sql 文件并尝试运行命令:
GRANT ALL PRIVILEGES ON . TO 'root'@'localhost' 通过 '%password%' 识别并授予选项; (我用实际密码替换了“%password%”)
当我尝试这样做时,我收到以下错误消息:
错误代码:1064。您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,了解在“IDENTIFIED BY '*********'WITH GRANT OPTION”附近使用的正确语法
A few classmates and I are creating a Java project which requires a database. I have created a connection in MySQL and connected it to my Java project successfully using the following Connect class:
package com.example.javaworkoutgame.Model;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class Connect {
static Connection con;
public Connect() {
connect();
}
// attempt to connect to MySQL database
public static void connect() {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
System.out.println("Driver Loaded Successfully");
con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/lab3", "root",
"**********"); // not the actual password
System.out.println("Successful Connection");
} catch (ClassNotFoundException cnfe) {
System.err.println(cnfe);
} catch (SQLException sqle) {
System.err.println(sqle);
}
}
}
This code runs properly on my machine.
I committed and pushed the code to Bitbucket so my partners could access it. However, when they run the code on their computers, they get the following error message:
java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)
Is there something I need to change in MySQL workbench in order for other people to be able to access the database? I could not find any information on this.
The only thing I was able to try was found at this thread:
java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)
I opened a new .sql file and tried running the command:
GRANT ALL PRIVILEGES ON . TO 'root'@'localhost' IDENTIFIED BY '%password%' WITH GRANT OPTION;
(I replaced '%password%' with the actual password)
When I tried that I got the following error message:
Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'IDENTIFIED BY '*********' WITH GRANT OPTION'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不,你需要停止这种想法并先做一些研究。
您的当前配置表明mysql服务器与代码运行在同一台物理机器上。你在你的开发机器上安装了 mysql,你的朋友需要在他们的机器上安装它,并且每个人都有自己独特的数据库(没有任何共享)。
相反,您可以使用您的 mysql 服务器,将其向世界开放(对于住宅连接中几乎所有的互联网可用方式,都需要干扰您的路由器来“打开端口”)。
但是你有一个开放的 mysql 服务器,并且用户名和密码位于公共 bitbucket 站点。
它还需要永久 IP(很少有住宅互联网提供商提供)或 dyndns 服务。更一般地说,托管大量流量的开放 MySQL 服务器会导致您的互联网关闭,这是有充分理由的。 你最终会接待一大群黑客。您网络中的所有硬件都将被破坏并变成僵尸网络。因此,非常非常糟糕的主意。
解决这个问题的好方法是:
No, and you need to stop this line of thought and do some research first.
Your current configuration says that the mysql server is on the very same physical machine that the code is running on. You installed mysql on your dev machine, your friends need to install it on theirs, and each has their own unique database (nothing is shared).
You could, instead, take your mysql server, open it up to the world (which, for virtually all ways internet is made available in residential connections, requires messing with your router to 'open a port').
But then you have an open mysql server, and the username and password are on a public bitbucket site.
It also requires either a permanent IP (which few residential internet providers offer) or a dyndns service. More generally, hosting open MySQL servers that see lots of traffic gets your internet shut down, for good reason. You'd end up hosting a whole bunch of hackers. All hardware in your network will be p0wned and turned into bot nets. Hence, very very bad idea.
Good ways to solve this problem:
您应该有一个属性类型文件,以便每个要与代码交互的人都拥有其本地数据,而无需复制您的数据,就像您可以在测试或生产环境的属性中拥有不同的值一样。
属性文件的示例:
system.properties
然后您应该有一个过程从 java 读取文件
Utils.java
中的属性最后调用从连接类
Connect.java
获取属性的函数关于 MYSQL错误,如果你的伙伴本地没有与你相同值的mysql环境,他们会遇到你描述的错误,由于你的配置是本地配置,如果你需要你的伙伴连接到你的电脑,你必须打开mysql 的端口并给他们你的公共IP(不推荐)
希望这个答案对您有帮助!
You should have a properties type file so that each person who is going to interact with the code has their local data without the need to replicate yours, in the same way you can have different values in the properties for test or production environments.
example of a property file:
system.properties
Then you should have a procedure to read from java the properties inside the file
Utils.java
And finally you make a call to the function that gets the properties from your connection class
Connect.java
About the MYSQL error, if your partners do not have a local mysql environment with the same values as you, they will experience the error you describe, since your configuration is a local configuration, if you need your partners to connect to your pc, you must open the ports of mysql and give them your public IP (not recommended)
I hope this answer helps you!