Von Linux mit PHP auf einen MS-SQL Server zugreifen
Da ich nicht möchte, daß MS den Webserver- Markt auch noch erobert, habe ich mich damit beschäftigt, wie ich von Linux aus mit PHP und Apache auf die Microsoft SQL-Datenbank komme. Für viele Unternehmen ist ein Grund, den IIS einzusetzen, daß die Daten auf MS-SQL liegen.
Vorwort
Dies ist auch mit anderen linuxfreundlichen Datenbanken wie Oracle und DB2 möglich. Mann kann dann mehre Unterstützungen gleichzeitig in PHP einkompilieren!
Die Originalanleitung habe ich nach langem Suchen bei phpbuilder.com gefunden. Ich beschreibe hier nur eine Möglichkeit, welche bei mir schnell zum Erfolg führte und ich am einfachsten für mich fand, da ich nicht der C-Spezialist bin.
Tatsache ist, daß MS früher keine Datenbank hatte und sich von Sybase eine gekauft hat, welche natürlich mit der Sybase-eigenen Datenbank das gleiche Protokoll TDS hat. Sybase stellt die Datenbank auch für Linux zur Verfügung.
Vorbereitung
Um von Linux aus mittels PHP auf einen Microsoft SQL-Server zu connecten, besorgt man sich von Sybase diese Pakete:
- sybase-ase-11.9.2-3.i386.rpm
- sybase-common-11.9.2-3.i386.rpm
- sybase-openclient-11.1.1-3.i386.rpm
Das sind um die 50 MB! Die Pakete werden dann mit dem RPM in /opt/sybase-11.9.2 installiert. Eventuell muss man die Installation von einem Paket mittels
rpm -i --nodeps sybase-ase-11.9.2-3.i386.rpm
erzwingen.
Ich habe mir dann von SuSE noch die Quellen von Apache und mysql aufgespielt und mod_perl installiert.
Konfiguration
Dann editiert man in /opt/sybase-11.9.2 eine Datei mit dem Namen interfaces mit folgendem Aufbau, da sie leider fehlt:
# # This format of the interfaces file is this: # # The entry starts with the servername beginning in the first column (no # whitespace preceding it). # # Next comes the services lines. They must be preceded by some whitespace # (preferably a tab). There are five fields in the services line. # # 1) The service. Currently only "query" means anything "master" is present # for historical reasons only. # 2) The transport. Currently ignored by FreeTDS, it should be "tcp". When # ipv6 support is added a second option of "tcp6" will # probably be available. # 3) Physical layer. Sybase uses this field for a description of the physical # layer (usually "ether"), FreeTDS can also use it to # specify the version of the protocol to use when connecting # to this server. # 4) Hostname. May be a hostname or IP address of the server machine. # 5) Port. The TCP port of the dataserver. MS SQL defaults to 1433 # # So putting it all together, let's say you have an NT box running a SQL Server # named "myserver" at ip address 10.0.2.1 and we are connecting using version # 4.2 of the protocol, your interfaces entry would look # like this: (without the comments) # datenbankname query tcp tds7.0 192.168.150.11 1433 # (Wichtig: vor query muss ein TAB stehen)
In /etc/init.d/apache (wird im Artikel als apachectl bezeichnet) habe ich folgendes eingetragen:
# Erforderlich, um Microsoft-SQL mit PHP zu connecten SYBASE=/opt/sybase-11.9.2 LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/opt/sybase11.9.2/lib export SYBASE LD_LIBRARY_PATH
Installation von PHP 4
Jetzt kann nach dem Download und Entpacken des PHP-Sourcecodes (PHP 4.0pl2) ein
./configure --with-mysql --with-apxs --with-sybase-ct=/opt/sybase-11.9.2
abgesetzt werden. Dann make
und make install
und fertig.
Start
Nun noch Apache neu starten. Wenn nach
ps ax|grep http
als Ergebnis
/usr/sbin/httpd -f /etc/httpd/httpd.conf -D PERL -D PHP4
erscheint, ist alles in Ordnung.
Als Beispiel noch zwei PHP-Skripte, mit denen man auf der Datenbank herumbrowsen kann.