swoole / library

📚 Swoole Library
https://wiki.swoole.com/#/library
Apache License 2.0
239 stars 56 forks source link

For Postgres PDO Connection Pool, the "charset" option gives error. #153

Closed fakharksa closed 1 year ago

fakharksa commented 1 year ago
  1. What did you do? If possible, provide a simple script for reproducing the error.
    
    // db_connection_pool.php

//const POSTGRES_SERVER_DRIVER = 'pgsql'; // I get warning "Already defined" if i uncomment it const POSTGRES_SERVER_HOST = 'localhost'; const POSTGRES_SERVER_PORT = 5432; const POSTGRES_SERVER_DB = 'swooledb'; const POSTGRES_SERVER_USER = 'postgres'; const POSTGRES_SERVER_PWD = 'passwd123';

function create_db_connection_pool() { return new PDOPool( (new PDOConfig()) ->withDriver(POSTGRES_SERVER_DRIVER) ->withHost(POSTGRES_SERVER_HOST) ->withPort(POSTGRES_SERVER_PORT) // ->withUnixSocket('/tmp/mysql.sock') ->withDbName(POSTGRES_SERVER_DB) //->withCharset('utf8mb4') ->withUsername(POSTGRES_SERVER_USER) ->withPassword(POSTGRES_SERVER_PWD) ); }

Also, tried ...

`->withCharset('utf8mb4')`

And, from inside a class, i make call to "create_db_connection_pool()" funtion:

include(db_connection_pool.php);

protected $conn_pool_swoole;

$this->conn_pool_swoole = create_db_connection_pool(); $pgsql_conn_swoole = $this->conn_pool_swoole->get();


2. What did you expect to see?

Creation of connection pool without any Fatal Error

3. What did you see instead?

PHP Fatal error: Uncaught PDOException: SQLSTATE[HY000] [1698] Access denied for user 'postgres'@'localhost' in @swoole-src/library/core/Database/PDOPool.php:44 Stack trace:

0 @swoole-src/library/core/Database/PDOPool.php(44): PDO->__construct()

1 @swoole-src/library/core/Database/PDOProxy.php(41): Swoole\Database\PDOPool->Swoole\Database{closure}()

2 @swoole-src/library/core/ConnectionPool.php(89): Swoole\Database\PDOProxy->__construct()

3 @swoole-src/library/core/ConnectionPool.php(58): Swoole\ConnectionPool->make()

4 /var/www/html/swoole-prac/sw_service.php(160): Swoole\ConnectionPool->get()

5 [internal function]: sw_service->{closure}()

6 {main}

thrown in @swoole-src/library/core/Database/PDOPool.php on line 44

And when used ->withCharset(''), the error became:

PHP Fatal error: Uncaught PDOException: SQLSTATE[08006] [7] invalid connection option "charset" in @swoole-src/library/core/Database/PDOPool.php:44 Stack trace:

0 @swoole-src/library/core/Database/PDOPool.php(44): PDO->__construct()

1 @swoole-src/library/core/Database/PDOProxy.php(41): Swoole\Database\PDOPool->Swoole\Database{closure}()

2 @swoole-src/library/core/ConnectionPool.php(89): Swoole\Database\PDOProxy->__construct()

3 @swoole-src/library/core/ConnectionPool.php(58): Swoole\ConnectionPool->make()

4 /var/www/html/swoole-prac/sw_service.php(160): Swoole\ConnectionPool->get()

5 [internal function]: sw_service->{closure}()

6 {main}

thrown in @swoole-src/library/core/Database/PDOPool.php on line 44


I can connect postgres using same credentials and using [open-smf](https://github.com/open-smf/connection-pool) package for connection pool for swoole.

I suspect that the issue is in the definition of dsn string in constructor of PDO() which is returned from inside the constructor of PDOPool class.

Unlike mysql, "postgres" does not take charset='something' as part of dsn string therefore it should not be concatenated if database driver is set as "pgsql" instead of "mysql".

For postgres, charset can be set using "options" key of dsn string, as below:

`options='--client_encoding=UTF8'`

4. What version of OpenSwoole are you using (show your `php --ri openswoole`)?

version 4.11.1

5. What is your machine environment used (show your `uname -a` & `php -v` & `gcc -v`) ?

uname -a: Linux fakhar-HP-Laptop 5.17.5-76051705-generic #202204271406~1651504840~20.04~63e51bd-Ubuntu SMP PREEMPT Wed Ma x86_64 x86_64 x86_64 GNU/Linux

php -v: PHP 8.1.12 (cli) (built: Oct 28 2022 17:39:57) (NTS) Copyright (c) The PHP Group Zend Engine v4.1.12, Copyright (c) Zend Technologies with Zend OPcache v8.1.12, Copyright (c), by Zend Technologies

gcc -v : Using built-in specs. COLLECT_GCC=gcc COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/11/lto-wrapper OFFLOAD_TARGET_NAMES=nvptx-none:amdgcn-amdhsa OFFLOAD_TARGET_DEFAULT=1 Target: x86_64-linux-gnu Configured with: ../src/configure -v --with-pkgversion='Ubuntu 11.3.0-1ubuntu1~22.04' --with-bugurl=file:///usr/share/doc/gcc-11/README.Bugs --enable-languages=c,ada,c++,go,brig,d,fortran,objc,obj-c++,m2 --prefix=/usr --with-gcc-major-version-only --program-suffix=-11 --program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --enable-bootstrap --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-plugin --enable-default-pie --with-system-zlib --enable-libphobos-checking=release --with-target-system-zlib=auto --enable-objc-gc=auto --enable-multiarch --disable-werror --enable-cet --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-offload-targets=nvptx-none=/build/gcc-11-xKiWfi/gcc-11-11.3.0/debian/tmp-nvptx/usr,amdgcn-amdhsa=/build/gcc-11-xKiWfi/gcc-11-11.3.0/debian/tmp-gcn/usr --without-cuda-driver --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu --with-build-config=bootstrap-lto-lean --enable-link-serialization=2 Thread model: posix Supported LTO compression algorithms: zlib zstd gcc version 11.3.0 (Ubuntu 11.3.0-1ubuntu1~22.04)



You can also try the following OpenSwoole support channels:

* [Documentation](https://openswoole.com/docs) - Documentation for Open Swoole
* [Slack](https://goo.gl/forms/wooTTDmhbu30x4qC3) - Slack channel of Open Swoole
* [Discord](https://discord.gg/5QC57RNPpw) - Discord server of Open Swoole
sy-records commented 1 year ago

Fixed via #154

fakharksa commented 1 year ago

Fixed via #154 @sy-records As this repository "Swoole\Library" is separate from main swoole "swoole\swoole-src" repository, so i want to know how or when the change in repository "Swoole\Library" will be incorporated into the main swoole "swoole\swoole-src" repository ?