在配置开源项目时,尝试将应用程序连接到本地运行的MySQL数据库时遇到问题。在确定了由于MySQL 8.0引入的某些更改而导致的不兼容性之后,即使详细信息正确且用户具有所有特权,但尝试使用root用户和另一个手动创建并手动分配了特权的用户,也无法创建连接。 My SQL Manager类如下。
public class MySqlManager {
private static PoolingDataSource<PoolableConnection> dataSource;
private static int minIdle;
private static int maxIdle;
public static void initialize()
throws ClassNotFoundException, SQLException, IllegalArgumentException {
minIdle = Configuration.getInt("mysql.idle.min");
maxIdle = Configuration.getInt("mysql.idle.max");
if (minIdle < 0) {
throw new IllegalArgumentException(Localization.get("mysql.error.pool"));
}
System.out.println("Host: " + Configuration.get("mysql.host"));
System.out.println("Port: " + Configuration.get("mysql.port"));
System.out.println("User: " + Configuration.get("mysql.user"));
System.out.println("Pass: " + Configuration.get("mysql.pass"));
// Load JDBC Driver. ClassNotFoundException may be thrown.
Class.forName("com.mysql.cj.jdbc.Driver");
// jdbc:mysql://host:port/database/?args
String url = "jdbc:mysql://" +
Configuration.get("mysql.host") + ":" +
Configuration.get("mysql.port") + "/" +
Configuration.get("mysql.database") + "?" +
"zeroDateTimeBehavior=CONVERT_TO_NULL" + "&" +
"autoReconnect=true";
System.out.println("URL: " + url);
setupDataSource(url, Configuration.get("mysql.user"), Configuration.get("mysql.pass"));
// Check if the connection with the database is working, otherwise SQLException is thrown
try (Connection con = getConnection()) {
if (con == null) {
throw new RuntimeException("Retrieved dataSource connection is null.");
}
}
}
private static void setupDataSource(String url, String user, String password) {
PoolableConnectionFactory factory = new PoolableConnectionFactory(
new DriverManagerConnectionFactory(url, user, password), null);
GenericObjectPoolConfig config = new GenericObjectPoolConfig();
config.setMaxTotal(maxIdle);
config.setMaxIdle(maxIdle);
config.setMinIdle(minIdle);
ObjectPool<PoolableConnection> connectionPool = new GenericObjectPool<>(factory, config);
factory.setPool(connectionPool);
dataSource = new PoolingDataSource<>(connectionPool);
}
static Connection getConnection() throws SQLException {
return dataSource.getConnection();
}
private MySqlManager() {}
}
错误消息显示Could not create connection to database server. Attempted reconnect 3 times. Giving up.
框架版本包括MySQL 8.0.16,DBCP 2.6.0,MySQL-Connector-Java 8.0.16。所述用户的特权证明如下。
GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, RELOAD, SHUTDOWN, PROCESS, FILE, REFERENCES, INDEX, ALTER, SHOW DATABASES, SUPER, CREATE TEMPORARY TABLES, LOCK TABLES, EXECUTE, REPLICATION SLAVE, REPLICATION CLIENT, CREATE VIEW, SHOW VIEW, CREATE ROUTINE, ALTER ROUTINE, CREATE USER, EVENT, TRIGGER, CREATE TABLESPACE, CREATE ROLE, DROP ROLE ON *.* TO `joao`@`localhost` WITH GRANT OPTION;
GRANT APPLICATION_PASSWORD_ADMIN,BACKUP_ADMIN,BINLOG_ADMIN,BINLOG_ENCRYPTION_ADMIN,CONNECTION_ADMIN,ENCRYPTION_KEY_ADMIN,GROUP_REPLICATION_ADMIN,PERSIST_RO_VARIABLES_ADMIN,REPLICATION_SLAVE_ADMIN,RESOURCE_GROUP_ADMIN,RESOURCE_GROUP_USER,ROLE_ADMIN,SERVICE_CONNECTION_ADMIN,SESSION_VARIABLES_ADMIN,SET_USER_ID,SYSTEM_USER,SYSTEM_VARIABLES_ADMIN,TABLE_ENCRYPTION_ADMIN,XA_RECOVER_ADMIN ON *.* TO `joao`@`localhost` WITH GRANT OPTION;