Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么我试图在Java程序中访问MariaDB时得到了';访问被拒绝';即使我的密码是正确的,也有例外吗?_Java_Mysql_Jdbc_Mariadb - Fatal编程技术网

为什么我试图在Java程序中访问MariaDB时得到了';访问被拒绝';即使我的密码是正确的,也有例外吗?

为什么我试图在Java程序中访问MariaDB时得到了';访问被拒绝';即使我的密码是正确的,也有例外吗?,java,mysql,jdbc,mariadb,Java,Mysql,Jdbc,Mariadb,我正在编写一个Java程序来查询SQL数据库,并提取我的查询,以查看我的数据库中是否存在用户名和密码。我的代码是: public UserInfo getUserExists(final String userEmail) throws SQLException, ClassNotFoundException { if (!isBlank(userEmail)) { Connection conn; Statement stmt; tr

我正在编写一个Java程序来查询SQL数据库,并提取我的查询,以查看我的数据库中是否存在用户名和密码。我的代码是:

public UserInfo getUserExists(final String userEmail) throws SQLException, ClassNotFoundException {

    if (!isBlank(userEmail)) {
        Connection conn;
        Statement stmt;

        try{
            Class.forName("org.mariadb.jdbc.Driver");
            System.out.println("Connecting to DB...");

            conn = DriverManager.getConnection("jdbc:mariadb://10.0.0.0:3306/mydb","myusername", "mypassword");

            System.out.println("Connected to DB successfully");
            System.out.println("querying if user is valid");

            stmt = conn.createStatement();

            ResultSet rs;
            rs = stmt.executeQuery("SELECT IF (EXISTS(select user_email, user_pass, meta_value from myorg_users left join myorg_usermeta on ( myorg_users.id = myorg_usermeta.user_id) where meta_value LIKE '%\"administrator\"%' and meta_key=\"myorg_capabilities\" AND user_email="+userEmail+"),1,0) AS result;");
            int result = 0;
            System.out.println(rs);

            while(rs.next()){
                result = Integer.parseInt(rs.getString("result"));
                System.out.println("result is " + result);
            }
            conn.close();
            if(result == 1){
                UserInfo SYSTEM_USER = new UserInfo.Builder(UserInfo.Username.valueOf("SYSTEM_USER")).withFirstName("System").withLastName("User").withEmail(userEmail).withUserId("sample").build();
                System.out.println(SYSTEM_USER);
                return SYSTEM_USER;
            }

        }
        catch(SQLException se){
            se.printStackTrace();
        }
        catch(Exception e){
            e.printStackTrace();
        }

    } else {
        throw new AuthenticationException("user does not exists in system");
    }
    System.out.println("executed new user exists");
    return null;
}
我的代码是:

public UserInfo getUserExists(final String userEmail) throws SQLException, ClassNotFoundException {

    if (!isBlank(userEmail)) {
        Connection conn;
        Statement stmt;

        try{
            Class.forName("org.mariadb.jdbc.Driver");
            System.out.println("Connecting to DB...");

            conn = DriverManager.getConnection("jdbc:mariadb://10.0.0.0:3306/mydb","myusername", "mypassword");

            System.out.println("Connected to DB successfully");
            System.out.println("querying if user is valid");

            stmt = conn.createStatement();

            ResultSet rs;
            rs = stmt.executeQuery("SELECT IF (EXISTS(select user_email, user_pass, meta_value from myorg_users left join myorg_usermeta on ( myorg_users.id = myorg_usermeta.user_id) where meta_value LIKE '%\"administrator\"%' and meta_key=\"myorg_capabilities\" AND user_email="+userEmail+"),1,0) AS result;");
            int result = 0;
            System.out.println(rs);

            while(rs.next()){
                result = Integer.parseInt(rs.getString("result"));
                System.out.println("result is " + result);
            }
            conn.close();
            if(result == 1){
                UserInfo SYSTEM_USER = new UserInfo.Builder(UserInfo.Username.valueOf("SYSTEM_USER")).withFirstName("System").withLastName("User").withEmail(userEmail).withUserId("sample").build();
                System.out.println(SYSTEM_USER);
                return SYSTEM_USER;
            }

        }
        catch(SQLException se){
            se.printStackTrace();
        }
        catch(Exception e){
            e.printStackTrace();
        }

    } else {
        throw new AuthenticationException("user does not exists in system");
    }
    System.out.println("executed new user exists");
    return null;
}
然而,当我运行我的API时,我得到了这个,我知道密码/用户名是正确的-

java.sql.SQLSyntaxErrorException: Access denied for user 'myusername'@'10.%' to database 'mydb'
    at org.mariadb.jdbc.internal.util.exceptions.ExceptionMapper.get(ExceptionMapper.java:163)
    at org.mariadb.jdbc.internal.util.exceptions.ExceptionMapper.getException(ExceptionMapper.java:106)
    at org.mariadb.jdbc.internal.protocol.AbstractConnectProtocol.connectWithoutProxy(AbstractConnectProtocol.java:1036)
    at org.mariadb.jdbc.internal.util.Utils.retrieveProxy(Utils.java:490)
    at org.mariadb.jdbc.MariaDbConnection.newConnection(MariaDbConnection.java:144)
    at org.mariadb.jdbc.Driver.connect(Driver.java:90)
    at java.sql.DriverManager.getConnection(DriverManager.java:664)
    at java.sql.DriverManager.getConnection(DriverManager.java:247)
    at com.intuit.wasabi.authentication.impl.DefaultAuthentication.getUserExists(DefaultAuthentication.java:168)
    at com.intuit.wasabi.api.AuthenticationResource.getUserExists(AuthenticationResource.java:214)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.sun.jersey.spi.container.JavaMethodInvokerFactory$1.invoke(JavaMethodInvokerFactory.java:60)
    at com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$ResponseOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:205)
    at com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:75)
    at com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:302)
    at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
    at com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:108)
    at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
    at com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:84)
    at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1542)
    at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1473)
    at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1419)
    at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1409)
    at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:409)
    at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:558)
    at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:733)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
    at com.google.inject.servlet.ServletDefinition.doServiceImpl(ServletDefinition.java:287)
    at com.google.inject.servlet.ServletDefinition.doService(ServletDefinition.java:277)
    at com.google.inject.servlet.ServletDefinition.service(ServletDefinition.java:182)
    at com.google.inject.servlet.ManagedServletPipeline.service(ManagedServletPipeline.java:91)
    at com.google.inject.servlet.FilterChainInvocation.doFilter(FilterChainInvocation.java:85)
    at com.intuit.autumn.web.WebFilter.doFilter(WebFilter.java:65)
    at com.google.inject.servlet.FilterChainInvocation.doFilter(FilterChainInvocation.java:82)
    at com.google.inject.servlet.ManagedFilterPipeline.dispatch(ManagedFilterPipeline.java:119)
    at com.google.inject.servlet.GuiceFilter$1.call(GuiceFilter.java:133)
    at com.google.inject.servlet.GuiceFilter$1.call(GuiceFilter.java:130)
    at com.google.inject.servlet.GuiceFilter$Context.call(GuiceFilter.java:203)
    at com.google.inject.servlet.GuiceFilter.doFilter(GuiceFilter.java:130)
    at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1676)
    at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:581)
    at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1160)
    at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:511)
    at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1092)
    at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141)
    at org.eclipse.jetty.server.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:213)
    at org.eclipse.jetty.server.handler.HandlerCollection.handle(HandlerCollection.java:119)
    at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:134)
    at org.eclipse.jetty.server.Server.handle(Server.java:518)
    at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:308)
    at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:244)
    at org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:273)
    at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:95)
    at org.eclipse.jetty.io.SelectChannelEndPoint$2.run(SelectChannelEndPoint.java:93)
    at org.eclipse.jetty.util.thread.strategy.ExecuteProduceConsume.produceAndRun(ExecuteProduceConsume.java:246)
    at org.eclipse.jetty.util.thread.strategy.ExecuteProduceConsume.run(ExecuteProduceConsume.java:156)
    at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:654)
    at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:572)
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.sql.SQLException: Access denied for user 'myusername'@'10.%' to database 'mydb'
    at org.mariadb.jdbc.internal.protocol.AbstractConnectProtocol.authentication(AbstractConnectProtocol.java:787)
    at org.mariadb.jdbc.internal.protocol.AbstractConnectProtocol.handleConnectionPhases(AbstractConnectProtocol.java:713)
    at org.mariadb.jdbc.internal.protocol.AbstractConnectProtocol.connect(AbstractConnectProtocol.java:402)
    at org.mariadb.jdbc.internal.protocol.AbstractConnectProtocol.connectWithoutProxy(AbstractConnectProtocol.java:1032)
    ... 59 more

