当前位置:网站首页>Nodejs connect mysql

Nodejs connect mysql

2022-07-06 11:36:00 Advanced diary

  1. install mysql drive

    npm install mysql
    
  2. Connect mysql database

    // Import mysql drive 
    const mysql = require("mysql");
    
    //  To configure mysql Connection information 
    const con = mysql.createConnection({
    	host:"127.0.0.1",//ip Address 
    	user:" Database account ",
    	password:" Database password ",
    	port:"3306",// Port number 
    	database:"book"// The name of the database to connect to 
    })
    
    //  Start connecting 
    con.connect()
    
    //  establish sql sentence 
    const sql = "select * from users;"
    
    // perform sql sentence 
    con.query(sql,(err,result)=>{
    	if(err){
    		console.error(err);
    		return
    	}
    	console.log(result);
    })
    
    //  Close the connection 
    con.end()
    

    Interested partners can learn together nodejs Knowledge compilation api Interface , You can see https://blog.csdn.net/qq_47824967/article/details/121909375?spm=1001.2014.3001.5501 How to write interfaces

原网站

版权声明
本文为[Advanced diary]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207060912569355.html