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.
node installation and configuration Mysql module
①Install Mysql module
cmd install Taobao image
npm install -g cnpm
Enter 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 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
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;
④.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();//断开连接[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!
Hot AI Tools
Undress AI Tool
Undress images for free
Undresser.AI Undress
AI-powered app for creating realistic nude photos
AI Clothes Remover
Online AI tool for removing clothes from photos.
Clothoff.io
AI clothes remover
Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!
Hot Article
Hot Tools
Notepad++7.3.1
Easy-to-use and free code editor
SublimeText3 Chinese version
Chinese version, very easy to use
Zend Studio 13.0.1
Powerful PHP integrated development environment
Dreamweaver CS6
Visual web development tools
SublimeText3 Mac version
God-level code editing software (SublimeText3)
What is the difference between npm and npm.cmd files in the nodejs installation directory?
Apr 21, 2024 am 05:18 AM
There are two npm-related files in the Node.js installation directory: npm and npm.cmd. The differences are as follows: different extensions: npm is an executable file, and npm.cmd is a command window shortcut. Windows users: npm.cmd can be used from the command prompt, npm can only be run from the command line. Compatibility: npm.cmd is specific to Windows systems, npm is available cross-platform. Usage recommendations: Windows users use npm.cmd, other operating systems use npm.
Is nodejs a backend framework?
Apr 21, 2024 am 05:09 AM
Node.js can be used as a backend framework as it offers features such as high performance, scalability, cross-platform support, rich ecosystem, and ease of development.
Can nodejs write front-end?
Apr 21, 2024 am 05:00 AM
Yes, Node.js can be used for front-end development, and key advantages include high performance, rich ecosystem, and cross-platform compatibility. Considerations to consider are learning curve, tool support, and small community size.
What are the global variables in nodejs
Apr 21, 2024 am 04:54 AM
The following global variables exist in Node.js: Global object: global Core module: process, console, require Runtime environment variables: __dirname, __filename, __line, __column Constants: undefined, null, NaN, Infinity, -Infinity
Is nodejs a back-end development language?
Apr 21, 2024 am 05:09 AM
Yes, Node.js is a backend development language. It is used for back-end development, including handling server-side business logic, managing database connections, and providing APIs.
How to deploy nodejs project to server
Apr 21, 2024 am 04:40 AM
Server deployment steps for a Node.js project: Prepare the deployment environment: obtain server access, install Node.js, set up a Git repository. Build the application: Use npm run build to generate deployable code and dependencies. Upload code to the server: via Git or File Transfer Protocol. Install dependencies: SSH into the server and use npm install to install application dependencies. Start the application: Use a command such as node index.js to start the application, or use a process manager such as pm2. Configure a reverse proxy (optional): Use a reverse proxy such as Nginx or Apache to route traffic to your application
How to connect nodejs to mysql database
Apr 21, 2024 am 06:13 AM
To connect to a MySQL database, you need to follow these steps: Install the mysql2 driver. Use mysql2.createConnection() to create a connection object that contains the host address, port, username, password, and database name. Use connection.query() to perform queries. Finally use connection.end() to end the connection.
What projects is nodejs suitable for?
Apr 21, 2024 am 05:45 AM
Node.js is suitable for the following project types: Network and server applications Event-driven applications Real-time applications Data-intensive applications Command-line tools and scripts Lightweight microservices