发生了什么事?我在DB中运行了这个查询,它在MariaDB中工作。为什么我不能在Java中实现我所需要的功能?

除了Java之外,您是否确定要在10.0.0.0上访问数据库?我有根据地猜测,您的MariaDB配置为在环回接口(127.0.0.1)上侦听,因此,如果您的MariaDB未配置为侦听其他接口,您将得到一个连接拒绝。因此,如果您确定可以在phpmyadmin中执行此操作,我将开始检查此操作(我猜您使用它,并且它可能通过localhost访问?)。10.0.0.0不应该是一个可能的ip。

好吧,撇开java不谈,您是否确定要在10.0.0.0上访问数据库?我有根据地猜测,您的MariaDB配置为在环回接口(127.0.0.1)上侦听,因此,如果您的MariaDB未配置为侦听其他接口,您将得到一个连接拒绝。因此,如果您确定可以在phpmyadmin中执行此操作,我将开始检查此操作(我猜您使用它,并且它可能通过localhost访问?)。而10.0.0.0不应该是一个可能的ip。

我只放了10.0.0.0,这样我就不会把实际的ip地址放在数据库所在的服务器上。我会检查您的mariaDB配置是否与环回以外的接口一起工作,如果是这样,您的防火墙是否接受到指定端口的连接。如何找到环回地址?我这样做了
\s,得到:连接id:13944870当前数据库:mydb当前用户:myusername@10.10.20.2SSL:未在使用当前寻呼机:stdout使用outfile:“”使用分隔符:;服务器:MySQL。。。连接:10.10.55.1通过TCP/IP服务器字符集:latin1 Db字符集:latin1客户端字符集:utf8连接字符集:utf8 TCP端口:3306在上面的回复中,我更改了IP地址的编号。我的java代码与我之前的ReplyLopBack中的10.10.55.1相当,通常是127.0.0.1,但是如果您的db没有在本地计算机上运行,并且您可以在没有java的情况下连接到它,这可能不是问题。我只放了10.0.0.0,这样我就不会把实际的IP地址放在带有db的服务器上,我会检查您的mariaDB config正在使用环回以外的接口,如果是,防火墙是否接受到指定端口的连接?如何查找环回地址?我这样做了
\s,得到:连接id:13944870当前数据库:mydb当前用户:myusername@10.10.20.2SSL:未在使用当前寻呼机:stdout使用outfile:“”使用分隔符:;服务器:MySQL。。。连接:10.10.55.1通过TCP/IP服务器字符集:latin1 Db字符集:latin1客户端字符集:utf8连接字符集:utf8 TCP端口:3306在上面的回复中,我更改了IP地址的编号。我的java代码中的10.10.55.1与我之前的ReplyLopBack中的10.10.55.1相当,通常是127.0.0.1,但是如果您的db没有在本地计算机上运行,并且您可以在没有java的情况下连接到它,那么这可能不是问题所在。