D T
D T

Reputation: 3746

Why can't connect to mysql?

This is my code working ok on local pc by Xampp:

$this->dbh = new PDO('mysql:host=localhost;dbname=dbdata', 'root','123456');
$this->dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

phi.ini:

;extension=php_bz2.dll
;extension=php_curl.dll
;extension=php_fileinfo.dll
;extension=php_ftp.dll
;extension=php_gd2.dll
;extension=php_gettext.dll
;extension=php_gmp.dll
;extension=php_intl.dll
;extension=php_imap.dll
;extension=php_interbase.dll
;extension=php_ldap.dll
extension=php_mbstring.dll
;extension=php_exif.dll      ; Must be after mbstring as it depends on it
extension=php_mysqli.dll
;extension=php_oci8_12c.dll  ; Use with Oracle Database 12c Instant Client
;extension=php_odbc.dll
extension=php_openssl.dll
;extension=php_pdo_firebird.dll
extension=php_pdo_mysql.dll
;extension=php_pdo_oci.dll
;extension=php_pdo_odbc.dll
extension=php_pdo_pgsql.dll
;extension=php_pdo_sqlite.dll
extension=php_pgsql.dll
;extension=php_shmop.dll

But when I uploaded it to my server: using win server 2012, apache 2.4, php 7.1, the given error occurred:

PDOException: PDO::__construct(): PHP was built without openssl extension, can't send password encrypted in

C:\Apache24\htdocs\simsodep\acwork\class\ACWDB.php:41 Stack trace: #0 C:\Apache24\htdocs\simsodep\acwork\class\ACWDB.php(41): PDO->__construct('mysql:host=loca...', 'root', '123456') #1 C:\Apache24\htdocs\simsodep\app\model\Home.php(21): ACWDB->__construct() #2 C:\Apache24\htdocs\simsodep\acwork\class\ACWController.php(164): Home_model::action_index() #3 C:\Apache24\htdocs\simsodep\acwork\class\ACWController.php(32): ACWController->dispach() #4 C:\Apache24\htdocs\simsodep\acwork\class\ACWCore.php(116): ACWController->main(NULL) #5 C:\Apache24\htdocs\simsodep\index.php(44): ACWCore::acwork() #6 {main} Next PDOException: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) in C:\Apache24\htdocs\simsodep\acwork\class\ACWDB.php:41 Stack trace: #0 C:\Apache24\htdocs\simsodep\acwork\class\ACWDB.php(41): PDO->__construct('mysql:host=loca...', 'root', '123456') #1 C:\Apache24\htdocs\simsodep\app\model\Home.php(21): ACWDB->__construct() #2 C:\Apache24\htdocs\simsodep\acwork\class\ACWController.php(164): Home_model::action_index() #3 C:\Apache24\htdocs\simsodep\acwork\class\ACWController.php(32): ACWController->dispach() #4 C:\Apache24\htdocs\simsodep\acwork\class\ACWCore.php(116): ACWController->main(NULL) #5 C:\Apache24\htdocs\simsodep\index.php(44): ACWCore::acwork() #6 {main}

Why can't connect to mysql?

Upvotes: 1

Views: 4402

Answers (2)

Emmanuel Medina
Emmanuel Medina

Reputation: 11

From MySQL doc :

If your MySQL installation must serve pre-8.0 clients and you encounter compatibility issues after upgrading to MySQL 8.0 or higher, the simplest way to address those issues and restore pre-8.0 compatibility is to reconfigure the server to revert to the previous default authentication plugin

Change the default authentication plugin setting in th my.cnf file and restart MySQL service.

[mysqld] default_authentication_plugin=mysql_native_password

Change also the account authentication plugin and password:

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';

It works for me.

Upvotes: 1

ashish
ashish

Reputation: 3900

Seems like openssl is not enabled in your php.ini. First open php.ini file

If you are using linux, find extension=php_openssl.so and uncomment it.

for windows, find extension=php_openssl.dll and uncomment it.

Also make sure to add use PDO; on top of the file if you havent.

Good Luck!

Upvotes: 2

Related Questions