简体   繁体   English

MySql远程访问被拒绝

[英]MySql Remote Access Denied

I am trying to connect to a mysql database. 我正在尝试连接到mysql数据库。 When I am trying to connect over mysql Workbench, I can but over java application, it says: 当我尝试通过mysql Workbench连接时,我只能通过Java应用程序连接,但显示:

Caused by: java.sql.SQLException: Access denied for user 'root'@'1.2.3.4' (using password: YES)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1084)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4232)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4164)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:926)
    at com.mysql.jdbc.MysqlIO.proceedHandshakeWithPluggableAuthentication(MysqlIO.java:1748)
    at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1288)
    at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2506)
    at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2539)
    at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2321)

The datasource definition is: 数据源定义为:

<bean id="datasource" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
    <property name="url" value="jdbc:mysql://3.3.3.3:3306"/>
    <property name="username" value="root"/>
    <property name="password" value="12345"/>
</bean>

and in db , user_priviliges table is: 在db中,user_priviliges表为:

'root'@'%', 'def', 'USAGE', 'NO'

Could please help me to solve this problem? 能帮我解决这个问题吗?

EDIT: 编辑:

I am using jdbcTemplate and the error is on this exact line: 我正在使用jdbcTemplate,并且错误在此确切的行上:

getJdbcTemplate().queryForList(sql.toString());

You need to create a an account for 'root'@'localhost' in addition to the 'root'@'%' account that you already have (with the same password). 除了已经拥有的'root'@'%'帐户(使用相同的密码)之外,您还需要为'root'@'localhost'创建一个帐户。 Why? 为什么? See the mysql docs on this subject . 请参阅有关此主题mysql文档 Here is the relevant part: 这是相关的部分:

Two of the accounts have a user name of monty and a password of some_pass. 其中两个帐户的用户名是monty,密码是some_pass。 Both accounts are superuser accounts with full privileges to do anything. 这两个帐户都是具有完全权限执行任何操作的超级用户帐户。 The 'monty'@'localhost' account can be used only when connecting from the local host. 仅在从本地主机连接时才能使用“ monty” @“ localhost”帐户。 The 'monty'@'%' account uses the '%' wildcard for the host part, so it can be used to connect from any host. “ monty” @“%”帐户将“%”通配符用于主机部分,因此可用于从任何主机进行连接。

It is necessary to have both accounts for monty to be able to connect from anywhere as monty. 必须同时拥有两个帐户的货币,才能从任何地方作为货币连接。 Without the localhost account, the anonymous-user account for localhost that is created by mysql_install_db would take precedence when monty connects from the local host. 如果没有localhost帐户,当monty从本地主机连接时,由mysql_install_db创建的localhost匿名用户帐户将具有优先权。 As a result, monty would be treated as an anonymous user. 结果,monty将被视为匿名用户。 The reason for this is that the anonymous-user account has a more specific Host column value than the 'monty'@'%' account and thus comes earlier in the user table sort order. 原因是匿名用户帐户比“ monty” @“%”帐户具有更特定的“主机”列值,因此在用户表排序顺序中排在较早的位置。 (user table sorting is discussed in Section 6.2.4, “Access Control, Stage 1: Connection Verification”.) (在第6.2.4节“访问控制,阶段1:连接验证”中讨论了用户表排序。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM