2018년 3월 10일 토요일

MySQL 자주 사용하는 명령어

mysql 설치

$ sudo apt install mysql-server

mysql 서버 접속

사용자 id가 root 이고 password 가 있는 경우

$ mysql -u root -p

database 목록 표시

mysql> show databases;

database 생성

mysql> create database <db name>;
또는
mysql> create schema <db name>;

예)
mysql> create database test_db;

사용할 database 선택

mysql> use <db name>;

예)
mysql> use test_db;
 

table 생성


양식: http://dev.mysql.com/doc/refman/5.1/en/create-table.html
mysql> create table table_name
    (create_definition, ...)
    [table options]
    [partition options];

예)
mysql> create table users (
    id varchar(256) not null,
    password varchar(256) not null,
    email varchar(256) not null,
    primary key (id)
);

또는 여러 field 값을 primary key 로 설정
mysql> create table users (
    id varchar(256) not null,
    password varchar(256) not null,
    email varchar(256) not null,
    primary key (id, pw)
);


Auto increment

mysql> create table users (id medium int not null auto_increment, ...)


table 속성 보기

msyql> desc <table name>;

예)
msyql> desc user; 
 

table 제거

http://dev.mysql.com/doc/refman/5.5/en/drop-table.html
mysql> drop table [if exists] table_name [, table_name]

예)
mysql> drop table if exists test_tbl;

계정 목록

mysql> select user from mysql.user;

계정 만들기

mysql> create user '<usename>'@'<host>' identified by '<password>';

예)
mysql> create user 'testuser'@'localhost' identified by 'testuserpassword';


계정 권한 주기

mysql> grant all privileges on <db name>.* to <user name>@<host> identified by '<password>' with grant option;

예)
mysql> grant all privileges on test_db.* to 'testuser'@'localhost' identified by 'testuserpassword' with grant option;

DB 백업 (dump)

$ mysqldump -u [username] -p [db name] > [filename]

예)
$ mysqldump -u testuser -p testdb > backupdump.sql

DB 백업 복구


복구하려는 database 는 미리 생성해야 함
$ mysql -u [username] -p [db name] < [filename]

예)
$ mysql -u testuser -p testdb < backupdump.sql

댓글 없음:

댓글 쓰기