UnixODBC CLI Install and Configuration
Posted by mpapet on Wed 7 Feb 2007 at 12:55
For those of you that may not know what unixodbc does, "ODBC is an open specification for providing application developers with a predictable API with which to access Data Sources. Data Sources include SQL Servers and any Data Source with an ODBC Driver." They include a text file driver as an example of a non-SQL source. Two examples are Asterisk and OpenOffice.org.
Unixodbc allows cross-platform use of databases with many bridges available in many popular programming languages.
Installing and configuring isn't necessarily as simple as "apt-get install unixodbc". This HOWTO was written on Etch and may vary if you are using a different version.
Step 1: Download Packages
Running: apt-get install unixodbc libmyodbc odbc-postgresql \ odbcinst1debian1
will get you the ODBC binaries, database drivers for MySQL and PostgreSQL and a Debian helper application for ODBC respectively.
Step 2: Add odbcinst.ini Records As the root user, check for two empty text files /etc/odbc.ini and /etc/odbcinst.ini. If they aren't there, then create them. (eg. "touch /etc/odbcinst.ini") Create a directory for the odbcinst.ini scripts:
Step 3: Adding ODBC Instances In order to minimize entry-error, I use text files to load ODBC instances. Odbcinst's error messages are a bit cryptic so I control errors using a separate file for each database type. As root create a file name pgsql and paste the following into it.
If you are on another version double-check your library names.
Name the file pgsql so the following command should work.
You can create another file for MySQL with the following contents: To use it run, as root: "odbcinst -i -d -f /home/username/odbc/mysql". With PostgreSQL and MySQL done, lets create a sample ODBC connection!
Step 4: Create ODBC Connection Create another text file and copy the text below into it. I called it asterisk. Copy the contents of this file into odbc.ini. (Note, in theory "odbcinst -i -s -f /path/to/file" writes the contents of the named file into /etc/odbc.ini. I could not get it to do so and there were no error messages) So, copy and paste the following into odbc.ini too Make sure your database permissions are configured to allow the login/connection before testing. Step 5 Test Connection: As the root enter run: This should put you into the asterisk database! The unixodbc site was very helpful to me. Check the manuals page for useful how-to's and additional driver configuration information.
username@host:~$ mkdir /home/username/odbc
[PostgreSQL]
Description = PostgreSQL driver for Linux & Win32
Driver = /usr/lib/odbc/psqlodbca.so
Setup = /usr/lib/odbc/libodbcpsqlS.so
FileUsage = 1
odbcinst -i -d -f /home/username/odbc/pgsql
[MySQL]
Description = MySQL driver for Linux & Win32
Driver = /usr/lib/odbc/libmyodbc3_r-3.51.11.so
Setup = /usr/lib/odbc/libodbcmyS.so
FileUsage = 1
[asterisk]
Description = MySQL Asterisk
Driver = MySQL
SERVER = localhost
USER = username
PASSWORD = password
PORT = 3306
DATABASE = asterisk
Option = 3
isql asterisk
[ Send Message | View dkg's Scratchpad | View Weblogs ]
FWIW, in debian the odbc-postrgresql package contains template files for use with the odbc utilities:
[0 dkg@marmoset ~]$ dpkg -L odbc-postgresql | grep ini /usr/share/doc/odbc-postgresql/examples/odbc.ini.template /usr/share/psqlodbc/odbcinst.ini.template [0 dkg@marmoset ~]$ cat /usr/share/psqlodbc/odbcinst.ini.template [PostgreSQL ANSI] Description = PostgreSQL ODBC driver (ANSI version) Driver = /usr/lib/odbc/psqlodbca.so Setup = /usr/lib/odbc/libodbcpsqlS.so Debug = 0 CommLog = 1 [PostgreSQL Unicode] Description = PostgreSQL ODBC driver (Unicode version) Driver = /usr/lib/odbc/psqlodbcw.so Setup = /usr/lib/odbc/libodbcpsqlS.so Debug = 0 CommLog = 1 [0 dkg@marmoset ~]$So you shouldn't need to create your own from scratch when doing the install.
[ Parent | Reply to this comment ]
[ Parent | Reply to this comment ]
I agree that there's not enough error condition displayed on connection failures. So, how about offering a bounty to have it coded by the maintainer?
The package might have limitations, but hey, it's free and works very nicely once you sort out connection issues.
[ Parent | Reply to this comment ]
Secondly, you need to make sure that mysql is listening where you think it is; I absent-mindedly told ODBC to look in /var/run/mysql/mysql.sock, which isn't where it was. If you're specifying a port number make sure that skip-networking is turned off.
Just a couple of thoughts.
-- cheesey
[ Parent | Reply to this comment ]
[IM002][unixODBC][Driver Manager]Data source name not found, and no default driver specified.
My odbc.ini is as follows:
[prosodia-db]
Description = MySQL Prosodia
Driver = MySQL
SERVER = localhost
USER = username
PASSWORD = password
PORT = 3306
DATABASE = prosodiadb
Option = 3
I can log in mysql using the username, password and database in odbc.ini (but the database has no tables, as I'm just testing it).
According to the MySQL documentation, "The error (2002) Can't connect to ... normally means that there is no MySQL server running on the system or that you are using an incorrect Unix socket filename or TCP/IP port number when trying to connect to the server." using ps -e tells both mysqld and mysqld_safe are running. I don't know how to check the socket filename ODBC is looking for, but the port seems correct...
Any ideas? Thanks in advance!
[ Parent | Reply to this comment ]
ReadOnly = No
Now it connects!
[ Parent | Reply to this comment ]
[ Send Message | View ronin42's Scratchpad | View Weblogs ]
The line to remind one to make sure their database permissions are correct to allow access is good reminder but for those of us who are not sure where or how to do that a link or explanation would be helpfull.
Not being snippy just a bit frustrated as I can login as root but not as user.
I have added myself to the mysql group and created the account within mysql through root but can't get in on the command line.
I have tried using the gui tool but it fails and says I am missing libmyodbc3_r-3.51.11.so. I google that lib and it brought me to this page.
[ Parent | Reply to this comment ]
mysql -u root
That should put you into mysql where you need to do some other things, like assign a password to root, create a non-root mysql account (with password) that interacts with the application database.
use google and the following search phrases, "mysql add user" "mysql root password" mysql docs should be on top of the list.
What you need strays gets into basic mysql administration which is outside the scope of setting up ODBC.
[ Parent | Reply to this comment ]