0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

CentOS9 PHPからMariaDBへのPDO接続メモ

Posted at

状況

・CentOSはローカルマシン内のVMとして。ホストともども192.168.1.0/24のネットワーク。
・SElinux、Firewalldは有効。適切に設定する前提。
・Apache入れ、Webサーバへのアクセス確認済み。
・そっからPHP入れ、動作確認済み。
・さらにMariaDB入れ、初期設定やDBやらテーブルやらPHP用ユーザやら作成。
・で、下記PHPでMariaDBに接続できず。「could not find driver」とかって怒られる。

$dsn = 'mysql:host=localhost;dbname=testdb;charset=utf8';
$user = 'phpnoyuuzaa';
$pass = 'hogehoge';

try{
    $db = new PDO($dsn, $user, $pass);
} catch(PDOException $e) {
    die('エラー:' . $e->getMessage());
}

echo('ここまできたらDB接続に成功');

考察して試行

・DBへの到達以前に、PHP側でDB利用のためのドライバ周辺が何か欠落か? phpadmin()して、そん中を「PDO」で検索してもノーヒットで、DBっぽい文言といえばsqliteしか見当たらず。ここにmysqlなりmariadbなり的な文言があるべきなのかと。
・何やら php-mysqlnd なるものを入れろとの情報。やってみる。
 

> sudo yum install php-mysqlnd.x86_64

パッケージのダウンロード:
php-mysqlnd-8.0.27-1.el9.x86_64.rpm             351 kB/s | 152 kB     00:00
--------------------------------------------------------------------------------
合計                                             98 kB/s | 152 kB     00:01
トランザクションの確認を実行中
トランザクションの確認に成功しました。
トランザクションのテストを実行中
トランザクションのテストに成功しました。
トランザクションを実行中
  準備             :                                                        1/1
  インストール中   : php-mysqlnd-8.0.27-1.el9.x86_64                        1/1
  scriptletの実行中: php-mysqlnd-8.0.27-1.el9.x86_64                        1/1
  検証             : php-mysqlnd-8.0.27-1.el9.x86_64                        1/1

インストール済み:
  php-mysqlnd-8.0.27-1.el9.x86_64

完了しました!

 
・いちおうPHP再起動しておく。

> sudo systemctl restart php-fpm.service

 

・行けた。
>  _人人人人人人人人人人人人人人人_
>  > ここまできたらDB接続に成功 <
>   ̄Y^Y^Y^Y^Y^Y^Y^Y^Y^Y^Y^Y^Y^ ̄
 
 
 
ありがとうございました。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?