简体   繁体   English

Node.js:访问mysql数据库

[英]Node.js: accessing mysql database

I am writing a Node.js application. 我正在编写一个Node.js应用程序。 In my node-modules/abc/static/example.js file, I want to access the mysql records by querying to the database. 在我的node-modules/abc/static/example.js文件中,我想通过查询数据库来访问mysql记录。 Is it possible to do so? 有可能这样做吗?

When I include mysql like this: 当我这样包含mysql时:

var mysql = require('mysql');

I get an error: 我收到一个错误:

Uncaught ReferenceError: require is not defined 未捕获的ReferenceError:require未定义

Is it because it is on client side? 是因为它在客户端吗? What are the other options to access mysql database from the 'example.js' file? 从“ example.js”文件访问mysql数据库的其他选项是什么?

I'm going to guess that you're attempting to use server side js in the client side. 我猜您正在尝试在客户端使用服务器端js。

You need to npm install a mysql adapter, @coreyg suggested this one ( https://www.npmjs.com/package/mysql ). 您需要npm安装一个mysql适配器,@coreyg建议使用此适配器( https://www.npmjs.com/package/mysql )。

Set up a nodejs project (if you have not already done so): 设置一个nodejs项目(如果尚未完成):

npm init
npm install --save mysql

You would then typically create some sort of webapp that would serve up a url in the client side. 然后,您通常会创建某种Web应用程序,该Web应用程序将在客户端提供URL。 You'd then (using ajax) request the url. 然后,您(使用Ajax)请求URL。

Behind the scenes your webapp then would query the mysql database (using the adaptor you installed) with whatever query you had in mind. 然后,您的Web应用程序将在幕后使用您想到的任何查询来查询mysql数据库(使用安装的适配器)。

Once your ajax call had finished you'd run your callback which would do what you wanted with the data that had been returned from the database. 一旦您的ajax调用完成,您将运行您的回调,该回调将对从数据库返回的数据进行所需的处理。

Thats the basic theory. 那就是基本理论。 If you want further information on how to do this I'd suggest reading this question: How do I get started with Node.js 如果您想了解更多有关如何执行此操作的信息,建议阅读以下问题: 如何开始使用Node.js

Following on Neil Munro's Answer, after initiating nodejs and installing mysql npm package, you can actually execute your code from the terminal. 按照Neil Munro的答案,在启动nodejs并安装mysql npm软件包之后,您实际上可以从终端执行代码。 to do that, 要做到这一点,

you can create a new js file on the same folder you are executing those commands, say mysqlTestProject.js 您可以在执行这些命令的同一文件夹中创建一个新的js文件,例如mysqlTestProject.js

inside this file, you can put what you had previously, (require stuff and all). 在此文件中,您可以放入以前的内容(需要所有内容)。 for example: 例如:

var mysql      = require('mysql');
var connection = mysql.createConnection({
  host     : 'localhost',
  user     : 'me',
  password : 'secret',
  database : 'my_db'
});

connection.connect();

connection.query('SELECT 1 + 1 AS solution', function(err, rows, fields) {
  if (err) throw err;

  console.log('The solution is: ', rows[0].solution);
});

connection.end();

`` codes were taken from https://www.npmjs.com/package/mysql ``代码取自https://www.npmjs.com/package/mysql

and then from the terminal (still on the same folder as you were), type in this line 然后从终端(仍然与您位于同一文件夹)中键入此行

node ./mysqlTestProject.js

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

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