我的春季启动应用程序的新版本无法连接到mysql

时间:2017-05-29 15:47:22

标签: java mysql spring tomcat jenkins

我的春季启动应用程序在localhost上正常运行,并且已经存在几个月的第一个版本工作正常,但现在我尝试上传新版本的应用程序或任何其他春季启动应用程序我收到以下错误:

not eligible for auto-proxying)
2017-05-29 17:20:48.565 ERROR 618 --- [           main] o.a.tomcat.jdbc.pool.ConnectionPool      : Unable to create initial connections of pool.

java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)
	at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1055) ~[mysql-connector-java-5.1.6.jar:na]
	at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956) ~[mysql-connector-java-5.1.6.jar:na]
	at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3491) ~[mysql-connector-java-5.1.6.jar:na]
	at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3423) ~[mysql-connector-java-5.1.6.jar:na]
	at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:910) ~[mysql-connector-java-5.1.6.jar:na]
	at com.mysql.jdbc.MysqlIO.secureAuth411(MysqlIO.java:3923) ~[mysql-connector-java-5.1.6.jar:na]
	at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1273) ~[mysql-connector-java-5.1.6.jar:na]
	at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2031) ~[mysql-connector-java-5.1.6.jar:na]
	at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:718) ~[mysql-connector-java-5.1.6.jar:na]
	at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:46) ~[mysql-connector-java-5.1.6.jar:na]
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_131]
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_131]
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_131]
	at java.lang.reflect.Constructor.newInstance(Constructor.java:423) ~[na:1.8.0_131]
	at com.mysql.jdbc.Util.handleNewInstance(Util.java:406) ~[mysql-connector-java-5.1.6.jar:na]
	at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:302) ~[mysql-connector-java-5.1.6.jar:na]
	at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:282) ~[mysql-connector-java-5.1.6.jar:na]
	at org.apache.tomcat.jdbc.pool.PooledConnection.connectUsingDriver(PooledConnection.java:310) ~[tomcat-jdbc-8.5.11.jar:na]
	at org.apache.tomcat.jdbc.pool.PooledConnection.connect(PooledConnection.java:203) ~[tomcat-jdbc-8.5.11.jar:na]
	at org.apache.tomcat.jdbc.pool.ConnectionPool.createConnection(ConnectionPool.java:732) [tomcat-jdbc-8.5.11.jar:na]

以下是我的应用程序属性文件:

spring.jpa.hibernate.ddl-auto=update

# Replace with your connection string
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/appName
spring.datasource.connectionProperties=useUnicode=true;characterEncoding=utf-8;

# Replace with your credentials
spring.datasource.username=root
spring.datasource.password=root


# Keep the connection alive if idle for a long time (needed in production)
spring.datasource.testWhileIdle = true
spring.datasource.validationQuery = SELECT 1

spring.datasource.test-on-borrow=true
spring.datasource.connection-test-query=SELECT 1
spring.datasource.tomcat.validation-interval=0
# Number of ms to wait before throwing an exception if no connection is available.
spring.datasource.tomcat.max-wait=10000

# Validate the connection before borrowing it from the pool.
spring.datasource.tomcat.test-on-borrow=true


spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect

# The SQL dialect makes Hibernate generate better SQL for the chosen database
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
#server.port = 8081

所以,已经检查过mysql正在运行并且我能够使用workbench连接到它并且已经说明了1个实时应用程序,我没有输入错误的用户名/密码,或者对application.properties文件进行了任何更改问题,问题是只有Spring启动应用程序,而Spring应用程序运行正常。 我使用Jenkins在Tomcat上部署应用程序,上面发布的错误输出来自jenkins。

2 个答案:

答案 0 :(得分:0)

  

java.sql.SQLException:拒绝用户&root;&#39; @&#39; localhost&#39;   (使用密码:是)

这是一个权限问题。您必须GRANT权限才能使用给定的用户名和密码从该客户端连接到该主机。我不建议任何应用程序root访问数据库。为应用程序的特定数据库创建应用程序凭据。

你这太复杂了。 Spring,Jen​​kins,Tomcat等不是你的问题。编写一个简单的main方法来建立JDBC连接,如下所示:

package database.util;

import org.mariadb.jdbc.MySQLDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * Database utilities
 * Created by Michael
 * Creation date 5/3/2016.
 * @link https://stackoverflow.com/questions/36999860/mysql-driver-problems/37000276#comment61553720_37000276
 */
public class DatabaseUtils {

    public static final String DEFAULT_DRIVER = "org.mariadb.jdbc.Driver";
    public static final String DEFAULT_URL = "jdbc:mariadb://localhost:3306/contact";
    public static final String DEFAULT_USERNAME = "contact";
    public static final String DEFAULT_PASSWORD = "contact";
    public static final String DEFAULT_HOST = "localhost";
    public static final int DEFAULT_PORT = 3306;
    public static final String DEFAULT_DATABASE = "contact";

    public static void main(String[] args) {
        Connection connection = null;
        try {
            connection = createConnection(DEFAULT_DRIVER, DEFAULT_URL, DEFAULT_USERNAME, DEFAULT_PASSWORD);
            DatabaseMetaData meta = connection.getMetaData();
            System.out.println(String.format("Connected to %s version %s", meta.getDatabaseProductName(), meta.getDatabaseProductVersion()));
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        } finally {
            close(connection);
        }
    }

    public static DataSource createDataSource(String host, int port, String database) throws ClassNotFoundException, SQLException {
        return new MySQLDataSource(host, port, database);
    }

    public static Connection createConnection(String driverClass, String url, String username, String password) throws ClassNotFoundException, SQLException {
        Class.forName(driverClass);
        return DriverManager.getConnection(url, username, password);
    }

    public static void close(Connection connection) {
        try {
            if (connection != null) {
                connection.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static void close(Statement st) {
        try {
            if (st != null) {
                st.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static void close(ResultSet rs) {
        try {
            if (rs != null) {
                rs.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

答案 1 :(得分:0)

我修复了:编辑mysqlId.cnf文件中的bind-address,该文件位于/etc/mysql/mysql.conf.d/目录,如下所示:
bind-address = 0.0.0.0

然后重新加载mysql服务。

然后我使用以下内容创建了一个新用户:

创建用户&#39; myuser&#39; @&#39; localhost&#39;通过&#39; mypass&#39;; 创建用户&#39; myuser&#39; @&#39;%&#39;通过&#39; mypass&#39;;

识别

全部开启 TO&#39; myuser&#39; @&#39; localhost&#39 ;; 全部授予 TO&#39; myuser&#39; @&#39;%&#39;;

和刷新的权限:

FLUSH PRIVILEGES;

积分转到@apesa you can find more detailed answer here