Database
0. 관련 패키지 설치
yum -y install httpd*
yum -y install php
yum -y install php --skip-broken
yum -y install php*
yum -y install php* --skip-broken
yum -y install php-*
yum -y install php-* --skip-broken
yum -y install mariadb*
1. DB 생성
MariaDB [mysql]> create database korea;
2. 사용자 생성/추가
MariaDB [mysql]> insert into user(host,user,password) values('localhost','test',password('123456'));
MariaDB [mysql]> flush privileges;
3. 권한부여
MariaDB [mysql]> grant all privileges on korea.* to test@loacalhost identified by '123456';
MariaDB [mysql]> flush privileges;
cf 0. mysql 접속
mysql -p mysql
MYSQL 계정명 <=데이터베이스로 접속할 MYSQL 의 계정명
-p <=패스워드를 입력하기 위한 옵션
cf 1. 데이터베이스 보기
show databases;
<결과>
--------------------+
| Database |
+--------------------+
| information_schema |
| korea |
| mysql |
| performance_schema |
| test |
+--------------------+
cf 2. 수정사항 반영
flush privileges;
- INSERT, DELETE, UPDATE를 통해 사용자를 추가, 삭제, 권한 변경
및 권한부여를 했을 때 수정사항을 반영하는 명령어
4. 테이블 생성
4-1. 회원가입 기능
MariaDB [mysql]> use korea;
Database changed
<테이블 생성>
MariaDB [korea]> create table class( <= 테이블 명이 class인 테이블 생성
-> c_no int not null auto_increment primary key, <= c_no : 널값을 못가짐, 번호 자동부여, 기본키
-> id char(20) not null, <= id : 고정문자형, 널값을 못가짐
-> pw char(20) not null, <= pw : 고정문자형, 널값을 못가짐
-> nick varchar(20) not null, <= nick : 가변문자형, 널값을 못가짐
-> date datetime not null); <= date : datetime의 값, 널값을 못가짐
- not null auto_increment
<구조 출력>
MariaDB [korea]> desc class;
+-------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
| c_no | int(11) | NO | PRI | NULL | auto_increment |
| id | char(20) | NO | | NULL | |
| pw | char(20) | NO | | NULL | |
| nick | varchar(20) | NO | | NULL | |
| date | datetime | NO | | NULL | |
+-------+-------------+------+-----+---------+----------------+
<내역 조회>
MariaDB [korea]> select * from class;
+------+------+--------+------+---------------------+
| c_no | id | pw | nick | date |
+------+------+--------+------+---------------------+
| 1 | asdf | 123456 | asdf | 2023-08-24 10:50:11 |
+------+------+--------+------+---------------------+
<내역 조회 (추가후)>
MariaDB [korea]> select * from class;
+------+------+--------+------+---------------------+
| c_no | id | pw | nick | date |
+------+------+--------+------+---------------------+
| 1 | asdf | 123456 | asdf | 2023-08-24 10:50:11 |
| 2 | qwer | 1234 | qwer | 2023-08-24 10:51:54 |
+------+------+--------+------+---------------------+
4-2. 게시판 기능
<테이블 생성>
MariaDB [korea]> create table b_tb(
-> b_no int not null auto_increment,
-> subject char(100),
-> user char(20) not null,
-> contents text not null,
-> reg_date datetime not null,
-> primary key(b_no));
<구조 출력>
MariaDB [korea]> desc b_tb
-> ;
+----------+-----------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+-----------+------+-----+---------+----------------+
| b_no | int(11) | NO | PRI | NULL | auto_increment |
| subject | char(100) | YES | | NULL | |
| user | char(20) | NO | | NULL | |
| contents | text | NO | | NULL | |
| reg_date | datetime | NO | | NULL | |
+----------+-----------+------+-----+---------+----------------+
<내역 조회>
MariaDB [korea]> select * from b_tb
-> ;
+------+------------+------+--------------+---------------------+
| b_no | subject | user | contents | reg_date |
+------+------------+------+--------------+---------------------+
| 1 | test title | asdf | test content | 2023-08-24 11:13:48 |
+------+------------+------+--------------+---------------------+
4-2. 파일 업로드 기능
MariaDB [korea]> create table file_tb(
-> file_no int not null auto_increment primary key,
-> file_name char(20) not null,
-> reg_date datetime,
-> file_own char(20) not null,
-> file_size char(10) not null);
MariaDB [korea]> desc file_tb
-> ;
+-----------+----------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+----------+------+-----+---------+----------------+
| file_no | int(11) | NO | PRI | NULL | auto_increment |
| file_name | char(20) | NO | | NULL | |
| reg_date | datetime | YES | | NULL | |
| file_own | char(20) | NO | | NULL | |
| file_size | char(10) | NO | | NULL | |
+-----------+----------+------+-----+---------+----------------+
실습파일 수정
conn
명령어 줄의 사용자를 root->test1으로 변경해주기
(다른 php 파일들도 설정해야함)
$sql
명령어 줄의 file_tb테이블과 테이블의 속성에 맞춰 테이블 생성
- 파일 업로드 디렉터리를 설정한 경로에 맞춰 생성해주어야한다.
(/var/www/html/file)
실습
서버1은 DNS서버
, 서버 2는 웹서버
, 서버4는 DB서버
로 구축 후 웹서버와 DB서버를 서로 연동시켜주기
서버1 DNS서버 설정
<rfc1912 파일 내용 추가>
zone "test2.co.kr" IN {
type master;
file "test2.co.kr.zone";
allow-update { none; };
};
<test2.~.zone 파일 생성>
$TTL 0
@ IN SOA test2.co.kr. admin.test2.co.kr. (
20230824 ; Serial
1D ; Refresh
1H ; Retry
1W ; Expire
1W ; Minimum TTL
)
IN NS test2.co.kr.
IN A 192.168.0.173
<resolv.conf 파일 설정>
# Generated by NetworkManager
nameserver 192.168.0.174
서버2 웹서버 설정
<실습파일 압축 해제>
- /var/www/html <=해당 디렉터리에서 압축 해제
<resolv.conf 파일 설정>
# Generated by NetworkManager
nameserver 192.168.0.174
<index.php 파일 수정>
mysql_connect("192.168.0.184","test2","123456") or die ("네트워크 연결 실패<br>");
-> localhost,root,123456 => [DB서버 IP],[DB서버 유저생성],123456 (수정)
서버4 DB서버 설정
<mysql db, 유저 생성 및 권한 부여>
MariaDB [mysql]> create database korea;
MariaDB [mysql]> insert into user(host,user,password) values('%','test2',password('123456'));
MariaDB [mysql]> grant all privileges on korea.* to test2@'%' identified by '123456'
<수정요소>
1. localhost => %
2. root => test2
DB 이중화
마스터 서버 : 서버1
<호스트명 변경>
hostnamectl set-hostname master
reboot
[root@master ~]# <= 호스트네임 변경 완료
<서버 환경설정>
vi /etc/my.cnf.d/server.cnf <= 환경설정파일
<server.cnf 내용 추가>
[mysqld]
server_id=1
log-basename=master1
log-bin
<슬레이브의 접근 계정 생성>
MariaDB [mysql]> create user 'replication_user'@'%' identified by 'replication_user00##';
<슬레이브의 접근 계정 권한 부여>
MariaDB [mysql]> grant replication slave on *.* to replication_user;
<마스터 서버DB 상태 확인>
MariaDB [mysql]> show master status;
+--------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+--------------------+----------+--------------+------------------+
| mariadb-bin.000001 | 590 | | |
+--------------------+----------+--------------+------------------+
슬레이브 서버 : 서버4
<호스트명 변경>
hostnamectl set-hostname slave
reboot
[root@slave ~]# <= 호스트네임 변경 완료
<서버 환경설정>
vi /etc/my.cnf.d/server.cnf <= 환경설정파일
<server.cnf 내용 추가>
[mysqld]
server_id=2
log-bin
log-basename=master1
report-host=slave1
- 변경 후 mariadb 리스타트
<변경된 서버 ID 확인>
[root@slave ~]# mysql -p mysql
MariaDB [mysql]> show variables like 'server_id';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id | 2 |
+---------------+-------+
<마스터-슬레이브 맵핑>
MariaDB [mysql]>
change master to master_host='192.168.0.174',
master_user='replication_user',
master_password='replication_user00##',
master_port=3306,
master_log_file='mariadb-bin.000001',
master_log_pos=590,
master_connect_retry=10;
MariaDB [mysql]> flush privileges;
MariaDB [mysql]> start slave;
마스터-슬레이브 연동 확인
마스터 서버 (서버1)
<DB 추가>
MariaDB [mysql]> create database sync_dbtest;
<해당 DB로 이동>
MariaDB [mysql]> use sync_dbtest;
<해당 DB에서 테이블 생성>
MariaDB [sync_dbtest]> create table usrtbl(
-> no int not null auto_increment primary key,
-> id char(20) not null,
-> name char(20) not null);
MariaDB [sync_dbtest]> flush privileges;
<테이블에 자료 추가>
MariaDB [sync_dbtest]> insert into usrtbl values(null,'khj','KIM');
MariaDB [sync_dbtest]> insert into usrtbl values(null,'hgd','HONG');
MariaDB [sync_dbtest]> flush privileges;
<테이블 생성 결과>
MariaDB [sync_dbtest]> select * from usrtbl;
+----+-----+------+
| no | id | name |
+----+-----+------+
| 1 | khj | KIM |
| 2 | hgd | HONG |
+----+-----+------+
슬레이브 서버 (서버4)
<해당 DB로 이동>
MariaDB [mysql]> use sync_dbtest;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
<해당DB에 생성된 테이블 확인>
MariaDB [sync_dbtest]> show tables;
+-----------------------+
| Tables_in_sync_dbtest |
+-----------------------+
| usrtbl |
+-----------------------+
<테이블에 생성된 자료 확인>
MariaDB [sync_dbtest]> select * from usrtbl;
+----+-----+------+
| no | id | name |
+----+-----+------+
| 1 | khj | KIM |
| 2 | hgd | HONG |
+----+-----+------+
cf 1. 생성한 테이블 확인
show tables;
+-----------------+
| Tables_in_korea |
+-----------------+
| b_tb |
| class |
| file_tb |
+-----------------+
cf 2. 마스터-슬레이브 상태에서의 게시판 내용 추가
select * from b_tb
+------+----------------------------------------------+------+--------------------------------------------------------------------------------+---------------------+
| b_no | subject | user | contents | reg_date |
+------+----------------------------------------------+------+--------------------------------------------------------------------------------+---------------------+
| 1 | test111 | asdf | 1111testest | 2023-08-24 15:07:40 |
| 2 | master slave | | test contensklfnlad | 2023-08-24 18:06:01 |
| 3 | 마스터-ìŠ¬ë ˆì´ë¸Œ | | 마스터-ìŠ¬ë ˆì´ë¸Œ ë‚´ìš© 확ì¸..!!! | 2023-08-24 18:06:14 |
+------+----------------------------------------------+------+--------------------------------------------------------------------------------+---------------------+
- 마스터 서버는 서버1이며 test.co.kr이라는 웹서버까지 구축되어있다.
- test.co.kr 웹 사이트의 게시판에 글을 등록
- 그 후 슬레이브 서버(서버4)에서의 테이블 내용을 보면 마스터 서버에 추가된 내용이 슬레이브 서버까지 추가된 것을 볼 수 있다.