简体   繁体   English

如何在Node.js中使用MySQL?

[英]How can I use MySQL with Node.js?

I've looked around for a bit, but I haven't found a way to use Node.js to access MySQL. 我环顾了一下,但是还没有找到使用Node.js访问MySQL的方法。 How would I do that without using a side program? 不使用辅助程序怎么办?

The way to go is to 要走的路是

  • download node.js, 下载node.js,
  • install mysql server (I had it bundled in wamp) 安装mysql服务器(我把它捆绑在沼泽中)
  • using node npm install mysql driver by felixge 使用节点npm通过felixge安装mysql驱动程序
  • connect to mysql from your server .js files 从服务器.js文件连接到mysql

1 download and install node.js 1下载并安装node.js

2 Install mysql server (google it) 2安装mysql服务器(google it)

3 install mysql driver ( node-mysql ) using node package manager (npm comes with node) 3使用节点包管理器安装mysql驱动程序( node-mysql )(npm随节点提供)

   c:\your_node_server_folder\npm install mysql@2.0.0-alpha8

4 Now, in your server.js file put something like: var PORT = 1983; 4现在,在您的server.js文件中输入以下内容:var PORT = 1983; //Tyear of my birth ;) var restify = require('restify'); //我的出生年份;)var restify = require('restify'); var db = require('./mysql_conn'); var db = require('./ mysql_conn');

var options = { serverName: 'Lets meetapp node.js apis', accept: [ 'application/json' ] } var options = {serverName:'Let metapp node.js apis',accept:['application / json']}

var PORT = 1983;
server.listen(PORT, '0.0.0.0');
console.log("listening "+PORT);
var db = require('./mysql_conn'); 

notice the last line. 注意最后一行。 I am importing the file mysql_conn.js that has the following content: 我正在导入具有以下内容的文件mysql_conn.js:

//Require mysql connector that you installed with npm
var mysql      = require('mysql');

var conn_conf= {
    host     : 'localhost',
    port     :3306,
    user     : 'root',
    password : 'root',
    database: 'mydatabasename'
}

var connection = mysql.createConnection(conn_conf);

connection.connect(function(err) {
    if(err) console.log("Could not connect to DB");
    else{
        console.log("Connected to "+conn_conf.database+' on '+conn_conf.host );
    }
});

The code above will connecto to mysql db that is on the same machine listening the default 3306 port.... 上面的代码将连接到同一台机器上的mysql db,监听默认的3306端口。

And finally a simple query: 最后是一个简单的查询:

connection.query( 'SELECT * FROM mydatabase.mytable ', function(err, rows) {
            console.log("SELECT * FROM mytable ");

            for(var i=0; i<rows.length; i++){
                console.log(rows[i]);
            }

            return rows;

    }

hope it helps! 希望能帮助到你!

Search 搜索 http://search.npmjs.org/ http://search.npmjs.org/ (broken link) for mysql (there are several) (断开的链接)用于mysql(有多个)

Looking back at this, I really should have simply went this route in the first place. 回顾这一点,我真的应该首先走这条路线。 Here's a little lib that I wrote for Node that is really, realy, realy helpful! 这是我为Node编写的一个lib,它确实非常有用,非常有用!

var fs = require('fs');
exports.saveJsonToFile = function(filePath, json) {
    fs.writeFileSync(filePath+'.json', JSON.stringify(json, null, " ")
}
exports.readJsonFromFile = function(filePath) {
    return JSON.parse(fs.readFileSync(filePath+'.json'))
}

Just save this to a file, then load the file to your project using: 只需将其保存到文件,然后使用以下命令将该文件加载到您的项目中:

var JsonFs = require('./Path/To/File');

players = JsonFs.readJsonFromFile('players');

JsonFs.saveJsonToFile('players', players);

PLEASE NOTE: JSON files don't support functions!!! 请注意: JSON文件不支持功能!!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM