내용

글번호 794
작성자 heojk
작성일 2018-01-16 10:34:38
제목 R에서 MySQL 데이터베이스 연결
내용 #MySQL 설치 후 계정 생성, 마리아디비 설치 후 Command Prompt 실행 C:\windows\system32>mysql -u root -p Enter password: ******* #설치시 입력한 관리자 비밀번호 입력 Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 11 Server version: 10.2.12-MariaDB mariadb.org binary distribution Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]>create database testdb; MariaDB [(none)]>create user user01@'%' identified by 'user123'; MariaDB [(none)]>grant all privileges on testdb.* to 'user01'@'%'; MariaDB [testdb]> Ctrl-C -- exit! Bye C:\windows\system32>mysql -u user01 -p Enter password: ******* # user123 Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 11 Server version: 10.2.12-MariaDB mariadb.org binary distribution Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]> use testdb Database changed MariaDB [testdb]> select count(*) from employees; +----------+ | count(*) | +----------+ | 107 | +----------+ 1 row in set (0.00 sec) #공유폴더에서 sql 파일(MySQL_HR_DDL.sql, MySQL_HR_DML.sql) 복사해서 C:\에 복사 #mysql 프롬프트에서 아래 내용 입력해서 sql 파일 실행 MariaDB [testdb]>source c:/MySQL_HR_DDL.sql MariaDB [testdb]>source c:/MySQL_HR_DML.sql MariaDB [testdb]>commit; MariaDB [testdb]> Ctrl-C -- exit! Bye --------------------------------- R에서 ------------------------------------------- > install.packages("RMySQL") > library(RMySQL) > drv <- dbDriver("MySQL") + con <- dbConnect(drv, dbname="testdb", + host="localhost", port=3306, + user="test", password="test123") > emp <- dbGetQuery(con, "select * from employees") > head(emp) employee_id first_name last_name email phone_number hire_date 1 100 Steven King SKING 515.123.4567 2003-06-17 2 101 Neena Kochhar NKOCHHAR 515.123.4568 2005-09-21 3 102 Lex De Haan LDEHAAN 515.123.4569 2001-01-13 4 103 Alexander Hunold AHUNOLD 590.423.4567 2006-01-03 5 104 Bruce Ernst BERNST 590.423.4568 2007-05-21 6 105 David Austin DAUSTIN 590.423.4569 2005-06-25 job_id salary commission_pct manager_id department_id 1 AD_PRES 24000 NA NA 90 2 AD_VP 17000 NA 100 90 3 AD_VP 17000 NA 100 90 4 IT_PROG 9000 NA 102 60 5 IT_PROG 6000 NA 103 60 6 IT_PROG 4800 NA 103 60