본문 바로가기

프로그래밍/MySQL

Ubuntu - MariaDB설치

설치 환경

 OS : Ubuntu 16.0.4

 MariaDB : 10.4.1

 

MariaDB를 관리하기 위한 기본적인 명령어를 살펴 보자

버전확인

sudo apt-cache policy mariadb-server

마리아 공식 홈페이지에서 OS별 최신버전 목록 업데이트 적용하기

https://downloads.mariadb.org/mariadb/repositories/#mirror=tuna

여기 들어가서 순차적으로 선택하면 최신 업데이트를 할 수 있는 명령어 목록이 나타남

https://downloads.mariadb.org/mariadb/repositories/#distro=Ubuntu&distro_release=xenial--ubuntu_xenial&mirror=tuna&version=10.4

MariaDB 설치

sudo apt-get install software-properties-common

sudo apt-key adv --recv-keys --keyserver hkp://keyserver.ubuntu.com:80 0xF1656F24C74CD1D8

sudo add-apt-repository 'deb [arch=amd64,arm64,i386,ppc64el] http://mirrors.tuna.tsinghua.edu.cn/mariadb/repo/10.4/ubuntu xenial main'

sudo apt update

sudo apt install mariadb-server

 

관리자(root) 암호 변경

~# sudo mysql_secure_installation

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none):
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

Set root password? [Y/n] Y
New password:
Re-enter new password:
Sorry, you can't use an empty password here.

New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
 ... Success!


By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] y
 ... Success!

 

MariaDB 환경설정 파일 찾기

mysqld --verbose --hlp | grep -A 1 'Default options'

 

MariaDB 버전 확인

use mysql;

select version();

MariaDB 삭제

sudo apt-get purge mariadb-*

sudo apt autoremove //의존성 패키지 자동 제거

dpkg -l | grep mysql //mysql 패키지 확인

sudo apt-get purge mysql-common // 남아있는 모듈 삭제

sudo reboot // 재시작

문자셋을 UTF8로 변경

vi /etc/mysql/mariadb.cnf

# MariaDB-specific config file.
# Read by /etc/mysql/my.cnf

[client]
# Default is Latin1, if you need UTF-8 set this (also in server section)
default-character-set = utf8

[mysqld]
#
# * Character sets
#
# Default is Latin1, if you need UTF-8 set all this (also in client section)
#
character-set-server  = utf8
collation-server      = utf8_unicode_ci
character_set_server   = utf8
collation_server       = utf8_unicode_ci
# Import all .cnf files from configuration directory
!includedir /etc/mysql/mariadb.conf.d/

RSync설치

$ sudo apt-get install rsync

참조 : https://osasf.net/discussion/606/rsync-%EC%84%A4%EC%B9%98-%EB%B0%8F-%EC%82%AC%EC%9A%A9-%EB%B0%A9%EB%B2%95-%EC%84%9C%EB%B2%84%EA%B0%84-%EB%98%90%EB%8A%94-%EC%84%9C%EB%B2%84%EB%82%B4-%ED%8C%8C%EC%9D%BC-%EB%8F%99%EA%B8%B0%ED%99%94-crontab%EC%9D%84-%ED%86%B5%ED%95%9C-%EC%8A%A4%EC%BC%80%EC%A5%B4%EB%A7%81-%ED%8F%AC%ED%95%A8

 

 

DATABASE생성

CREATE DATABASE TESTDB CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

// UTF8이면서 모바일에서 이모티콘 도 표시가 가능한 문자셋으로 데이터베이스 생성

사용자 생성

use mysql;

CREATE USER 'testuser'@'%' identified by 'mypassword'; //사용자 생성

//생성된 사용자 확인

SELECT host, user, password FROM user;

//생성된 사용자의 비번이 암호화 되어 보일테지만 비어 있는 경우에 수동 세팅

UPDATE user SET password=password('mypassword') WHERE user='testuser';

사용자 권한 부여

//외부에서 접근 가능하도록 설정

GRANT ALL PRIVILEGES ON testdb.* TO 'testuser'@'%' IDENTIFIED BY 'mypassword';

flush privileges; //권한 적용

외부에서 접근이 안되는 경우 체크해 볼 사항

1. 방화벽

  - 외부방화벽에서 외부 아이피 및 3306 포트 개발

  - 자체 방화벽(ufw) 확인

    . 설정상태 체크 : ufw status

    . 3306 MySQL 포트 개방 : ufw allow 3306

 

2. MySQL 설정파일 /etc/mysql/my.cnf에서 "bind-address"항목 비활성

# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
#bind-address           = 127.0.0.1 # 활성화 되어 있으면 로컬에서만 접근 가능

3. 그래도 접속이 안되는 경우

    방화벽에서 3306 포트 오픈해 주면 됨.

    iptables -I INPUT 1 -p tcp --dport 3306 -j ACCEPT