nodejs的ODBC連接解決方案
如需轉載請標明出處:http://blog.csdn.net/itas109
QQ技術交流羣:129518033
環境:
windows:
OS:win7 64bit
Node:10.15.3 64bit
node-gyp:6.0.0
編譯器:vs2015
python:2.7.5
async: 3.0.1(odbc依賴)
node-addon-api: 1.7.1(odbc依賴)
odbc:2.2.1
postgresql:10.10
Linux:
OS:deepIn 15.11
Node:10.15.3 64bit
node-gyp:6.0.0
編譯器:gcc 6.3.0 20170516
python:2.7.5
unixodbc:2.3.7
async: 3.0.1(odbc依賴)
node-addon-api: 1.7.1(odbc依賴)
odbc:2.2.1
postgresql:10.10
1.windows下odbc
1.1 安裝node和node-gyp
略
1.2 安裝nodejs的odbc模塊
npm i odbc
或者
cnpm i odbc
1.3 設置odbc數據源
這裏以PostgreSQL爲例。
1.4 測試代碼
const odbc = require('odbc');
async function queryDB() {
const connectionConfig = {
connectionString: 'DSN=PostgreSQL30',
connectionTimeout: 10,
loginTimeout: 10,
}
const connection = await odbc.connect(connectionConfig);
const result = await connection.query('SELECT * FROM student;');
console.log(JSON.stringify(result));// [{"name":"ZhangSan","age":18}]
}
queryDB();
2.Linux下odbc
2.1 安裝node和node-gyp
略
2.2 安裝nodejs的odbc模塊
npm i odbc
或者
cnpm i odbc
2.3 安裝unixodbc
2.3.1 下載unixodbc
MD5:274a711b0c77394e052db6493840c6f9
2.3.2 編譯安裝unixodbc
./configure --prefix=/usr/local/unixODBC-2.3.7 --includedir=/usr/include --libdir=/usr/lib --bindir=/usr/bin --sysconfdir=/etc
make -j 8
make install
2.3.3 unixodbc測試
odbcinst -j
注意:
這裏由於動態庫安裝路徑找不到的問題。一種處理就是按照上面的./configure 參數配置,再有一種就是軟鏈接
odbcinst -j
odbcinst: error while loading shared libraries: libodbcinst.so.2: cannot open shared object file: No such file or directory
2.4 PostgreSQL的ODBC驅動安裝
2.4.1 psqlodbc-10.03源碼編譯
下載地址:
psqlodbc-10.03
sudo ./configure --with-libpq=/opt/PostgreSQL/10
sudo make -j 8
sudo make install
其中,–with-libpq參數爲pg安裝根目錄,默認路徑爲/opt/PostgreSQL/10
2.4.2 命令安裝
sudo apt-get install odbc-postgresql
其他:
mysql
sudo apt-get install libmyodbc
2.5 設置odbc數據源
2.5.1 手動配置
- /usr/local/etc/odbcinst.ini配置
[PostgreSQL]
Description = ODBC for PostgreSQL
Driver = /usr/lib/x86_64-linux-gnu/odbc/psqlodbca.so
Setup = /usr/lib/x86_64-linux-gnu/odbc/libodbcpsqlS.so
FileUsage = 1
驗證:
$ odbcinst -q -d
[PostgreSQL]
- /usr/local/etc/odbc.ini系統數據源配置
[PostgreSQL30]
Description = PostgreSQL connection
Driver = PostgreSQL
Database = test
Servername = localhost
UserName = postgres
Password = 123456
Port = 5432
Protocol = 8.1
ReadOnly = No
RowVersioning = No
ShowSystemTables = No
ShowOidColumn = No
FakeOidIndex = No
ConnSettings =
驗證:
$ isql PostgreSQL30
+---------------------------------------+
| Connected! |
| |
| sql-statement |
| help [tablename] |
| quit |
| |
+---------------------------------------+
SQL>
注意:
如果不是連接Linux本機的PostgreSQL數據庫,需要驗證Linux中是否能夠正常連接PostgreSQL。
PostgreSQL默認只能本地連接。
例如,出現
$ isql PostgreSQL301
[ISQL]ERROR: Could not SQLConnect
可以嘗試使用pgAdmin 4進行連接,可以查看到更多錯誤。
2.5.2 gui圖像化配置
unixODBC-GUI-Qt
暫時略過
2.6 測試代碼
const odbc = require('odbc');
async function queryDB() {
const connectionConfig = {
connectionString: 'DSN=PostgreSQL30',
connectionTimeout: 10,
loginTimeout: 10,
}
const connection = await odbc.connect(connectionConfig);
const result = await connection.query('SELECT * FROM student;');
console.log(JSON.stringify(result));// [{"name":"ZhangSan","age":18}]
}
queryDB();
3.Installing and Configuring ODBC博文備份
Installing and Configuring ODBC
The ODBC connector is a database abstraction layer that makes it possible for Asterisk to communicate with a wide range of databases without requiring the developers to create a separate database connector for every database Asterisk wants to support. This saves a lot of development effort and code maintenance. There is a slight performance cost, because we are adding another application layer between Asterisk and the database, but this can be mitigated with proper design and is well worth it when you need powerful, flexible database capabilities in your Asterisk system.
Before you install the connector in Asterisk, you have to install ODBC into Linux itself. To install the ODBC drivers, use one of the following commands.
On CentOS:
$ sudo yum install unixODBC unixODBC-devel libtool-ltdl libtool-ltdl-devel
If you’re using a 64-bit installation, remember to add .x86_64 to the end of your development packages to make sure the i386 packages are not also installed, as stability problems can result if Asterisk links against the wrong libraries.
On Ubuntu:
$ sudo apt-get install unixODBC unixODBC-dev
See Chapter 3, Installing Asterisk for the matrix of packages you should have installed.
You’ll also need to install the unixODBC development package, because Asterisk uses it to build the ODBC modules we will be using throughout this chapter.
The unixODBC drivers shipped with distributions are often a few versions behind the officially released versions on the http://www.unixodbc.org website. If you have stability issues while using unixODBC, you may need to install from source. Just be sure to remove the unixODBC drivers via your package manager first, and then update the paths in your /etc/odbcinst.ini file.
By default, CentOS will install the drivers for connecting to PostgreSQL databases via ODBC. To install the drivers for MySQL, execute the following command:
$ sudo yum install mysql-connector-odbc
To install the PostgreSQL ODBC connector on Ubuntu:
$ sudo apt-get install odbc-postgresql
Or to install the MySQL ODBC connector on Ubuntu:
$ sudo apt-get install libmyodbc
Configuring ODBC for PostgreSQL
Configuration for the PostgreSQL ODBC driver is done in the /etc/odbcinst.ini file.
On CentOS the default file already contains some data, including that for PostgreSQL, so just verify that the data exists. The file will look like the following:
[PostgreSQL]
Description = ODBC for PostgreSQL
Driver = /usr/lib/libodbcpsql.so
Setup = /usr/lib/libodbcpsqlS.so
FileUsage = 1
On Ubuntu, the /etc/odbcinst.ini file will be blank, so you’ll need to add the data to that configuration file. Add the following to the odbcinst.ini file:
[PostgreSQL]
Description = ODBC for PostgreSQL
Driver = /usr/lib/odbc/psqlodbca.so
Setup = /usr/lib/odbc/libodbcpsqlS.so
FileUsage = 1
On 64-bit systems, you will need to change the path of the libraries from /usr/lib/ to /usr/lib64/ in order to access the correct library files.
In either case, you can use cat > /etc/odbcinst.ini to write a clean configuration file, as we’ve done in other chapters. Just use Ctrl+D to save the file once you’re done.
Verify that the system is able to see the driver by running the following command. It should return the label name PostgreSQL if all is well:
$ odbcinst -q -d
[PostgreSQL]
Next, configure the /etc/odbc.ini file, which is used to create an identifier that Asterisk will use to reference this configuration. If at any point in the future you need to change the database to something else, you simply need to reconfigure this file, allowing Asterisk to continue to point to the same place[141]:
[asterisk-connector]
Description = PostgreSQL connection to 'asterisk' database
Driver = PostgreSQL
Database = asterisk
Servername = localhost
UserName = asterisk
Password = welcome
Port = 5432
Protocol = 8.1
ReadOnly = No
RowVersioning = No
ShowSystemTables = No
ShowOidColumn = No
FakeOidIndex = No
ConnSettings =
Configuring ODBC for MySQL
Configuration for the MySQL ODBC driver is done in the /etc/odbcinst.ini file.
On CentOS the default file already contains some data, including that for MySQL, but it needs to be uncommented and requires a couple of changes. Replace the existing text with the following:
[MySQL]
Description = ODBC for MySQL
Driver = /usr/lib/libmyodbc3.so
Setup = /usr/lib/libodbcmyS.so
FileUsage = 1
On Ubuntu, the /etc/odbcinst.ini file will be blank, so you’ll need to add the data to that configuration file. Add the following to the odbcinst.ini file:
[MySQL]
Description = ODBC for MySQL
Driver = /usr/lib/odbc/libmyodbc.so
Setup = /usr/lib/odbc/libodbcmyS.so
FileUsage = 1
On 64-bit systems, you will need to change the path of the libraries from /usr/lib/ to /usr/lib64/ in order to access the correct library files.
In either case, you can use cat > /etc/odbcinst.ini to write a clean configuration file, as we’ve done in other chapters. Just use Ctrl+D to save the file once you’re done.
Verify that the system is able to see the driver by running the following command. It should return the label name MySQL if all is well:
$ odbcinst -q -d
[MySQL]
Next, configure the /etc/odbc.ini file, which is used to create an identifier that Asterisk will use to reference this configuration. If at any point in the future you need to change the database to something else, you simply need to reconfigure this file, allowing Asterisk to continue to point to the same place:
[asterisk-connector]
Description = MySQL connection to 'asterisk' database
Driver = MySQL
Database = asterisk
Server = localhost
UserName = asterisk
Password = welcome
Port = 3306
Socket = /var/lib/mysql/mysql.sock
覺得文章對你有幫助,可以掃描二維碼捐贈給博主,謝謝!
如需轉載請標明出處:http://blog.csdn.net/itas109
QQ技術交流羣:129518033
License
License under CC BY-NC-ND 4.0: 署名-非商業使用-禁止演繹
Reference:
1.https://www.npmjs.com/package/odbc
2.https://github.com/markdirish/node-odbc/
3.http://www.unixodbc.org/
4.http://www.asteriskdocs.org/en/3rd_Edition/asterisk-book-html-chunk/installing_configuring_odbc.html