Handling problems when php calls oracle

Environment:
PHP 7.4
Framework: laravel6
Extensions installed: oci8-2.2.0, pdo-oci
Database: oracle12
Environment variables configured

Use oci_connect to connect and execute from the command line:

php -r 'var_dump(oci_connect("username", "password", "host:port/database"));'

Problem 1 occurs:

ErrorException
oci_connect(): OCIEnvNlsCreate() failed. There is something wrong with your system - please check that LD_LIBRARY_PATH includes the directory with Oracle Instant Client libraries

Use pdo to connect and execute from the command line:

php -r 'var_dump(new PDO("oci:dbname=host:port/database", "username", "password"));'

Problem 2 occurs:

SQLSTATE[HY000]: pdo_oci_handle_factory: Error while trying to retrieve text for error ORA-01804
 (/builddir/build/BUILD/php-7.4.32/ext/pdo_oci/oci_driver.c:714)

Use pdo to connect and execute from the command line:

php -r 'var_dump(new PDO("oci:dbname=host:port/database?charset=AL32UTF8", "username", "password"));'

Question 3:

SQLSTATE[HY000]: OCINlsCharSetNameToId: unknown character set name (/builddir/build/BUILD/php-7.4.32/ext/pdo_oci/oci_driver.c:689)

The above problems are all related to the configuration of environment variables. After LD_LIBRARY_PATHthe configuration is correct, everything will be fine when executed from the command line.

But a strange problem occurred. When calling the interface through the browser, the above problem appeared again. I knew that it was an issue with environment variables. I also tried it through various information on the Internet, such as directly using PHP to set environment variables in the code. Setting environment variables in php-fpm's www.conf has no effect.
Some people have solved the problem through the following configuration in www.conf, but mine did not work. If anyone knows the reason, please tell me, thank you.

# 都需要修改修改成你自己的路径
env[ORACLE_HOME] = /usr/lib/oracle/12.2/client64
env[LD_LIBRARY_PATH] = /usr/local/lib/instantclient21.8
env[C_INCLUDE_PATH] = /usr/include/oracle/12.2/client64
env[NLS_LANG] = "SIMPLIFIED CHINESE_CHINA.AL32UTF8"

Since my machine starts php through systemctl, after many attempts, I finally solved the problem by setting the environment when starting php. The configuration file /usr/lib/systemd/system/php74-php-fpm.service:

# It's not recommended to modify this file in-place, because it
# will be overwritten during upgrades.  If you want to customize,
# the best way is to use the "systemctl edit" command.

[Unit]
Description=The PHP FastCGI Process Manager
After=syslog.target network.target

[Service]
Type=notify
ExecStart=/opt/remi/php73/root/usr/sbin/php-fpm --nodaemonize
ExecReload=/bin/kill -USR2 $MAINPID
PrivateTmp=true
# 新增加的环境变量,需要修改成你自己的路径
Environment="LD_LIBRARY_PATH=:/usr/local/lib/instantclient21.8/"

[Install]
WantedBy=multi-user.target

Guess you like

Origin blog.csdn.net/sting_bo/article/details/127780060