Configure unixodbc and freetds on Linux to access ms SQL Server

Source: Internet
Author: User
Tags dsn

1. Install unixodbc

Download the installation package.
UnixODBC-2.2.11-1.RHEL4.1.i386.rpm
UnixODBC-devel-2.2.11-1.RHEL4.1.i386.rpm
UnixODBC-kde-2.2.11-1.RHEL4.1.i386.rpm

Install
Rpm-uvh unixODBC-2.2.11-1.RHEL4.1.i386.rpm
Rpm-uvh unixODBC-devel-2.2.11-1.RHEL4.1.i386.rpm
If you are prompted to have dependencies on other installation packages during installation, follow the prompts to install
Packages.

Or install unixodbc from the source code.
Download source code set
UnixODBC-2.2.11.tar.gz
# Tar xzf unixODBC-2.2.11.tar.gz
# Cd unixODBC-2.2.11
#./Configure
# Make
# Make install

./Configure may also prompt that some dependent libraries cannot be found.
Download the source code, compile, and install these dependent libraries.

The difference between RPM installation and source code compilation and installation lies in the configuration file ODBC. ini and odbcinst. ini.
The former is/etc, and the latter is/usr/local/etc.

2. Install freetds

Unixodbc provides support for ODBC in Linux, but it is only an ODBC manager and needs to be connected
The actual database must provide the ODBC driver for this database.

Freetds is a free ODBC driver used in Linux to connect ms SQL Server and Sybase.
Note: you must first install unixodbc and then freetds.

Freetds-0.64 is currently the latest stable version.
Download source package freetds-stable.tgz
# Tar xzf freetds-stable.tgz
#./Configure -- With-unixodbc =/usr/local -- With-tdsver = 8.0
# Make
# Su
# Make install
Install to/usr/local

If unixodbc is installed by source code
-- With-unixodbc =/usr/local
If unixodbc is installed at rpm
This option is not required

3. Configure unixodbc and freetds

3.1 register freetds driver with unixodbc
Modify/etc/odbcinst. ini (or/usr/local/etc/odbcinst. INI)
Add
=========================================/Etc/odbcinst. INI =
# Driver from freetds package
# Setup from freetds package
[Freetds]
Description = ODBC of freetds for ms SQL 2000
Driver =/usr/local/lib/libtdsodbc. So
Setup =/usr/local/lib/libtds. So
Fileusage = 1

========================================================== ======================================

3.2 add access information pointing to a specific database in the freetds configuration file
Modify/usr/local/etc/freetds. conf
Add the following content at the end of the configuration file. Read the original content carefully. This is a good teaching material.
========================================================== ======================================
[Mysqlserver]
Host = 192.168.0.5; IP address or domain name of the ms SQL Server
Port = 1433
TDS version = 8.0
Client charset = UTF-8; character set that can be recognized by client software.
========================================================== ======================================
Note that the client charset must be greater than or equal to the character set used by the server database.
For example, the server database is ms SQL Server 2000 and the character set is gb2312.
Then the client charset can be set to gb2312 (equal to), gb18030 (greater than), UTF-8 (greater)
But cannot be set to ISO-8859-1 (less than), shift_jis (not equal ).
Since the UTF-8 is the superset of the character set, it is always feasible to set it to the UTF-8.
In addition, the freetds client charset cannot be set to utf16. at present, the limitations of the software design are as follows,
According to its official website, support for client charset UTF16 may be added in future versions.

3.3 Modify/etc/ODBC. ini (if ODBC is installed from the source code,/usr/local/etc/ODBC. INI)
Add DSN.
===========================================/Etc/ODBC. INI =
[ODBC data sources]
Test1dsn = my first test DSN
Test2dsn = my second test DSN

[Test1dsn]; DSN name
Driver =/usr/local/lib/libtdsodbc. So
Description = my first test DSN
Trace = No
Servername = mysqlserver; defined in freetds. conf
Database = mytestdb; Database Name

[Test2dsn]
Driver =/usr/local/lib/libtdsodbc. So
Description = my second test DSN
Trace = No
Server = 192.168.0.5; you can directly write the access information of the database server.
Database = mytestdb
Port = 1433
Tds_version = 8.0

[Default]
Driver =/usr/local/freetds/lib/libtdsodbc. So
========================================================== ======================================

Note:
There are two configuration methods to access ms SQL Server through freetds with unixodbc.

(1) write the server information in the configuration file $ prefix/etc/freetds. conf of freetds,
In/etc/ODBC. ini, servername is used to point to the DSN set in freetds. conf.
In the above example, [test1dsn]
(2). Another method is to write the server information in/etc/ODBC. ini. [test2dsn] in the preceding example.
Note that the keywords are different. For example, the TDS version in freetds. conf is
In/etc/ODBC. ini, tds_version is used.

Method (2) is relatively simple, but there are only a few keywords to control freetds.
For other features, use the default configuration of freetds.

Method (1) Although complicated, freetds can be controlled in more detail. For example, you can specify a client
Character Set.

Recommended method (1) for configuration.

4. Access the database:

Whether using client software or programming to access the database, three parameters are usually provided.
DSN, username, password.
For example,
DSN = test1dsn or test2dsn
Username = somename,
Password = somepasswd,

It means to access the ms SQL Server library mytestdb at 192.168.0.5. query results
Character Set for UTF-8.

A universal GUI Database Connection Client provided by unixodbc is datamanager.

V. Character Set:

Freetds can automatically identify the charset on the server. Therefore, freetds requires you to set
Charset. This is the charset used by the client application to obtain data from freetds.
Once the client charset is set, freetds implements
.

If both client applications have access to the same ms SQL Server, but unfortunately, both client applications
The accepted character sets are UTF-8 and gb2312. then the solution is to set in freetds. conf
Two sets of datasource, whose server settings are the same, but the client charset is set to UTF-8 and gb2312.
ODBC. INI also sets two different DSN groups to point to the two datasource groups respectively.
Use the corresponding DSN respectively.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.