2

When attempting to connect to my local SQL Server instance I am receiving an error stating Authentication failed for login. However I am able to login directly to the server in SQL using the provided login.

Here is my code that is attempting to connect to the server.

var Sequelize = require('sequelize');


const sequelize = new Sequelize('GraphQLTests', 'gql', 'Password1', {

  dialect: 'mssql',
  host:'localhost'
});

sequelize
  .authenticate()
  .then(() => {
    console.log('Connection has been established successfully.');
  })
  .catch(err => {
    console.error('Unable to connect to the database:', err);
  });

I have printed to the console in the Sequelize code to verify that the correct credentials are getting passed but still receive this error.

name: 'SequelizeAccessDeniedError',
  parent:
   { ConnectionError: Login failed for user ''.}

Please let me know if there is any other info I can provide.

2 Answers 2

1

try this

const sequelize = new Sequelize('DB Name', 'Username', 'Password', {
    host: 'Host',
    dialect: 'mssql',
    dialectOptions: {
        options: {
            encrypt: true,
        }
    }
  });

sequelize.authenticate().then((err) => {
    console.log('Connection successful', err);
})
.catch((err) => {
    console.log('Unable to connect to database', err);
});

Try this link as reference Connecting to MSSQL server with Sequelize

0

This is what worked for me, where I have put the server details in an YAML file. (this way, you can switch servers on the fly).

this is the sequelize code

//get the configure from the YAML file
const YAML = await fs.readFile(process.env.SEQUELIZE_CONNECT,'utf8');
//load the database parameters into our system
const params = jsyaml.safeLoad(YAML, 'utf8'); 
//initiate our database server details to connect to our underlying database system 
//as described in the YAML file.
sequlz = new Sequelize(params.dbname, params.username, params.password, params.params); 

Here is how my YAML file looks. (I have left my code comments as it is)

#this should work to whatever you are using anywhere.
#as per the YAML file title, I am using a MS SQL server hosted on Azure.
# you can edit values. as per your requirement.
# check the dialect help file of your server on the sequelize documentation
# https://sequelize.org/v5/file/lib/dialects/mssql/connection-manager.js.html

#change the values as per your server.

dbname: databasenamehere 
username: usernamehere
password: passwordhere
params: 
    host: servernamehere.database.windows.net
    dialect: mssql 
    dialectOptions: 
        {
            options: {
                encrypt: true,
                requestTimeout: 10000
            }
        }

So, that worked for me. (I have remixed answers from above post, and also from the textbook and multiple online resources I was referring).

Note : The server is running on Azure with the Firewall IP address set to ALL IP address.

1
  • A gentle note: An easier way is to have the config in a JSON file .. So you get all benefits from require instead of readFile and no need to jsyaml anymore.
    – Mouneer
    Commented Sep 1, 2021 at 12:35

Not the answer you're looking for? Browse other questions tagged or ask your own question.