簡介兩個數據庫:html
導入已有數據庫:node
unsw@unsw-UX303UB$ mysql -u root -p #登陸 Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 44 Server version: 5.5.59-0ubuntu0.14.04.1 (Ubuntu) Copyright (c) 2000, 2018, 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> source /root/android-workplace/netty-server/db-mysql/localhost-testdb.sql #導入數據庫
基本操做:from linkmysql
1.顯示數據庫列表
show databases;
2.選擇一個數據庫
use mysql
3.顯示當前數據庫的表單:
show tables;
以上是常規操做,利用nodejs,該如何處理;linux
1. 安裝nodejs環境下的mysql配置android
(1)
npm install mysql
(2) from link Be sure to run npm install if you haven't tried that. If you have, try: npm install mysql --save
2. 數據庫位置git
mysql> select @@datadir; +-----------------+ | @@datadir | +-----------------+ | /var/lib/mysql/ | +-----------------+ 1 row in set (0.00 sec)
root權限看到數據文件。github
unsw@unsw-UX303UB$ sudo ls /var/lib/mysql/ [sudo] password for unsw: debian-5.5.flag ib_logfile0 unsw_mh_test mysql_upgrade_info ibdata1 ib_logfile1 mysql performance_schema
3. 使用nodejs操做數據庫web
var mysql = require('mysql'); var connection = mysql.createConnection({ host : 'localhost', user : 'root', password : '123456',
port : '3306' database : 'mysql' // 數據庫的名字 }); connection.connect();
3.1 簡單的select語句示範sql
connection.query('SELECT 1 + 1 AS solution', function (error, results, fields) { if (error) throw error; console.log('The solution is: ', results[0].solution); });
3.2 查詢數據mongodb
var sql = 'SELECT * FROM websites';
connection.query(sql,function (err, result) { if(err){ console.log('[SELECT ERROR] - ',err.message); return; } console.log('--------------------------SELECT----------------------------'); console.log(result); console.log('------------------------------------------------------------\n\n'); }); connection.end();
3.3 插入數據
var addSql = 'INSERT INTO websites(Id,name,url,alexa,country) VALUES(0,?,?,?,?)';
var addSqlParams = ['菜鳥工具', 'https://c.runoob.com','23453', 'CN'];
connection.query(addSql, addSqlParams, function (err, result) { if(err){ console.log('[INSERT ERROR] - ',err.message); return; } console.log('--------------------------INSERT----------------------------'); //console.log('INSERT ID:',result.insertId); console.log('INSERT ID:',result); console.log('-----------------------------------------------------------------\n\n'); }); connection.end();
3.4 更新數據
var modSql = 'UPDATE websites SET name = ?,url = ? WHERE Id = ?'; var modSqlParams = ['菜鳥移動站', 'https://m.runoob.com',6]; connection.query(modSql, modSqlParams, function (err, result) { if(err){ console.log('[UPDATE ERROR] - ',err.message); return; } console.log('--------------------------UPDATE----------------------------'); console.log('UPDATE affectedRows',result.affectedRows); console.log('-----------------------------------------------------------------\n\n'); }); connection.end();
3.5 刪除數據
var delSql = 'DELETE FROM websites where id=6'; connection.query(delSql, function (err, result) { if(err){ console.log('[DELETE ERROR] - ',err.message); return; } console.log('--------------------------DELETE----------------------------'); console.log('DELETE affectedRows',result.affectedRows); console.log('-----------------------------------------------------------------\n\n'); }); connection.end();
MongoDB是一種文檔導向數據庫管理系統,基於分佈式文件存儲的數據庫,由C++撰寫而成。
本章節咱們將爲你們介紹如何使用 Node.js 來鏈接 MongoDB,並對數據庫進行操做。
若是你尚未 MongoDB 的基本知識,能夠參考咱們的教程:MongoDB 教程。
安裝:
Ref: How to Install and Configure MongoDB on Ubuntu 14.04
Ref: Linux平臺安裝MongoDB