[Database] MySQL 설치 및 초기 설정 방법 및 기본 문법 (Mac)

haejun-kim·2020년 8월 15일
1

[DATABASE]

목록 보기
2/7
post-thumbnail

MySQL을 다시 설치해야할 경우를 대비하여 저장해놓는 포스팅
위코드에서 정리해서 준 자료를 블로그에 옮겨만 놓은 포스팅

MySQL 설치하기

  • MySQL 설치

    $ brew install mysql

    brew 명령어를 위한 HomeBrew 패키지 관리자 미설치시 아래 명령어를 통해 설치 필요

    $ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

    MySQL 설정

    아래 명령어로 mysql을 시작합니다.

    $ mysql.server start

    아래 명령어로 기본 설정을 시작합니다.

    $ mysql_secure_installation

    이제 여러 질문들이 출력됩니다.출력에 대한 답은 본인이 한 방법으로만 간단하게 기술하겠습니다.

    우선 아래의 과정에 앞서 정리해보자면 아래와 같습니다.

    1. 비밀번호 복잡도 검사 과정 (n)

    2. 비밀번호 입력 & 확인

    3. 익명 사용자 삭제 (y)

    4. 원격 접속 허용하지 않을 것인가? (y)

    5. test DB 삭제 (y)

    6. previlege 테이블을 다시 로드할 것인지 (y or n)

      아래는 위에 나타낸 과정의 자세한 내용입니다.

      Securing the MySQL server deployment. Connecting to MySQL using a blank password. VALIDATE PASSWORD PLUGIN can be used to test passwords and improve securitNy. It checks the strength of password and allows the users to set only those passwords which are secure enough. Would you like to setup VALIDATE PASSWORD plugin? Press y|Y for Yes, any other key for No:

      위의 과정은 복잡한 비밀번호 설정을 위한 과정을 거치겠냐고 묻는 과정이며,No로 스킵하였습니다.

      Please set the password for root here. New password: Re-enter new password:

      위의 과정은 루트 비밀번호를 입력하는 과정입니다.비밀번호와 비밀번호 확인란을 입력하게 됩니다.

      By default, a MySQL installation has an anonymous user, allowing anyone to log into MySQL 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? (Press y|Y for Yes, any other key for No) : y

      익명 사용자를 삭제할 것인지 묻습니다.y를 입력하였습니다.

      Normally, root should only be allowed to connect from 'localhost'. This ensures that someone cannot guess at the root password from the network. Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y

      원격 접속을 허용할 것인지 묻습니다,로컬에서만 개발 예정이기에 y를 입력했습니다.

      By default, MySQL comes with a database named 'test' that anyone can access. This is also intended only for testing, and should be removed before moving into a production environment. Remove test database and access to it? (Press y|Y for Yes, any other key for No) :

      test 데이터베이스 삭제를 묻고 있습니다.No를 입력하였습니다.

      Reload privilege tables now? (Press y|Y for Yes, any other key for No) :

      previlege 테이블을 다시 로드할 것인지 묻습니다.

      yes를 입력, 과정을 마칩니다.

      All done!

      위의 메세지와 함께 설정이 종료됩니다.

      추가적으로 mysql server가 재부팅과 상관없이 켜져있을 수 있도록 brew services를 이용하여 서버를 켜두겠습니다.

      $ brew services start mysql

      MySQL 사용

      $ mysql -u root -p

      위 명령어 입력 후 루트 비밀번호를 입력하여 mysql을 사용할 수 있게 됩니다.

  • 우분투 mysql root 로그인시 sudo 안쓰기! + 맥도 해주는 게 좋음!

    mysql > use mysql;
    mysql > ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '';
    mysql > FLUSH PRIVILEGES;

→ ALTER 사용해서 비밀번호 수정하는 작업을 뜻하는 명령어. 아직은 비밀번호를 치고 실행시키고 싶어서 설정은 하지 않음.

  • Django 3.0 MySQL 연결하기
pip install mysqlclient

기본 문법

  1. DDL(Data Define Language)
    CREATE / ALTER / DROP
    1) 데이터 베이스 생성 구문
    CREATE DATABASE [database name] CHARACTER SET [character set];
    CREATE DATABASE mysql_test CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
    2) 테이블 작성/변경/삭제
    Syntax
    CREATE TABLE [table] (column1, column2 …)
    ALTER TABLE [table] ADD [column][datatype]
    DROP TABLE [table];
  1. DML(Data Manipulation Language) : 데이터를 조작하는 명령
    INSERT / DELETE / UPDATE
    Syntax
    INSERT INTO [table] VALUES (value1, value2, value3…)
    SELECT * FROM [table];
    DELETE FROM [table] WHERE [condition]
    UPDATE [table] SET [column]=[value] WHERE [condition]

utf8mb4

Emoji같은 글자들은 utf-8 인코딩 되는 경우 글자당 4bytes까지 필요하다. 하지만 기존 MySQL의 utf-8 필드의 경우 글자당 최대 3bytes 까지만 지원하는 한계점이 있다. 때문에 MySQL database에서 utf8mb4 설정을 해두지 않으면 해당 글자들이 포함 된 텍스트가 입력되었을 때 제대로 저장을 하지 못하고 문자가 깨져버리는 사태가 발생한다. 따라서 utf8mb4를 쓰도록 하자.

collation

database에서 문자열을 정렬할 때 어떤 문자들이 먼저 올지를 결정하는 기준을 말한다.collation의 경우 utf8mb4_general_ci 가 기본값이다. 이 설정을 사용할 경우 정렬 속도가 utf8mb4_unicode_ci 설정에 비해 약간 빠르다고는 하지만 미미한 수준이라고 하며 한글이나 일본어같은 비라틴계 언어들에 대해서 조금 어색한 정렬 순서가 존재한다고 한다. 따라서 utf8mb4_unicode_ci를 사용하는것이 더 좋고, 이를 위해서 추가 설정을 해주어야 한다.

변경된 설정 확인하기

SHOW GLOBAL VARIABLES WHERE Variable_name LIKE 'character_set_%' OR Variable_name

0개의 댓글