Home >Web Front-end >JS Tutorial >How to install mysql module in nodejs
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.
The operating environment of this tutorial: windows7 system, nodejs version 12.19.0, Dell G3 computer.
cmd install Taobao image
npm install -g cnpm
Enter the project directory and install the MySQL module in the project
cnpm install mysql
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
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
3. Enter the root password
Enter password: ******
4. Change the encryption method
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'password' PASSWORD EXPIRE NEVER;
5. Change the password: In this example 123456 is the new password
mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '123456';
6. Refresh:
mysql> FLUSH PRIVILEGES;
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();//断开连接
[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!