命令行 php mysqli 失败,但可通过 Web 服务器运行并在本地运行

2021-12-25 00:00:00 shell centos php mysqli

我有一个 shell 脚本,它调用一个 php 脚本在解析文本文件后在数据库中插入记录.当它调用 PHP 脚本时,我收到找不到 mysqli 的错误:

I have a shell script that calls a php script to insert records in database after parsing text file. When it calls the PHP script I get errors that it cannot find mysqli:

PHP Fatal error:  Class 'mysqli' not found in /path/to/php/file.php on line 5

这在使用 Nginx 和 PHP-FPM 以及 Mysql 5.5.x 的 Macbook Pro 本地失败,在我将套接字参数添加到 mysqli 调用后,它在本地工作.这适用于本地数据库!

This failed in local on Macbook Pro with Nginx and PHP-FPM and Mysql 5.5.x and after I added the socket param to mysqli call, it worked in local. This worked with local db!

$mysqli = new mysqli("localhost", "user", "pass", "mydb", 3306, "/tmp/mysql.sock");

我部署到 Linux 服务器(安装了yum install php"和 PHP 5.3.x 的 CentOS6).我更改了与远程数据库服务器的连接,因此删除了/tmp/mysql.sock",因为数据库未在此服务器上运行.

I deployed to Linux server (CentOS6 that has 'yum install php' with PHP 5.3.x) installed. I changed the connection to a remote db server so dropped the "/tmp/mysql.sock" because db not running on this server.

shell脚本调用php执行脚本:

Shell script calling php to execute script:

php -c "/etc/php.ini" -f "/path/to/php/file.php"

鉴于我没有在本地运行 mysql,如何让它识别 mysqli 库并执行.它在 CentOS6 上.我什至尝试将 mysqli.so 添加到 php.ini 文件并得到以下错误:

Given I am not running mysql locally, how do I get it to recognize the mysqli lib and execute. It's on CentOS6. I even tried adding mysqli.so to php.ini file and got following error:

[mike@app2 myapp]# php -version
PHP Warning:  PHP Startup: Unable to load dynamic library '/usr/lib64/php/modules/mysqli.so' - /usr/lib64/php/modules/mysqli.so: cannot open shared object file: No such file or directory in Unknown on line 0
PHP 5.3.3 (cli) (built: Feb  2 2012 23:47:49) 
Copyright (c) 1997-2010 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies

感谢您帮助解决此问题.通常我会使用 mysql(query) 但需要 mysqli() 来处理一些准备好的语句和脚本中的其他位.

Thanks for any help resolving this. Normally I'd use mysql(query) but need mysqli() for some prepared statements and other bits in the script.

推荐答案

发现mysqli没有安装php,运行如下命令:

I found that mysqli was not installed with PHP and ran the following command:

>php -i

这返回了 phpinfo() 并没有发现 mysqli.然后我运行'yum'命令来安装php-mysqli:

This returned the phpinfo() and found no mention of mysqli. I then ran the 'yum' command to install php-mysqli:

>yum install php-mysqli

此后脚本成功运行.希望这能帮助其他可能遇到类似问题的人.

After this the script worked successfully. Hope this helps others that might run into similar.

相关文章