Node.js 기본 수업으로 기초적인 게시판 페이지 만들기를 익히고, 내 원래 목적인 DB연동 웹페이지 개발을 위해 Node.js + MySQL 수업으로 넘어갔다. 기본 수업에서 만들었던 소스를 그대로 가져가서 수정하면서 강의가 진행되기 때문에 선수강은 필수였던 것 같다.
MySQL 설치
MySQL의 무료 버전인 Community edition을 설치한다.
공식 홈페이지에서 다운받아도 되지만 복잡해서 강의에서는 Bitmani WAMP를 사용했다.
💡 WAMP : Windows에서 사용하기 위한 Apache, MySQL, PHP 솔루션 모음
운영체제에 맞는 버전으로 다운로드받고, 설치 시 root계정의 비밀번호를 입력한다.
설치가 끝나면 매니저 프로그램이 실행되는데…. 나는 설치하면서 자동으로 리부팅이 되었고 부팅 이후에 프로그램이 켜지지가 않아서 설치 위치로 찾아가 직접 열었다(C:\Bitnami\wampstack-8.1.8-0\manager-windows.exe) . 그런데 켜보니 설치된 서버가 아무것도 없어서… 결국 폴더를 다 지우고 다시 인스톨했다. 두번째에야 수업에 나오는 대로 잘 설치되었고 끝나고 나서 WAMP package도 열렸다. 그리고 강의 버전에서는 MySQL인데 현재는 MariaDB로 설치된다. 기능상 차이는 없다고 한다. 강의 페이지의 댓글을 보고 use mysql
명령어를 사용해 데이터베이스를 바꿔 주었다.
C:\Users\Lee>cd C:\Bitnami\wampstack-8.1.8-0\mariadb\bin
C:\Bitnami\wampstack-8.1.8-0\mariadb\bin>mysql -uroot -p
Enter password: ******
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 8
Server version: 10.4.25-MariaDB mariadb.org binary distribution
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)]> use mysql
Database changed
MariaDB [mysql]>
MYSQL 서버 접속
C:\Bitnami\wampstack-8.1.8-0\mariadb\bin\mysql -uroot -p
데이터베이스 생성하기
MariaDB [mysql]> CREATE DATABASE opentutorials;
Query OK, 1 row affected (0.002 sec)
MariaDB [mysql]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| opentutorials |
| performance_schema |
| test |
+--------------------+
5 rows in set (0.001 sec)
MariaDB [mysql]> use opentutorials;
Database changed
여기까지 한 후, 다운받은 소스들 중 example.sql 파일의 쿼리를 모두 복사해서 콘솔에 붙여넣기한다. 개행문자가 들어가 있으므로 알아서 쭉쭉 실행이 된다. 모두 실행된 후 다음과 같이 확인해본다.
MariaDB [opentutorials]> show tables;
+-------------------------+
| Tables_in_opentutorials |
+-------------------------+
| author |
| topic |
+-------------------------+
2 rows in set (0.000 sec)
MariaDB [opentutorials]> select * from topic;
+----+------------+-------------------+---------------------+-----------+
| id | title | description | created | author_id |
+----+------------+-------------------+---------------------+-----------+
| 1 | MySQL | MySQL is... | 2018-01-01 12:10:11 | 1 |
| 2 | Oracle | Oracle is ... | 2018-01-03 13:01:10 | 1 |
| 3 | SQL Server | SQL Server is ... | 2018-01-20 11:01:10 | 2 |
| 4 | PostgreSQL | PostgreSQL is ... | 2018-01-23 01:03:03 | 3 |
| 5 | MongoDB | MongoDB is ... | 2018-01-30 12:31:03 | 1 |
+----+------------+-------------------+---------------------+-----------+
5 rows in set (0.000 sec)
MariaDB [opentutorials]> select * from author;
+----+--------+---------------------------+
| id | name | profile |
+----+--------+---------------------------+
| 1 | egoing | developer |
| 2 | duru | database administrator |
| 3 | taeho | data scientist, developer |
+----+--------+---------------------------+
3 rows in set (0.000 sec)
Node.js에서의 MySQL 모듈 사용
cmd에서, 수업을 위한 소스코드를 다운받아 놓은 위치로 이동한 후 npm을 사용해 mysql을 설치해 준다. 이 때 package.json의 dependencies에도 mysql이 자동으로 추가되도록 하기 위해 —save
옵션을 사용한다.(일단 하라는 대로 따라했다는 이야기이다)
D:\study\Nodejs_Mysql>npm install --save mysql
그리고 연결 정보 설정을 위해 mysql.js파일을 다음과 같이 만들어 준다.
var mysql = require('mysql');
// 비밀번호는 별도의 파일로 분리해서 버전관리에 포함시키지 않아야 합니다.
var connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : 'pass12',
database : 'opentutorials'
});
connection.connect();
connection.query('SELECT * FROM topic', function (error, results, fields) {
if (error) {
console.log(error);
}
console.log(results);
});
connection.end();
확인을 위해 쿼리를 하나 날리고 console.log로 출력하도록 했다. 실행시키면 다음과 같이 잘 나온다.
D:\study\Nodejs_Mysql>node nodejs/mysql.js
[
RowDataPacket {
id: 1,
title: 'MySQL',
description: 'MySQL is...',
created: 2018-01-01T03:10:11.000Z,
author_id: 1
},
RowDataPacket {
id: 2,
title: 'Oracle',
description: 'Oracle is ...',
created: 2018-01-03T04:01:10.000Z,
author_id: 1
},
RowDataPacket {
id: 3,
title: 'SQL Server',
description: 'SQL Server is ...',
created: 2018-01-20T02:01:10.000Z,
author_id: 2
},
RowDataPacket {
id: 4,
title: 'PostgreSQL',
description: 'PostgreSQL is ...',
created: 2018-01-22T16:03:03.000Z,
author_id: 3
},
RowDataPacket {
id: 5,
title: 'MongoDB',
description: 'MongoDB is ...',
created: 2018-01-30T03:31:03.000Z,
author_id: 1
}
]
'공부 저장소 > Node.js' 카테고리의 다른 글
[생활코딩 Node.js+MySQL] 3. 글 상세보기 만들기 (0) | 2022.08.10 |
---|---|
[생활코딩 Node.js+MySQL] 2. DB를 이용한 홈페이지 글목록 출력 (0) | 2022.08.10 |
[생활코딩 Node.js] 16. 글 삭제 기능 만들기 (0) | 2022.08.04 |
[생활코딩 Node.js] 15. 글 수정 기능 만들기 (0) | 2022.08.04 |
[생활코딩 Node.js] 14. 데이터 받아서 파일 저장하기 (0) | 2022.08.04 |