从远程计算机到MySql的JDBC连接 [英] JDBC connection to MySql from a remote machine

查看:444
本文介绍了从远程计算机到MySql的JDBC连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已在Linux VM(Windows桌面)中安装了MySql,我试图从在父Windows操作系统中运行的Spark应用程序连接到MySql.连接时,我遇到错误...

I have installed MySql in a Linux VM (in a windows desktop), I am trying to connect to MySql from a Spark application running in a parent windows OS. While connecting I am getting below error...

这是示例代码

def main(args: Array[String]) {

val conf = new SparkConf()
.setAppName("LoadMySql")
.setMaster("local[*]")
.set("spark.sql.warehouse.dir", "file:///C:/temp")

val spark = SparkSession
.builder
.config(conf)
.getOrCreate()

val option = Map("url" -> "jdbc:mysql://VMIP:3306/TestDB", "dbtable" -> "Users", "user" -> "root", "password" -> "*****", "driver" -> "com.mysql.jdbc.Driver")

val dataRDD = spark.sqlContext.read.format("org.apache.spark.sql.jdbc").options(option).load()

}

错误...

Exception in thread "main" java.lang.NullPointerException
at org.apache.spark.sql.execution.datasources.jdbc.JDBCRDD$.resolveTable(JDBCRDD.scala:158)
at org.apache.spark.sql.execution.datasources.jdbc.JDBCRelation.<init>(JDBCRelation.scala:117)
at org.apache.spark.sql.execution.datasources.jdbc.JdbcRelationProvider.createRelation(JdbcRelationProvider.scala:53)
at org.apache.spark.sql.execution.datasources.DataSource.resolveRelation(DataSource.scala:315)
at org.apache.spark.sql.DataFrameReader.load(DataFrameReader.scala:149)
at org.apache.spark.sql.DataFrameReader.load(DataFrameReader.scala:122)
at com.Lab.SparkAPI.LoadMySql$.main(LoadMySql.scala:29)
at com.Lab.SparkAPI.LoadMySql.main(LoadMySql.scala)

我已经更改了/etc/mysql/mysql.conf.d/mysqld.cnf中的bind-address属性. 我已经尝试了几种绑定地址选项-linux VM IP,0.0.0.0并对其进行完全注释.

I already changed bind-address property in /etc/mysql/mysql.conf.d/mysqld.cnf. I have tried few options for bind-address - linux VM IP, 0.0.0.0 and completely commenting it.

到目前为止,所有选项都无效.

None of the options worked so far.

我的猜测是,我的Windows应用程序无法访问在VM中运行的MySql.

My guess is, my windows application is not able to access MySql which is running in a VM.

有人可以帮助我解决这个问题吗?

Can somebody help me in resolving this issue?

谢谢.

推荐答案

最后,我能够远程连接MySQL ...

Finally I am able to connect MySQL remotely...

我做了以下2个设置...

I did below 2 settings...

  • 将所有名称授予DBName.*到"root" @'%',由"password"标识;
  • 修改/etc/mysql/mysql.conf.d/mysqld.cnf以便在[mysqld]部分下注释#bind-address = 127.0.0.1

此处回答了该问题

这篇关于从远程计算机到MySql的JDBC连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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