MySQL database 및 유저 추가 및 삭제



유저 추가 방법은 콘솔에서 직접 추가하는 방법과 쿼리로 하는 방법이 있습니다.


그리고 phpMyAdmin 에서 쉽게 추가가 가능합니다.



▶ 콘솔에서 추가


mysql -uroot -p비밀번호 mysql -e "create database example"

mysql -uroot -p비밀번호 mysql -e "GRANT ALL PRIVILEGES on example.* to 'example'@'localhost' identified by '유저비밀번호'"

mysql -uroot -p비밀번호 mysql -e "GRANT ALL PRIVILEGES on example.* to 'example'@'%' identified by '유저비밀번호'"



▶ 쿼리로 추가


create database example;

GRANT ALL PRIVILEGES on example.* to 'example'@'localhost' identified by '유저비밀번호';

GRANT ALL PRIVILEGES on example.* to 'example'@'%' identified by '유저비밀번호';

grant 로 추가한 경우에는 flush privileges 명령은 실행할 필요가 없습니다.


▶ 콘솔에서 삭제


mysql -uroot -p비밀번호 mysql -e "drop user 'example'@'%'"

mysql -uroot -p비밀번호 mysql -e "drop user 'example'@'localhost'"

mysql -uroot -p비밀번호 mysql -e "drop database example"



▶ 쿼리로 삭제


drop user 'example'@'%';

drop user 'example'@'localhost';

drop database example;



편한 방법대로 하면 됩니다.


phpMyAdmin 최근 버전에서는 사용자추가가 아주 쉽게 되어 있습니다.


MySQL database 및 유저 추가MySQL database 및 유저 추가


사용자에서 사용자 추가가 있습니다.


사용자 추가를 클릭하면 아래 화면이 나옵니다.



MySQL database 및 유저 추가MySQL database 및 유저 추가


사용자명과 비밀번호를 입력하고 실행버튼을 누르면 됩니다.




블로그 이미지

영은파더♥

가상서버호스팅 VPS 리눅스 서버관리 윈도우 IT

,

CentOS 7.x mysql 포트 변경



default 포트는 3306 인데 port 번호를 한번 바꿔보겠습니다.



1. /etc/my.cnf 수정


[mysqld]

port=33306

port= 뒤에 원하는 포트번호를 적어주면 됩니다.


2. mysql 재시작


# systemctl restart mariadb.service

여기까지 하면 내부에서는 접속이 가능하지만 외부에서는 접속이 안됩니다.


3. 방화벽 포트 허용


[root@conoha-jp ~]# firewall-cmd --permanent --zone=public --add-port=33306/tcp

success

[root@conoha-jp ~]# firewall-cmd --reload

success

[root@conoha-jp ~]# firewall-cmd --list-all

public (active)

  target: default

  icmp-block-inversion: no

  interfaces: eth0

  sources:

  services: dhcpv6-client ftp http https mysql ssh

  ports: 33306/tcp

  protocols:

  masquerade: no

  forward-ports:

  sourceports:

  icmp-blocks:

  rich rules:


이제는 외부에서도 접근이 가능합니다.


포트번호가 변경되었기 때문에 외부에서 접속하는 방법은 -P 옵션을 사용해서 접속하여야 합니다.


mysql -h도메인 -P포트번호 -u유저명 -p비밀번호 이런씩으로 사용하면 됩니다.


블로그 이미지

영은파더♥

가상서버호스팅 VPS 리눅스 서버관리 윈도우 IT

,

리눅스 MySQL 3306 외부망 차단하기



iptables 로 3306 포트를 외부IP는 접속을 차단하고 로컬(127.0.0.1)IP는 허용을 하면 됩니다.


외부로 부터 공격시도가 많다면 한번 시도해보세요~


# iptables -I INPUT -p tcp --dport 3306 -j DROP

# iptables -I INPUT -s 127.0.0.1 -p tcp --dport 3306 -j ACCEPT

# service iptables save

순서가 바뀌면 안됩니다. drop 을 먼저 하고 accept 를 하여야 합니다.


룰이 잘 적용되었는지 한번 확인해 봅시다.


[root@vps log]# iptables -L | grep mysql

ACCEPT     tcp  --  localhost.localdomain  anywhere            tcp dpt:mysql

DROP       tcp  --  anywhere             anywhere            tcp dpt:mysql


잘 적용되었네요.



외부에서 접속을 한번 시도해 보겠습니다.


[root@conoha-jp ~]# mysql -hexample.com -uroot

ERROR 2003 (HY000): Can't connect to MySQL server on 'example.com' (110)


접근이 안되는 것을 확인되었습니다.


블로그 이미지

