简体   繁体   中英

Node with SQL Server (Microsoft) connection to database

I need to provide the SQL connection (using the library called mssql) for modules, so I provide the code like this:

var config = {
    server: "149.xxx.xx.x",
    database: "Database",
    user: "User",
    password: "Password",
    connectionTimeout: 300000,
    requestTimeout: 300000,
    pool: {
        idleTimeoutMillis: 300000,
        max: 100
    }
};

function getEmp() {
    var connection = new sql.Connection(config);
    var req = new sql.Request(connection);

    connection.connect(function (error) {
        if(error) {
            console.log(error);
            return;
        }
        req.query('Procedure', function(err, data) {
            if (err) {
                console.log(err);
            } else {
                console.log(data);
            }
            connection.close();
        });
    });
}

getEmp();` 

and I receive the error:

 { ConnectionError: Failed to connect to 149.xxx.xx.x:1433 - connect
ETIMEDOUT 149.xxx.xx.x:1433 at Connection.<anonymous>
(/Users/xx/Learning/NodeJS/srv-express/node_modules/mssql/lib/tedious.js:353:25)
at Connection.g (events.js:292:16) at emitOne (events.js:96:13) at
Connection.emit (events.js:188:7) at Connection.socketError
(/Users/xx/Learning/NodeJS/srv-express/node_modules/tedious/lib/connection.js:791:12)
at Socket.<anonymous>
(/Users/xx/Learning/NodeJS/srv-express/node_modules/tedious/lib/connection.js:33:15)
at emitOne (events.js:96:13) at Socket.emit (events.js:188:7) at
emitErrorNT (net.js:1277:8) at _combinedTickCallback
(internal/process/next_tick.js:80:11)


name: 'ConnectionError',   message: 'Failed to connect to
49.xxx.xx.x:1433 - connect ETIMEDOUT 149.xxx.xx.x:1433',   code: 'ESOCKET' }

For sure the data are correct- DataGrip is connecting fine here.

I found at google the similar problem, the problem was Disabled TCP/IP. I checked this one and it's Enabled with this port 1443.

The TCP/IP, Named Pipes and Shared Memory is Enabled.

TCP Dynamic Ports: 1443 TCP Port: empty

To find the reason of 18456 error "login failed for user..." one should check SQLServer error log.

In case of

Reason: Failed to open the explicitly specified database 'Database Name'

one should first check the state of the database, if it's online, like this:

 select name, 
       user_access_desc, 
       state_desc 
 from sys.databases where name='yourDatabase'

In case the database is online, the next step is to check is the user can access the database (if it's mapped to the database:)

 select * 
 from yourDB.sys.database_principals 
 where name = 'YourUserName';

This code is valid for checking existance of user corresponding to sql server login only (windows login can access the server using windows group login, so does the user)

In case the user does not exists we should create user for this login and give him appropriate permissions. Here is the link for user cteation CREATE USER (Transact-SQL)

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