ν‹°μŠ€ν† λ¦¬ λ·°

728x90

 

πŸ“Œ μ΄ μ‹€μŠ΅μ„ μ§„ν–‰ν•˜κΈ° μœ„ν•΄μ„œλŠ” MySQL μ»¨ν…Œμ΄λ„ˆκ°€ μƒμ„±λ˜μ–΄μžˆμ–΄μ•Όν•œλ‹€.

생성을 μ•ˆ ν–ˆλ‹€λ©΄ μ•„λž˜μ˜ ν¬μŠ€νŒ…μ„ μ°Έκ³ ν•΄μ„œ μ§„ν–‰ν•˜κ³  λ‹€μ‹œ λŒμ•„μ˜€μž!!

도컀 (Docker) - MySQL μ„€μΉ˜ (tistory.com)

 

도컀 (Docker) - MySQL μ„€μΉ˜

도컀 ν™˜κ²½μ—μ„œ MySQL μ»¨ν…Œμ΄λ„ˆλ₯Ό μƒμ„±ν•˜μ—¬ μ‹€μŠ΅ν•΄λ³Έλ‹€. MySQL 이미지 μ„€μΉ˜ 및 μ»¨ν…Œμ΄λ„ˆ 생성 // mysql 이미지 λ‹€μš΄λ‘œλ“œ $ docker pull mysql:5.7 // mysql μ»¨ν…Œμ΄λ„ˆ 생성 $ docker run -d -p 3360:3306 -e MYSQL_R..

programmer-ririhan.tistory.com

 

🐳 tomcat8을 μ„€μΉ˜ ν•  ubuntu:18.04 μ»¨ν…Œμ΄λ„ˆ 생성 및 접속

$ docker run -d -it  -p 8080:8080 --name=ubuntu1 ubuntu:18.04

$ docker exec -it ubuntu1 bash

 

🐳 ν•„μš”ν•œ λ“œλΌμ΄λ²„ μ„€μΉ˜

# apt update

# apt install nano
# apt install openjdk-11-jdk
# apt install libmysql-java
# apt install tomcat8

 

🐳 tomcat8κ³Ό mysql-connector μ—°κ²°

# cd /usr/share/java
# ln -s /usr/share/java/mysql-connector-java.jar /usr/share/tomcat8/lib/

// tomcat8 μ„œλ²„ μ‹€ν–‰
# service tomcat8 start

 

🐳 MySQL μ„€μ •

πŸ“Œ mysql μ»¨ν…Œμ΄λ„ˆμ— 접속 ν›„!!

# apt install mysql-server
# apt install mysql-client
// μ‚¬μš©μž κ³„μ •μ˜ λΉ„λ°€λ²ˆν˜Έ λ³€κ²½ (blackpink0808)
mysql> alter user 'root'@'localhost' identified with mysql_native_password by 'blackpink0808';
mysql> flush privileges;

// 전체 λ°μ΄ν„°λ² μ΄μŠ€μ— λŒ€ν•œ κΆŒν•œμ„ 가진 μ‚¬μš©μž 계정 생성 (계정이름 : root, ν˜ΈμŠ€νŠΈμ΄λ¦„ : localhost)
mysql> grant all privileges on *.* to 'root'@'localhost' with grant option;

mysql> select user, authentication_string, plugin, host from mysql.user;
+---------------+-------------------------------------------+-----------------------+-----------+
| user          | authentication_string                     | plugin                | host      |
+---------------+-------------------------------------------+-----------------------+-----------+
| root          | *7E1B89E78378B539B469E74AE3D70BEBB499A5E3 | mysql_native_password | localhost |
| mysql.session | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | mysql_native_password | localhost |
| mysql.sys     | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | mysql_native_password | localhost |
| root          | *7E1B89E78378B539B469E74AE3D70BEBB499A5E3 | mysql_native_password | %         |
+---------------+-------------------------------------------+-----------------------+-----------+
4 rows in set (0.00 sec)

mysql> exit;
# service mysql restart

 

🐳 Classpath μ„€μ •

πŸ“Œ ubuntu μ»¨ν…Œμ΄λ„ˆμ— 접속 ν›„!!

root ν΄λ”μ˜ .bashrc νŒŒμΌμ— 경둜λ₯Ό μΆ”κ°€ν•΄μ€€λ‹€.

# nano ~/.bashrc
export CLASSPATH=$CLASSPATH:/usr/share/java/mysql-connector-java.jar

"Ctrl + o" > "Enter" > "Ctrl + x"둜 λ³€κ²½ λ‚΄μš© μ €μž₯

 

경둜 μΆ”κ°€ ν›„ μ•„λž˜μ˜ λͺ…λ Ήμ–΄λ₯Ό μ΄μš©ν•΄ λ³€κ²½λœ 파일 λ‚΄μš©μ„ μ μš©μ‹œμΌœμ€€λ‹€.

source ~/.bashrc

 

🐳 JDBC μ‹€μŠ΅

λ¨Όμ € μƒˆλ‘œμš΄ 터미널(λͺ…λ Ήν”„λ‘¬ν”„νŠΈ)을 μ—΄μ–΄μ„œ MySQL의 IPμ£Όμ†Œλ₯Ό ν™•μΈν•œλ‹€.

