programing

mysql-connector-python 쿼리 스크립트가 작동하지 않지만 PHP에서만 작동합니다.

minimums 2023. 7. 2. 19:11
반응형

mysql-connector-python 쿼리 스크립트가 작동하지 않지만 PHP에서만 작동합니다.

저는 라즈베리 파이4에서 64비트 Raspbian OS로 LAMP 설정을 실행하고 있습니다.

저는 'utf8'과 mariadb의 데이터베이스에 대한 커넥터를 사용하는 PHP에서 파이썬 스크립트를 실행하는 데 이상한 문제가 있습니다.

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의 다른 논리적 파이썬 스크립트를 실행/반환하는 데 문제가 없습니다.

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()에서 이 파이썬 스크립트를 실행하려고 합니다.

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() 내에 첫 번째 연결을 도입하기 전까지는 모든 에서 잘 작동했습니다.

나는 다음과 같은 모든 토끼굴 근처에 빌어먹을 내려갔습니다.

  • shell_exec()와 같은 다양한 PHP exec 함수를 시도하는 중

  • 재설치(python, python3, python 모듈, pip, pip3, mysql-connector-python, php)

  • my.conf mysql 파일을 수정하여 모든 기본값에 대해 'utf8mb4'(및 다양한 기타)를 포함한 후 데이터베이스를 약 5회 복원합니다.(하지만 지금은 훌륭한 데이터베이스 작성 스크립트를 받았습니다 :)

  • 다양한 utf8 구문에 대한 시행착오

  • mysql.connect.connect 대신 mariadb.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

현재 데이터베이스 db.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

간단한 해결책은 Python 대신 PHP를 통해 모든 데이터베이스 연결을 실행하는 것입니다.하지만 저는 이미 파이썬으로 모든 것을 썼고 다른 모든 것은 잘 작동하고 있습니다. 저는 이 장애물을 극복할 수 있어야 한다고 생각하지만, 저는 꽤 막혔습니다.

어떤 팁이든 감사합니다, 건배!

이는 MySQL Connector/Python 8.0.30이 MariaDB와 함께 순수 Python 구현을 사용할 때 도입한 알려진 문제입니다.

당신의 경우 Raspbian OS를 사용하고 있기 때문에 Connector/Python의 C 확장 구현이 없는 시나리오가 있습니다.

C 확장을 구축하기 위해 소스에서 컴파일 MySQL Connector/Python을 8.0.29 버전으로 다운그레이드하는 두 가지 방법이 있습니다. https://dev.mysql.com/doc/connector-python/en/connector-python-installation-source.html 을 참조하십시오.

이 문제는 다음 릴리스에서 해결될 예정입니다.

언급URL : https://stackoverflow.com/questions/74145611/mysql-connector-python-query-script-not-working-but-only-from-php

반응형