ν°μ€ν 리 λ·°
λ컀 (Docker) - JDBCλ‘ Ubuntu(tomcat8)μ MySQL μ°κ²°
ellie.strong 2021. 10. 24. 00:30
π μ΄ μ€μ΅μ μ§ννκΈ° μν΄μλ MySQL 컨ν μ΄λκ° μμ±λμ΄μμ΄μΌνλ€.
μμ±μ μ νλ€λ©΄ μλμ ν¬μ€ν μ μ°Έκ³ ν΄μ μ§ννκ³ λ€μ λμμ€μ!!
λ컀 (Docker) - MySQL μ€μΉ (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.