Java MySQL 连接到服务器 [英] Java MySQL Connect to server

查看:60
本文介绍了Java MySQL 连接到服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个代码可以在我安装了数据库的机器上运行良好.

I have a code which runs fine on the machine, where i have installed the database.

我正在尝试从其他机器运行此代码,该机器连接到安装了 MySQL 的机器并尝试更新数据.但是,此代码不适用于其他机器.我正在尝试按如下方式连接.

I am trying to run this code from other machine, which connects to the machine where MySQL is installed and tries to update data. However this code is not working from other machine. i am trying to connect as follows.

ds = new BasicDataSource();
ds.setDriverClassName("com.mysql.jdbc.Driver");
ds.setUrl("jdbc:mysql://192.168.0.104:3306/sd_mmm_data");
ds.setUsername("<UserName>");
ds.setPassword("<PassCode>");
ds.setMaxIdle(1);
ds.setMaxWaitMillis(40000);

我已经用相应的 IP 替换了 localhost.但是,当我运行代码时,总是找不到错误表.

I have replaced the localhost with the corresponding IP. However when i run the code, i always get the error table not found.

我创建了一个可以从其他机器连接的新用户,并且该用户拥有所有必需的权限.我已尝试从客户端系统上的 cmd 提示符连接,并且连接按预期运行,查询按预期运行.我尝试从 mysql WorkBench 连接,连接从客户端系统工作.但该连接在 Java 代码中不起作用.我错过了什么?

I have created a new user which can connect from other machines and the user has all of the required privileges. i have tried connecting from cmd prompt on client system and the connection works as expected queries run as expected. i tried connecting from mysql WorkBench and the connection works from client system. but the connection doesn't work from Java code. What am i missing?

我在客户端系统主机文件中添加了一个条目,如下所示服务器名称我尝试使用 ServerName 代替 Java 代码中的 IP 地址.即使这样也没有用.

I have added an entry in client system hosts file as follows ServerName And i tried using the ServerName in place of IP address in java code. Even this didn't work.

推荐答案

我认为您必须使用 grant the remote user 来启用远程用户的访问权限.

I think you have to use grant the remote user which enables access for remote user.

. 上的所有权限授予由密码"标识的用户名"@1.2.3.4",并带有授予选项;

GRANT ALL PRIVILEGES ON . TO 'USERNAME'@'1.2.3.4' IDENTIFIED BY 'PASSWORD' WITH GRANT OPTION;

这篇关于Java MySQL 连接到服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