리눅스/linux

MySQL 설치 및 Tomcat 연동

박다큐 2023. 2. 19. 19:30
  • MySQL 설치
[root@ ~]# wget https://dev.mysql.com/get/mysql80-community-release-el7-7.noarch.rpm
[root@ ~]# rpm -ivh mysql80-community-release-el7-7.noarch.rpm
[root@ ~]# yum -y install mysql-server
  • MySQL 접속
[root@ ~]# mysql -uroot -p
Enter password: 
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)

-> systemctl start mysqld 해주면 됨

 

  • 초기 비밀번호 확인
[root@ ~]# grep 'temporary password' /var/log/mysqld.log
[root@ ~]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.32

Copyright (c) 2000, 2023, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

 

Tomcat 과 연동

  • connector-J 설치 및 이동
[root@ ~]# wget https://dev.mysql.com/get/Downloads/Connector-J/mysql-connector-java-8.0.30.tar.gz
[root@ ~]# tar zvfx mysql-connector-java-8.0.30.tar.gz
[root@ ~]# cd mysql-connector-java-8.0.30
[root@ ~]# mv mysql-connector-java-8.0.30.jar /usr/local/tomcat/lib/
  • database.jsp 생성
[root@ ~]# cd /usr/local/tomcat/webapps/ROOT/
[root@ ~]# vim database.jsp
database.jsp
<%@ page import="java.sql.*" %>
<%@ page contentType="text/html;charset=utf-8" %>
<%
         String DB_URL = "jdbc:mysql://db주소/db이름";
         String DB_USER = "db유저";
         String DB_PASSWORD= "패스워드";
         Connection conn;
         Statement stmt;

         try {
              Class.forName("com.mysql.jdbc.Driver");
              conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);
              stmt = conn.createStatement();
              conn.close();
              out.println("MySQL Connection Success!");
         }
         catch(Exception e){
              out.println(e);
         }
%>
~

※ db가 연동이 되면 http://아이피주소/database.jsp 접속시 [ MySQL Connection Success! ] 라는 문구가 뜸

 

'리눅스 > linux' 카테고리의 다른 글

tomcat 설치 및 apache 연동  (0) 2023.02.19
Apache 컴파일 설치  (0) 2022.08.08