1、yum在线安装驱动
2、配置驱动
3、测试连接
二、编译安装
一、在线安装 1、yum在线安装驱动# yum -y install unixODBC # yum -y install mysql-connector-odbc
2、配置驱动
(1)查看驱动程序相关信息
# cat /etc/odbcinst.ini /************************************************ # Example driver definitions # Driver from the postgresql-odbc package # Setup from the unixODBC package [PostgreSQL] Description = ODBC for PostgreSQL Driver = /usr/lib/psqlodbc.so Setup = /usr/lib/libodbcpsqlS.so Driver64 = /usr/lib64/psqlodbc.so Setup64 = /usr/lib64/libodbcpsqlS.so FileUsage = 1 # Driver from the mysql-connector-odbc package # Setup from the unixODBC package [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 ************************************************/
(2)配置MySQL驱动
# cat /etc/odbc.ini #添加如下信息 /************************************************ [mysql-hr] Driver = /usr/lib64/libmyodbc5.so #注意驱动程序的选择 Description = MyODBC 5 Driver SERVER = 192.168.235.140 #要连接的数据库信息 PORT = 3306 USER = root Password = root Database = hr OPTION = 3 charset=UTF8 ************************************************/
3、测试连接
(1)连接MySQL创建测试用户、测试库
mysql> grant all privileges on *.* to test@192.168.152.129 identified by "root" with grant option; mysql> create database hr;
##注:赋权时host与对应的MySQL ODBC驱动中配置要保持一致,如为IP或者localhost
(2)测试连接
# isql mysql-hr test root -v ##语法:isql 数据源名称 用户名 密码 选项 +---------------------------------------+ | Connected! | | | | sql-statement | | help [tablename] | | quit | | | +---------------------------------------+ SQL>
二、编译安装编译安装注意要预先安装编译工具,卸载默认安装的驱动程序并选择合适的版本。
1、MySQL创建测试用户和测试库mysql> grant all privileges on *.* to test@192.168.152.129 identified by "root" with grant option; mysql> create database test;
##注:赋权时host与对应的MySQL ODBC驱动中配置要保持一致,如为IP或者localhost
2、安装驱动
yum -y install gcc gcc-c++ make wget http:// wget https://cdn.mysql.com//Downloads/Connector-ODBC/5.3/mysql-connector-odbc-5.3.9-linux-glibc2.12-x86-64bit.tar.gz tar -zxvf unixODBC-2.3.4.tar.gz -C /usr/local tar -zxvf mysql-connector-odbc-5.3.9-linux-glibc2.12-x86-64bit.tar.gz -C /usr/local/ cd /usr/local/unixODBC-2.3.4/ ./configure --prefix=/usr/local/unixODBC-2.3.4 --includedir=/usr/include --libdir=/usr/local/lib -bindir=/usr/bin --sysconfdir=/usr/local/etc make && make install
编辑/etc/ld.so.conf
# cat /etc/ld.so.conf include ld.so.conf.d/*.conf echo "/usr/local/lib" >> /etc/ld.so.conf ldconfig
目的:解决错误 ./myodbc-installer: error while loading shared libraries: libodbc.so.2: cannot open shared object file: No such file or directory
注册驱动
# ln -s /usr/local/mysql-connector-odbc-5.3.9/lib/libmyodbc5a.so /usr/lib/libmyodbc5a.so # /usr/local/mysql-connector-odbc-5.3.9/bin/myodbc-installer -d -a -n "MySQL ODBC 5.3.9 Driver" -t "DRIVER=/usr/lib/libmyodbc5a.so;SETUP=/usr/lib/libmyodbc5a.so"
3、配置驱动
# vim /usr/local/etc/odbc.ini
内容如下:
[mysql-hr] Driver = /usr/lib/libmyodbc5a.so Description = MyODBC 5 Driver SERVER = 192.168.152.129 PORT = 3306 USER = test Password = root Database = test OPTION = 3 charset=UTF8
4、测试