[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

httpd can not connect mysql with pdo/php

From: Yoshinori Arai
Subject: httpd can not connect mysql with pdo/php
Date: Thu, 21 Feb 2019 13:08:51 +0900
User-agent: NeoMutt/20180716


I have tried to build web development environment like as XAMPP.

At first, I have done system reconfigure to add httpd, mysql and
php-fpm service.  My system configuration that followed guix manual
was as follows,

  (packages (cons* fluxbox cwm                    ;window managers
                   nss-certs                      ;for HTTPS access
                   ;;httpd mysql php                ;for web develpment

  ;; Use the "desktop" services, which include the X11
  ;; log-in service, networking with NetworkManager, and more.
  (services (cons* (service httpd-service-type
                               (server-name "")
                               (document-root "/srv/www")
                               (modules (cons*
                                          (name "proxy_module")
                                          (file "modules/"))
                                          (name "proxy_fcgi_module")
                                          (file "modules/"))
                               (extra-config (list "\
<FilesMatch \\.php$>
    SetHandler \"proxy:unix:/var/run/php-fpm.sock|fcgi://localhost/\"
                   (service php-fpm-service-type
                             (socket "/var/run/php-fpm.sock")
                             (socket-group "httpd")))

And I put my script into /srv/www. My script is as follows,

  $servername = 'localhost';
  $username = 'yoshi';
  $password = 'kumagusuM18';

  try {
      $conn = new PDO("mysql:host=$servername", $username, $password);
      // set the PDO error mode to exception
      $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
      $sql = "CREATE DATABASE myDBPDO";
      // use exec() because no results are returned
      echo "Database created successfully<br>";
  catch(PDOException $e)
      echo $sql."<br>".$e->getMessage();

$conn = null;

But httpd said 'SQLSTATE[HY000] [2002] No such file or directory' I
thank its cause was wrong configuration of mysql.socket location in
php.ini but I can't edit it easily.

$ php -i | grep socket
pdo_mysql.default_socket => /tmp/mysql.sock => /tmp/mysql.sock

or above configuration of httpd.conf was wrong.

<FilesMatch \\.php$>
    SetHandler \"proxy:unix:/var/run/php-fpm.sock|fcgi://localhost/\"

Its correct configuration will be

<FileMatch \\.php$>
     SetHandler \"proxy:fcgi:\"

At second, I have tried to build its environment without system
reconfigure. My system went back previous generation, and installed
httpd and php personally.  Sqlite was installed instead of mysql.

And configuration files was copyed from
/gnu/store/...-httpd-2.4.38/etc/httpd/* to ~/.config/httpd/
/gnu/store/...-php-7.3.1/etc/php-fpm.conf to ~/.config/php-fpm.conf
                        php-fpm.d/* to ~/.config/php-fpm.d/www.conf

copy sysvinit file from php-7.3.1 source
sapi/fpm/ to ~/bin/php-fpm

I edited those files adequately after made those writable.

httpd, php-fpm daemon were invoked,
sudo apachectl -f ~/.config/httpd/httpd.conf -k start
sudo bin/php-fpm start

Now httpd and php-fpm conbination is correct when try with below php script in

<?php phpinfo();?>

It was hard to me a little.
Please tell me more easy way.

reply via email to

[Prev in Thread] Current Thread [Next in Thread]