2024/04/03: 1개의 글

[Linux-MariaDB] 마리아DB 설치방법/사용방법

Posted by nkjok
2024. 4. 3. 17:44 OS&기타/Linux
반응형

설치, 서비스활성화, 서비스재부팅이후에도활성화, 방화벽허용

yum -y install mariadb-server 
systemctl restart mariadb
systemctl enable mariadb
firewall-cmd --permanent --add-service=mysql




show databases;

use mysql;

show tables;

select * from user;

select user, host from user where user NOT LIKE '';
유저이름이 공백이아니면 출력을 해라



외부에서 접근가능하게 설정
MariaDB [mysql]>  여기서 아래 명령어 입력
GRANT ALL PRIVILEGES ON *.* TO winuser@'192.168.101.%' IDENTIFIED BY '1234'; 
-win유저만들면서 이대역으로 들어올수 있도록 추가
GRANT ALL PRIVILEGES ON *.* TO root@'192.168.101.%' IDENTIFIED BY '1234'; 
-루트가유저가 설정한 대역으로 들어올수 있도록 추가




create database mqtt;

show databases;

use mqtt;

(삭제 : drop database mqtt;)


두개의 테이블 생성
아래 명령어 한줄로 쭉 적으면 됨
create table cu(
 id varchar(10) not null primary key,
 name nvarchar(5),
 age int,
 address nvarchar(5));


create table pu(
 no int not null primary key auto_increment,
 cust_id varchar(10),
 data char(8),
 product nvarchar(5));
---------------




테이블확인 (주키, 정수, 문자 등의정보나옴)
desc cu;
desc pu;


테이블 컬럼타입 변경
ALTER TABLE 테이블명MODIFY 컬럼명 변경할컬럼타입;
alter table cu modify product varchar(10);


테이블내용확인
select * from cu;
select * from pu;



------------------------------------------------------------------
mariadb (mysql) 한글 안될 때 해결방법


1. vi /etc/my.cnf  파일 열어서 아래 내용 최하단에 복붙
[mysqld]
character-set-server = utf8

[client]
default-character-set = utf8

[mysql]
default-character-set = utf8


2. systemctl restart mariadb


3. mysql 접속해서 status 확인하면 Server, Db, Client, Conn 총 4개가 utf8로 변경되어있음
-----------------------------------------------------------------


테이블 내용 삽입
insert into cu values ('hong','홍길동',22,'경기');
insert into cu values ('dong','당탕이',23,'충북');
insert into cu values ('ppuni','이뿌니',30,'서울');
insert into cu values ('john','존밴이',28,'강원');
insert into cu values ('rey','레이나',29,'서울');
insert into cu values ('kazu','카즈야',34,'서울');


insert into pu values (null, 'hong','20160122','TV');
insert into pu values (null, 'ppuni','20160211','TV');
insert into pu values (null, 'john','20160211','냉장고');
insert into pu values (null, 'hong','20160222','세탁기');
insert into pu values (null, 'john','20160311','비디오');
insert into pu values (null, 'rey','20220430','청소기');
insert into pu values (null, 'kazu','20220430','자격증');




컬럼삭제
ALTER TABLE 테이블명 DROP COLUMN 컬럼명;


검색

select * from cu where address ='서울';
테이블에서 서울로 되있는 사람 출력

select * from cu where age > 25;
나이가 25보다 많은 사람

select data, product from purchase where cust_id = 'john';
위에 프로덕트에서 id가 존인 사람
-----------------------------------

-----------------------------------

----- 아래 true 연산자
1) select * from cu,pu where address = '서울' and name = '카즈야' and product = 'TV'; (서울, 카즈야, TV 출력)
+------+-----------+------+---------+----+---------+----------+---------+
| id   | name      | age  | address | no | cust_id | data     | product |
+------+-----------+------+---------+----+---------+----------+---------+
| kazu | 카즈야    |   33 | 서울    |  1 | hong    | 20160122 | TV      |
| kazu | 카즈야    |   33 | 서울    |  2 | ppuni   | 20160211 | TV      |
+------+-----------+------+---------+----+---------+----------+---------+
2 rows in set (0.00 sec)
2) select * from cu,pu where address = '서울' and no >=5; (서울이고 5보다 크거나 같거나)
3) select * from cu where id = 'kazu' or id = 'rey'; (kazu 이거나 rey 이거나)
4) select * from cu,pu where age >= 30 or product = 'TV'; (서른이 넘거나 TV이거나)
5) select * from cu,pu where (address = '서울' or product = 'TV') and name = '카즈야'; 
= 5)는 가로없이 쿼리하면 마지막 and 카즈야이 제대로 조건적용 안되므로 가로 ()를 사용함
6) 같은 칼럼 안 or 조건이라면 in 옵션이 유용, order by data 는 data 칼럼 내용의 오름차순으로 정렬함
select * from cu,pu where name = '카즈야' or name= '레이나' order by data;
select * from cu,pu where name in ('카즈야','레이나') order by data;
----- 아래 not true 연산자
7) 아래는 다 같은 결과값임, not, !, <> 동일한 연산자임, 주의할점은 !, <>는 배제대상을 or로 하면안됨
select * from cu,pu where not name in ('카즈야','레이나') order by data; (in 쓰면 베스트)
select * from cu,pu where not name = '카즈야' or name= '레이나' order by data; (필터 안됨) 
select * from cu,pu where name != '카즈야' and name <> '레이나' order by data; (정상적으로 필터됨)
select * from cu,pu where name != '카즈야' or name <> '레이나' order by data; (!랑 <> 쓸때 or 쓰면 필터안됨)
select * from cu,pu where name != '카즈야' and name != '레이나' order by data;  (베스트)
select * from cu,pu where name <> '카즈야' and name <> '레이나' order by data; (베스트)
효율적 예시)) 
select * from cu,pu where name != '카즈야' and name != '레이나'; 
select * from cu,pu where not name in ('카즈야','레이나','임지영');


show variables like 'c%';
인코딩설정확인

반응형