TIL-JS&MYSQL
db.query(`
INSERT INTO topic (title, description, created, author_id)
VALUES(?, ?, NOW(), ?)`,
[post.title, post.description, 1],
why we use DataBase ?
DataHandling efficent.
all O.S have their own file system,
mysql 설치 경로 MaC
$ cd /usr/local/mysql/bin
make datatable
$ ./mysql -uroot -p
[로그인 오류]
유형 1) ERROR 1045 (28000): Access denied for user 'root@'localhost' (using password: NO)
- 사용자의 비밀번호가 없을 경우 나타나는 오류 문구, 아래 해결 방법에 있는 명령어들 중 하나를 선택해 입력.
[해결 방법]
1. mysql -u 사용자
2. mysql -u 사용자 -p 비밀번호
3. mysql -u 사용자 -p
Enter password : 비밀번호 입력
유형 2) ERROR 1045 (28000): Access denied for user 'root@'localhost' (using password: YES)
- 사용자의 비밀번호가 틀렸을 경우 나타나는 오류 문구, 아래 해결 방법에 나와있는 명령어들을 입력.
[해결 방법]
mysql > use mysql
mysql > update user set password=password('비밀번호') where user='사용자'; // 비밀번호 변경
mysql > flush privileges; // 변경사항 적용
>>> reinstall
1.
mysql>CREATE DATABASE opentutorials;
2.
mysql> show databases;
3.
mysql>use opentutorials;
4. enter query string
mysql>show tables;
mysql>select * from topic;
--
-- Table structure for table `author`
--
CREATE TABLE `author` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(20) NOT NULL,
`profile` varchar(200) DEFAULT NULL,
PRIMARY KEY (`id`)
);
--
-- Dumping data for table `author`
--
INSERT INTO `author` VALUES (1,'egoing','developer');
INSERT INTO `author` VALUES (2,'duru','database administrator');
INSERT INTO `author` VALUES (3,'taeho','data scientist, developer');
--
-- Table structure for table `topic`
--
CREATE TABLE `topic` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(30) NOT NULL,
`description` text,
`created` datetime NOT NULL,
`author_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
);
--
-- Dumping data for table `topic`
--
INSERT INTO `topic` VALUES (1,'MySQL','MySQL is...','2018-01-01 12:10:11',1);
INSERT INTO `topic` VALUES (2,'Oracle','Oracle is ...','2018-01-03 13:01:10',1);
INSERT INTO `topic` VALUES (3,'SQL Server','SQL Server is ...','2018-01-20 11:01:10',2);
INSERT INTO `topic` VALUES (4,'PostgreSQL','PostgreSQL is ...','2018-01-23 01:03:03',3);
INSERT INTO `topic` VALUES (5,'MongoDB','MongoDB is ...','2018-01-30 12:31:03',1);
>>>> node js setting ,
package.json 의 dependencies 확인 후 npm install 을 해준다 .
node_module 확인
>> pm2 start main.js --watch
:mysql module 설치 & file package.json dependencies 에 추가
>> npm install --save mysql
>> mysql npm install site
https://www.npmjs.com/package/mysql
mysql
A node.js driver for mysql. It is written in JavaScript, does not require compiling, and is 100% MIT licensed.. Latest version: 2.18.1, last published: 2 years ago. Start using mysql in your project by running `npm i mysql`. There are 6678 other projects i
www.npmjs.com
// file mysql.js
connection 변수의 연결 할 dp 정보를 입력 한다 .
host :
-connection 설정 이후 connect() 서버에 연결한다 . (??? >>공부 하자)
-connection.query(a,b)
a: 쿼리문 , a 실행 완료시 b 함수 call. back
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 (error, results, fields) {
if (error) throw error;
console.log('The solution is: ', results[0].solution);
});
connection.end();
ERROR :
sqlMessage: 'Client does not support authentication protocol requested by server; consider upgrading MySQL client',
>>>> node js require mysql -> 'mysql2' 로 수정 및 설치: var mysql = require('mysql2');
main.js 의 하위 코드 추가.
var mysql = require('mysql2')
var db = mysql.createConnection({
host: 'localhost', // server 의 위치
user: 'root',
password: 'kris2486!',
database: 'opentutorials'
});
db.connect();
db.query('SELECT * FROM topic', function (error, results_topic, fields)
{
if (error) {
console.log(error);
}
else{
console.log('The solution is: ', results_topic);
response.writeHead(200);
response.end('Good');
}
});
Q. writeHead(200) 이거 뭐야 꼭 필요한가?? >>> 꼭 파일 전송확인 .
//// local 300 만 죽이자....
$ lsof -i tcp:3000
$ kill -9 PID
다음 과 같은 구조의 디비 라 할때
title 은 객체로 접근한다 ex list[i].title
[ {
id: 5,
title: 'MongoDB',
description: 'MongoDB is ...',
created: 2018-01-30T03:31:03.000Z,
author_id: 1
}
]
1.
java script
`<li><a href="/?id=${filelist[i].title}">${filelist[i].title}</a></li>`;
html
<a href="/?id=MySQL">MySQL</a>
2.
java script
`<li><a href="/?id=${filelist[i].id}">${filelist[i].title}</a></li>`;
html
<a href="/?id=1">MySQL</a>
SELECT * FROM topic where id=${queryData.id}
,,,,
db.query(`SELECT * FROM topic where id=${queryData.id}`, function (error2, results_topic)
/////// 보안 처리 = >id
db.query(`SELECT * FROM topic where id=?`,[queryData.id],function (error2, results_topic)