我正在复盆子Pi4上运行LAMP设置,带有64位复盆子操作系统。
我在“utf8”和从PHP执行python脚本时遇到了一个奇怪的问题,这些脚本使用连接到我的mariadb数据库的连接器。
通过IDE运行脚本和修改数据库没有问题
使用PHP手动将记录插入同一个表中没有问题:
// Create connection
$conn = new mysqli($servername, $username, $password, $database);
$sql = "INSERT INTO commandRequestLog VALUES (0, 1001, 'phpinsert', CURRENT_TIME(), CURRENT_TIME(), 0, 'testing php sql connection', null)";
使用exec()在PHP中运行/返回其他不使用相同语法连接到数据库的逻辑python脚本没有问题:
exec('python3 command_processor.py '.$UserType.' '.$UserId.' '.$PyCommandLvl.' "'.$CommandHdr.'" "'.$strCommand.'"', $output);
foreach ($output as $line) {
echo $line ."<br>";
}
直到我在脚本中引入到数据库的连接:
import mysql.connector
def newConnection():
return mysql.connector.connect(host='localhost',
port=3306,
##charset='utf8',
database='xxxxxxx',
user='xxx',
password='xxxxxxxx')
我正在尝试从PHP exec()运行此python脚本:
if insert_commandRequest(intUserId, strRequestedCommand, strDescription) == -1:
strOutput = 'sql still not working wtf...'+intUserId+strRequestedCommand+strDescription
print(strOutput)
try:
newConn = newConnection()##Fails right here...
except Error as error:
return error
finally:
print('finally')
然后它将返回此异常错误以及预期输出的其余部分:
Character set 'utf8' unsupported ##Primary Error...
Command Requested : [asdf]
Command Function : [asdf]
我记得最初甚至在我的IDE中也遇到过utf8的问题,但快速修复方法是将我的mysql connector python版本降级到8.0.29。在引入任何PHP exec()**中的第一个连接之前,这一切都很好。
我在每个兔子洞附近都跑过,比如:
- 尝试各种PHP exec函数,如shell_exec()
- 重新安装(python、python3、python模块、pip、pip3、mysql连接器python,php)
- 修改我的。conf mysql文件中包含所有默认值的“utf8mb4”(以及其他各种),然后像5次一样恢复我的数据库。(现在有了一个很棒的数据库创建脚本:)
- 各种utf8语法的试错
- 使用mariadb。connect而不是mysql.connector。connect会导致我的所有PHP exec()都没有返回任何输出
MariaDB服务器版本
obi@raspberrypi:~ $ sudo mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 64
Server version: 10.5.15-MariaDB-0+deb11u1 Debian 11
当前数据库数据库。opt文件
default-character-set=utf8mb4
default-collation=utf8mb4_unicode_ci
当前/etc/mysql/my。cnf文件:
[client-server]
# Port or socket location where to connect
# port = 3306
socket = /run/mysqld/mysqld.sock
# Import all .cnf files from configuration directory
!includedir /etc/mysql/conf.d/
!includedir /etc/mysql/mariadb.conf.d/
[client]
default-character-set=utf8mb4
[mysql]
default-character-set=utf8mb4
[mysqld]
collation-server = utf8mb4_unicode_ci
init-connect='SET NAMES utf8mb4'
character-set-server = utf8mb4
Python3和PHP包信息:
obi@raspberrypi:~ $ sudo apt-cache show python3
Package: python3
Source: python3-defaults
Version: 3.9.2-3
Installed-Size: 89
Maintainer: Matthias Klose <doko@debian.org>
Architecture: arm64
Replaces: python3-minimal (<< 3.1.2-2)
Provides: python3-profiler
Depends: python3.9 (>= 3.9.2-0~), libpython3-stdlib (= 3.9.2-3)
Pre-Depends: python3-minimal (= 3.9.2-3)
Suggests: python3-doc (>= 3.9.2-3), python3-tk (>= 3.9.2-0~), python3-venv (>= 3.9.2-3)
Description-en: interactive high-level object-oriented language (default python3 version)
Python, the high-level, interactive object oriented language,
includes an extensive class library with lots of goodies for
network programming, system administration, sounds and graphics.
.
This package is a dependency package, which depends on Debian's default
Python 3 version (currently v3.9).
Description-md5: 691450e63b8789f4ba89699b19c60642
Multi-Arch: allowed
Homepage: https://www.python.org/
Cnf-Extra-Commands: python
Cnf-Priority-Bonus: 5
Tag: devel::interpreter, devel::lang:python, devel::library,
implemented-in::c, implemented-in::python, role::devel-lib,
role::program, role::shared-lib
Section: python
Priority: optional
Filename: pool/main/p/python3-defaults/python3_3.9.2-3_arm64.deb
Size: 37880
MD5sum: 3cb918cd19c9186711e9c0395e668277
SHA256: 79197285d25e73a2a07667efe80af152dd932ac5ef3e13717f1ac824d111ea81
obi@raspberrypi:~ $ sudo apt-cache show php
Package: php
Source: php-defaults (76)
Version: 2:7.4+76
Installed-Size: 13
Maintainer: Debian PHP Maintainers <team+pkg-php@tracker.debian.org>
Architecture: all
Depends: php7.4
Description-en: server-side, HTML-embedded scripting language (default)
PHP (recursive acronym for PHP: Hypertext Preprocessor) is a widely-used
open source general-purpose scripting language that is especially suited
for web development and can be embedded into HTML.
.
This package is a dependency package, which depends on latest stable
PHP version (currently 7.4).
Description-md5: 99c58fa41ae5c5908fd17241a53bfdd9
Section: php
Priority: optional
Filename: pool/main/p/php-defaults/php_7.4+76_all.deb
Size: 6340
MD5sum: 33859926f35bdfdc784d9e7312d16a92
SHA256: 10ca22712771dc343fb6600cbdbc88069069467fcc5c85782ee89abbc8c81f59
我知道简单的解决方案是通过PHP而不是Python运行所有数据库连接。但我已经用Python写了所有的东西,其他的一切都很好,我觉得我应该能够克服这个障碍,但我还是被卡住了。
任何小费都非常感谢,干杯!
1条答案
按热度按时间lstz6jyr1#
这是MySQL Connector/Python 8.0.30和MariaDB在使用纯Python实现时引入的一个已知问题。
在您的案例中,您的场景是没有Connector/Python的C扩展实现,因为您使用的是Raspbian OS。
您有两个选择,一个是降级到8.0.29版本,另一个是从源代码编译MySQLConnector/Python以构建C扩展,请参阅https://dev.mysql.com/doc/connector-python/en/connector-python-installation-source.html
这个问题将在即将发布的版本中解决。