使用Maven SQL Plugin遇Access Denied错误,寻求解决方案
Troubleshooting "Access denied for user 'root'@'localhost'" with maven-sql-plugin
Let's break down your issue step by step—there's a clear mismatch between your plugin configuration and the actual database your application is trying to connect to, which is causing the access denied error.
Key Observation from the Exception
Looking at your stack trace, all the error classes are from com.mysql.jdbc.*:
java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:996) ...
But your maven-sql-plugin configuration explicitly uses the H2 database (driver org.h2.Driver and H2-specific JDBC URL). This means one of two things:
- Your plugin configuration isn't being applied, and your test code/another component is hardcoding a MySQL connection.
- There's a conflicting configuration elsewhere in your project that's overriding the H2 setup.
Issues in Your Plugin Configuration
Let's go through your plugin setup to spot problems:
- Misplaced GRANT Statement:
You added a MySQL-styleGRANTcommand in<sqlCommand>, but H2 doesn't require this. The defaultroot/rootuser in H2 has full permissions out of the box. This statement is irrelevant for H2 and could cause unexpected errors. - Potential Path Permissions:
Your H2 file URL isjdbc:h2:file:target/db/employee—ensure thetarget/dbdirectory can be created by Maven (no filesystem permission issues) when running the plugin.
Fix Steps to Resolve the Error
Option 1: Stick with H2 Database (as configured in your plugin)
- Align Test Code with Plugin Configuration:
Double-check your test classes or any application-level datasource configs—make sure they're using the same H2 driver, URL, credentials as your plugin:- Driver:
org.h2.Driver - URL:
jdbc:h2:file:target/db/employee;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;FILE_LOCK=NO;INIT=CREATE SCHEMA IF NOT EXISTS employee\;SET SCHEMA employee - Username/Password:
root/root
- Driver:
- Remove the MySQL GRANT Statement:
Delete the<sqlCommand>block from your plugin configuration—it's not needed for H2. - Verify H2 Connection Manually:
Use the H2 Console to test the connection:- Download the H2 jar, run
java -jar h2-x.x.x.jar - Enter your JDBC URL, username, password, and click "Connect". If this fails, fix the URL/path first.
- Download the H2 jar, run
Option 2: Switch to MySQL (if that's your intended database)
If you actually want to use MySQL instead of H2:
- Update Plugin Configuration:
Uncomment the MySQL driver and URL, comment out H2 settings:<plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>sql-maven-plugin</artifactId> <version>1.5</version> <executions> <execution> <id>create-database</id> <phase>pre-integration-test</phase> <goals> <goal>execute</goal> </goals> <configuration> <!--<driver>org.h2.Driver</driver>--> <driver>com.mysql.jdbc.Driver</driver> <url>jdbc:mysql://localhost:3306/employee?createDatabaseIfNotExist=true</url> <username>root</username> <password>root</password> <srcFiles> <srcFile>src/test/resources/employee.sql</srcFile> </srcFiles> <!-- Fix the GRANT syntax for MySQL --> <sqlCommand>GRANT ALL PRIVILEGES ON employee.* TO 'root'@'localhost' IDENTIFIED BY 'root' WITH GRANT OPTION;</sqlCommand> </configuration> </execution> </executions> <dependencies> <!--<dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <version>1.3.166</version> </dependency>--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.18</version> </dependency> </dependencies> </plugin> - Validate MySQL User Access:
- Ensure MySQL server is running locally on port 3306.
- Log into MySQL manually with
mysql -u root -pand enter passwordroot—if this fails, reset the root password or fix user permissions in MySQL. - Run the corrected GRANT statement directly in MySQL to ensure the user has access to the
employeedatabase.
Your Original Plugin Configuration (for reference)
<plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>sql-maven-plugin</artifactId> <version>1.5</version> <executions> <execution> <id>create-database</id> <phase>pre-integration-test</phase> <goals> <goal>execute</goal> </goals> <configuration> <driver>org.h2.Driver</driver> <!--<driver>com.mysql.jdbc.Driver</driver> --> <url>jdbc:h2:file:target/db/employee;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;FILE_LOCK=NO;INIT=CREATE SCHEMA IF NOT EXISTS employee\;SET SCHEMA employee</url> <username>root</username> <password>root</password> <srcFiles> <srcFile>src/test/resources/employee.sql</srcFile> </srcFiles> <sqlCommand>${GRANT ALL PRIVILEGES ON . TO 'root'@'localhost' IDENTIFIED BY 'root' WITH GRANT OPTION}</sqlCommand> </configuration> </execution> </executions> <dependencies> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <version>1.3.166</version> </dependency> <!--<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.18</version> </dependency> --> </dependencies> </plugin>
Your Original Exception Stack Trace (for reference)
[com.mchange.v2.resourcepool.BasicResourcePool:run-1851] - com.mchange.v2.resourcepool.BasicResourcePool$ScatteredAcquireTask@462393ff -- Acquisition Attempt Failed!!! Clearing pending acquires. While trying to acquire a needed new resource, we failed to succeed more than the maximum number of allowed acquisition attempts (30). Last acquisition attempt exception: java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:996) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3887) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3823) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:870) at com.mysql.jdbc.MysqlIO.proceedHandshakeWithPluggableAuthentication(MysqlIO.java:1659) at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1206) at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2234) at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2265) at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2064) at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:790) at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:44) at sun.reflect.GeneratedConstructorAccessor4.newInstance(Unknown Source) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:422) at com.mysql.jdbc.Util.handleNewInstance(Util.java:377) at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:395) at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:325) at com.mchange.v2.c3p0.DriverManagerDataSource.getConnection(DriverManagerDataSource.java:146) at com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:195) at com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:184) at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool$1PooledConnectionResourcePoolManager.acquireResource(C3P0PooledConnectionPool.java:200) at com.mchange.v2.resourcepool.BasicResourcePool.doAcquire(BasicResourcePool.java:1086) at com.mchange.v2.resourcepool.BasicResourcePool.doAcquireAndDecrementPendingAcquiresWithinLockOnSuccess(BasicResourcePool.java:1073) at com.mchange.v2.resourcepool.BasicResourcePool.access$800(BasicResourcePool.java:44) at com.mchange.v2.resourcepool.BasicResourcePool$ScatteredAcquireTask.run(BasicResourcePool.java:1810) at com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread.run(ThreadPoolAsynchronousRunner.java:648)
内容的提问来源于stack exchange,提问作者Sat
相关产品推荐
相关产品推荐

