Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql Node.js在本地存储远程数据库(同步)_Mysql_Database_Node.js - Fatal编程技术网

Mysql Node.js在本地存储远程数据库(同步)

Mysql Node.js在本地存储远程数据库(同步),mysql,database,node.js,Mysql,Database,Node.js,我有一个应用程序使用此脚本连接到远程MySQL数据库: var mysql = require('mysql'); var $ = require('jquery'); var connection = mysql.createConnection({ host : 'somehost.com', user : 'admin', password : 'admin', database : 'food' }); connection

我有一个应用程序使用此脚本连接到远程MySQL数据库:

var mysql      = require('mysql');
var $          = require('jquery');

var connection = mysql.createConnection({
  host     : 'somehost.com',
  user     : 'admin',
  password : 'admin',
  database : 'food'
});

connection.connect(function(err) {
  if (err) {
    document.write('error connecting: ' + err.stack + '<br>');
    return;
  }

  document.write('Conectat cu ID ' + connection.threadId + '<br>');
});

connection.query('SELECT * FROM recipes', function(err, rows, fields) {
  if (err) throw err;

  rows.forEach(function (item) {
    $("body").append('<span style="color: blue; font-weight: bold;">Denumire:</span> ' + item.denumire + ' <span style="color: blue; font-weight: bold;">Ingrediente:</span> ' + item.ingrediente + '<br>');
  })

});

connection.end();
问题是,当离线互联网可能失败时,我无法使用该应用程序,但我仍然需要能够使用该应用程序。
是否有任何方法可以在应用程序启动时将数据库存储在本地?是否可以将数据库存储在会话内存中,而不是在会话内存中,这样即使我重新启动应用程序,我也可以访问数据库?

我已使用以下脚本将数据库本地复制到JSON文件:

connection.query('select * from database.table;', function(err, results, fields) {
    if (!err) {
      fs.writeFile('location\name.json', JSON.stringify(results), function(err) {});
    }
  });

像您一样使用本地数据库。您不需要internet连接就可以达到127.0.0.1。您应该将connection.query放在connect回调中。但这并不能保证连接到远程服务器时的脱机使用。127.0.0.1是本地服务器,不需要连接。localhost仅用于测试,而不是实际的主机。我已经编辑了我的帖子,所以127.0.0.1不会误导你。你需要编写自己的数据同步系统,这一点都不微不足道。