Some minor problems that appear in connection NodeJS and MySQL for the first time

MySQL Error Log

The first landing time, MySQL reported the following error

ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

I think this problem occurs when I forgot my password log in as root, enter the password given as follows

ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

That I was a bit confused. To check the next solution is to overwrite the root user's password just fine.


NodeJS error when connecting to a remote database

Before the adoption of WorkBench and remote database files successfully established. After the start of the next step in the NodeJSproject file by npminstalling mysqlthe components, referring to GitHub began to prepare a document to establish a connection to the database. Console error after first run

ER_NOT_SUPPORTED_AUTH_MODE: Client does not support authentication protocol requested by server; consider upgrading MySQL client

This question appears to be because the database version is too low cause does not support remote connections, but I updated the server and client mysql MySQL and NodeJS of components, this problem still exists.

That this problem after the fact is that the multi-query data NodeJS support mysql version is too low / no strict encryption , the solution is very simple, just need to be connected encryption instead of database encryption like ordinary

Change strict encryption to encrypt ordinary SQL code is as follows

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'your_password'
FLUSH PRIVILEGES;

Or find and modify the common encryption just fine in WorkBench GUI management software, etc.


NodeJS return results after SQL query operation

Compared to advantages and difficulties with other server-side language NodeJS is its asynchronous function. Almost all NodeJS of I / O operations are asynchronous, which also led to often occur not properly understand the "strange" phenomenon in use.

After establishing a connection to the remote database, using the mysqlcomponents of remote data query, the query result correctly in the console output, but in any case it can not be returned by the object. This is because mysqlthe query component method is asynchronous, before the inquiry has been completed returns an empty result object.

Although you can use genlock to solve this problem, but I do not feel elegant, and ultimately plan to use ECMAScript6the new features introduced in Promiseto solve.

PromiseThe effect is to make the code had become similar to the asynchronous execution of synchronous execution, that is, after the execution, the state will determine, at this time calls .thenthe result of the method will return to the state to determine the specific tutorials can see the link below

Bodhi Tree Jane book

My implementation in the following code stickers

function query () {
    // 使用Promise更改异步操作为同步操作,使得回调函数能正确执行
    let promise = new Promise(function (resolve, reject) {
        // 建立连接
        connection.connect(function (err) {
            if (err) {
                console.error('Error connecting: ' + err.stack);
                return;
            }
            console.log('Connected as id ' + connection.threadId);
        });

        // TODO: 假设数据库中代表场景序号的属性为 scene_no
        let querySql = 'SELECT * FROM test;'
        connection.query(querySql, function (err, result) {
            if (err) {
                console.log('[SELECT ERROR] - ', err.message);
                return 0;
            }
            resolve(result);
        });

        // 终止连接
        connection.end();
    });

    promise.then(function (queryResult) {
        console.log(queryResult);
        return queryResult;
    }, function (value) {});
    return promise;
}

Guess you like

Origin www.cnblogs.com/simon-ghost-riley/p/12059078.html