Home > News list > Programming/Database >> Database Operation Tutorial

Steps to connect to a SQL Server database through Linux installation of ODBC

Database Operation Tutorial 2023-05-12 12:13:06 Source: Network

catalogue1. Download the ODBC installation package2

Operating system: Centos7.2

1. Download the ODBC installation package

unixODBC-2.3.9.tar.gz

freetds-1.3.9.tar.gz

Download these two compressed packages at:
ftp://ftp.freetds.org/pub/freetds/stable/
ftp://ftp.unixodbc.org/pub/unixODBC/

2. Installation steps

Step 1: Upload two installation packages to the server

Step 2: Install unixODBC, first install this, and then install freetds

Note: If the installation sequence is inconsistent here, the libtdsodbc.so driver file will not be generated (very important)

2.1. Install gcc for the operating system first

yuminstallgcc

2.2. Installing ODBC

tar-xvfunixODBC-2.3.9.tar.gzcdunixODBC-2.3.9./configure--prefix=/usr/local/unixODBCmakemakeinstall

2.3. Installing Freetds

tar-xvffreetds-1.3.9.tar.gzcdfreetds-1.3.9./configure--prefix=/usr/local/freetds--with-unixodbc=/usr/local/unixODBC/makemakeinstall

2.4. Configuring the Freetds driver

catalogue/usr/local/freetds/etc/freetds.conftds version7.3hostportSQLServer

[SQLServer]host=serveripport=1433tdsversion=7.3

2.5. Test whether the configuration is correct

/usr/local/freetds/bin/tsql-S  SQLServer - Uusername to connect to SQL Server (where - S specifies the already configured TDS service name and - U specifies the username for logging into the database), and successful connection proves that the driver configuration is correct. If there is an error in the connection, it is likely because the tdsversion setting is incorrect. You can try modifying the value to reconnect

/usr/local/freetds/bin/tsql-SSQLServer-UsaPassword:localeis"zh_CN.UTF-8"localecharsetis"UTF-8"usingdefaultcharset"UTF-8"1>

2.6. Configuring UnixODBC

Specify the location of the database driver and the authentication information required to connect to the database for Unixodbc. Modify the specific values in the following configuration information according to the actual situation, and configure the driver in/usr/local/unixODBC/etc/odbcinst. ini

[FreeTDS] # Driver Name Description=FreeTDSDriverDriver=/usr/local/freetds/lib/libtdsodbc. so # Driver Location

/Configure in usr/local/unixODBC/etc/odbc. ini

[msSQL] # DSN Name Description=sqlserverconnectionDriver=FreeTDS # The configured driver name TDS_ Version=7.3 # TDS driver version TRACE=On # Whether to output log information TraceFile=stderr # Location of log information Server=serverip # Database network address Port=1433 # Database port User=name # Login username Password=pwd # Password Database=dbname # Database name to connect to

2.7. Test whether it is possible to connect to the database

/usr/local/unixODBC/bin/isqlmssqlnamepwd+---------------------------------------+|Connected!|||| sql-statement||help[tablename]||quit|||+---------------------------------------+SQL> select*fromsys.objects+---------------------------------------------------------------------------------------------------------------------------------+------------+-------------+------------+-----------------+-----+-------------------------------------------------------------+------------------------+------------------------+--------------+-------------+--------------------+|name|object_ id|principal_ id|schema_ id|parent_ object_ id|type|type_ desc|create_ date|modify_ date|is_ ms_ shipped|is_ published|is_ schema_ published|

Note: Since we have already configured the connection database name in the configuration file, do not include the database name when querying SQL, otherwise an error will be reported.

  2.8. Shell Script Connection to Database

SQL="/usr/local/unixODBC/bin/isqlmssqlsa123"i_ sql="execdbo.bulkinsert10,20,'dbo.stu';"echo$i_ sqlresult=`echo$i_ sql|$SQL`

  Method 2:

SQL="select*fromStudent"EMPLOYEES=`isqlmssqlsa123<& lt; EOF$SQLgoEOF

Method 3:

echo"$SQL">$ TMP/sql.$$ EMPLOYEES=`isqlmssqlsa123-i$TMP/sql.$$` rm$TMP/sql.$$

  2.9. Calling commands to execute stored procedures on machines with SQL Server installed

osql.exe-Sip-Usa-P123-dWAI-Q"execdbo.bulkinsert%start%,%end%,stu

That's all for this article about installing Linux and connecting to SQL Server databases through ODBC. For more information on connecting to SQL Server databases through ODBC, please search for previous articles or continue browsing related articles below. We hope everyone can support Script Home more in the future!

Tag: to Steps connect SQL Server database through Linux installation


Disclaimer: The content of this article is sourced from the internet. The copyright of the text, images, and other materials belongs to the original author. The platform reprints the materials for the purpose of conveying more information. The content of the article is for reference and learning only, and should not be used for commercial purposes. If it infringes on your legitimate rights and interests, please contact us promptly and we will handle it as soon as possible! We respect copyright and are committed to protecting it. Thank you for sharing.

AdminSo

http://www.adminso.com

Copyright @ 2007~2024 All Rights Reserved.

Powered By AdminSo

Open your phone and scan the QR code on it to open the mobile version


Scan WeChat QR code

Follow us for more hot news

AdminSo Technical Support