$ docker inspect mysql1 | find "IPAddress"
            "SecondaryIPAddresses": null,
            "IPAddress": "172.17.0.3",
                    "IPAddress": "172.17.0.3",

 

μ•„λž˜μ˜ μ½”λ“œ 쀑 mysql μ„œλ²„λ‘œ http μš”μ²­μ„ ν•  λ•Œ μ‚¬μš©λœλ‹€.

String dbUrl = "jdbc:mysql://172.17.0.3:3306/employees";

 

πŸ’‘ μœ„λŠ” Windows ν™˜κ²½μ—μ„œμ˜ λͺ…λ Ήμ–΄λ‘œ MacOSμ—μ„œλŠ” μ•„λž˜μ˜ λͺ…λ Ήμ–΄λ₯Ό μ‚¬μš©ν•œλ‹€.  

$ docker inspect mysql1 | grep IPAddress

 

statement.jsp νŒŒμΌμ„ μƒμ„±ν•œλ‹€. 

nano /var/lib/tomcat8/webapps/ROOT/statemen.jsp
<%@ page import = "java.sql.*" %>
<% 
String dbUrl = "jdbc:mysql://172.17.0.3:3306/employees";
Connection conn = null; Statement stmt = null; ResultSet rs = null;
try {
        Class.forName("com.mysql.jdbc.Driver");
        conn = DriverManager.getConnection(dbUrl, "root", "blackpink0808");
        stmt = conn.createStatement();
        if( stmt.execute ("select * from employees limit 10")) {
                rs = stmt.getResultSet();
                out.println("<h1>select * from employees limit 10</h1>");
        }
        while(rs.next()) {
                out.println("emp_no : "+rs.getInt("emp_no") +" name : "+ rs.getString("first_name") +" "+ rs.getString("last_name"));
                out.write("<br/>");
        }
        rs.close();
        stmt.close();
} catch(Exception e) {
        out.println("@@ Error : " + e);
} finally {
        if (conn != null) {
                conn.close();
        }
}
%>

 

"http://localhost:8080/statement.jsp"둜 μ ‘μ†ν•˜λ©΄

λ‹€μŒκ³Ό 같이 mysql의 employees ν…Œμ΄λΈ”μ—μ„œ 데이터λ₯Ό 잘 μ‘°νšŒν•΄μ˜€λŠ” λͺ¨μŠ΅μ„ 확인할 수 μžˆλ‹€.

 

❌ μ§„μ§œ μ§„μ§œ 주의 ❌

μœ„μ˜ νŽ˜μ΄μ§€λ₯Ό μš”μ²­ν•  경우 μ—¬λŸ¬κ°€μ§€ μ—λŸ¬κ°€ λ°œμƒν•  수 μžˆλ‹€. (λ‚΄κ°€ κ·Έλž¬λ‹€...γ…œ)

1. jdbc driverλ₯Ό 찾지 λͺ»ν•˜λŠ” 문제

.bashrc νŒŒμΌμ— classpath에 λŒ€ν•œ 섀정을 μΆ”κ°€ν•˜μ§€ μ•Šμ•˜μ„ 경우 λ°œμƒν•˜λ©° μœ„μ—μ„œ κ΄€λ ¨ λ‚΄μš©μ΄ μžˆμœΌλ‹ˆ 이λ₯Ό μΆ”κ°€ν•΄μ€€λ‹€. 

 

2. mysqlκ³Ό 연결이 μ•ˆλ˜λŠ” 문제

예 : javax.servlet.ServletException: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

 

μ§„μ§œ 이게 λŒ€λ°•μΈκ²Œ ν•œ 5μ‹œκ°„ λ™μ•ˆ 계속 μ„œμΉ˜ν•΄λ³΄λ©΄μ„œ μ»¨ν…Œμ΄ μ‚­μ œν•˜κ³  λ‹€μ‹œ κΉ”κ³ λ₯Ό λ°˜λ³΅ν•΄λ΄λ„ 해결이 μ•ˆλλ‹€.. κ·Έλž˜μ„œ κ²°κ΅­ κ΅μˆ˜λ‹˜κ»˜ 메일을 λ³΄λƒˆκ³  SSL μ˜΅μ…˜ μ„€μ • λ¬Έμ œλΌλŠ” 것을 μ•Œκ²Œλλ‹€.... 빨리 λ¬Όμ–΄λ³Όκ±Έ..

πŸ“Œ μ°Έκ³  : [java] MySQL λ°μ΄ν„°λ² μ΄μŠ€μ— μ—°κ²°ν•  λ•Œ SSL 연결에 λŒ€ν•œ κ²½κ³  - λ¦¬λ·°λ‚˜λΌ (daplus.net)

 

mysql에 μš”μ²­μ„ λ³΄λ‚΄λŠ” url을 λ‹€μŒκ³Ό 같이 λ³€κ²½ν•΄μ„œ SSL을 λΉ„ν™œμ„±ν•˜μ˜€λ‹€. 

String dbUrl = "jdbc:mysql://172.17.0.3:3306/employees?autoReconnect=true&useSSL=false";

 


Ref.

 

[Docker] docker μ‹€μŠ΅ν•˜κΈ° (버전 확인, container 생성 및 접속, docker mySQL μ„€μΉ˜, testDB μ„€μΉ˜) (tistory.com)

728x90
λŒ“κΈ€
곡지사항
μ΅œκ·Όμ— 올라온 κΈ€