How to install the mysql module in nodejs
Editor to share with you how to install nodejs mysql module, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to understand it!
Nodejs install mysql module method: 1, open the cmd window, execute the "npm install-g cnpm" command to install cnpm tools; 2, use the cd command to enter the project directory, execute the "cnpm install mysql" command in the project to install the MySQL module.
The operating environment of this tutorial: windows7 system, nodejs 12.19.0, Dell G3 computer.
Node install and configure Mysql module ① install Mysql module
Cmd installs Taobao image
Npm install-g cnpm
Go to the project directory and install the MySQL module in the project
Cnpm install mysql ②. Configure environment variables
Add C:\ Program Files\ MySQL\ MySQL Server 8.0\ bin to the environment variable
Variable names can be picked up at will, such as mysql_home
The variable value is C:\ Program Files\ MySQL\ MySQL Server 8.0\ bin
3. Error
Client does not support authentication protocol requested by server
1. Enter the extracted mysql root directory (C:\ Program Files\ MySQL\ MySQL Server 8.0\ bin) through the command line.
2. Log in to the database
Mysql-u root-p
3. Enter the password of root
Enter password: *
4. Change the encryption method
Mysql > ALTER USER 'root'@'localhost' IDENTIFIED BY' password' PASSWORD EXPIRE NEVER
5. Change the password: 123456 in this example is the new password
Mysql > ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY' 123456'
6. Refresh:
Mysql > FLUSH PRIVILEGES; ④ .js file format var mysql = require ('mysql'); / / reference Mysql var connection = mysql.createConnection ({/ / configure connection host:' localhost',// database address user: "xxx", / / database user password: "xxx", / / database password database: "world" / / database to be connected}); connection.connect () / / Connect to the database connection.query ('select * from city',function (err,rows,fields) {/ / execute the sql statement if (err) throw err; console.log (' thesolution is:', rows [0]);}); connection.end (); / / disconnect all the contents of the article "how nodejs installs the mysql module". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!