강좌
클라우드/리눅스에 관한 강좌입니다.
데이터베이스 분류

리눅스마스터1급 : MySQL(MariaDB) 데이터베이스 연동 테스트를 위한 DB 및 사용자 생성

작성자 정보

  • 관리자 작성
  • 작성일

컨텐츠 정보

본문

리눅스마스터1: MySQL(MariaDB) 데이터베이스 연동 테스트를 위한 DB 및 사용자 생성

 

 





웹과 MySQL을 연동하기 위해 MySQLDB와 사용자를 생성해야 한다.

 

 

 

다음 예제는 sulinux DB를 생성하고, sulinux 사용자를 생성하여 sulinux DB에 대한 모든 권한을 주는 것이며, “FLUSH PRIVILEGES"는 마지막에 권한에 대한 설정이 저장된 mysql DB의 내용을 갱신하게 한다.

 

 

 

 

[root@RockyLinux01 ~]# mysql -u root -p

Enter password:

Welcome to the MariaDB monitor. Commands end with ; or \g.

Your MariaDB connection id is 5

Server version: 10.5.16-MariaDB MariaDB Server

 

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

 

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

 

MariaDB [(none)]>

MariaDB [(none)]>

MariaDB [(none)]> create database sulinux;

Query OK, 1 row affected (0.000 sec)

 

MariaDB [(none)]>

MariaDB [(none)]> grant all privileges on sulinux.* sulinux@localhost identified by '11111111' with grant option;

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'sulinux@localhost identified by '11111111' with grant option' at line 1

MariaDB [(none)]>

MariaDB [(none)]>

MariaDB [(none)]> GRANT ALL PRIVILEGES ON sulinux.* TO sulinux@localhost IDENTIFIED BY '비밀번호' WITH GRANT OPTION;

Query OK, 0 rows affected (0.008 sec)

 

MariaDB [(none)]>

MariaDB [(none)]> flush privileges;

Query OK, 0 rows affected (0.001 sec)

 

MariaDB [(none)]>

 

 

 

DB 연동 테스트

 

 

PHP에서 DB를 연동하는 아주 간단한 프로그램을 작성하여 테스트 해 보도록 하겠다.

 

 

 

"/usr/local/apache/htdocs/dbtest.php"파일을 다음과 같이 작성해 본다.

 

 

 

 

 

 

 

 

 

<?php

echo "DB Connect<BR>";

$mysql = mysql_connect("localhost", "sulinux", "비밀번호") or die("DB Connect Faied");

echo "Select sulinux DB<BR>";

$status = mysql_select_db("sulinux",$mysql);

echo "Create sutable table<BR>";

$result = mysql_query("CREATE TABLE sutable ( a text )",$mysql);

echo "Insert data<BR>";

$result = mysql_query("INSERT INTO sutable VALUES('DataBase Test')",$mysql);

echo "Select data<BR>";

$result = mysql_query("SELECT * FROM sutable",$mysql);

echo "Fetch data<BR>";

$row = mysql_fetch_array($result);

echo "Result : <FONT color=red>".$row["a"]."</FONT><BR>";

echo "Drop sutable table<BR>";

$result = mysql_query("DROP TABLE sutable",$mysql);

?>

 

 

 

 

 

 

 

위 프로그램은 MySQL DB에 연결하고, sulinux DB를 선택하며, "sutable"이라는 테이블을 생성한다.

 

 

 

생성되 테이블에 "DataBase Test"라는 값을 삽입하고, 삽입된 데이터는 SELECT 구문을 사용하여 뽑아낸다.

 

 

 

뽑아낸 데이터를 배열로 만든 다음, 출력한다.

 

 

 

출력 후에는 "sutable"을 제거하는 간단한 프로그램이다.

 

 

 

웹브라우저에서 "http://localhost/dbtest.php"를 호출하면 다음과 같은 결과를 볼 수 있을 것이다.

 

 

 

 

 

7ce9390993b6024bf1a77eac34869b85_1686882626_0061.png
 

 

관련자료

댓글 0
등록된 댓글이 없습니다.

공지사항


뉴스광장


  • 현재 회원수 :  60,035 명
  • 현재 강좌수 :  35,794 개
  • 현재 접속자 :  130 명