java 连接到 MySQL 服务器 JDBC
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11076948/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Connecting to MySQL Server JDBC
提问by Kgrover
I am trying to connect to a MySQL Server using JDBC tool in java (using eclipse). I was just wondering how to enter 2 user/password combinations. The first one is the one I use to connect to the server(for example when I ssh into the server) and the second one I enter into phpmyadmin. As of now, I am putting in the phpmyadmin password only into the jdbc connection properties and it's not connecting. This is my current statement:
我正在尝试使用 java 中的 JDBC 工具(使用 eclipse)连接到 MySQL 服务器。我只是想知道如何输入 2 个用户/密码组合。第一个是我用来连接到服务器的(例如当我 ssh 进入服务器时),第二个是我进入 phpmyadmin 的。到目前为止,我只将 phpmyadmin 密码输入 jdbc 连接属性,但它没有连接。这是我目前的声明:
conn = DriverManager.getConnection("jdbc:mysql://[IP of server]:3306/[Database Table name]", "[UserName (same as phpmyadmin)]","[Password (same as phpmyadmin)]");
I am getting a
我得到一个
java.sql.SQLException: null, message from server: "Host '[My computer's full host name]' is not allowed to connect to this MySQL server"
I was just wondering if I needed to enter my server login/password (the one I use for ssh) as well in addition to the phpmyadmin username/pwd. I am new to JDBC and MySQL server, so I would appreciate any tips.
我只是想知道除了 phpmyadmin 用户名/密码之外,我是否还需要输入我的服务器登录名/密码(我用于 ssh 的那个)。我是 JDBC 和 MySQL 服务器的新手,所以我很感激任何提示。
Just for background, I am able to connect successfully through ssh and I can login to the server through phpmyadmin.
仅作为背景,我可以通过 ssh 成功连接,并且可以通过 phpmyadmin 登录到服务器。
回答by Marvo
Here's how you can create an account that can access your server from another client machine:
以下是创建可以从另一台客户端计算机访问您的服务器的帐户的方法:
CREATE USER 'bobby'@'localhost' IDENTIFIED BY 'some_password';
That creates the user, and says he can connect from localhost.
这将创建用户,并说他可以从本地主机连接。
If he is on the machine 192.168.0.5, you'd do something like this:
如果他在 192.168.0.5 机器上,你会做这样的事情:
CREATE USER 'bobby'@'192.168.0.5' IDENTIFIED BY 'some_password';
Then of course, you have to grant privileges appropriately:
那么当然,您必须适当地授予权限:
GRANT ALL PRIVILEGES ON databasename.* TO 'bobby'@'localhost' WITH GRANT OPTION;
GRANT ALL PRIVILEGES ON databasename.* TO 'bobby'@'192.168.0.5' WITH GRANT OPTION;
That's been my experience anyway.
反正这就是我的经历。
You're probably better off reading this section on how to specify MySQL accounts.
您最好阅读有关如何指定 MySQL 帐户的这一节。
回答by Alex Lynch
When you log in from PHPMyAdmin, the web server is located on the same server that hosts the Mysql database (in your case). Mysql, by default, does not allow connections from remote hosts; only the local machine may access the database.
当您从 PHPMyAdmin 登录时,Web 服务器位于托管 Mysql 数据库(在您的情况下)的同一台服务器上。默认情况下,Mysql 不允许来自远程主机的连接;只有本地机器可以访问数据库。
I'll take a shot in the dark and say that the computer you're running the java code on is not the same machine that is hosting the mysql server. You must configure Mysql to allow connections from remote hosts via the config file and then change the Host row of the mysql
.users
table for the specified user to allow connection from your IP address (or, if security isn't your concern, any IP address.)
我会在黑暗中试一试,并说您运行 java 代码的计算机与托管 mysql 服务器的计算机不同。您必须通过配置文件配置 Mysql 以允许来自远程主机的连接,然后更改mysql
. users
指定用户的表以允许从您的 IP 地址进行连接(或者,如果您不关心安全性,则可以使用任何 IP 地址。)
To configure mysql to allow connections from remote hosts you must remove the "bind-address=" line from the configuration file.
要将 mysql 配置为允许来自远程主机的连接,您必须从配置文件中删除“bind-address=”行。
To allow any host to log on to a specific mysql user, you must set the mysql
.users
Host` column to "%".
要允许任何主机登录到特定的 mysql 用户,您必须设置mysql
. users
主机`列到“%”。
Both of these must be done.
这两个都必须做到。
回答by user1439039
public class MysqlConnect{
public static void main(String[] args) {
System.out.println("MySQL Connect Example.");
Connection conn = null;
String url = "jdbc:mysql://localhost:3306/";
String dbName = "jdbctutorial";
String driver = "com.mysql.jdbc.Driver";
String userName = "root";
String password = "root";
try {
Class.forName(driver).newInstance();
conn = DriverManager.getConnection(url+dbName,userName,password);
System.out.println("Connected to the database");
conn.close();
System.out.println("Disconnected from database");
} catch (Exception e) {
e.printStackTrace();
}
}
}