작은숲:위키노트/PHP mysqli options: 두 판 사이의 차이
잔글 (판 1개를 가져왔습니다: 자유위키의 위키노트 데이터 가져옴) |
잔글 (Utolee90님이 PHP mysqli options 문서를 넘겨주기를 만들지 않고 작은숲:위키노트/PHP mysqli options 문서로 이동했습니다: 위키노트 문서 작은숲으로 이동) |
||
| (같은 사용자의 중간 판 7개는 보이지 않습니다) | |||
| 1번째 줄: | 1번째 줄: | ||
{{DISPLAYTITLE:PHP mysqli_options}} | {{DISPLAYTITLE:PHP mysqli_options}} | ||
[[위키노트 | [[작은숲:위키노트/PHP|PHP]]에서 <tt>mysqli</tt>로 [[작은숲:위키노트/MySQL|MySQL]] 데이터베이스 접속할 때 필요한 설정들을 [[작은숲:위키노트/.my.cnf|.my.cnf]]와 같은 형식의 설정 파일에 저장해놓고 쓸 수 있다. | ||
< | <syntaxhighlight lang="cfg"> | ||
[client] | [client] | ||
host = localhost | host = localhost | ||
| 8번째 줄: | 8번째 줄: | ||
database = sample | database = sample | ||
default-character-set = utf-8 | default-character-set = utf-8 | ||
</ | </syntaxhighlight> | ||
이렇게 작성된 설정 파일은 PHP의 <code>[[위키노트 | 이렇게 작성된 설정 파일은 PHP의 <code>[[작은숲:위키노트/PHP:mysql_options|mysql_options()]]</code> 함수를 써서 접속할 때 사용한다. | ||
< | <syntaxhighlight lang="php"> | ||
$sql = mysqli_init(); | $sql = mysqli_init(); | ||
mysql_options(MYSQLI_READ_DEFAULT_FILE, '/path/to/my.cnf'); | mysql_options(MYSQLI_READ_DEFAULT_FILE, '/path/to/my.cnf'); | ||
mysql_options(MYSQLI_READ_DEFAULT_GROUP, "client"); | mysql_options(MYSQLI_READ_DEFAULT_GROUP, "client"); | ||
mysqli_real_connect($sql); | mysqli_real_connect($sql); | ||
</ | </syntaxhighlight> | ||
하지만 이 경우 password 지시자의 값을 제대로 읽어오지 못하는데 이건 버그라고 알려져 있으며 아직 수정되지 않은 상태이다. 따라서 PHP 소스에 MySQL 접속 비밀번호를 직접 쓰지 않고 설정 파일을 쓰려면 <code>[[위키노트 | 하지만 이 경우 password 지시자의 값을 제대로 읽어오지 못하는데 이건 버그라고 알려져 있으며 아직 수정되지 않은 상태이다. 따라서 PHP 소스에 MySQL 접속 비밀번호를 직접 쓰지 않고 설정 파일을 쓰려면 <code>[[작은숲:위키노트/PHP:parse_ini_file|parse_ini_file()]]</code> 함수를 쓴다. | ||
< | <syntaxhighlight lang="php"> | ||
$opts = parse_ini_file('/path/to/my.cnf'); | $opts = parse_ini_file('/path/to/my.cnf'); | ||
$sql = mysqli_init(); | $sql = mysqli_init(); | ||
mysqli_real_connect($sql, $opts['host'], $opts['user'], $opts['password'], $opts['database']); | mysqli_real_connect($sql, $opts['host'], $opts['user'], $opts['password'], $opts['database']); | ||
</ | </syntaxhighlight> | ||
== 참고 == | == 참고 == | ||
* {{언어|영어}} [https://bugs.php.net/bug.php?id=43812 'password' parameter in my.cnf not honored even with mysqli_options()] | * {{언어|영어}} [https://bugs.php.net/bug.php?id=43812 'password' parameter in my.cnf not honored even with mysqli_options()] | ||
| 27번째 줄: | 27번째 줄: | ||
{{PHP}} | {{PHP}} | ||
{{MySQL}} | {{MySQL}} | ||
[[분류:공유]] | [[분류:위키노트/공유]] | ||
[[분류:PHP]] | [[분류:PHP]] | ||
[[분류:MySQL]][[분류:위키노트에서 가져온 문서]] | [[분류:MySQL]]{{퍼온문서|위키노트|{{#invoke:string|replace|{{PAGENAME}}|위키노트:|}}}}[[분류:위키노트에서 가져온 문서]] | ||
2022년 5월 7일 (토) 19:19 기준 최신판
PHP에서 mysqli로 MySQL 데이터베이스 접속할 때 필요한 설정들을 .my.cnf와 같은 형식의 설정 파일에 저장해놓고 쓸 수 있다.
[client]
host = localhost
user = someone
password = simplepassword
database = sample
default-character-set = utf-8
이렇게 작성된 설정 파일은 PHP의 mysql_options() 함수를 써서 접속할 때 사용한다.
$sql = mysqli_init();
mysql_options(MYSQLI_READ_DEFAULT_FILE, '/path/to/my.cnf');
mysql_options(MYSQLI_READ_DEFAULT_GROUP, "client");
mysqli_real_connect($sql);
하지만 이 경우 password 지시자의 값을 제대로 읽어오지 못하는데 이건 버그라고 알려져 있으며 아직 수정되지 않은 상태이다. 따라서 PHP 소스에 MySQL 접속 비밀번호를 직접 쓰지 않고 설정 파일을 쓰려면 parse_ini_file() 함수를 쓴다.
$opts = parse_ini_file('/path/to/my.cnf');
$sql = mysqli_init();
mysqli_real_connect($sql, $opts['host'], $opts['user'], $opts['password'], $opts['database']);
참고
- (영어) 'password' parameter in my.cnf not honored even with mysqli_options()
- (영어) MySQL won't read password from configuration file
| PHP 7.0 | |
|---|---|
| PHP 5.6 | |
| PHP 5.5 | |
| 확장 기능 | |
| 활용 | |
| 소프트웨어 | |
| 설정과 문제 해결 | |
|---|---|
| 활용 | |