以前都是在Windows上用cache的SqlGateway通过odbc创建连接操作其他数据库。现在都用Linux了,那么和其他数据进行调用咋办呢。
可以看到是可以创建JDBC的连接的,而Java是跨平台的,所以可以用JDBC操作其他数据库,这次以mysql为例。
先到CentOS7上安装个mysql测试使用
wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm
sudo rpm -ivh mysql-community-release-el7-5.noarch.rpm
sudo yum install mysql-server
然后到Shell测试安装的数据库
[root@localhost /]# mysql -u root
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 17
Server version: 5.6.51 MySQL Community Server (GPL)
Copyright (c) 2000, 2021, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> select * from user;
+-----------------------+------+-------------------------------------------+-------------+-------------+-------------+-------------+-------------+-----------+-------------+---------------+--------------+-----------+------------+-----------------+------------+------------+--------------+------------+-----------------------+------------------+--------------+-----------------+------------------+------------------+----------------+---------------------+--------------------+------------------+------------+--------------+------------------------+----------+------------+-------------+--------------+---------------+-------------+-----------------+----------------------+-----------------------+-----------------------+------------------+
| Host | User | Password | Select_priv | Insert_priv | Update_priv | Delete_priv | Create_priv | Drop_priv | Reload_priv | Shutdown_priv | Process_priv | File_priv | Grant_priv | References_priv | Index_priv | Alter_priv | Show_db_priv | Super_priv | Create_tmp_table_priv | Lock_tables_priv | Execute_priv | Repl_slave_priv | Repl_client_priv | Create_view_priv | Show_view_priv | Create_routine_priv | Alter_routine_priv | Create_user_priv | Event_priv | Trigger_priv | Create_tablespace_priv | ssl_type | ssl_cipher | x509_issuer | x509_subject | max_questions | max_updates | max_connections | max_user_connections | plugin | authentication_string | password_expired |
+-----------------------+------+-------------------------------------------+-------------+-------------+-------------+-------------+-------------+-----------+-------------+---------------+--------------+-----------+------------+-----------------+------------+------------+--------------+------------+-----------------------+------------------+--------------+-----------------+------------------+------------------+----------------+---------------------+--------------------+------------------+------------+--------------+------------------------+----------+------------+-------------+--------------+---------------+-------------+-----------------+----------------------+-----------------------+-----------------------+------------------+
| localhost | root | | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | | | | | 0 | 0 | 0 | 0 | mysql_native_password | | N |
| localhost.localdomain | root | | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | | | | | 0 | 0 | 0 | 0 | mysql_native_password | | N |
| 127.0.0.1 | root | | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | | | | | 0 | 0 | 0 | 0 | mysql_native_password | | N |
| ::1 | root | | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | | | | | 0 | 0 | 0 | 0 | mysql_native_password | | N |
| localhost | | | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | | | | | 0 | 0 | 0 | 0 | mysql_native_password | NULL | N |
| localhost.localdomain | | | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | | | | | 0 | 0 | 0 | 0 | mysql_native_password | NULL | N |
| % | root | *7C51592F226DA6B71CF9E334486068916B768B7B | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | | | | | 0 | 0 | 0 | 0 | mysql_native_password | | N |
+-----------------------+------+-------------------------------------------+-------------+-------------+-------------+-------------+-------------+-----------+-------------+---------------+--------------+-----------+------------+-----------------+------------+------------+--------------+------------+-----------------------+------------------+--------------+-----------------+------------------+------------------+----------------+---------------------+--------------------+------------------+------------+--------------+------------------------+----------+------------+-------------+--------------+---------------+-------------+-----------------+----------------------+-----------------------+-----------------------+------------------+
7 rows in set (0.00 sec)
mysql>
然后设置远程访问密码
[root@localhost /]# mysql -u root
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 18
Server version: 5.6.51 MySQL Community Server (GPL)
Copyright (c) 2000, 2021, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'zlz' WITH GRANT OPTION;FLUSH PRIVILEGES
Query OK, 0 rows affected (0.00 sec)
->
然后到Windows下安装mysql驱动就能创建odbc数据源和用sqldbx等访问了
驱动到官网上下载
然后测试IRIS创建JDBC的SqlGateway
先到mysql官网下载JDBC驱动jar包
下载好之后把rpm包上传到Linux上
该包依赖java-headless,所以要先安装java-headless
yum install java-headless
然后安装驱动包
rpm -i mysql-connector-j-8.0.32-1.el7.noarch.rpm
安装后查看包
[root@localhost /]# ll /usr/share/java
总用量 2416
-rwxrwxrwx. 1 root root 2472941 12月 7 2022 mysql-connector-j.jar
[root@localhost /]#
然后就是配置IRIS的环节了
测试成功后即可用M操作mysql了,这块M不能用%SQLGatewayConnection了,%SQLGatewayConnection看类说明只支持ODBC,查了半天,最后咨询姚工解决了,感谢感谢
Class OTH.TEST Extends %RegisteredObject
{
/// 测试JDBC连接
/// w ##class(OTH.TEST).TestJDBC()
ClassMethod TestJDBC()
{
s conn = $system.SQLGateway.GetJDBCConnection("mysql")
i (conn = "") quit 0
set cmd = "select * from user"
set st = %JDBCGateway.prepareStatement(conn, cmd)
do %JDBCGateway.setString(st,1,9) // assign c1 to 9
set res = %JDBCGateway.execQuery(st)
if (res) {
s error = %JDBCGateway.getErrorText()
w "error = ",error,!
q
}
while %JDBCGateway.next(st) {
s c1 = %JDBCGateway.getString(st,1)
s c2 = %JDBCGateway.getString(st,2)
s c3 = %JDBCGateway.getString(st,3)
s c4 = %JDBCGateway.getString(st,4)
s c5 = %JDBCGateway.getString(st,5)
w c1_","_c2_","_c3_","_c4_","_c5,!
}
w $system.SQLGateway.DropConnection("mysql")
q 1
}
}
这样在没Linux下odbc驱动的数据可以用jdbc操作,有odbc的还是可以用odbc
怎么在CentOS下安装和使用odbc呢
先安装unixodbc
[root@localhost /]# yum install unixODBC unixODBC-devel
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.ustc.edu.cn
* extras: ftp.sjtu.edu.cn
* updates: ftp.sjtu.edu.cn
软件包 unixODBC-2.3.1-14.el7.x86_64 已安装并且是最新版本
正在解决依赖关系
--> 正在检查事务
---> 软件包 unixODBC-devel.x86_64.0.2.3.1-14.el7 将被 安装
--> 解决依赖关系完成
依赖关系解决
================================================================================================================================================================================================================================
Package 架构 版本 源 大小
================================================================================================================================================================================================================================
正在安装:
unixODBC-devel x86_64 2.3.1-14.el7 base 55 k
事务概要
================================================================================================================================================================================================================================
安装 1 软件包
总下载量:55 k
安装大小:186 k
Is this ok [y/d/N]: y
Downloading packages:
unixODBC-devel-2.3.1-14.el7.x86_64.rpm | 55 kB 00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
正在安装 : unixODBC-devel-2.3.1-14.el7.x86_64 1/1
验证中 : unixODBC-devel-2.3.1-14.el7.x86_64 1/1
已安装:
unixODBC-devel.x86_64 0:2.3.1-14.el7
完毕!
先安装mysql的odbc连接驱动
[root@localhost /]# yum install mysql-connector-odbc
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.ustc.edu.cn
* extras: ftp.sjtu.edu.cn
* updates: ftp.sjtu.edu.cn
正在解决依赖关系
--> 正在检查事务
---> 软件包 mysql-connector-odbc.x86_64.0.8.0.33-1.el7 将被 安装
--> 正在处理依赖关系 unixODBC >= 2.3,它被软件包 mysql-connector-odbc-8.0.33-1.el7.x86_64 需要
--> 正在处理依赖关系 libodbc.so.2()(64bit),它被软件包 mysql-connector-odbc-8.0.33-1.el7.x86_64 需要
--> 正在处理依赖关系 libodbcinst.so.2()(64bit),它被软件包 mysql-connector-odbc-8.0.33-1.el7.x86_64 需要
--> 正在检查事务
---> 软件包 unixODBC.x86_64.0.2.3.1-14.el7 将被 安装
--> 正在处理依赖关系 libltdl.so.7()(64bit),它被软件包 unixODBC-2.3.1-14.el7.x86_64 需要
--> 正在检查事务
---> 软件包 libtool-ltdl.x86_64.0.2.4.2-22.el7_3 将被 安装
--> 解决依赖关系完成
依赖关系解决
================================================================================================================================================================================================================================
Package 架构 版本 源 大小
================================================================================================================================================================================================================================
正在安装:
mysql-connector-odbc x86_64 8.0.33-1.el7 mysql-connectors-community 4.4 M
为依赖而安装:
libtool-ltdl x86_64 2.4.2-22.el7_3 base 49 k
unixODBC x86_64 2.3.1-14.el7 base 413 k
事务概要
================================================================================================================================================================================================================================
安装 1 软件包 (+2 依赖软件包)
总下载量:4.8 M
安装大小:24 M
Is this ok [y/d/N]: y
Downloading packages:
(1/3): libtool-ltdl-2.4.2-22.el7_3.x86_64.rpm | 49 kB 00:00:00
(2/3): unixODBC-2.3.1-14.el7.x86_64.rpm | 413 kB 00:00:00
(3/3): mysql-connector-odbc-8.0.33-1.el7.x86_64.rpm | 4.4 MB 00:00:07
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
总计 679 kB/s | 4.8 MB 00:00:07
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
警告:RPM 数据库已被非 yum 程序修改。
正在安装 : libtool-ltdl-2.4.2-22.el7_3.x86_64 1/3
正在安装 : unixODBC-2.3.1-14.el7.x86_64 2/3
正在安装 : mysql-connector-odbc-8.0.33-1.el7.x86_64 3/3
Success: Usage count is 1
Success: Usage count is 1
验证中 : mysql-connector-odbc-8.0.33-1.el7.x86_64 1/3
验证中 : libtool-ltdl-2.4.2-22.el7_3.x86_64 2/3
验证中 : unixODBC-2.3.1-14.el7.x86_64 3/3
已安装:
mysql-connector-odbc.x86_64 0:8.0.33-1.el7
作为依赖被安装:
libtool-ltdl.x86_64 0:2.4.2-22.el7_3 unixODBC.x86_64 0:2.3.1-14.el7
完毕!
安装好之后在/etc/odbcinst.ini有驱动配置
[PostgreSQL]
Description=ODBC for PostgreSQL
Driver=/usr/lib/psqlodbcw.so
Setup=/usr/lib/libodbcpsqlS.so
Driver64=/usr/lib64/psqlodbcw.so
Setup64=/usr/lib64/libodbcpsqlS.so
FileUsage=1
[MySQL]
Description=ODBC for MySQL
Driver=/usr/lib/libmyodbc5.so
Setup=/usr/lib/libodbcmyS.so
Driver64=/usr/lib64/libmyodbc5.so
Setup64=/usr/lib64/libodbcmyS.so
FileUsage=1
[MySQL ODBC 8.0 Unicode Driver]
Driver=/usr/lib64/libmyodbc8w.so
UsageCount=1
[MySQL ODBC 8.0 ANSI Driver]
Driver=/usr/lib64/libmyodbc8a.so
UsageCount=1
在/etc下新建配置文件odbc.ini。配置如下,Driver 名字和/etc/odbcinst.ini的名字一样
[mysqlodbc]
Driver = MySQL ODBC 8.0 Unicode Driver
SERVER = 172.26.185.167
PORT = 3306
DATABASE = mysql
OPTION = 67108864
USER = root
PASSWORD = zlz
然后测试连接
[root@localhost /]# isql -v mysqlodbc
+---------------------------------------+
| Connected! |
| |
| sql-statement |
| help [tablename] |
| quit |
| |
+---------------------------------------+
SQL> select * from user;
+-------------------------------------------------------------+-----------------+------------------------------------------+------------+------------+------------+------------+------------+----------+------------+--------------+-------------+----------+-----------+----------------+-----------+-----------+-------------+-----------+----------------------+-----------------+-------------+----------------+-----------------+-----------------+---------------+--------------------+-------------------+-----------------+-----------+-------------+-----------------------+----------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+--------------+------------+----------------+---------------------+-----------------------------------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-----------------+
| Host | User | Password | Select_priv| Insert_priv| Update_priv| Delete_priv| Create_priv| Drop_priv| Reload_priv| Shutdown_priv| Process_priv| File_priv| Grant_priv| References_priv| Index_priv| Alter_priv| Show_db_priv| Super_priv| Create_tmp_table_priv| Lock_tables_priv| Execute_priv| Repl_slave_priv| Repl_client_priv| Create_view_priv| Show_view_priv| Create_routine_priv| Alter_routine_priv| Create_user_priv| Event_priv| Trigger_priv| Create_tablespace_priv| ssl_type | ssl_cipher | x509_issuer | x509_subject | max_questions| max_updates| max_connections| max_user_connections| plugin | authentication_string | password_expired|
+-------------------------------------------------------------+-----------------+------------------------------------------+------------+------------+------------+------------+------------+----------+------------+--------------+-------------+----------+-----------+----------------+-----------+-----------+-------------+-----------+----------------------+-----------------+-------------+----------------+-----------------+-----------------+---------------+--------------------+-------------------+-----------------+-----------+-------------+-----------------------+----------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+--------------+------------+----------------+---------------------+-----------------------------------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-----------------+
| localhost | root | | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | | | | | 0 | 0 | 0 | 0 | mysql_native_password | | N |
| localhost.localdomain | root | | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | | | | | 0 | 0 | 0 | 0 | mysql_native_password | | N |
| 127.0.0.1 | root | | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | | | | | 0 | 0 | 0 | 0 | mysql_native_password | | N |
| ::1 | root | | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | | | | | 0 | 0 | 0 | 0 | mysql_native_password | | N |
| localhost | | | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | | | | | 0 | 0 | 0 | 0 | mysql_native_password | | N |
| localhost.localdomain | | | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | | | | | 0 | 0 | 0 | 0 | mysql_native_password | | N |
| % | root | *7C51592F226DA6B71CF9E334486068916B768B7B| Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | | | | | 0 | 0 | 0 | 0 | mysql_native_password | | N |
+-------------------------------------------------------------+-----------------+------------------------------------------+------------+------------+------------+------------+------------+----------+------------+--------------+-------------+----------+-----------+----------------+-----------+-----------+-------------+-----------+----------------------+-----------------+-------------+----------------+-----------------+-----------------+---------------+--------------------+-------------------+-----------------+-----------+-------------+-----------------------+----------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+--------------+------------+----------------+---------------------+-----------------------------------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-----------------+
SQLRowCount returns 7
7 rows fetched
SQL>
[root@localhost /]#
[root@localhost /]#
[root@localhost /]#
[root@localhost /]#
然后到portal测试成功,奇怪的是DSN没我配置的名字也能测试成功,后面再说吧!!!