Home > Web Front-end > JS Tutorial > How to install mysql module in nodejs

How to install mysql module in nodejs

青灯夜游
Release: 2021-11-11 17:21:25
Original
2983 people have browsed it

How to install the mysql module in nodejs: 1. Open the cmd window and execute the "npm install -g cnpm" command to install the cnpm tool; 2. Use the cd command to enter the project directory and execute "cnpm install mysql" in the project command to install the MySQL module.

How to install mysql module in nodejs

The operating environment of this tutorial: windows7 system, nodejs version 12.19.0, Dell G3 computer.

node installation and configuration Mysql module

①Install Mysql module

cmd install Taobao image

npm install -g cnpm
Copy after login

Enter the project directory and install the MySQL module in the project

cnpm install mysql
Copy after login

②. Configure environment variables

Add C:\Program Files\MySQL\MySQL Server 8.0\bin
to the environment variables. The variable name can be arbitrary. For example, mysql_home
variable value is C:\Program Files\MySQL\MySQL Server 8.0\bin

③. Error report

Client does not support authentication protocol requested by server

1. Enter the decompressed mysql root directory through the command line (C:\Program Files\MySQL\MySQL Server 8.0\bin)Down.

2. Log in to the database

mysql -u root -p
Copy after login

3. Enter the root password

Enter password: ******
Copy after login

4. Change the encryption method

mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'password' PASSWORD EXPIRE NEVER;
Copy after login

5. Change the password: In this example 123456 is the new password

mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '123456';
Copy after login

6. Refresh:

mysql> FLUSH PRIVILEGES;
Copy after login

④.js file format

    var mysql = require('mysql');//引用Mysql
    var connection = mysql.createConnection({//配置连接
        host: 'localhost',//数据库地址
        user : "xxx",//数据库用户
        password: "xxx",//数据库密码
        database : "world"//需要连接的数据库
    });
    
    connection.connect();//连接数据库
    
    connection.query('select * from city',function(err,rows,fields){//执行sql语句
        if(err) throw err ;
        console.log('thesolution is:' ,rows[0]);
    });
    
    connection.end();//断开连接
Copy after login

[Recommended learning: "nodejs tutorial"]

The above is the detailed content of How to install mysql module in nodejs. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template