Previous Next

Thread: PHP and MySQL: Can't Create Socket Error (5)

Last post 05-24-2008 6:32 PM by mvz. 11 replies.

Average Rating Rate It (5)

RSS

Page 1 of 1 (12 items)

Sort Posts:

  • 05-16-2008, 1:58 PM

    • Jiriki
    • Not Ranked
    • Joined on 05-16-2008, 5:51 PM
    • Posts 6
    • Jiriki

    PHP and MySQL: Can't Create Socket Error (5)

    Win2K3sp2 system with PHP v5.2.6 (IASPI) and MySQL v5.0.41 and IIS.

    PHP is functioning, php.info displays and I have other .php files serving properly.

    When I attempt the Database check (local or remote), I get the error:

    Error: MySQL Server Problems
    Unable to connect to your database server

    I can connect to MySQL via MySQL Manager Lite. Created several users for localhost and server IP. I’m able to remotely connect to DB with MySQL Man.Lt. with remote accounts. Can create tables, etc. Also installed MySQL Tools and they connect and function appropriately.

    A Basic connection script displayes:  Can't create TCP/IP socket (5)
     
    phpMySQLAdmin displays:  #2004 - Can't create TCP/IP socket (5)
     
    Expression Engine's Wizard.php causes the following to be written to the error log:
    mysql_connect() [<a href=’function.mysql-connect’>function.mysql-connect</a>]: Can’t create TCP/IP socket (5)

    Since I’m running this from the mysql server (localhost) and MySQL Tools Admin and MySQL Admin Lite connects fine, I don’t see where this is a port block or other such config error. (using port 3306).

    Several posts talk about setting the mysql.default (in php.ini) or the $cfg[’Servers’][$i][’socket’] = (config.inc.php) set to /tmp/mysql.sock. I can’t find a similar file on windows.

    Another post specified that the user you connect with had to have the ‘GRANT’ priv’s. I created users for localhost, 127.0.0.1 and even tried ‘%’ set with Super/Grant with no different results.

    I think the problem is residing around the (5) sub error and I'm not finding any info on it. Other (xxx) sub-errors are on all kinds of forums, but none give a desc. or fix for the (5). I'm pretty sure its a permissions issue on IIS or folder security, but am not finding much in the way of work arounds.

    Any contructive help would be appreciated and please don't go off on *NIX or Apache usage over Win/IIS...
  • 05-18-2008, 6:10 PM In reply to

    • brashquido
    • Top 25 Contributor
    • Joined on 05-22-2006, 12:31 AM
    • Australia
    • Posts 150
    • IIS MVPs
    • brashquido

    Re: PHP and MySQL: Can't Create Socket Error (5)

    Is this a new install of PHP, or have you upgraded? Have you enabled the skip-networking option in my.ini? If so, then comment it out and restart MySQL.
    ----------------
    Dominic Ryan
    Microsoft IIS MVP
    www.iis-aid.com
    My IIS articles
    My PHP articles
    My How-to guides
  • 05-19-2008, 2:32 AM In reply to

    • Jiriki
    • Not Ranked
    • Joined on 05-16-2008, 5:51 PM
    • Posts 6
    • Jiriki

    Re: PHP and MySQL: Can't Create Socket Error (5)

    Sorta of an upgrade.  I was having the exact same problem with v5.2.2, so I moved the contents of the previous folder to a backup, extracted the new 5.2.6, extracted the specific extensions and used the same php.ini.  No change.

     I checked the my.ini file and the only 'skip-' section I have is the skip-innodb.

  • 05-19-2008, 4:47 AM In reply to

    • brashquido
    • Top 25 Contributor
    • Joined on 05-22-2006, 12:31 AM
    • Australia
    • Posts 150
    • IIS MVPs
    • brashquido

    Re: PHP and MySQL: Can't Create Socket Error (5)

    It could be possible that your locally install MySQL apps are using named pipes for connection, where as far as I am aware all web apps will use TCP/IP sockets. From here I'd suggest verifying that port 3306 is open and accepting connections. Try connecting to your MySQL server via Telnet;

    Telnet <your_ip> 3306

     

     

     

    ----------------
    Dominic Ryan
    Microsoft IIS MVP
    www.iis-aid.com
    My IIS articles
    My PHP articles
    My How-to guides
  • 05-19-2008, 3:43 PM In reply to

    • Jiriki
    • Not Ranked
    • Joined on 05-16-2008, 5:51 PM
    • Posts 6
    • Jiriki

    Re: PHP and MySQL: Can't Create Socket Error (5)

    I know the 3306 port is functioning as I can connect via MySQL Manager Lite from a remote machine using the IP or DNS name, edit users, tables, etc.

    I completely removed MySQL and verified that the sub-error (5) still happens, so basically, it is definitely a PHP issue and not a MySQL one.

    I've re-installed MySQL with the default my.ini and have verified it backup and running.

    Are there any permissions or settings in IIS to allow connection/sockets, etc to be created to ports by a specific application pool?  My guess is IIS config somewhere or simple permissions on specific files for specific users.

     I'm setting up a VM instance of 2K3 from scratch and going to try to mirror the environment one step at a time to see if I can figure something out. 

  • 05-19-2008, 5:26 PM In reply to

    • Jiriki
    • Not Ranked
    • Joined on 05-16-2008, 5:51 PM
    • Posts 6
    • Jiriki

    Re: PHP and MySQL: Can't Create Socket Error (5)

    I've definitely narrowed this down to a permissions issue.  The VM base 2K3 system with no security measures in place works fine with the same installers for PHP and MySQL which talk to each other.

     Basically i remove EVERYONE group from the whole system, change the name of the IUSR_xxx account, update all the services and application pools, all IIS and related folders/files (PHP and MySQL) go on a 2nd physical drive from the system drive, all permissions are removed from the base of that drive and then only specified accounts are granted the least possible at a time.

    I'm going to try to step-by-step remove permissions on the VM system to see what the critical permissions are to allow this.  Very frustrating that there isn't a full 'required' base permissions for PHP or MySQL.  Throwing files into the Windows and System32 folder is just rediculous.

  • 05-19-2008, 7:43 PM In reply to

    • brashquido
    • Top 25 Contributor
    • Joined on 05-22-2006, 12:31 AM
    • Australia
    • Posts 150
    • IIS MVPs
    • brashquido

    Re: PHP and MySQL: Can't Create Socket Error (5)

    Yep, I'd say you are onto something there. I would try using filemon to track back what permissions are restricting your operation. You will need to ensure your IIS user accounts (Network Service, IUSR, etc) have permissions to your PHP installation. What permissions have been changed for your MySQL insallation? You should never have to put any PHP files into your Windows system directories so long as your environment variables are correctly set.

    ----------------
    Dominic Ryan
    Microsoft IIS MVP
    www.iis-aid.com
    My IIS articles
    My PHP articles
    My How-to guides
  • 05-20-2008, 4:16 AM In reply to

    • Jiriki
    • Not Ranked
    • Joined on 05-16-2008, 5:51 PM
    • Posts 6
    • Jiriki

    Re: PHP and MySQL: Can't Create Socket Error (5)

    OK, pretty sure this is permissions, so I did a base install on a VMWare image.  Below is the process I’ve taken and my conclusions so far.  My Hope is that this helps someone else who may be trying to harden their IIS box rather than take the default…  

     Installed Base 2K3 std SP2

    Installed IIS, defaults.

    Moved IIS root to E:\webroot

    Installed PHP to E:\php (just unzipped v5.2.6 to folder)
     - Added E:\php to beginning of system PATH env. var.
     - Added registry entries:
      HKLM\Software\PHP\InstallDir   "E:\php\"
      HKLM\Software\PHP\IniFileDir   "E:\php\"
     - Copied php.ini-recommended to php.ini
     - created folders and updated appropriate php.ini entries:
      E:\php\session
      E:\php\logs
       *php.ini section is E:\php\logs\phperror.log
     - updated extensions dir in php.ini to E:\php\ext
     - NOTE:  I did not copy any files outside of the PHP directory.
       I did not create the PHPRC env. Variable


    Created files:

     C:\webroot\info.php    just <?php phpinfo(); ?>
     C:\webroot\testdb.php (ripped code from a forum, sorry I don't remember author/URL)
      <?PHP
      $rst = @mysql_connect("localhost","userid","password");

      if (!$rst){
        echo( "<p>Unable to connect to database manager.</p>");
              die('Could not connect: ' . mysql_error());
        exit();
      } else {
        echo("<p>Successfully Connected to MySQL Database Manager!</p>");
      }

      if (! @mysql_select_db("mySchema") ){
        echo( "<p>Unable to  connect database...</p>");
        exit();
      } else {
        echo("<p>Successfully Connected to Database 'mySchema'!</p>");
      }
      ?>
     C:\webroot\wizard.php (this is the test your server script offered by www.expressionengine.com
      to verify your system is ready for thier product.

    Verified scripts worked (MySQL tests failed with Socket (5) errors).  php log file would not create.
     - pre-created c:\php\logs\phperror.log
     - retested, but no change.
     - granted IUSR_ full priv's, log still updated.
     - removed all but Write/Append, it now updates properly, but write priv's are required.

    Installed MySQL v5.0.51b
     - Server Mode, non-strict, Transactional, had bin it added to env. var. PATH (defaults on rest).
     - E:\mysql

    Installed MySQL tools v5.0-r12
     - E:\mysql\tools

    Installed ImageMagick v6.4.1.1-Q16 (non-static)
     - E:\subfolder\IM

    Using MySQL Administrator:
     - created schema 'mySchema'
     - created user 'userid'
     - created localhost for userid
     - granted SELECT, INSERT, UPDATE, DELETE, DROP, ALTER, CREATE and Grant to userid@localhost for mySchema

    Uncommented following extensions:
     - php_bz2.dll
     - php_gd2.dll
     - php_gettext.dll
     - php_mcrypt.dll
     - php_mime_magic.dll
     - php_mysql.dll
     - php_smtp.dll
     - php_zip.dll

    Tested the 3 .php files:
     - info.php  "OK"
     - testdb.php "OK"
     - Wizard.php "all supported except for URL Segment and Built-in Spellchecker"
     - Wizard.php database check "OK"

    Base permissions:
     Users:  3 special perms to all base drives:
       Read Execute on This folder, subfolders and files
       Create Folders/Append on This folder and subfolders
       Create Files/Append on Subfolders only
      * These are on %systemdrive%, %systemroot%, %systemroot%\system32 and E:
      ** systemroot does not inherit from drive and system32 does not inherit from systemroot
         but have same explicit ACL's

     Administrators:  Full on all

     Everyone:  Root of drive only, Read/Execute that folder only.

     System: Full on all

     Creator Owner:  Full on subfolders and files only
     

    Removed Everyone from C: (read/Exec. This folder only), no change.

    Removed Everyone from E: (read/Exec. This folder only), no change.

    Removed Interactive and Authenticated from Users group, broke web, mysql access
     (no change actually until I restarted IIS.  So assume from now on, each step I restarted IIS
             after making the change)

    Removed Interactive from Users group, no change.

    Denied IUSR_ account to C:, no change (note, none of the default directories on C: inherit this).

    Removed Authenticated from Users group, broke web access, mysql access.
     * NOTE: I want to do this as eventually my production system will be in a domain environment
      as a stand-alone domain server.  If Authenticated Users group is allowed,
      then any user in the domain can gain user access to whatever the User group
      has access to.  By removing it and then only adding the Domain Users/Groups
      I choose, I gain tighter control of my server content.

    Created group Web-read, added IUSR account, granted Read/Execute on E: and all sub folders.
     (can now view info.php
      get Socket (5) error from MySQL connect test.php
      loose ExpressionEngine (EE) Wizard.php 1 option for Ping and TraceBack
      EE Wizard.php failes DB check - will refer to this as a 'Parital breakdown' here on out)

    Added Network Service to Web-read group, no change.

    Added IWAM_ user to Web-read group, no change.

    Added Service to Web-Read, no change.

    **Note: e:\php\phperror.log is still be appended to, so IUSR_ MUST have at least Write priv's is proved.

    Added Web-Read to C:\windows, no change.

    Added Web-Read to C:\windows\systeme32 (does not inherit C:\windows permissions),

    Added Authenticated to Web-Read, no change.

    Removed Authenticated from web-Read and re-added to Users just to check, "OK on All"
     (remember I restart IIS after each)

    Removed Authenticated from Users.
     *This time, instead of restarting IIS, I just recycled the DefaultApplication pool.
     - Now I'm backed to only the PHP.info working, Wizard.php losses Ping/trace support.

    Added IUSR_, IWAM_, Network Service, Service accounts to the Users group, "OK on ALL"

    Removed Service account from Users group, "OK on ALL"

    Removed IUSR_ from Users group, "back to parital broke"

    Readded IUSR_ and removed IWAM from users group, "OK on ALL"

    Removed Service account from Web-Read, "OK on ALL"

    Removed Network Service account from Web-Read, "OK on ALL"

    Removed IWAM_ from Web-read, "OK on ALL"

    Removed Web-read as it's its basically nulled, "OK on ALL"
     ** So basically we've proven that none of those other accounts need explicit
     read/write access (whether they get it implicit some other way is another question,
     some are in the IIS_WPG group, but I cannot find any explicit rights granted to any
     folder to this group). IUSR_ basically has read/write access to all parts of my
     system, which I do not want.  So I will start explicitly
     dening write access for IUSR_ to various directories to override it's "write priv's"
     from being in the users group.

    Denied IUSR_ %program files% (all sub folders inherit), "OK on ALL"

    Denied IUSR_ %systemroot%, "OK on ALL"

    Denied IUSR_ %systemroot%\system32, "OK on ALL"

    Denied IUSR_ e:\mysql, "OK on ALL"
     * Restarted mysql service just to make sure on that as well

    Denied IUSR_ e:\php, "ALL Broke"

    Reset to just Deny write to e:\php to IUSR_, "OK on ALL"

    ** So now we have on E: at least, Explicit deny Write rights on all folders, with only read/execute
       e:\webroot and e:\php (explicit write rights given on e:\php\logs\phperror.log

    Removed IUSR_ from Users group, "back to partial breakdown"

    Readded IUSR_ to Users group, but reomved User's group priv's from E:, this broke everything.

    Added IUSR_ to E: with same as previous 3 speical users group, everything still broke.

    Removed IUSR_ from E: and added back the 3 special Users group, Everything works.

    !! This doesn't make sense to me.

    Denied Users group write priv's to E:, "OK on ALL"

    So this seems to indicate to me that the Users group has a special "user security/policy
    right assignment", similar to 'log on as batch job' type requirement in order to create a socket.  So my theory now is, if I can identify the policy right(s), I can grant it to the IUSR_ accont and remove it from the Users group.  I'm out of time at the moment.  I'll try to investigate this soon.

  • 05-20-2008, 8:04 PM In reply to

    • brashquido
    • Top 25 Contributor
    • Joined on 05-22-2006, 12:31 AM
    • Australia
    • Posts 150
    • IIS MVPs
    • brashquido

    Re: PHP and MySQL: Can't Create Socket Error (5)

    Just had a quick look at one of my servers and it has the following local security policy settings for the users group;

    Access this computer from the network

    Allow log on locally

    Bypass traverse checking

    This should be pretty close to the defaults for the users group unless another SA has modified the permissions on the server I'm looking at. Hopefully this helps.

     

    ----------------
    Dominic Ryan
    Microsoft IIS MVP
    www.iis-aid.com
    My IIS articles
    My PHP articles
    My How-to guides
  • 05-21-2008, 12:37 AM In reply to

    • Jiriki
    • Not Ranked
    • Joined on 05-16-2008, 5:51 PM
    • Posts 6
    • Jiriki

    Re: PHP and MySQL: Can't Create Socket Error (5)

    ** Another point of interest.  When I remove the IUSR_ account from the Users group, the 'System' field in the info.php file which normally shows OS, computer name, ver, build; replaces the computer name with various random ASCII characters or nothing what so ever.

    *** Correction:  IUSR_ account and Network Service (or account the Application pool that runs the php ISAPI mapping) must have both read and write access to the log file.

    --- you can skip to bottom for conclusions:

    Users group has the default User Rights Assignments,
    I've marked * next to those that IUSR_ and a # next to the ones IWAM_ explicitly have as well:
     Access Computer from network * #
     Allow og on Locally *
     Bypass Traverse checking

    In addition IWAM (#), IUSR_ (*) and IIS_WPG ($) have the following rights that Users group does not:

     Replace a process level token #
     Impoersonate a client after authentication $
     Adjust memory quotas for a process #
     
     
    So basically the only rights that Users group has over IUSR_ is the Bypass traverse checking, however, the Everyone group is assigned here, so technically the IUSR_ has this implicitly.

    Remove Everyone from Bypass Traverse Checking and Access this computer from the network, "OK on ALL"

    Removed IUSR from Users Group, Partial Break.

    Added IUSR the right to Bypass Traverse Checking, no change from Partial Break.


    So that blows the theory that it's a User Rights issue (or at least only a user rights issue). All I think left to check are all the subdirectories under %systemroot% since none inherit from parent.

    Ran FileMon and filtered on w3wp.exe (worker process running as Network Service).  With IUSR_ in the Users group, about 8 processes occur with a success result on testdb.php.  When I remove IUSR_ from Users group and requery testdb.php, I get roughly 200 process attemps for w3wp.exe.
    Most are retries as it filters down through the PATH statement for various files and directories.
    Most end up succeeding except for:

    test.php.fail.log(309): 309 10:01:08 PM w3wp.exe:2524 FASTIO_QUERY_OPEN C:\WINDOWS\system32\mswsock.dll ACCESS DENIED Attributes: Error 
    test.php.fail.log(311): 311 10:01:08 PM w3wp.exe:2524 FASTIO_QUERY_OPEN C:\WINDOWS\system32\hnetcfg.dll ACCESS DENIED Attributes: Error 
    test.php.fail.log(318): 318 10:01:08 PM w3wp.exe:2524 FASTIO_QUERY_OPEN C:\WINDOWS\system32\hnetcfg.dll ACCESS DENIED Attributes: Error

    If I explicitly give IUSR_ read access to these 2 files, %systemroot%, %systemroot%\WinSXS,
    %systemroot%\system32, %systemroot%\system32\inetsrv; all of which are accessed by w3wp.exe,
    the test still partially breaks (i.e. php pages serve, but connection to database fails with
    Can't create Socket (5) error, and ping/trace becomes unsupported on Wizard.php)

    After all this, Filemon filters 0 results for 'DENIED', 'NOT FOUND', 'ERROR' with basically all 'SUCCESSES'
    when I load testdb.php or run the Wizard.php database check.

    CONCLUSIONS:

     1. explicit permissions required are extremely difficult to find in for the php-to-MySQL connection process. So much so, I'm questioning the feasibility to limiting the IUSR_ account beyond its default settings.
      (I wish I was smarter and had more time for this)
      1.a  - Either Authenticated Users or the IUSR_ accounts must be in the Users group
       (unless you can unlock the secret I've failed to do)
      1.b  - If you are serving simple php scripts and no MySQL, you can remove IUSR_ from
       almost any directory and Users group except for the PHP and folder containing
       the php file (assuming you are allowing anonymous access to .php file).

     2. All of the so-called requiremens of tutorials to copy files from either PHP or MySQL into the %systemroot% or %systemroot%\system32 folders is a "Myth".
      2.a - putting the PHP install path at the beginning of the PATH env. var. is a good idea
            as w3wp.exe process does start looking for called files in that order so if you
            similar .dll's in MySQL and PHP folder, this could become an issue.  Using
            the Reg entry instead (I don't outline this above, but was able to get it to work)
            for PHP dir (InstallDir key) allows for possible multiple php versions to run.
      2.b - PHPRC Env. Var is not needed.  Registry entry for INI (IniFilePath key) seems better for basic purposes as cycling App Pool allows updates without reboots (switching to a test php install or testing an update without touching your existing install)
            and potential alterations to allow multiple PHP versions to run.
      2.c - All .dll's can remain in the PHP and MySQL directories.
      2.d - For basic connection, mysqli extention is not needed.  Again, I've not put any
       major php mysql query/actions into play, so it has a purpose, but not for
       basic select query statements.

     3.  There are certain steps that you can take to limit the IUSR_ account as an attack vector.
      3.a - Renaming the IUSR_ account is very doable.  Although not as secure as limiting the account
         could thrawt account specific type web attacks. (I didn't go into this above, but was successful)
         Manual updating in the IIS config properties for sites is required.
      3.b - Even though the IUSR_ has implicit access via the Users group, explicitly dening
       Write access to almost all folders allowed all to function with the exception
       of the php error log.  Again, this is a very rudenmentry setup. If your PHP
       scripts write to .xml or other generated static content files,

     

    I'm not claiming "authority on this subject", by far I'm not even close.  This is simply my conclusions from the processes I carried out above to determine a work around for my "Can't Create Socket (5) error which I have now resolved by adding the IUSR_ account back to the Users group.  If anyone can help in identifying the precise file permissions for a php-to-mysql call can be made without this action, I would be much appreciative to limit the IUSR_ account as much as possible.

  • 05-21-2008, 5:48 AM In reply to

    • brashquido
    • Top 25 Contributor
    • Joined on 05-22-2006, 12:31 AM
    • Australia
    • Posts 150
    • IIS MVPs
    • brashquido

    Re: PHP and MySQL: Can't Create Socket Error (5)

    Nice detail there! Just before other users go ahead and start trying this for themselves though, be sure you have a good grasp of Windows file permissioning and ensure you have backed up your system state BEFORE you start doing anything. You can seriously bork your environment by altering these default permission settings, and unless you have a good idea of what you are doing and keep track of everything you've changed (as above) it can be a very difficult and time consuming excercise to rectify.  

    ----------------
    Dominic Ryan
    Microsoft IIS MVP
    www.iis-aid.com
    My IIS articles
    My PHP articles
    My How-to guides
  • 05-24-2008, 6:32 PM In reply to

    • mvz
    • Not Ranked
    • Joined on 05-24-2008, 6:04 PM
    • Posts 1
    • mvz

    Re: PHP and MySQL: Can't Create Socket Error (5)

     I have faced the same problem with an application I wrote in a server to which I don't have access. As I have no control on the user/group management, the only workaround I found was to use named pipes to connect to MySQL (both IIS and MySQL are in the same server). This problem was very frustrating because I found little information searching the web and because at the beginning I could not replicate the problem in my test server. Somebody somewhere years ago mentioned that it may be a permissions-related problem and so I did what you did, tested adding and removing groups and users from the Users group until I could replicate the problem. That was a couple of weeks ago... at least now I know I am not alone. Thanks for sharing your findings, I'm sure now other persons will search and found more information about this problem with PHP's network connectivity under IIS with some hardening setups.

    By the way, I'm still writing my report on this problem,  so I'm still trying to nail down exactly what item is IUSR_ being denied to fail. If I found out, I'll add my findings here.

     Regards,

     MV


     

Page 1 of 1 (12 items)
Page view counter