Files
wahyd4.github.com/source/_posts/node-js-connect-mysql.md
Hermes Bot 232ecd7c22 Rebuild blog source from GitHub Pages artifact
- 308 posts reverse-extracted from wahyd4.github.com (Hexo 6.3.0 + NexT 8.25)
- Hexo project with NexT theme, reading-experience custom styles
- publish-post.sh: write post -> hexo build -> PR (master untouched)
2026-08-04 09:44:07 +10:00

59 lines
2.0 KiB
Markdown
Raw Permalink Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
---
title: "使用node.js连接mysql数据库"
date: 2011-10-25 00:00:00
tags: [mysql, node, query, 错误]
---
[![](http://junv-wordpress.stor.sinaapp.com/uploads/2011/10/1036320-296x300.png "1036320")](http://junv-wordpress.stor.sinaapp.com/uploads/2011/10/1036320.png)
Javascript连接数据库可能很多同学之前都没有想到吧,不过现在使用node.js我们可以很容易地连接各种数据库。比如传统关系型数据库Mysql以及现在正火的非关系型数据库。这里讲一下如何连接mysql数据库其实官方的文档以及很详细了。
首先我们需要连接mysql所需的Module
```jscript
npm install mysql
```
接下来就可以开始编程,连接数据库了。
```jscript
var mysql = require('mysql');//导入mysql Module
var client = mysql.createClient({
user:'root',
password :'root'
});
/*
设置数据库的地址,这里请不要使用localhost,
至少我在windows这里出错,如果不写系统默认为localhost
*/
client.host ='127.0.0.1';
client.port = 3306; //设置数据库端口,如果不写默认为3306
client.database = 'node';
//create a database
client.query('create database test_db',function(err){
if (err && err.number != mysql.ERROR_DB_CREATE_EXISTS) {
throw err; //添加异常处理
}
});
```
这里我们使用的是client.query();执行sql语句,官方的API中client.query(sql,[params,cb])方法中还可以添加回调函数。
执行其他SQL语句也是使用client.query()函数。这里说一下插入语句
```jscript
client.query('insert into node_t  values(?,?)',[1000,'Junv']  );
```
推荐这种参数化插入,而不是将值直接写在里面,可以防止sql注入。
注意:今天下午我最初尝试连接数据库的时候,它一直报错“Domain name not found”,只要我们加上
```jscript
client.host ='127.0.0.1';
```
将系统默认的host=localhost改成127.0.0.1即可。具体原因不明。
是不是很简单呢?接下来我会写关于express 和jade的博客了。一边学习一边写博客。哈哈。