使用 PHP 和 OCI8 连接到 Oracle 自治数据库

使用 PHP 和 OCI8 连接到 Oracle 自治数据库

我正在使用 Oracle 云基础设施。我有一个装有 Ubuntu-22.04-Minimal 的 VM 实例。Apache、PHP 和 OCI8 均已安装并正常运行。

当我尝试连接到数据库时,我得到:

Warning: oci_connect(): ORA-28759: failure to open file in /var/www/html/oci8.php on line 6

Fatal error: Could not connect to database: ORA-28759: failure to open file in /var/www/html/oci8.php on line 10

看来 PHP 代码无法访问 /home/ubuntu/wallet 钱包中的文件

下面的代码已经修复并且可以工作:

<?php

error_reporting(E_ALL);
ini_set('display_errors', 'On');

$conn = oci_connect('Harley', '******', 'tcps://adb.ap-melbourne-1.oraclecloud.com:1522/*******_harleys_high.adb.oraclecloud.com?wallet_location=/opt/wallet');
if (!$conn) {
    $e = oci_error();
    trigger_error(htmlentities($e['message'] ?? ''), E_USER_ERROR);
}
var_dump($conn);


// Parse the statement. Note there is no final semi-colon in the SQL statement
// https://www.php.net/manual/en/function.oci-parse.php
// ADMIN IS THE USER THAT CREATED THE ACCOUNTS TABLE
$stid = oci_parse($conn, 'SELECT * FROM ADMIN.ACCOUNTS');
if (!$stid) {
    $e = oci_error($conn);
    trigger_error(htmlentities($e['message'] ?? ''), E_USER_ERROR);
}
var_dump($stid);


// Define variables to store column values
oci_define_by_name($stid, 'USER_ID', $column1);
oci_define_by_name($stid, 'EMAIL', $column2);
oci_define_by_name($stid, 'PASSWORD', $column3);

oci_execute($stid);

// https://www.php.net/manual/en/function.oci-fetch-array.php
// Fetch and display results
echo "<table border='1'>\n";
while (oci_fetch($stid)) {
    echo "<tr>\n";
    echo "    <td>" . htmlentities($column1 ?? '') . "</td>\n";
    echo "    <td>" . htmlentities($column2 ?? '') . "</td>\n";
    echo "    <td>" . htmlentities($column3 ?? '') . "</td>\n";
    echo "</tr>\n";
}
echo "</table>\n";

oci_free_statement($stid);
oci_close($conn);

?>
GRANT SELECT ON ACCOUNTS TO Harley;

文件权限是一个问题...我将位置更改为/opt/wallet:

root@canonical-ubuntu-22-04-minimal-2023-10-15-0:/opt/wallet# ls -al
total 56
drwxrwxr-x 2 ubuntu ubuntu 4096 Jan 15 00:58 .
drwxr-xr-x 4 root   root   4096 Jan 15 00:57 ..
-rw-rw-r-- 1 ubuntu ubuntu 3033 Jan 15 00:53 README
-rw-rw-r-- 1 ubuntu ubuntu 5349 Jan 15 00:53 cwallet.sso
-rw-rw-r-- 1 ubuntu ubuntu 5304 Jan 15 00:53 ewallet.p12
-rw-rw-r-- 1 ubuntu ubuntu 5710 Jan 15 00:53 ewallet.pem
-rw-rw-r-- 1 ubuntu ubuntu 3193 Jan 15 00:53 keystore.jks
-rw-rw-r-- 1 ubuntu ubuntu  691 Jan 15 00:53 ojdbc.properties
-rw-rw-r-- 1 ubuntu ubuntu  111 Jan 15 00:58 sqlnet.ora
-rw-rw-r-- 1 ubuntu ubuntu 1290 Jan 15 00:53 tnsnames.ora
-rw-rw-r-- 1 ubuntu ubuntu 2056 Jan 15 00:53 truststore.jks
root@canonical-ubuntu-22-04-minimal-2023-10-15-0:/opt/wallet# cat sqlnet.ora
WALLET_LOCATION = (SOURCE = (METHOD = file) (METHOD_DATA = (DIRECTORY="/opt/wallet")))
SSL_SERVER_DN_MATCH=yes

答案1

文件权限...我将位置更改为/opt/wallet:

root@canonical-ubuntu-22-04-minimal-2023-10-15-0:/opt/wallet# ls -al
total 56
drwxrwxr-x 2 ubuntu ubuntu 4096 Jan 15 00:58 .
drwxr-xr-x 4 root   root   4096 Jan 15 00:57 ..
-rw-rw-r-- 1 ubuntu ubuntu 3033 Jan 15 00:53 README
-rw-rw-r-- 1 ubuntu ubuntu 5349 Jan 15 00:53 cwallet.sso
-rw-rw-r-- 1 ubuntu ubuntu 5304 Jan 15 00:53 ewallet.p12
-rw-rw-r-- 1 ubuntu ubuntu 5710 Jan 15 00:53 ewallet.pem
-rw-rw-r-- 1 ubuntu ubuntu 3193 Jan 15 00:53 keystore.jks
-rw-rw-r-- 1 ubuntu ubuntu  691 Jan 15 00:53 ojdbc.properties
-rw-rw-r-- 1 ubuntu ubuntu  111 Jan 15 00:58 sqlnet.ora
-rw-rw-r-- 1 ubuntu ubuntu 1290 Jan 15 00:53 tnsnames.ora
-rw-rw-r-- 1 ubuntu ubuntu 2056 Jan 15 00:53 truststore.jks

我已经更新了问题中的 PHP 代码。现在可以正常工作了。

相关内容