mysqladmin -u root -p shutdown
확인 : ps -ef | grep mysql
기본명령어
show databases; (=> database 목록보기)
use [db이름]; (=> database 선택)
show tables; (=> table 목록보기)
desc [table이름]; (=> table 구조보기)
database 생성 및 삭제
create database [db이름];
drop database [db이름];
table 생성 및 삭제
create table [table이름] (필드이름1 속성, 필드이름2 속성, …);
ex) create table test (num int(5), name char(10));
drop table [table이름];
table 구조변경
필드추가 : alter table [table이름] add [필드이름] 속성;
필드삭제 : alter table [table이름] drop [필드이름] cascade;
필드변경 : alter table [table이름] change [필드이름] [새 필드이름] 새필드속성;
ex) alter table test change num number int(10);
data 넣기 & 삭제
insert into [table이름] (필드이름1, 필드이름2) values ('값1' , '값2');
(=> 필드를 선택해서 넣을 수 있다.)
insert into [table이름] values ('값1','값2');
(=> 모든 필드에 값을 넣을 때 사용해야 함)
delete from [table이름];
(=> table에 있는 정보를 전부 지운다.)
delete from [table이름] where절;
(=> 조건에 맞는 정보를 지운다.)
data 추출
select 필드이름1, 필드이름2 from [table이름];
(=> 선택한 필드의 값 보기)
select * from [table이름];
(=> table에 있는 모든 정보를 봄)
select count(필드이름) from [table이름];
(=> 해당하는 값을 개수를 셈)
select DISTINCT * from [tbale이름];
(=> 중복체크를 하면서 값을 검색)
select avg(필드이름) from [table이름];
(=> 평균을 낸다.)
where 및 조건절
and, or, not, >, <, =, between, like, regexp 등등
ex) select * from test where num='2';
(=> num 이 2인 것을 test 에서 가져온다.)
ex) select * from test where name like '%김%' ;
(=> name에 김 자가 들어가는 사람들을 검색)
ex) select * from test where name regexp "^[bB]";
(=> name이 b 혹은 B로 시작되는 조건으로 검색)
ex) select name from test where name is not null;
(=> name 중에 not null 인 것만 검색)
ex) select * from test where num in ('1','2');
(=> num 이 1과 2인 것을 검색)
order by
ex) select * from test order by num desc;
(=> num 이 큰 순서대로 값을 검색)
ex) select * from test order by num asc;
(=> num 이 작은 순으로 값을 추출, asc를 생략가능)
시간관련
now() (=> 현재 날짜 및 시간을 반환해준다.)
MOD(string1, string2)
(=> string2로 string1 을 나눈 나머지 값을 반환)
join (두개이상의 table에서 원하는 정보 추출)
from 절에 사용할 table을 모두 적어준다.
where조건절을 써줘야 한다.
table이름과 필드이름을 점으로 구분하여 적어준다.
ex) select a.num, a.name, b.address from test a, test2 b where a.num='1' and a.name=b.name;
back up 생성 및 복구
생성 : mysqldump -u 사용자 -p [db이름] > [file명] (=> 엔터친후 비번입력)
복구 : mysql -u 사용자 -p [db이름] < [file명] (=> 엔터친후 비번입력)
datebase 생성 후 권한 주기
use mysql; (=>우선 mysql database사용)
show tables; (=> table 목록을 살펴본다. )
desc user; (=> user table의 구조를 본다.)
insert into user values ('localhost','사용자이름',password('비번'),'y','y',….'y');
(=> database에 사용자 권한을 준다. 비밀번호 입력하는 방법에 주의한다.)
desc db; (=> db table의 구조를 본다.)
insert into db values ('%','db이름','사용자','y','y','y','y',…,'y');
(=> 선택한db에 사용자의 권한을 준다.)
quit ;
./mysqladmin -u root -p reload (=> reload를 해야 한다.)