konrms's avatar

Oci8 Exception - laravel on centos 7

Hello guys. My project runs fine on windows using xampp. Now I try to implement it on a live server environment and I have come across a few issues (I have asked the same on stackoverflow too) . My OS is CENTOS 7 Server. The Database version is the oracle 12c release2 and my Laravel version is the 5.7.28

I have installed php and oci8 and the necessary yajra package for laravel to oracle connectivity. The table models were integrated in the framework from the initial projects and when ran

php artisan migrate

the tables were successfully migrated into my database as hopefully expected. However, when running the project and the query is called via laravel controller I get this error:

Yajra \ Pdo \ Oci8 \ Exceptions \ Oci8Exception

With the following error description:

/var/www/laravel/vendor/yajra/laravel-pdo-via-oci8/src/Pdo/Oci8.php

         * @param string $dsn
         * @param string $username
         * @param string $password
         * @param array  $options
         * @param string $charset
         * @throws Oci8Exception
         */
        private function connect($dsn, $username, $password, array $options, $charset)
        {
            $sessionMode = array_key_exists('session_mode', $options) ? $options['session_mode'] : null;

            if (array_key_exists(PDO::ATTR_PERSISTENT, $options)) {
                $this->dbh = @oci_pconnect($username, $password, $dsn, $charset, $sessionMode);
            } else {
                $this->dbh = @oci_connect($username, $password, $dsn, $charset, $sessionMode);
            }

            if (! $this->dbh) {
                $e = oci_error();
                throw new Oci8Exception($e['message']);
            }
        }

My .env file section for oracle database connectivity is this:

DB_CONNECTION=oracle
DB_HOST=localhost
DB_PORT=1521
DB_DATABASE=orcl
DB_SID_ALIAS=konrms
DB_USERNAME=korms
DB_PASSWORD=Any_password!
DB_CHARSET=AL32UTF8

Is it possible oci8 not to be working as expected? In that case I guess migration would fail... Furthermore I don't know how if it is necessary to add $dsn variable to my .env.

0 likes
3 replies
aurawindsurfing's avatar
Level 50

Hi @konrms

I have a similar setup working. Some points from my notes that might help you troubleshoot.

Did you add

extension=oci8.so

to your php-fpm.ini?

check if it is installed:

php -m | grep oci

or

<?php phpinfo(); ?>
i
print_r(get_loaded_extensions());

test connection from your app:

$conn = oci_connect("USER", "passwprd", "localhost/xe");
 echo $conn;

Only here install yajra

Hope it helps!

konrms's avatar

Hi @aurawindsurfing

After a lot of hours of googling I found that I should also add the Environmental Variables for the Apache Service in /etc/sysconfig/httpd. This link helped me a lot: https://ilanea.net/connect-to-oracle-db-with-instantclient-php7-oci8-centos-7/

So, for my case I should just point to oracle position. This resolved the problem and queries finally ran successfully:

ORACLE_HOME=/home/data/oracle/12c/dbhome_1
PATH=$PATH:$ORACLE_HOME/bin
LD_LIBRARY_PATH=$ORACLE_HOME/lib
TNS_ADMIN=$ORACLE_HOME/network/admin

I had thought of adding the extension=oci8.so to php.ini as I had done in windows. But it caused a warning that oci8 was already loaded when starting the Apache server:

service httpd start

Thanks a lot for replying!

Please or to participate in this conversation.