카테고리 보관물: DBMS

MySQL Operation, Maintenance

MySQL Operation

  • DB 구성 상태 확인
> show databases;
// 데이터베이스 목록 보기

> show tables;
// 테이블 목록 보기

> show columns from 'table name';
// 테이블 칼럼 목록 보기

> SHOW VARIABLES LIKE 'c%';
// 캐릭터셋 보기

MySQL Maintenance

  • DBMS 상태 확인
> show status;
// MySQL 데이타베이스의 현재 상황

> show Processlist;
// MySQL 프로세스 목록

> show variables
// 설정 가능한 모든 변수 목록

> SELECT table_schema "Database Name",  SUM(data_length + index_length) / 1024 / 1024 "Size(MB)"  FROM information_schema.TABLES  GROUP BY table_schema;
// DB별 사용량 확인

SELECT table_name, table_rows, round(data_length/(1024*1024),2) as 'DATA_SIZE(MB)', round(index_length/(1024*1024),2) as 'INDEX_SIZE(MB)' FROM information_schema.TABLES where table_schema = '데이터베이스이름' GROUP BY table_name ORDER BY data_length DESC LIMIT 20;
// 해당 DB의 테이블 사이즈 상위 20개 정렬
  • Connection 및 Client 상태 확인
> show variables like '%max_connection%';
// 최대 커넥션 가능 수량 확인

> show status like '%connect%';
// 커넥션 연결 상태 확인

> show status like '%clients%';
// 클라이언트 연결 상태 확인

> show status like '%thread%';
// 쓰레드 상태 확인
TopicDesc
Aborted_clients클라이언트 프로그램이 비 정상적으로 종료된 수
Aborted_connectsMySQL 서버에 접속이 실패된 수
Max_used_connections최대로 동시에 접속한 수
Threads_cachedThread Cache의 Thread 수
Threads_connected현재 연결된 Thread 수
Threads_created접속을 위해 생성된 Thread 수
Threads_runningSleeping 되어 있지 않은 Thread 수
wait_timeout종료전까지 요청이 없이 기다리는 시간 (TCP/IP 연결, Shell 상의 접속이 아닌 경우)
thread_cache_sizethread 재 사용을 위한 Thread Cache 수로써, Cache 에 있는 Thread 수보다 접속이 많으면 새롭게 Thread를 생성한다.
max_connections최대 동시 접속 가능 수
참고값

Oracle Start, Stop

오라클 서버 시작, 종료

  • 오라클 서버의 MA(Maintenance)를 위해서는 서비스를 종료, 시작 하는 과정이 필요합니다.
  • 당연한 것이지만 오라클 서비스가 종료되기전에 DBMS와 연결된 커넥션을 모두 해제 해야 됩니다. 보통 WAS나 AP를 종료 시키는 것이 일련의 과정입니다.
  • WAS와 AP 종료 후 Client 연결 상태 확인( 기본적으로는 커넥션을 맺은 계정명만 확인이 가능합니다.) 출처>https://stackoverflow.com/questions/1043096/how-to-list-active-open-connections-in-oracle
$ sqlplus / as sysdba
// sysdba 권한을 가진 계정으로 진행

SQL> SELECT username FROM v$session WHERE username IS NOT NULL ORDER BY username ASC;
  • 서비스 종료(오라클 서비스 제어용 계정으로 진행)
$ sqlplus / as sysdba

SQL> shutdown immediate
SQL> exit

$ lsnrctl stop
  • 서비스 시작(MA등의 작업 종료후 오라클 서비스 제어용 계정으로 진행)
$ lsnrctl start

$ sqlplus / as sysdba

SQL> startup
  • WAS나 AP를 기동시켜 검증이 가능하겠지만 보통 정상적으로 데이터 조회가 가능한지 먼저 검증을 해보는 것이 좋습니다.

Oracle JDBC Connection

JAVA에서 오라클 JDBC Driver를 연결하는 방법

  • JAVA 어플리케이션에서 드라이버(ojdbc)를 통해 오라클 DBMS와 연결을 가능하게 해줍니다.
  • ojdbc 드라이버는 오라클 DBMS의 설치 경로에서 확인이 가능합니다. (ex> oracle/product/11.2.0/db_1/jdbc/lib/ojdbc6.jar)
  • 해당 ojdbc 드라이버 파일을 JAVA의 Classpath에 복사 또는 별도도 지정해줘야 import가 가능 합니다. (이클립스에서는 해당 프로젝트 > Build Path > Configure Build Path > Add External JARs > 해당 드라이버 추가)
  • JAVA 코드 예시(Oracle 11G 기준)
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Connection;

public class jdbc_connect {

    public static void main(String[] args) {
        String driver = "oracle.jdbc.driver.OracleDriver";
        String url = "jdbc:oracle:thin:@192.168.0.100:1521:orcl";
        String user = "tester";
        String password = "tester";
        Connection conn = null;

        try {
            Class.forName(driver);
            System.out.println("jdbc driver loading success");
            conn = DriverManager.getConnection(url, user, password);
            System.out.println("Oracle connection seccess");

        } catch (ClassNotFoundException e) {
            System.out.println("jdbc driver loading failure");
        } catch (SQLException e) {
            System.out.println("Oracle connection failure");
        }
        try {
            conn.close();
        } catch (SQLException e) {
            System.out.println("connection close failure");
        }
    }
}