{"id":884,"date":"2024-01-30T14:12:34","date_gmt":"2024-01-30T13:12:34","guid":{"rendered":"https:\/\/www.meb.ki.se\/sites\/meb-it\/?p=884"},"modified":"2024-02-02T13:54:37","modified_gmt":"2024-02-02T12:54:37","slug":"how-to-set-up-odbc-access-to-kosmos","status":"publish","type":"post","link":"https:\/\/www.meb.ki.se\/sites\/meb-it\/how-to-set-up-odbc-access-to-kosmos\/","title":{"rendered":"How to set up ODBC access to kosmos"},"content":{"rendered":"\n<p>&#8230; with special attention to Mac and Linux users. On Windows installations this is already set up, as it is on the managed compute servers.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Prerequisites<\/h3>\n\n\n\n<p>You must have <code><strong>Kerberos<\/strong><\/code> installed and correctly configured to use ODBC to connect to kosmos. Macs come with Kerberos installed.<\/p>\n\n\n\n<p>If you are on a Mac, you need to have <code><strong>homebrew<\/strong><\/code> installed.<\/p>\n\n\n\n<p>We&#8217;ll show how to do this in a terminal window. For Mac users: help on Terminal <a href=\"https:\/\/support.apple.com\/guide\/terminal\/welcome\/mac\" data-type=\"link\" data-id=\"https:\/\/support.apple.com\/guide\/terminal\/welcome\/mac\" target=\"_blank\" rel=\"noreferrer noopener\">here<\/a>, and on editing text files <a href=\"https:\/\/support.apple.com\/guide\/terminal\/apdb02f1133-25af-4c65-8976-159609f99817\/2.14\/mac\/14.0\" target=\"_blank\" rel=\"noreferrer noopener\">here<\/a>. There is also a helper app for Kerberos in MacOS, called <code>Ticket Viewer<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 1: Download and install a ODBC Driver Manager<\/h2>\n\n\n\n<p>On the Mac &#8212; go to the <a href=\"#driver_installation\">next step<\/a>: the driver install as described will also install the driver manager as a dependency.<\/p>\n\n\n\n<p>On Linux &#8212; install the <code><strong>unixodbc<\/strong><\/code> package from your distribution.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"driver_installation\">Step 2: Download the Microsoft SQL Server driver <\/h2>\n\n\n\n<p><a href=\"https:\/\/learn.microsoft.com\/en-us\/sql\/connect\/odbc\/linux-mac\/install-microsoft-odbc-driver-sql-server-macos?view=sql-server-ver16\">https:\/\/learn.microsoft.com\/en-us\/sql\/connect\/odbc\/linux-mac\/install-microsoft-odbc-driver-sql-server-macos?view=sql-server-ver16<\/a> has instructions for both Linux and Mac.<\/p>\n\n\n\n<p>Once the installation is finished, you should be able to run  <code>odbcinst -j<\/code>. Run this in a terminal, and note the <code>DRIVERS<\/code> and the <code>USER DATA SOURCES<\/code> keywords; in my case the DRIVERS has the value <code>\/etc\/odbcinst.ini<\/code>. This will maybe be different in your machine.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash\">[staber@linux ~]$ odbcinst -j\nunixODBC 2.3.12\nDRIVERS............: \/etc\/odbcinst.ini\nSYSTEM DATA SOURCES: \/etc\/odbc.ini\nFILE DATA SOURCES..: \/etc\/ODBCDataSources\nUSER DATA SOURCES..: \/home\/staber\/.odbc.ini\nSQLULEN Size.......: 8\nSQLLEN Size........: 8\nSQLSETPOSIROW Size.: 8<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Step 3: Check\/edit the odbcinst.ini file<\/h2>\n\n\n\n<p>The DRIVERS configuration (<code>\/etc\/odbcinst.ini<\/code>) should have a stanza that defines the SQL Server ODBC driver:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash\">[staber@linux ~]$ cat \/etc\/odbcinst.ini \n[ODBC Driver 18 for SQL Server]\nDescription=Microsoft ODBC Driver 18 for SQL Server\nDriver=\/opt\/microsoft\/msodbcsql\/lib64\/libmsodbcsql-18.3.so.2.1\nUsageCount=1<\/code><\/pre>\n\n\n\n<p>(it may have many more &#8212; default locations for other drivers &#8212; ignore those!)<\/p>\n\n\n\n<p>The string in hooks (<code>[ODBC Driver 18 for SQL Server]<\/code>) is the <strong>name<\/strong> of the driver, and you&#8217;ll need it in the next step. The <strong>Driver<\/strong> value is the actual dynamic library file for the driver. This should have been filled in by the installation script. If it is not, then you will need to find it and edit the value to point at it.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 4: Edit the ~\/.odbc.ini file<\/h2>\n\n\n\n<p>The USER DATA SOURCES file will need to be created (&#8220;<code>touch ~\/.odbc.ini<\/code>&#8220;), and should have the following text:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash\">[staber@linux ~]$ cat ~\/.odbc.ini \n[kosmos]\nDriver = ODBC Driver 18 for SQL Server\nServer = meb-sql02.meb.ki.se\nTrustServerCertificate = yes\nTrusted_Connection = yes<\/code><\/pre>\n\n\n\n<p>This will make a DSN (<strong>D<\/strong>ata <strong>S<\/strong>ource <strong>N<\/strong>ame)  &#8220;<code><strong>kosmos<\/strong><\/code>&#8221; (in hooks at the top of the file) that connect with the Microsoft <code><strong>ODBC Driver 18 for SQL Server<\/strong><\/code> to the database server <code><strong>meb-sql02.meb.ki.se<\/strong><\/code>. It will use your MEB login (<code>Trusted_Connection = yes<\/code>) and it will trust the locally signed certificate from the server (<code>TrustServerCertificate = yes<\/code>).<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 5: test with isql and odbcinst<\/h2>\n\n\n\n<p>To access the database with your MEB user account (this is the default) you need a Kerberos ticket. Make one by saying <code>kinit<\/code> (possibly adding your username and the domain, if kinit doesn&#8217;t see the right one) , and giving your MEB password. <\/p>\n\n\n\n<p>There are two utilities included with the installation of the driver manager. One is <code>odbcinst<\/code> that we used above. It can be used for querying the installation for which drivers it knows about (<code>odbcinst -q -d)<\/code> or for what DSNs it knows (<code>odbcinst -q -s<\/code>).<\/p>\n\n\n\n<p><code>isql<\/code> is a small terminal query application. It is best used for checking your installation: if you say <code>isql -v kosmos<\/code> (with the setup as described) it should give you a <code>SQL&gt; <\/code> prompt:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash\">[staber@linux ~]$ kinit \nPassword for staber@MEB.KI.SE: \n[staber@linux ~]$ isql -v kosmos\n+---------------------------------------+\n| Connected!                            |\n|                                       |\n| sql-statement                         |\n| help [tablename]                      |\n| echo [string]                         |\n| quit                                  |\n|                                       |\n+---------------------------------------+\nSQL&gt; <\/code><\/pre>\n\n\n\n<p>If the connection does not work, you&#8217;ll get a descriptive error message &#8212; for instance, if you misspell the DSN, then this is what you get:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash\">[staber@linux ~]$ isql -v cosmos\n[IM002][unixODBC][Driver Manager]Data source name not found and no default driver specified\n[ISQL]ERROR: Could not SQLConnect<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Special tweaks<\/h2>\n\n\n\n<p>The DSN specification (in <code>~\/.odbc.ini<\/code>) can take more parameters (list <a href=\"https:\/\/learn.microsoft.com\/en-us\/sql\/connect\/odbc\/dsn-connection-string-attribute?view=sql-server-ver16\">here<\/a>); the most common one is <code>Database<\/code>, where you can specify which database to connect to.  <\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8230; with special attention to Mac and Linux users. On Windows installations this is already set up, as it is on the managed compute servers. Prerequisites You must have Kerberos installed and correctly configured to use ODBC to connect to kosmos. Macs come with Kerberos installed. If you are on a Mac, you need to [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[11,33,32,22],"tags":[],"class_list":["post-884","post","type-post","status-publish","format-standard","hentry","category-dba","category-linux","category-mac","category-sql"],"blocksy_meta":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.meb.ki.se\/sites\/meb-it\/wp-json\/wp\/v2\/posts\/884","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.meb.ki.se\/sites\/meb-it\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.meb.ki.se\/sites\/meb-it\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.meb.ki.se\/sites\/meb-it\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.meb.ki.se\/sites\/meb-it\/wp-json\/wp\/v2\/comments?post=884"}],"version-history":[{"count":15,"href":"https:\/\/www.meb.ki.se\/sites\/meb-it\/wp-json\/wp\/v2\/posts\/884\/revisions"}],"predecessor-version":[{"id":904,"href":"https:\/\/www.meb.ki.se\/sites\/meb-it\/wp-json\/wp\/v2\/posts\/884\/revisions\/904"}],"wp:attachment":[{"href":"https:\/\/www.meb.ki.se\/sites\/meb-it\/wp-json\/wp\/v2\/media?parent=884"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.meb.ki.se\/sites\/meb-it\/wp-json\/wp\/v2\/categories?post=884"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.meb.ki.se\/sites\/meb-it\/wp-json\/wp\/v2\/tags?post=884"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}