重启后,IBM DB2无法从PHP访问数据库

System: Windows 8

I have been working on a project for school using IBM DB2 Express-C, Xampp, and PHP to access the database.

Everything has been working perfectly. Then one day I rebooted my computer.

IBM Database Manager returned the following error at startup:

SQL5005C The operation failed because the database manager failed to access either the database manager configuration file or the database configuration file.

I have tried running db2stop and db2start.

I am still able to access my database via the db2 command line terminal as if nothing is wrong.

However, when I try to access my database from PHP I now get the following error from my PHP file:

58031 [IBM][CLI Driver] SQL1031N The database directory cannot be found on the indicated file system. SQLSTATE=58031 SQLCODE=-1031

You might want to double check the db2diag.log to find the reason for SQL5005C. In the better case there was a temporary problem accessing the configuration files. However, in the worse case one of the DBM or DB configuration files may have become corrupt. In the case of a corrupt DBM CFG, I usually use db2idrop/db2icrt. A corrupt DB CFG can be "fixed" (as in "reset", you will lose all custom changes) by RESET DB CFG.

The subsequent SQL1031N tells me the database you are trying to access has been uncataloged. This is also odd, especially since you are telling me you are able to access the database from the DB2 command line. You might want to do LIST DB DIRECTORY from your command line to show your database entries, and also LIST NODE DIRECTORY to see which nodes the databases reside on.

The possible reason is that you lost the database files on your hard disk.

  • Use command to check.

    db2diag -g db=database name > log.txt
    
  • open log.txt,you will see the message like below. In this case, I lost some db2 config files. enter image description here

  • Create missing folder and copy the db2 config files from the backup enter image description here

  • Using backup file to restore database.

When you try to interact with DB2 from a programming language (PHP, python or others) you use a library (example ibm_db for python) that interact with a db2 client application to connect/query your DB.

If you have more than one IBM Data Server client packages installed on your workstation you might not use the right one to access your DB.

So you have to verify which client your PHP api has using to do this job. To do this:

  1. identify your ../clidriver/bin folder that contains your db2 client utilities (in my case it was in my python installation folder/site-packages/ibm_db-2.0.8-py3.6.egg/clidriver/bin
  2. run command db2cli validate that will show you the client you are using
  3. If you have more than one entry in List of all IBM Data Server client packages on the current workstation: and you are not using one of the IBMDBCL_ client, you must switch the client by double clicking on db2swtch.exe (run as administrator) and following the visual instructions to switch to the IBMDBCL_ item
  4. if it doesn't work, uninstall and reinstall your library (in my case pip uninstall ibm_db and easy_install ibm_db)

You can find more information about the management of more db2 client on the same machine here