执行Spark时出现:java.sql.SQLException: Access denied for user ‘root‘@‘master‘ (using password: YES)异常

文章目录

  • 错误样式
  • 异常原因
  • 解决方法

错误样式

scala> :paste
// Entering paste mode (ctrl-D to finish)

val studentDF = spark.read.format("jdbc")
  .option("url", "jdbc:mysql://master:3306/spark_db?useSSL=false")     
  .option("driver","com.mysql.jdbc.Driver")  
  .option("dbtable", "student")  
  .option("user", "root")  
  .option("password", "LZYp@ssw0rd")  
  .load()

// Exiting paste mode, now interpreting.

java.sql.SQLException: Access denied for user 'root'@'master' (using password: YES)
  at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:965)
  at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3933)
  at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3869)
  at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:864)
  at com.mysql.jdbc.MysqlIO.proceedHandshakeWithPluggableAuthentication(MysqlIO.java:1707)
  at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1217)
  at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2189)
  at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2220)
  at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2015)
  at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:768)
  at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
  at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
  at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
  at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
  at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
  at com.mysql.jdbc.Util.handleNewInstance(Util.java:403)
  at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:385)
  at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:323)
  at org.apache.spark.sql.execution.datasources.jdbc.connection.BasicConnectionProvider.getConnection(BasicConnectionProvider.scala:49)
  at org.apache.spark.sql.execution.datasources.jdbc.connection.ConnectionProviderBase.create(ConnectionProvider.scala:102)
  at org.apache.spark.sql.jdbc.JdbcDialect.$anonfun$createConnectionFactory$1(JdbcDialects.scala:122)
  at org.apache.spark.sql.jdbc.JdbcDialect.$anonfun$createConnectionFactory$1$adapted(JdbcDialects.scala:118)
  at org.apache.spark.sql.execution.datasources.jdbc.JDBCRDD$.getQueryOutputSchema(JDBCRDD.scala:63)
  at org.apache.spark.sql.execution.datasources.jdbc.JDBCRDD$.resolveTable(JDBCRDD.scala:58)
  at org.apache.spark.sql.execution.datasources.jdbc.JDBCRelation$.getSchema(JDBCRelation.scala:242)
  at org.apache.spark.sql.execution.datasources.jdbc.JdbcRelationProvider.createRelation(JdbcRelationProvider.scala:37)
  at org.apache.spark.sql.execution.datasources.DataSource.resolveRelation(DataSource.scala:350)
  at org.apache.spark.sql.DataFrameReader.loadV1Source(DataFrameReader.scala:228)
  at org.apache.spark.sql.DataFrameReader.$anonfun$load$2(DataFrameReader.scala:210)
  at scala.Option.getOrElse(Option.scala:189)
  at org.apache.spark.sql.DataFrameReader.load(DataFrameReader.scala:210)
  at org.apache.spark.sql.DataFrameReader.load(DataFrameReader.scala:171)
  ... 55 elided

异常原因

在mysql环境正常,输入命令无误的情况下,出现上述错误,原因是mysql没开远程访问权限

解决方法

添加程访问权限,输入命令:grant all privileges on . to ‘root’@‘master’ identified by ‘密码’ with grant option;

在这里插入图片描述
更新权限,输入命令:flush privileges;
执行Spark时出现:java.sql.SQLException: Access denied for user ‘root‘@‘master‘ (using password: YES)异常_第1张图片

查看权限,输入命令:SHOW GRANTS FOR ‘root’@‘master’;
执行Spark时出现:java.sql.SQLException: Access denied for user ‘root‘@‘master‘ (using password: YES)异常_第2张图片
重新执行命令,成功解决
执行Spark时出现:java.sql.SQLException: Access denied for user ‘root‘@‘master‘ (using password: YES)异常_第3张图片

你可能感兴趣的:(java,spark,开发语言,异常处理)