kkamagi's story

IT, 정보보안, 포렌식, 일상 공유

Database

Mysql 명령어 정리

까마기 2020. 10. 26. 13:49
반응형

mysql의 history 기능은 mysql에 접속해서 입력한 쿼리에 대한 기록을 저장한다. 

default 파일은 mysql에 접속하기전 로그인한 계정 home 디렉토리 내의 .mysql_history 파일이다 

 

1. mysql history 확인 

 

- 아래와 같이 home 디렉토리내의 .mysql_history 파일에서 그동안 입력한 쿼리에 대한 history를 확인 할 수 있다. 일반 계정으로 로그인해서 mysql에 접속을 했다면, /home/계정/.mysql_history 파일에서 확인 할 수 있다. 

 

[root@localhost ~]# cat /root/.mysql_history 

select40*40from40processlist; 

select40*40from40CHARACTER_SETS; 

select40version(); 

show status; 

show status like %version%; 

status like '%version%'; 

show status like '%version%'; 

show variables like '%log%'; 

show variables like '%history%'; 

show variables; 

use mysql 

select * from db; 

use mysql; 

select * from user; 

desc db; 

desc user;

 

 

2. mysql history 파일 변경 

 

- mysql history가 저장되는 파일명을 변경 하려면 MYSQL_HISTFILE 환경변수에 원하는 파일 경로를 설정해 주면 된다. 

/etc/profile 이나 ~/.bash_profile 에 아래와 같은 식으로 MYSQL_HISTFILE 경로를 지정해 주고 적용해 준다.

 

[root@localhost ~]# cat /root/.bash_profile 

# .bash_profile 

# Get the aliases and functions 

if [ -f ~/.bashrc ]; then 

. ~/.bashrc 

fi 

# User specific environment and startup programs 

PATH=$PATH:$HOME/bin 

export PATH 

unset USERNAME 

export MYSQL_HISTFILE=/home/mysql/data/mysql.history 

[root@localhost ~]# source /root/.bash_profile 

[root@localhost ~]# cat /home/mysql/data/mysql.history 

_HiStOrY_V2_ 

use40mysql; 

help40hist 

help40history 

use mysql 

show tables; 

use mysql; 

select * from db; 

use mysql 

help use; 

help insert; 

[root@localhost ~]#

 

3. mysql history 남기지 않도록 설정 

 

- 이미 생성된 mysql history는 아래의 두가지 방법으로 ~/.mysql_history 파일을 삭제하거나 내용을 지운다. 

 

[root@localhost ~]# rm -rf .mysql_history 

[root@localhost ~]# cat /dev/null > .mysql_history 

 

- mysql history를 남기지 않도록 하기 위해서는 MYSQL_HISTFILE=/dev/null로 설정 해주면 된다. 

 

[root@localhost ~]# cat /root/.bash_profile 

# .bash_profile 

# Get the aliases and functions 

if [ -f ~/.bashrc ]; then 

. ~/.bashrc 

fi 

# User specific environment and startup programs 

PATH=$PATH:$HOME/bin 

export PATH 

unset USERNAME 

export MYSQL_HISTFILE=/dev/null 

[root@localhost ~]# source /root/.bash_profile 

 

- mysql history 파일을 아래와 같이 /dev/null로 synbolic link를 걸어도 history가 생성되지 않게 된다. 

 

[root@localhost ~]# rm -rf .mysql_history 

[root@localhost ~]# ln -s /dev/null .mysql_history 

[root@localhost ~]# ls -l .mysql_history 

lrwxrwxrwx 1 root root 9 Feb 18 11:15 .mysql_history -> /dev/null 

[root@localhost ~]#

 

 

MySQL 테이블 있는지 확인

 

SHOW TABLES LIKE '테이블명';

SHOW TABLES IN 디비명 LIKE '테이블명';

 

SELECT 1 FROM Information_schema.tables

WHERE table_schema = 'DB명'

AND table_name = '테이블명'

 

SELECT EXISTS (

SELECT 1 FROM Information_schema.tables

 

$mysqli = new mysqli('localhost','my_user','my_password','world');$result = $mysqli->query("SHOW TABLES LIKE '테이블명'");$exist = ( $result->num_rows > 0 );

WHERE table_schema = 'DB명'

AND table_name = '테이블명'

) AS flag

 

MySQL 컬럼 있는지 확인

SHOW COLUMNS FROM `테이블명` LIKE '컬럼명';

 

SELECT 1 FROM Information_schema.columnsWHERE table_schema = 'DB명'

AND table_name = '테이블명'

AND column_name = '컬럼명'

 

SELECT EXISTS (

SELECT 1 FROM Information_schema.columns

WHERE table_schema = 'DB명'

AND table_name = '테이블명'

AND column_name = '컬럼명') AS flag

 

 

$mysqli = new mysqli('localhost','my_user','my_password','world');$result = $mysqli->query("SHOW COLUMNS FROM `테이블명` LIKE '컬럼명'");$exist = $result->num_rows > 0;

반응형