Cấu hình MySQL với unixODBC trên Ubuntu 14.04

Install ODBC drivers:

sudo apt-get install libmyodbc unixodbc-bin

Lookup the ODBC driver’s location:

find / -name 'lib*odbc*.so'

OUTPUT (EXAMPLE):
/usr/lib/i386-linux-gnu/odbc/libodbcnnS.so
/usr/lib/i386-linux-gnu/odbc/libodbctxtS.so
/usr/lib/i386-linux-gnu/odbc/libodbcdrvcfg1S.so
/usr/lib/i386-linux-gnu/odbc/libodbcmyS.so
/usr/lib/i386-linux-gnu/odbc/liboraodbcS.so
/usr/lib/i386-linux-gnu/odbc/libodbcpsqlS.so
/usr/lib/i386-linux-gnu/odbc/liboplodbcS.so
/usr/lib/i386-linux-gnu/odbc/libodbcminiS.so
/usr/lib/i386-linux-gnu/odbc/libmyodbc.so
/usr/lib/i386-linux-gnu/odbc/libodbcdrvcfg2S.so

Create your custom ODBC installation file:

sudo gedit /etc/odbcinst.ini

Put this in your odbcinst.ini:

[myodbc_mysql]
Description     = ODBC for MySQL
Driver          = /usr/lib/i386-linux-gnu/odbc/libmyodbc.so
Setup           = /usr/lib/i386-linux-gnu/odbc/libodbcmyS.so
UsageCount      = 1

Lookup the MySQL socket path:

mysqladmin -u root -p version

OUTPUT (EXAMPLE):
Server version 5.5.37-0ubuntu0.14.04.1
Protocol version 10
Connection Localhost via UNIX socket
UNIX socket /var/run/mysqld/mysqld.sock
Uptime: 7 hours 14 min 15 sec

Create your ODBC configuration file:

sudo gedit /etc/odbc.ini

Put this in your odbc.ini:

[myodbc_mysql_dsn]                        # this is your system DSN
Description = description of your DSN
Driver      = myodbc_mysql                # custom driver name
Server      = localhost                   # or external IP if needed
Port        = 3306                        # or custom port if needed
Socket      = /var/run/mysqld/mysqld.sock # socket, see above
Database    = mysqldb1                    # MySQL DB name or empty
Option      = 3
ReadOnly    = No

Install the ODBC driver:

sudo odbcinst -i -d -f /etc/odbcinst.ini

Install your system DSN:

sudo odbcinst -i -s -l -f /etc/odbc.ini

Test if your system DSN was installed successfully:

odbcinst -s -q

OUTPUT (EXAMPLE):
[myodbc_mysql_dsn]

Test if you can connect to your MySQL database via ODBC:

isql -v myodbc_mysql_dsn root admin

OUTPUT (EXAMPLE):
+---------------------------------------+
| Connected!                            |
|                                       |
| sql-statement                         |
| help [tablename]                      |
| quit                                  |
|                                       |
+---------------------------------------+

If get error, [IM002][unixODBC][Driver Manager]Data source name not found, and no default driver specified
[ISQL]ERROR: Could not SQLConnect

It’s because of, in odbc.ini the comments caused some problems. We modify odbc.ini

[myodbc_mysql_dsn]
Description = description of your DSN
Driver      = myodbc_mysql
Server      = localhost
Port        = 3306
Socket      = /var/run/mysqld/mysqld.sock
Database    = mysqldb1
Option      = 3
ReadOnly    = No
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s