Node.js 连接mySQL程序

环境:Oracle Enterprise Linux R5U7

  • 安装mySQL

关于离线安装,下次在尝试,目前先来在线安装,过程如下:

$ rpm -qa | grep -i mysql
$ wget http://public-yum.oracle.com/public-yum-el5.repo
$ sed -e "s/enabled=0/enabled=1/" public-yum-el5.repo > /etc/yum.repos.d/public-yum-el5.repo
$ yum install -y mysql-server mysql

输出如下

[root@localhost ~]# yum install -y mysql-server mysql
Loaded plugins: rhnplugin, security
This system is not registered with ULN.
ULN support will be disabled.
el5_addons | 1.2 kB 00:00
el5_addons/primary | 117 kB 00:01
el5_addons 448/448
el5_ga_base | 1.1 kB 00:00
el5_ga_base/primary | 993 kB 00:01
el5_ga_base 3887/3887
el5_latest | 1.4 kB 00:00
el5_latest/primary | 29 MB 00:19
el5_latest 15394/15394
el5_oracle_addons | 951 B 00:00
el5_oracle_addons/primary | 1.7 kB 00:00
el5_oracle_addons 7/7
el5_u1_base | 1.1 kB 00:00
el5_u1_base/primary | 1.0 MB 00:02
el5_u1_base 3968/3968
el5_u2_base | 1.1 kB 00:00
el5_u2_base/primary | 1.0 MB 00:03
el5_u2_base 4101/4101
el5_u3_base | 1.1 kB 00:00
el5_u3_base/primary | 1.1 MB 00:02
el5_u3_base 4172/4172
el5_u4_base | 1.1 kB 00:00
el5_u4_base/primary | 1.1 MB 00:01
el5_u4_base 4285/4285
el5_u5_base | 1.1 kB 00:00
el5_u5_base/primary | 1.1 MB 00:01
el5_u5_base 4372/4372
el5_unsupported | 1.2 kB 00:00
el5_unsupported/primary | 370 kB 00:00
el5_unsupported 1320/1320
ol5_UEK_base | 1.2 kB 00:00
ol5_UEK_base/primary | 685 kB 00:13
ol5_UEK_base 28/28
ol5_UEK_latest | 1.2 kB 00:00
ol5_UEK_latest/primary | 28 MB 00:22
ol5_UEK_latest 595/595
ol5_spacewalk20_client | 1.2 kB 00:00
ol5_spacewalk20_client/primary | 8.4 kB 00:00
ol5_spacewalk20_client 42/42
ol5_spacewalk22_client | 1.2 kB 00:00
ol5_spacewalk22_client/primary | 10 kB 00:00
ol5_spacewalk22_client 51/51
ol5_spacewalk24_client | 1.2 kB 00:00
ol5_spacewalk24_client/primary | 8.5 kB 00:00
ol5_spacewalk24_client 41/41
ol5_u10_base | 1.4 kB 00:00
ol5_u10_base/primary | 1.5 MB 00:37
ol5_u10_base 4716/4716
ol5_u11_base | 1.4 kB 00:00
ol5_u11_base/primary | 1.5 MB 00:40
ol5_u11_base 4721/4721
ol5_u5_base | 951 B 00:00
ol5_u5_base/primary | 14 kB 00:00
ol5_u5_base 53/53
ol5_u6_base | 1.1 kB 00:00
ol5_u6_base/primary | 1.5 MB 00:02
ol5_u6_base 4551/4551
ol5_u7_base | 1.1 kB 00:00
ol5_u7_base/primary | 1.5 MB 00:03
ol5_u7_base 4605/4605
ol5_u8_base | 1.4 kB 00:00
ol5_u8_base/primary | 1.5 MB 00:34
ol5_u8_base 4639/4639
ol5_u9_base | 1.4 kB 00:00
ol5_u9_base/primary | 1.2 MB 00:02
ol5_u9_base 4688/4688
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package mysql.i386 0:5.0.95-5.el5_9 set to be updated
--> Processing Dependency: perl(DBI) for package: mysql
---> Package mysql.x86_64 0:5.0.95-5.el5_9 set to be updated
---> Package mysql-server.x86_64 0:5.0.95-5.el5_9 set to be updated
--> Processing Dependency: perl-DBD-MySQL for package: mysql-server
--> Running transaction check
---> Package perl-DBD-MySQL.x86_64 0:3.0007-2.el5 set to be updated
---> Package perl-DBI.x86_64 0:1.52-2.el5 set to be updated
--> Finished Dependency Resolution

Dependencies Resolved

======================================================================================================
Package Arch Version Repository Size
======================================================================================================
Installing:
mysql i386 5.0.95-5.el5_9 el5_latest 4.9 M
mysql x86_64 5.0.95-5.el5_9 el5_latest 4.9 M
mysql-server x86_64 5.0.95-5.el5_9 el5_latest 9.9 M
Installing for dependencies:
perl-DBD-MySQL x86_64 3.0007-2.el5 ol5_u7_base 147 k
perl-DBI x86_64 1.52-2.el5 ol5_u7_base 605 k

Transaction Summary
=======================================================================================================
Install 5 Package(s)
Upgrade 0 Package(s)

