nodejs + mariadb 연동

시스템 2019. 1. 14. 15:10

출처 : http://kamang-it.tistory.com/entry/NodeJSNodeJS%EC%99%80-MysqlMariaDB%EC%97%B0%EB%8F%99%ED%95%98%EA%B8%B0-expressJS-%EC%83%81%EC%97%90%EC%84%9C-%EA%B0%84%EB%8B%A8%ED%95%9C-%ED%9A%8C%EC%9B%90%EA%B0%80%EC%9E%85%EB%A1%9C%EA%B7%B8%EC%9D%B8%ED%9A%8C%EC%9B%90%ED%83%88%ED%87%B4-%EA%B5%AC%ED%98%84

 

우분투 18

mariadb 환경

root@tkpark-VirtualBox:~/project/myapp/routes# mysql -u tkpark -p
Enter password:
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 78
Server version: 10.1.34-MariaDB-0ubuntu0.18.04.1 Ubuntu 18.04

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]>
 

 

 

 

index.js

module.exports = router;
var router = express.Router();

var mysql = require('mysql');

router.get('/', function (req, res, next) {
    var connection = mysql.createConnection({
        host: 'localhost',
        post: 3306,
        user: 'tkpark',
        password: '패스워드',
        database: 'tkpark'
    });
    connection.connect();
    connection.query('select * from transactionscan', function (err, rows, fields) {
        connection.end();
        if (!err) {
            console.log(rows);
            console.log(fields);
            var result = 'rows : ' + JSON.stringify(rows) + '<br><br>' +
                'fields : ' + JSON.stringify(fields);
            res.send(result);
        } else {
            console.log('query error : ' + err);
            res.send(err);
        }
    });
});


module.exports = router; 

 

실행결과

# yarn start
yarn run v1.12.3
$ node ./bin/www
[ RowDataPacket {
    idx: 1,
    from_address: 'TVcaZYGf94J5K6WkPsfSDVUu5cWreZz1h9',
    to_address: 'TY2HycNQV4qMRZF7TrmTqrAENG59tpsGix',
    amount: 1000000000000000000,
    txid: 'fc3e6e97d5619e186cb4c14ea6326965aa43df85619a6ccd74ee819767104e48' },
  RowDataPacket {
    idx: 2,
    from_address: 'TVcaZYGf94J5K6WkPsfSDVUu5cWreZz1h9',
    to_address: 'TY2HycNQV4qMRZF7TrmTqrAENG59tpsGix',
    amount: 10000000000,
    txid: 'fc3e6e97d5619e186cb4c14ea6326965aa43df85619a6ccd74ee819767104e48' },
  RowDataPacket {
    idx: 3,
    from_address: 'TVcaZYGf94J5K6WkPsfSDVUu5cWreZz1h9',
    to_address: 'TY2HycNQV4qMRZF7TrmTqrAENG59tpsGix',
    amount: 10000000000,
    txid: 'fc3e6e97d5619e186cb4c14ea6326965aa43df85619a6ccd74ee819767104e48' } ]
[ FieldPacket {
    catalog: 'def',
    db: 'tkpark',
    table: 'transactionscan',
    orgTable: 'transactionscan',
    name: 'idx',
    orgName: 'idx',
    charsetNr: 63,
    length: 11,
    type: 3,
    flags: 16899,
    decimals: 0,
    default: undefined,
    zeroFill: false,
    protocol41: true },
  FieldPacket {
    catalog: 'def',
    db: 'tkpark',
    table: 'transactionscan',
    orgTable: 'transactionscan',
    name: 'from_address',
    orgName: 'from_address',
    charsetNr: 33,
    length: 300,
    type: 253,
    flags: 4097,
    decimals: 0,
    default: undefined,
    zeroFill: false,
    protocol41: true },
  FieldPacket {
    catalog: 'def',
    db: 'tkpark',
    table: 'transactionscan',
    orgTable: 'transactionscan',
    name: 'to_address',
    orgName: 'to_address',
    charsetNr: 33,
    length: 300,
    type: 253,
    flags: 4097,
    decimals: 0,
    default: undefined,
    zeroFill: false,
    protocol41: true },
  FieldPacket {
    catalog: 'def',
    db: 'tkpark',
    table: 'transactionscan',
    orgTable: 'transactionscan',
    name: 'amount',
    orgName: 'amount',
    charsetNr: 63,
    length: 20,
    type: 8,
    flags: 4097,
    decimals: 0,
    default: undefined,
    zeroFill: false,
    protocol41: true },
  FieldPacket {
    catalog: 'def',
    db: 'tkpark',
    table: 'transactionscan',
    orgTable: 'transactionscan',
    name: 'txid',
    orgName: 'txid',
    charsetNr: 33,
    length: 450,
    type: 253,
    flags: 4097,
    decimals: 0,
    default: undefined,
    zeroFill: false,
    protocol41: true } ]
GET / 200 69.467 ms - 1770
 

 

 

mariadb모듈로 접근해보았는데 npm 설치하고

생각보다 잘 안붙는다

놀라지말고 위소스대로 잘 붙여 보길 바란다.

 

 

 

 

 

'시스템' 카테고리의 다른 글

pm2  (0) 2019.03.28
nodejs - form submit  (0) 2019.01.22
nodejs json파일 MongDB에 입력  (0) 2018.12.04
nodejs json파일 컨트롤  (0) 2018.12.04
Mongdb 설치 ubuntu 18  (0) 2018.11.30
블로그 이미지

iesay

,