使用Java连接多台计算机的MySQL连接

发布于 2025-01-16 12:20:38 字数 1712 浏览 3 评论 0原文

我和几个同学正在创建一个需要数据库的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 技术交流群。

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

发布评论

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

评论(2

心房敞 2025-01-23 12:20:38

不,你需要停止这种想法并先做一些研究。

您的当前配置表明mysql服务器与代码运行在同一台物理机器上。你在你的开发机器上安装了 mysql,你的朋友需要在他们的机器上安装它,并且每个人都有自己独特的数据库(没有任何共享)。

相反,您可以使用您的 mysql 服务器,将其向世界开放(对于住宅连接中几乎所有的互联网可用方式,都需要干扰您的路由器来“打开端口”)。

但是你有一个开放的 mysql 服务器,并且用户名和密码位于公共 bitbucket 站点

它还需要永久 IP(很少有住宅互联网提供商提供)或 dyndns 服务。更一般地说,托管大量流量的开放 MySQL 服务器会导致您的互联网关闭,这是有充分理由的。 你最终会接待一大群黑客。您网络中的所有硬件都将被破坏并变成僵尸网络。因此,非常非常糟糕的主意

解决这个问题的好方法是:

  • 每个人都安装自己的MySQL服务器。这是明智的;你正在编写代码并且必然会犯错误,如果你编写的所有代码都是首次运行并在实时数据上进行测试,那将非常糟糕。您不希望您的朋友擦除的数据库。如果您需要一些初始数据来测试,请正确设置它,并阅读如何进行 SQL 转储。有了这样的转储文件,您可以将任何 mysql 服务器重置为该确切状态 - 这就是您和您的朋友开发的方式:将数据库设置为已知状态,编写一些代码,如果您通过以下方式破坏了数据库这样做,没问题。重新设置一下就可以了。
  • 在您的朋友之间设置 VPN。 现在您可以在 VPN 共享您的系统所拥有的 IP(将是 10.、172.16.、192.168.* - 如果是127.0.0.1,它是localhost,即每个人都需要自己安装mysql并且没有任何东西是共享的,如果是其他东西,你将它向世界开放,你可以不想做)。请勿将 VPN 用户名/密码信息放在该位桶中的任何位置。而且你需要信任你的朋友。

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:

  • Everybody installs their own MySQL server. This is sensible; you're writing code and bound to make mistakes, it'd be real bad if all code you write is first-run and tested on live data. You don't want one of your friends to wipe your database. If you need some initial data to test with, set it up properly, and read up on how to make an SQL dump. With such a dump file you can reset any mysql server to that exact state - and that'd be how you and your friends develop: Set up the DB to be in that known state, write some code, and if you ruin the db by doing so, no problem. Just reset it again.
  • Set up a VPN between your friends. NOW you can share the IP your system has within the VPN (it'll be 10., 172.16., 192.168.* - if it's 127.0.0.1, it's localhost, i.e. everybody needs to install mysql on their own and nothing is shared, and if it's anything else, you're opening it to the world, which you don't want to do). Do not put the VPN username/password info anywhere in that bitbucket. And you need to trust your friends.
無心 2025-01-23 12:20:38

您应该有一个属性类型文件,以便每个要与代码交互的人都拥有其本地数据,而无需复制您的数据,就像您可以在测试或生产环境的属性中拥有不同的值一样。

属性文件的示例:

system.properties

#BD
db.driver=com.mysql.cj.jdbc.Driver
db.user=user
db.pass=password
db.server=server_IP
db.port= port_IP
db.db = DB

然后您应该有一个过程从 java 读取文件

Utils.java

package com.example.javaworkoutgame.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public final class Utils {

    public static Properties getProperties() {
        String path = String.format("PATH to your properties FILE/system.properties",
                System.getProperty("user.dir"));
        Properties properties = new Properties();
        try (InputStream is = new FileInputStream(new File(path))) {
            properties.load(is);
        } catch (IOException e) {
            throw new IllegalStateException(e);
        }

        return properties;
    }
}

中的属性最后调用从连接类

Connect.java

package com.example.javaworkoutgame.Model;


import com.example.javaworkoutgame.util.Utils;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class Connect {

    Properties properties = Utils.getProperties();

    static Connection con;

    public Connect() {
        connect();
    }

    // attempt to connect to MySQL database
    public static void connect() {
        try {
            String driver = properties.getProperty("db.driver");
            String ip = properties.getProperty("db.ip");
            String port = properties.getProperty("db.port"); 
            String db = properties.getProperty("db.db");
            String user = properties.getProperty("db.user");
            String pass = properties.getProperty("db.pass"):

            Class.forName(driver);
            System.out.println("Driver Loaded Successfully");
            con = DriverManager.getConnection("jdbc:mysql://"+ip+":"+port+"/"+db, user,
                    pass);             
            System.out.println("Successful Connection");
        } catch (ClassNotFoundException cnfe) {
            System.err.println(cnfe);
        } catch (SQLException sqle) {
            System.err.println(sqle);
        }
    }

}

获取属性的函数关于 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

#BD
db.driver=com.mysql.cj.jdbc.Driver
db.user=user
db.pass=password
db.server=server_IP
db.port= port_IP
db.db = DB

Then you should have a procedure to read from java the properties inside the file

Utils.java

package com.example.javaworkoutgame.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public final class Utils {

    public static Properties getProperties() {
        String path = String.format("PATH to your properties FILE/system.properties",
                System.getProperty("user.dir"));
        Properties properties = new Properties();
        try (InputStream is = new FileInputStream(new File(path))) {
            properties.load(is);
        } catch (IOException e) {
            throw new IllegalStateException(e);
        }

        return properties;
    }
}

And finally you make a call to the function that gets the properties from your connection class

Connect.java

package com.example.javaworkoutgame.Model;


import com.example.javaworkoutgame.util.Utils;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class Connect {

    Properties properties = Utils.getProperties();

    static Connection con;

    public Connect() {
        connect();
    }

    // attempt to connect to MySQL database
    public static void connect() {
        try {
            String driver = properties.getProperty("db.driver");
            String ip = properties.getProperty("db.ip");
            String port = properties.getProperty("db.port"); 
            String db = properties.getProperty("db.db");
            String user = properties.getProperty("db.user");
            String pass = properties.getProperty("db.pass"):

            Class.forName(driver);
            System.out.println("Driver Loaded Successfully");
            con = DriverManager.getConnection("jdbc:mysql://"+ip+":"+port+"/"+db, user,
                    pass);             
            System.out.println("Successful Connection");
        } catch (ClassNotFoundException cnfe) {
            System.err.println(cnfe);
        } catch (SQLException sqle) {
            System.err.println(sqle);
        }
    }

}

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!

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