【发布时间】:2017-09-26 04:10:59
【问题描述】:
我正在尝试从 Java 写入 mysql 数据库,但出现此错误:
java.sql.SQLException: Access denied for user 'root'@'172.17.0.1' (using password: YES)
我正在连接到一个本地 mysql 实例,老实说我不知道 172.17.0.1 ip 是什么。
我写的小代码:
public static void writeToDB(String value, String unit) throws ClassNotFoundException, SQLException, SQLException
{
// create a mysql database connection
String myDriver = "com.mysql.cj.jdbc.Driver";
String myUrl = "jdbc:mysql://localhost:3306/MyDB";
Class.forName(myDriver);
Connection conn = null;
conn = DriverManager.getConnection(myUrl, "root", "MyPass");
// the mysql insert statement
String query = " insert into data (unit, value)"
+ " values (?, ?)";
// create the mysql insert preparedstatement
PreparedStatement preparedStmt = null;
preparedStmt = conn.prepareStatement(query);
preparedStmt.setString (1, unit);
preparedStmt.setString (2, value);
// execute the preparedstatement
preparedStmt.execute();
conn.close();
}
我跑了
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'MyPass' WITH GRANT OPTION;
但没有任何改变。
我的 my.cnf 都被注释掉了。 我的本地 IP 是 192.168.1.46。我尝试在 myUrl 变量上使用这个 ip 和 127.0.0.1 而不是 localhost 但没有任何改变。
我重启了mysql服务器,eclipse和pc都没有成功。
编辑:附加信息 我停止了服务器并运行了代码。我得到了完全相同的例外。我想我根本无法访问服务器。
我可以在我的 Python 脚本中使用“localhost”。它与 java 代码写在同一张表上。
【问题讨论】:
-
你能检查一下这个解决方案吗stackoverflow.com/a/11922393/227775
-
@dasrohith 我已经尝试过了,但没有解决我的问题
-
你试过用ip还是locahost?
-
@dasrohith 是的,我尝试了 localhost、127.0.0.1、我的本地 IP 和异常显示的 IP(我不认识)。还是没有运气
-
执行刷新权限;在授予所有特权之后