您的位置:首页 > 博客中心 > 数据库 >

nodejs使用xampp中的mysql教程

时间:2022-03-14 04:30

以下为通过node.js连接本机mysql数据库的方法:

var mysql = require('mysql');
var connection = mysql.createConnection({
  host     : 'localhost',
  user     : 'xxxx',//此处的xxxx替换为你的mysql登录用户名
  password : 'xxxx',//此处的xxxx替换为你的mysql登录密码
  database : 'xxxx',//此处的xxxx替换为你要连接database名称
  socketPath : '/Applications/XAMPP/xamppfiles/var/mysql/mysql.sock',
  port:3306
});

connection.connect(function(err) {
  if (err) {
    console.error('error connecting: ' + err.stack);
    return;
  }

  console.log('connected as id ' + connection.threadId);
});


这里的重点是要配置socketPath,否则会报以下这个错:

error connecting: Error: connect ECONNREFUSED

热门排行

今日推荐

热门手游