1. development environment vue+ node
2. computer system windows 10 pro
3. In the process of development , We always use A back-end language To manipulate the database , I chose node, The database is mysql , Let me briefly say , Use node How to connect mysql database , And return the queried database data to the front end . The method is as follows :
4. stay node Add the following code to the interface in :
4.1 First To install stay node Install in mysql , The code is as follows :
npm install mysql --save 4.2 I don't say much nonsense , Operate directly :
// newly build mysql Folder ,mysql Under the new index.js, The code is as follows
const mysql=require(`mysql`);
const connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: 'chen',
database: 'chen',
port: '3306'
});
// Create a connection
connection.connect(function(err){
if(err){
console.log(' The connection fails :'+err);
return;
}
console.log(' Successful connection ');
});
module.exports=connection;4-3. Use... In the corresponding interface
const connection=require('../mysql')
router.post("/",function(req,res,next){
console.log(req.body);
console.log("+++++++++++");
connection.query("select * from userTable where name='"+req.body.name+"'",(err,result)=>{
if(err){
console.log(" The query failed "+err);
}else{
console.log(result);
let data=undefined;
if(result.length===0){
data={
code:200,
msg:" The request is successful , Temporarily no data ",
data: result,
}
}else {
data={
code:200,
msg:" The request is successful , Registered interface ",
data:result
};
}
res.json(data);
}
})
})
module.exports = router;4-4. Expand
// Be careful : If you report an error when connecting to the database , The content of the error report is as follows :
ER_NOT_SUPPORTED_AUTH_MODE: Client does not support authentication protocol requested by server; consider upgrading MySQL client
Don't panic when you see this mistake , The solution is as follows :
The first step is to enter mysql , You can enter mysql command , Execute the following command :
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'chen';
// Be careful : This Local chen It's the database password .
5. This sharing ends here , Is it simple ! Let's work together to get to the top ! Don't bow your head don't give up .