영은파더♥

가상서버호스팅 VPS 리눅스 서버관리 윈도우 IT

,

MySQL 바이너리 로그 및 슬로우쿼리 로그 삭제 방법



서버를 운영하다 보면 binary 로그와 slow-query.log 가 계속해서 쌓이게 됩니다.


/etc/my.cnf 파일에 아래 부분을 적용하면 로그 조절이 가능합니다.


[mysqld]

log-bin=mysql-bin

expire_logs_days=7

long_query_time=3

slow_query_log=1

slow_query_log_file=slow-query.log



log-bin=mysql-bin  바이너리 로그 파일명을 지정할 수 있습니다.

expire_logs-days=7  최근 일주일 분량만 남기는 옵션입니다.

long_query_time=3  쿼리타임이 3초를 넘어가면 로그를 남깁니다.

slow_query_log=1  슬로우쿼리 로그를 작성하는다는 옵션입니다.

slow_query_log_file=slow-query.log  슬로우쿼리 로그 파일명을 지정할 수 있습니다.


여기까지는 환경설정으로 컨트롤 하는 것에 대해서 알아 보았습니다.



콘솔에서 로그를 지우는 방법에 대해서 알아봅니다.



바이너리 로그를 특정날짜 이전은 지우는 명령어입니다.


# mysql -u유저아이디 -p비밀번호 -e "PURGE MASTER LOGS BEFORE '2016-12-31 23:59:59'"



바이너리 로그를 7일 이전 데이터를 지우는 명령어입니다.


# mysql -u유저아이디 -p비밀번호 -e "PURGE MASTER LOGS BEFORE DATE_SUB(CURRENT_DATE, INTERVAL 7 DAY)"



슬로우쿼리 로그를 지우는 명령어입니다.


# cp /dev/null /var/lib/mysql/slow-query.log

cp: overwrite `/var/lib/mysql/slow-query.log'? y



블로그 이미지

영은파더♥

가상서버호스팅 VPS 리눅스 서버관리 윈도우 IT

,

MySQL now() 와 sysdate() 차이점



now 함수와 sysdate 함수의 차이점입니다.


MySQL now() 와 sysdate() 차이점MySQL now() 와 sysdate() 차이점


비교해보면 now 는 실행시점이고 sysdate 는 호출시점입니다.


위의 쿼리문에 sleep 함수를 사용하여 임의적으로 실행시간을 지연시켜서 테스트 해보면 차이점을 알 수가 있습니다.







블로그 이미지

영은파더♥

가상서버호스팅 VPS 리눅스 서버관리 윈도우 IT

,

리눅스 mysql root 비밀번호 변경 및 분실시 초기화

 

 

mysqladmin 을 이용한 mysql 비밀번호 변경 방법이다.

 

# mysqladmin -u root -p password

Enter password:

New password:

Confirm new password:

순서대로 기존암호, 변경암호, 확인암호

 

 

 


 

 

이번엔 분실시 초기화에 대해서 알아보자.

CentOS 7.x 버전에는 mariadb 이다. 그래서 6.x 버전과는 좀 다르다.

 

# systemctl stop mariadb.service

먼저 위의 명령어로 mariadb 서비스를 종료하자.

 

다음엔 아래의 굵은 글씨를 참고해서 변경해주면 된다.

[root@conoha ~]# mysqld_safe --skip-grant-tables --skip-networking &

[1] 14248

[root@conoha ~]# 160406 13:43:10 mysqld_safe Logging to '/var/log/mariadb/mariadb.log'.

160406 13:43:10 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql

 

[root@conoha ~]# mysql -u root

Welcome to the MariaDB monitor.  Commands end with ; or \g.

Your MariaDB connection id is 1

Server version: 5.5.44-MariaDB MariaDB Server

 

Copyright (c) 2000, 2015, Oracle, MariaDB Corporation Ab and others.

 

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

 

MariaDB [(none)]> use mysql;

Reading table information for completion of table and column names

You can turn off this feature to get a quicker startup with -A

 

Database changed

MariaDB [mysql]> update user set password=PASSWORD('1234') where User='root';

Query OK, 4 rows affected (0.00 sec)

Rows matched: 4  Changed: 4  Warnings: 0

 

MariaDB [mysql]> flush privileges;

Query OK, 0 rows affected (0.00 sec)

 

MariaDB [mysql]> quit

Bye

[root@conoha ~]#

 

 

여기까지 완료했다면 mysql 재시작

# systemctl start mariadb.service

 

이제 사용하면 된다.

 

블로그 이미지

영은파더♥

가상서버호스팅 VPS 리눅스 서버관리 윈도우 IT

,