{"id":206,"date":"2011-08-11T21:02:27","date_gmt":"2011-08-12T02:02:27","guid":{"rendered":"http:\/\/guywyant.info\/log\/?p=206"},"modified":"2020-06-02T00:48:03","modified_gmt":"2020-06-02T05:48:03","slug":"connecting-to-ms-sql-server-from-ubuntu","status":"publish","type":"post","link":"http:\/\/guywyant.info\/log\/206\/connecting-to-ms-sql-server-from-ubuntu\/","title":{"rendered":"Connecting to MS SQL Server from Ubuntu"},"content":{"rendered":"<p>And now, in a break from the previous trend of fluffy posts, we have a tutorial on how to (deep breath): connect PHP to a MSSQL Server 2008 instance over ODBC from Ubuntu Linux using the FreeTDS driver and unixODBC. Theoretically it would also work for SQL Server 2005.<\/p>\n<p>I don&#8217;t know whether half of the settings flags are necessary or even correct, but what follows Worked for Me&#x2122;, YMMV, etc, etc.<\/p>\n<p>In the commands below, I\u2019ll use 192.168.0.1 as the server housing the SQL Server instance, with a SQL Server user name of <em>devuser<\/em>, password <em>devpass<\/em>. I\u2019m assuming SQL Server is set up to listen on its default port, 1433. Keep an eye out, because you\u2019ll need to change these things to your own settings.<\/p>\n<p>First, install unixODBC:<br \/>\n<code>sudo apt-get install unixodbc unixodbc-dev<\/code><br \/>\nI also installed the following (perhaps necessary) packages:<br \/>\n<code>sudo apt-get install tdsodbc php5-odbc<\/code><br \/>\nThen download, untar, compile, and install FreeTDS (warning, the URL may change):<br \/>\n<code>cd \/usr\/local<br \/>\nwget http:\/\/ibiblio.org\/pub\/Linux\/ALPHA\/freetds\/stable\/freetds-stable.tgz<br \/>\ntar xvfz freetds-stable.tgz<br \/>\ncd freetds-0.82<br \/>\n.\/configure --enable-msdblib --with-tdsver=8.0 --with-unixodbc=\/usr<br \/>\nmake<br \/>\nmake install<br \/>\nmake clean<br \/>\n<\/code><br \/>\nAttempt a connection over Telnet to your SQL Server instance:<br \/>\n<code>telnet 192.168.0.1 1433<\/code><\/p>\n<p>Use the tsql tool to test out the connection:<br \/>\n<code>tsql -S 192.168.0.1 -U devuser<\/code><\/p>\n<p>This should prompt you for the password, after which you can <em>hope against hope<\/em> to see this beautiful sign:<br \/>\n<code>1&gt;<\/code><\/p>\n<p>If that worked, I recommend throwing a (coding) party. Next up is some configging. Open the FreeTDS config file.<br \/>\n<code> \/usr\/local\/etc\/freetds.conf<\/code><\/p>\n<p>Add the following entry to the bottom of the file. We&#8217;re setting up a datasource name (DSN) called &#8216;MSSQL&#8217;.<br \/>\n<code>[MSSQL]<br \/>\nhost = 192.168.0.1<br \/>\nport = 1433<br \/>\ntds version = 8.0<\/code><\/p>\n<p>Now open the ODBC configuration file:<br \/>\n<code> \/usr\/local\/etc\/odbcinst.ini<\/code><\/p>\n<p>And add the following MSSQL driver entry (FreeTDS) at the end:<br \/>\n<code>[FreeTDS]<br \/>\nDescription = FreeTDS driver<br \/>\nDriver = \/usr\/local\/lib\/libtdsodbc.so<br \/>\nSetup=\/usr\/lib\/odbc\/libtdsS.so<br \/>\nFileUsage = 1<br \/>\nUsageCount = 1 <\/code><\/p>\n<p>Then, finally, set up the DSN within ODBC in the odbc.ini file here<br \/>\n<code>\/usr\/local\/etc\/odbc.ini<\/code><br \/>\nBy adding this bit to the file:<br \/>\n<code>[MSSQL]<br \/>\nDescription = MS SQL Server<br \/>\nDriver = \/usr\/local\/lib\/libtdsodbc.so<br \/>\nServer = 192.168.2.3<br \/>\nUID = devuser<br \/>\nPWD = devpass<br \/>\nReadOnly = No<br \/>\nPort = 1433<\/code><\/p>\n<p>Test out the connection using the isql tool:<br \/>\n<code>isql -v MSSQL devuser 'devpass'<\/code><br \/>\nIf you see &#8220;Connected!&#8221; you&#8217;re golden, congratulations! If not, I&#8217;m truly sorry; see below where there are some resources that might help.<\/p>\n<p>Now restart Apache and test it from PHP using &#8216;MSSQL&#8217; as the DSN. If something doesn&#8217;t work, you might try installing any or all of these packages:<br \/>\n<code>mdbtools libmdbodbc libmdbtools mdbtools-gmdb<\/code><\/p>\n<p>Here are some other resources that were helpful to me through this disastrous journey.<\/p>\n<ul>\n<li><a href=\"http:\/\/www.linuxjournal.com\/article\/6636\">Nice step-by-step at LinuxJournal<\/a><\/li>\n<li><a href=\"https:\/\/bugs.php.net\/bug.php?id=8151\">Sample installation (in a bug report!)<\/a><\/li>\n<li><a href=\"https:\/\/secure.kitserve.org.uk\/content\/accessing-microsoft-sql-server-php-ubuntu-using-pdo-odbc-and-freetds\">Accessing Microsoft SQL Server from PHP<\/a> (I found this after I was done, I wish I had earlier! He has luck getting it running simply by installing packages, which wouldn&#8217;t ever work for me.)<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>And now, in a break from the previous trend of fluffy posts, we have a tutorial on how to (deep breath): connect PHP to a MSSQL Server 2008 instance over ODBC from Ubuntu Linux using the FreeTDS driver and unixODBC. &hellip; <a href=\"http:\/\/guywyant.info\/log\/206\/connecting-to-ms-sql-server-from-ubuntu\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"http:\/\/guywyant.info\/log\/wp-json\/wp\/v2\/posts\/206"}],"collection":[{"href":"http:\/\/guywyant.info\/log\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/guywyant.info\/log\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/guywyant.info\/log\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/guywyant.info\/log\/wp-json\/wp\/v2\/comments?post=206"}],"version-history":[{"count":7,"href":"http:\/\/guywyant.info\/log\/wp-json\/wp\/v2\/posts\/206\/revisions"}],"predecessor-version":[{"id":257,"href":"http:\/\/guywyant.info\/log\/wp-json\/wp\/v2\/posts\/206\/revisions\/257"}],"wp:attachment":[{"href":"http:\/\/guywyant.info\/log\/wp-json\/wp\/v2\/media?parent=206"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/guywyant.info\/log\/wp-json\/wp\/v2\/categories?post=206"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/guywyant.info\/log\/wp-json\/wp\/v2\/tags?post=206"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}