简体   繁体   中英

Access denied for user 'user'@'localhost' (MySQL DB connection error from Intellij)

UPDATE

I am trying to setup a Spring boot project using JPA and MySQL. I am having issues connecting to a simple MySQL DB with some tutorials I am following online. I am also trying to create a table called members and insert data using a script. I have the MySQL server running on my local right now and I get this error message:

ERROR 21205 --- [           main] o.a.tomcat.jdbc.pool.ConnectionPool      : Unable to create initial connections of pool.

java.sql.SQLException: Access denied for user 'user'@'localhost' (using password: YES)
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129) ~[mysql-connector-java-8.0.18.jar:8.0.18]
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97) ~[mysql-connector-java-8.0.18.jar:8.0.18]

Here is my application.properties file

spring.datasource.url = jdbc:mysql://localhost:3306/members
spring.datasource.username = user
spring.datasource.password = password
spring.jpa.hibernate.ddl-auto = update
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect
spring.profiles.active=dev
#spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.

This is my create.sql

CREATE TABLE MEMBERS(
  memberId INT NOT NULL,
  companyName VARCHAR(50) NOT NULL,
  companySize INT NOT NULL,
  primaryContact VARCHAR(50) NOT NULL,
  city VARCHAR(50) NOT NULL,
  state VARCHAR(50) NOT NULL,
  netZeroTimeline INT NOT NULL,
  numCreditsDesired INT NOT NULL,
  --preferredOffsets VARCHAR(50) NOT NULL,
  budget INT NOT NULL,
  carbonCreditId INT NOT NULL,
  PRIMARY KEY (memberId)
);

Lastly this is my member entity class (model)

package org.example.data.jpa.model;

import javax.persistence.*;
import java.util.ArrayList;

@Entity
@Table(name = "member")
public class Member {

    Long memberId;
    String companyName;
    Integer companySize;
    String primaryContact;
    String city;
    String state;
    Integer netZeroTimeLine;
    Integer numCreditsDesired;
    //ArrayList<String> preferredOffsets = new ArrayList<String>();
    Double budget;
    Integer carbonCreditId;


    public Long getMemberId() {
        return memberId;
    }

    public void setMemberId(Long memberId) {
        this.memberId = memberId;
    }

    public String getCompanyName() {
        return companyName;
    }

    public void setCompanyName(String companyName) {
        this.companyName = companyName;
    }

    public Integer getCompanySize() {
        return companySize;
    }

    public void setCompanySize(Integer companySize) {
        this.companySize = companySize;
    }

    public String getPrimaryContact() {
        return primaryContact;
    }

    public void setPrimaryContact(String primaryContact) {
        this.primaryContact = primaryContact;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public Integer getNetZeroTimeLine() {
        return netZeroTimeLine;
    }

    public void setNetZeroTimeLine(Integer netZeroTimeLine) {
        this.netZeroTimeLine = netZeroTimeLine;
    }

    public Integer getNumCreditsDesired() {
        return numCreditsDesired;
    }

    public void setNumCreditsDesired(Integer numCreditsDesired) {
        this.numCreditsDesired = numCreditsDesired;
    }

    public Double getBudget() {
        return budget;
    }

    public void setBudget(Double budget) {
        this.budget = budget;
    }

    public Integer getCarbonCreditId() {
        return carbonCreditId;
    }

    public void setCarbonCreditId(Integer carbonCreditId) {
        this.carbonCreditId = carbonCreditId;
    }

}

Thanks for your time

@user6952090 Then if there is no problem with the service, then according to your log, I personally think that there are only two reasons for the exception, the first is that your password is wrong, and the second is that your sql permissions are insufficient. If you have a database management system installed on a windows system, you can do the following:

  1. Enter net stop mysql in cmd.exe to stop the MySQL service
  2. Enter the command line to go to the bin directory of mysql
  3. Enter mysqld --defaults-file="D:\MySQL\my.ini" --console --skip-grant-tables in the bin directory
  4. Open another cmd.exe, go to the bin directory of mysql, and enter: mysql -uroot mysql
  5. Input: update user set authentication_string=password('123456') where user='root';
  6. Refresh privileges: mysql>flush privileges;
  7. mysql>quit
  8. If you get an error when shutting down, type mysqladmin -uroot -p shutdown [new_password]
  9. After changing the password, you can enter the command line: net start mysql to start the MySQL service, mysql -uroot -p , enter the password to enter mysql

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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