MySQL ODBC for Linux
参考自http://blog.csdn.net/allens_zhou/article/details/8575400
centos7 64bit [IP:192.168.0.100]
yum install unixODBC unixODBC-devel mysql-connector-odbc
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/psqlodbcw.so Setup = /usr/lib/libodbcpsqlS.so Driver64 = /usr/lib64/psqlodbcw.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
验证MySQL驱动,返回/etc/odbcinst.ini中的配置段标题表示成功,如:[MySQL]
odbcinst -q -d
现在利用isql功能验证能连到你的数据库上,当你输入指令后会看到结果为1的返回值就表明连接成功了
echo "select 1" | isql -v asterisk-connector
开启数据库MySQL端口
firewall-cmd --zone=public --add-port=3306/tcp --permanent firewall-cmd --reload
MySQL授权
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'password' WITH GRANT OPTION; FLUSH PRIVILEGES;
用Python测试
from pyodbc import connect as ct host = "Driver={MySQL};Server=192.168.0.100;Database=mysql;UID=root;PWD=password" sql = "SELECT *FROM user;" conn = ct(host) cr = conn.cursor() cr.execute(sql) rows = cr.fetchall() print rows
PS 安装pyodbc [pip install pyodbc] 时出错【大意为gcc返回1】,处理办法:yum install -y python-devel