2013-12-09 43 views
0

我想在我的Vagrant虛擬框上運行node.js。當我嘗試將node.js連接到我的數據庫時,它會失敗,如下所示。任何人都可以向我解釋我應該如何解決它?爲什麼node.js不能連接到我的數據庫?

% sequelize -m 
Loaded configuration file "config/config.json". 
Using environment "development". 
You need to install mysql package manually 

TypeError: Cannot call method 'createConnection' of undefined 
    at module.exports.connect (/usr/local/lib/node_modules/sequelize/lib/dialects/mysql/connector-manager.js:259:28) 
    at Object.pool.Pooling.Pool.create (/usr/local/lib/node_modules/sequelize/lib/dialects/mysql/connector-manager.js:125:19) 
    at createResource (/usr/local/lib/node_modules/sequelize/node_modules/generic-pool/lib/generic-pool.js:258:13) 
    at dispense (/usr/local/lib/node_modules/sequelize/node_modules/generic-pool/lib/generic-pool.js:250:9) 
    at Object.me.acquire (/usr/local/lib/node_modules/sequelize/node_modules/generic-pool/lib/generic-pool.js:316:5) 
    at module.exports.enqueue (/usr/local/lib/node_modules/sequelize/lib/dialects/mysql/connector-manager.js:312:19) 
    at module.exports.ConnectorManager.query (/usr/local/lib/node_modules/sequelize/lib/dialects/mysql/connector-manager.js:164:15) 
    at module.exports.Sequelize.query (/usr/local/lib/node_modules/sequelize/lib/sequelize.js:252:34) 
    at null.<anonymous> (/usr/local/lib/node_modules/sequelize/lib/query-interface.js:689:32) 
    at null.<anonymous> (/usr/local/lib/node_modules/sequelize/lib/emitters/custom-event-emitter.js:26:18) 

我可以保證,我的數據庫已經啓動並運行,並連接:

% mysql -u root -pmyPassWord 


Welcome to the MySQL monitor. Commands end with ; or \g. 
Your MySQL connection id is 45 
Server version: 5.5.34-0ubuntu0.12.04.1 (Ubuntu) 
Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved. 
Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. 
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. 
mysql> 

我也可以告訴你,我已經使用NPM安裝mysql軟件包:

% sudo npm install -g [email protected] 
npm http GET https://registry.npmjs.org/mysql/2.0.0-alpha8 
npm http 304 https://registry.npmjs.org/mysql/2.0.0-alpha8 
npm http GET https://registry.npmjs.org/require-all/0.0.3 
npm http GET https://registry.npmjs.org/bignumber.js/1.0.1 
npm http 304 https://registry.npmjs.org/bignumber.js/1.0.1 
npm http 304 https://registry.npmjs.org/require-all/0.0.3 
[email protected] /home/vagrant/.nvm/v0.10.22/lib/node_modules/mysql 
├── [email protected] 
└── [email protected] 

% sudo npm install -g mysql 
npm http GET https://registry.npmjs.org/mysql 
npm http 304 https://registry.npmjs.org/mysql 
npm http GET https://registry.npmjs.org/require-all/0.0.3 
npm http GET https://registry.npmjs.org/bignumber.js/1.0.1 
npm http 304 https://registry.npmjs.org/bignumber.js/1.0.1 
npm http 304 https://registry.npmjs.org/require-all/0.0.3 
[email protected] /home/vagrant/.nvm/v0.10.22/lib/node_modules/mysql 
├── [email protected] 
└── [email protected] 
+0

您的數據庫正在運行,但你的MySQL包未正確設置。 (因此「你需要手動安裝mysql包」)。我從來沒有經歷過,但這絕對是你的問題。 – jraede

+0

如果我已經使用npm成功安裝了node.js mysql模塊,爲什麼它說MySQL包沒有正確設置?看起來應該是這樣。 –

+0

我同意。我不知道它爲什麼這麼說。但很明顯,出於某種原因,這就是問題發生的地方。我的猜測是這與你的系統有關,它可能會把它放在MySQL庫或Node本身上。 – jraede

回答

0

我解決了這個問題。 問題是節點在/ usr/local/lib/node_modules 中尋找它的模塊,但npm將它們安裝在〜/ .nvm/v0.10.22/lib/node_modules中。

所以我取代在/ usr/local/lib目錄/ node_modules用軟鏈接到〜/ .nvm/v0.10.22/lib中/ node_modules

相關問題