Total download size: 20 M
Downloading Packages:
(1/5): perl-DBD-MySQL-3.0007-2.el5.x86_64.rpm | 147 kB 00:00
(2/5): perl-DBI-1.52-2.el5.x86_64.rpm | 605 kB 00:01
(3/5): mysql-5.0.95-5.el5_9.i386.rpm | 4.9 MB 00:07
(4/5): mysql-5.0.95-5.el5_9.x86_64.rpm | 4.9 MB 02:32
(5/5): mysql-server-5.0.95-5.el5_9.x86_64.rpm | 9.9 MB 00:11
----------------------------------------------------------------------------------------------------------------------------------------------------------
Total 117 kB/s | 20 MB 02:57
Running rpm_check_debug
Running Transaction Test
Finished Transaction Test
Transaction Test Succeeded
Running Transaction
Installing : perl-DBI 1/5
Installing : mysql 2/5
Installing : perl-DBD-MySQL 3/5
Installing : mysql-server 4/5
Installing : mysql 5/5

Installed:
mysql.i386 0:5.0.95-5.el5_9 mysql.x86_64 0:5.0.95-5.el5_9 mysql-server.x86_64 0:5.0.95-5.el5_9

Dependency Installed:
perl-DBD-MySQL.x86_64 0:3.0007-2.el5 perl-DBI.x86_64 0:1.52-2.el5

Complete!
[root@localhost ~]#

启动mysql

$ /etc/init.d/mysqld start
$ mysql -uroot -e "SELECT VERSION()"

+-----------+
| version() |
+-----------+
| 5.0.95 |
+-----------+

建表 user

mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| test |
+--------------------+
3 rows in set (0.00 sec)

mysql> select now(),user(),version();
+---------------------+----------------+-----------+
| now() | user() | version() |
+---------------------+----------------+-----------+
| 2016-10-12 14:12:27 | root@localhost | 5.0.95 |
+---------------------+----------------+-----------+
1 row in set (0.00 sec)

mysql> use test
Database changed
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| test |
+--------------------+
3 rows in set (0.00 sec)

mysql> select database();
+------------+
| database() |
+------------+
| test |
+------------+
1 row in set (0.00 sec)

mysql> show tables;
Empty set (0.00 sec)

mysql> create table user(
-> id int,
-> name varchar(50),
-> age tinyint(4)
-> );
Query OK, 0 rows affected (0.00 sec)

  • Node.js的Express框架下的配置

在node.js项目中安装mysql.

[weblogic@localhost myapp-express]$ npm install mysql
mysql@2.11.1 node_modules/mysql
├── sqlstring@2.0.1
├── bignumber.js@2.3.0
└── readable-stream@1.1.14 (string_decoder@0.10.31, isarray@0.0.1, inherits@2.0.3, core-util-is@1.0.2)

在myapp-express下建立三个目录,并加入一些js文件

util – 工具方法

conf – 配置

dao – 与数据库交互

添加文件如下

conf下:

db.js,主要保留和数据库的连接信息

module.exports = {
mysql: {
host: '127.0.0.1',
user: 'root',
password: '',
database:'test',
port: 3306
}
};

util下:

util.js

module.exports = {
extend: function(target, source, flag) {
for(var key in source) {
if(source.hasOwnProperty(key))
flag ?
(target[key] = source[key]) :
(target[key] === void 0 && (target[key] = source[key]));
}
return target;
}
}

dao下 usrSqlMapping.js (主要存放sql语句)

// dao/userSqlMapping.js
var user = {
insert:'INSERT INTO user(id, name, age) VALUES(0,?,?)',
update:'update user set name=?, age=? where id=?',
delete: 'delete from user where id=?',
queryById: 'select * from user where id=?',
queryAll: 'select * from user'
};

module.exports = user;

业务逻辑 userDao.js

var mysql = require('mysql');
var $conf = require('../conf/db');
var $util = require('../util/util');
var $sql = require('./userSqlMapping');

var pool = mysql.createPool($util.extend({}, $conf.mysql));

var jsonWrite = function (res, ret) {
if(typeof ret === 'undefined') {
res.json({
code:'1',
msg: 'Error Msg'
});
} else {
res.json(ret);
}
};

module.exports = {
add: function (req, res, next) {
pool.getConnection(function(err, connection) {
var name = req.query.name;
var age = req.query.age;
connection.query($sql.insert, [name,age], function(err, result) {
if(result) {
result = {
code: 200,
msg:'Add Successfully'
};
}

在routes目录的index.js下添加

var userDao = require('../dao/userDao');

router.get('/addUser', function(req, res, next) {
userDao.add(req,res,next);
});

测试,访问 http://127.0.0.1:3000/addUser?name=ericnie&age=42

然后查看mysql数据库

mysql> select * from user;
+------+---------+------+
| id | name | age |
+------+---------+------+
| 0 | ericnie | 42 |
+------+---------+------+
1 row in set (0.00 sec)

经过实践,node.js确实比较容易使用!值得深入学习

上一篇:Python Revisited Day 01


下一篇:Node.js连接mysql报加密方式错误解决方案